ctdb-tests: Add iteration support for protocol tests
[Samba.git] / ctdb / tests / src / protocol_common_basic.c
blob668fb46cfb3b7e4fdc8e613dff7bc7119a5d5bfb
1 /*
2 protocol tests - common functions - basic types
4 Copyright (C) Amitay Isaacs 2015-2017
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
22 #include <assert.h>
24 #include "tests/src/protocol_common_basic.h"
26 uint8_t BUFFER[1024*1024];
29 * Functions to generation random data
32 int rand_int(int max)
34 return random() % max;
37 uint8_t rand8(void)
39 uint8_t val = rand_int(256) & 0xff;
40 return val;
43 uint16_t rand16(void)
45 uint16_t val = rand_int(0xffff) & 0xffff;
46 return val;
49 int32_t rand32i(void)
51 return INT_MIN + random();
54 uint32_t rand32(void)
56 return random();
59 uint64_t rand64(void)
61 uint64_t t = random();
62 t = (t << 32) | random();
63 return t;
66 double rand_double(void)
68 return 1.0 / rand64();
71 void fill_buffer(void *p, size_t len)
73 size_t i;
74 uint8_t *ptr = p;
76 for (i=0; i<len; i++) {
77 ptr[i] = rand8();
81 void verify_buffer(void *p1, void *p2, size_t len)
83 if (len > 0) {
84 assert(memcmp(p1, p2, len) == 0);
88 void fill_string(char *p, size_t len)
90 size_t i;
92 for (i=0; i<len-1; i++) {
93 p[i] = 'A' + rand_int(26);
95 p[len-1] = '\0';
98 void verify_string(const char *p1, const char *p2)
100 assert(strlen(p1) == strlen(p2));
101 assert(strcmp(p1, p2) == 0);
104 void fill_ctdb_uint8(uint8_t *p)
106 *p = rand8();
109 void verify_ctdb_uint8(uint8_t *p1, uint8_t *p2)
111 assert(*p1 == *p2);
114 void fill_ctdb_uint16(uint16_t *p)
116 *p = rand16();
119 void verify_ctdb_uint16(uint16_t *p1, uint16_t *p2)
121 assert(*p1 == *p2);
124 void fill_ctdb_int32(int32_t *p)
126 *p = rand32i();
129 void verify_ctdb_int32(int32_t *p1, int32_t *p2)
131 assert(*p1 == *p2);
134 void fill_ctdb_uint32(uint32_t *p)
136 *p = rand32();
139 void verify_ctdb_uint32(uint32_t *p1, uint32_t *p2)
141 assert(*p1 == *p2);
144 void fill_ctdb_uint64(uint64_t *p)
146 *p = rand64();
149 void verify_ctdb_uint64(uint64_t *p1, uint64_t *p2)
151 assert(*p1 == *p2);
154 void fill_ctdb_double(double *p)
156 *p = rand_double();
159 void verify_ctdb_double(double *p1, double *p2)
161 assert(*p1 == *p2);
164 void fill_ctdb_bool(bool *p)
166 if (rand_int(2) == 0) {
167 *p = true;
168 } else {
169 *p = false;
173 void verify_ctdb_bool(bool *p1, bool *p2)
175 assert(*p1 == *p2);
178 void fill_ctdb_string(TALLOC_CTX *mem_ctx, const char **p)
180 char *str;
181 int len;
183 len = rand_int(1024) + 2;
184 str = talloc_size(mem_ctx, len+1);
185 assert(str != NULL);
187 fill_string(str, len);
188 *p = str;
191 void verify_ctdb_string(const char **p1, const char **p2)
193 if (*p1 == NULL || *p2 == NULL) {
194 assert(*p1 == *p2);
195 } else {
196 verify_string(*p1, *p2);
200 void fill_ctdb_stringn(TALLOC_CTX *mem_ctx, const char **p)
202 fill_ctdb_string(mem_ctx, p);
205 void verify_ctdb_stringn(const char **p1, const char **p2)
207 verify_ctdb_string(p1, p2);
210 void fill_ctdb_pid(pid_t *p)
212 *p = rand32();
215 void verify_ctdb_pid(pid_t *p1, pid_t *p2)
217 assert(*p1 == *p2);
220 void fill_ctdb_timeval(struct timeval *p)
222 p->tv_sec = rand32();
223 p->tv_usec = rand_int(1000000);
226 void verify_ctdb_timeval(struct timeval *p1, struct timeval *p2)
228 assert(p1->tv_sec == p2->tv_sec);
229 assert(p1->tv_usec == p2->tv_usec);
232 static unsigned int seed;
233 static char protocol_test_iterate_buf[1024];
235 static void protocol_test_iterate_abort_handler(int sig)
237 struct sigaction act = {
238 .sa_handler = SIG_DFL,
241 fprintf(stderr, "Failed with seed: %d\n", seed);
242 if (protocol_test_iterate_buf[0] != '\0') {
243 fprintf(stderr, " tag: %s\n", protocol_test_iterate_buf);
245 sigaction(SIGABRT, &act, NULL);
246 abort();
249 void protocol_test_iterate_tag(const char *fmt, ...)
251 va_list ap;
252 int count;
254 va_start(ap,fmt);
255 count = vsnprintf(protocol_test_iterate_buf,
256 sizeof(protocol_test_iterate_buf),
257 fmt,
258 ap);
259 va_end(ap);
261 assert(count >= 0);
262 protocol_test_iterate_buf[sizeof(protocol_test_iterate_buf) - 1] = '\0';
265 void protocol_test_iterate(int argc,
266 const char *argv[],
267 void (*test_func)(void))
269 struct sigaction act = {
270 .sa_handler = protocol_test_iterate_abort_handler,
272 unsigned int min, max;
274 if (argc == 2 || argc == 3) {
275 min = atoi(argv[1]);
277 if (argc == 3) {
278 max = atoi(argv[2]);
279 if (min >= max) {
280 fprintf(stderr,
281 "%s: min must be less than max\n",
282 argv[0]);
283 exit(1);
286 } else {
287 max = min;
289 } else {
290 fprintf(stderr, "usage: %s min [max]\n", argv[0]);
291 exit(1);
294 sigaction(SIGABRT, &act, NULL);
296 for (seed = min; seed <= max ; seed++) {
297 srandom(seed);
299 test_func();