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/>.
24 #include "tests/src/protocol_common_basic.h"
26 uint8_t BUFFER
[1024*1024];
29 * Functions to generation random data
34 return random() % max
;
39 uint8_t val
= rand_int(256) & 0xff;
45 uint16_t val
= rand_int(0xffff) & 0xffff;
51 return INT_MIN
+ random();
61 uint64_t t
= random();
62 t
= (t
<< 32) | random();
66 double rand_double(void)
68 return 1.0 / rand64();
71 void fill_buffer(void *p
, size_t len
)
76 for (i
=0; i
<len
; i
++) {
81 void verify_buffer(void *p1
, void *p2
, size_t len
)
84 assert(memcmp(p1
, p2
, len
) == 0);
88 void fill_string(char *p
, size_t len
)
92 for (i
=0; i
<len
-1; i
++) {
93 p
[i
] = 'A' + rand_int(26);
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
)
109 void verify_ctdb_uint8(uint8_t *p1
, uint8_t *p2
)
114 void fill_ctdb_uint16(uint16_t *p
)
119 void verify_ctdb_uint16(uint16_t *p1
, uint16_t *p2
)
124 void fill_ctdb_int32(int32_t *p
)
129 void verify_ctdb_int32(int32_t *p1
, int32_t *p2
)
134 void fill_ctdb_uint32(uint32_t *p
)
139 void verify_ctdb_uint32(uint32_t *p1
, uint32_t *p2
)
144 void fill_ctdb_uint64(uint64_t *p
)
149 void verify_ctdb_uint64(uint64_t *p1
, uint64_t *p2
)
154 void fill_ctdb_double(double *p
)
159 void verify_ctdb_double(double *p1
, double *p2
)
164 void fill_ctdb_bool(bool *p
)
166 if (rand_int(2) == 0) {
173 void verify_ctdb_bool(bool *p1
, bool *p2
)
178 void fill_ctdb_string(TALLOC_CTX
*mem_ctx
, const char **p
)
183 len
= rand_int(1024) + 2;
184 str
= talloc_size(mem_ctx
, len
+1);
187 fill_string(str
, len
);
191 void verify_ctdb_string(const char **p1
, const char **p2
)
193 if (*p1
== NULL
|| *p2
== NULL
) {
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
)
215 void verify_ctdb_pid(pid_t
*p1
, pid_t
*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
);
249 void protocol_test_iterate_tag(const char *fmt
, ...)
255 count
= vsnprintf(protocol_test_iterate_buf
,
256 sizeof(protocol_test_iterate_buf
),
262 protocol_test_iterate_buf
[sizeof(protocol_test_iterate_buf
) - 1] = '\0';
265 void protocol_test_iterate(int argc
,
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) {
281 "%s: min must be less than max\n",
290 fprintf(stderr
, "usage: %s min [max]\n", argv
[0]);
294 sigaction(SIGABRT
, &act
, NULL
);
296 for (seed
= min
; seed
<= max
; seed
++) {