ctdb: add ctdb_canonicalize_ip_inplace() helper
[Samba.git] / ctdb / tests / src / protocol_common_basic.c
blob7567f7b6520af7579f324f71621e6f643a5c11be
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"
21 #include "system/wait.h"
23 #include <assert.h>
25 #include "lib/util/fault.h"
27 #include "tests/src/protocol_common_basic.h"
29 uint8_t BUFFER[1024*1024];
32 * Functions to generation random data
35 int rand_int(int max)
37 return random() % max;
40 uint8_t rand8(void)
42 uint8_t val = rand_int(256) & 0xff;
43 return val;
46 uint16_t rand16(void)
48 uint16_t val = rand_int(0xffff) & 0xffff;
49 return val;
52 int32_t rand32i(void)
54 return INT_MIN + random();
57 uint32_t rand32(void)
59 return random();
62 uint64_t rand64(void)
64 uint64_t t = random();
65 t = (t << 32) | random();
66 return t;
69 double rand_double(void)
71 return 1.0 / rand64();
74 void fill_buffer(void *p, size_t len)
76 size_t i;
77 uint8_t *ptr = p;
79 for (i=0; i<len; i++) {
80 ptr[i] = rand8();
84 void verify_buffer(void *p1, void *p2, size_t len)
86 if (len > 0) {
87 assert(memcmp(p1, p2, len) == 0);
91 void fill_string(char *p, size_t len)
93 size_t i;
95 for (i=0; i<len-1; i++) {
96 p[i] = 'A' + rand_int(26);
98 p[len-1] = '\0';
101 void verify_string(const char *p1, const char *p2)
103 assert(strlen(p1) == strlen(p2));
104 assert(strcmp(p1, p2) == 0);
107 void fill_ctdb_uint8(uint8_t *p)
109 *p = rand8();
112 void verify_ctdb_uint8(uint8_t *p1, uint8_t *p2)
114 assert(*p1 == *p2);
117 void fill_ctdb_uint16(uint16_t *p)
119 *p = rand16();
122 void verify_ctdb_uint16(uint16_t *p1, uint16_t *p2)
124 assert(*p1 == *p2);
127 void fill_ctdb_int32(int32_t *p)
129 *p = rand32i();
132 void verify_ctdb_int32(int32_t *p1, int32_t *p2)
134 assert(*p1 == *p2);
137 void fill_ctdb_uint32(uint32_t *p)
139 *p = rand32();
142 void verify_ctdb_uint32(uint32_t *p1, uint32_t *p2)
144 assert(*p1 == *p2);
147 void fill_ctdb_uint64(uint64_t *p)
149 *p = rand64();
152 void verify_ctdb_uint64(uint64_t *p1, uint64_t *p2)
154 assert(*p1 == *p2);
157 void fill_ctdb_double(double *p)
159 *p = rand_double();
162 void verify_ctdb_double(double *p1, double *p2)
164 assert(*p1 == *p2);
167 void fill_ctdb_bool(bool *p)
169 if (rand_int(2) == 0) {
170 *p = true;
171 } else {
172 *p = false;
176 void verify_ctdb_bool(bool *p1, bool *p2)
178 assert(*p1 == *p2);
181 void fill_ctdb_string(TALLOC_CTX *mem_ctx, const char **p)
183 char *str;
184 int len;
186 len = rand_int(1024) + 2;
187 str = talloc_size(mem_ctx, len+1);
188 assert(str != NULL);
190 fill_string(str, len);
191 *p = str;
194 void verify_ctdb_string(const char **p1, const char **p2)
196 if (*p1 == NULL || *p2 == NULL) {
197 assert(*p1 == *p2);
198 } else {
199 verify_string(*p1, *p2);
203 void fill_ctdb_stringn(TALLOC_CTX *mem_ctx, const char **p)
205 fill_ctdb_string(mem_ctx, p);
208 void verify_ctdb_stringn(const char **p1, const char **p2)
210 verify_ctdb_string(p1, p2);
213 void fill_ctdb_pid(pid_t *p)
215 *p = rand32();
218 void verify_ctdb_pid(pid_t *p1, pid_t *p2)
220 assert(*p1 == *p2);
223 void fill_ctdb_timeval(struct timeval *p)
225 p->tv_sec = rand32();
226 p->tv_usec = rand_int(1000000);
229 void verify_ctdb_timeval(struct timeval *p1, struct timeval *p2)
231 assert(p1->tv_sec == p2->tv_sec);
232 assert(p1->tv_usec == p2->tv_usec);
235 static unsigned int seed;
236 static char protocol_test_iterate_buf[1024];
238 static void protocol_test_iterate_abort_handler(int sig)
240 struct sigaction act = {
241 .sa_handler = SIG_DFL,
244 fprintf(stderr, "Failed with seed: %d\n", seed);
245 if (protocol_test_iterate_buf[0] != '\0') {
246 fprintf(stderr, " tag: %s\n", protocol_test_iterate_buf);
248 log_stack_trace();
249 sigaction(SIGABRT, &act, NULL);
250 abort();
253 void protocol_test_iterate_tag(const char *fmt, ...)
255 va_list ap;
256 int count;
258 va_start(ap,fmt);
259 count = vsnprintf(protocol_test_iterate_buf,
260 sizeof(protocol_test_iterate_buf),
261 fmt,
262 ap);
263 va_end(ap);
265 assert(count >= 0);
266 protocol_test_iterate_buf[sizeof(protocol_test_iterate_buf) - 1] = '\0';
269 void protocol_test_iterate(int argc,
270 const char *argv[],
271 void (*test_func)(void))
273 struct sigaction act = {
274 .sa_handler = protocol_test_iterate_abort_handler,
276 unsigned int min, max;
278 if (argc == 2 || argc == 3) {
279 min = atoi(argv[1]);
281 if (argc == 3) {
282 max = atoi(argv[2]);
283 if (min >= max) {
284 fprintf(stderr,
285 "%s: min must be less than max\n",
286 argv[0]);
287 exit(1);
290 } else {
291 max = min;
293 } else {
294 fprintf(stderr, "usage: %s min [max]\n", argv[0]);
295 exit(1);
298 sigaction(SIGABRT, &act, NULL);
300 for (seed = min; seed <= max ; seed++) {
301 srandom(seed);
303 test_func();