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/>.
21 #include "system/wait.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
37 return random() % max
;
42 uint8_t val
= rand_int(256) & 0xff;
48 uint16_t val
= rand_int(0xffff) & 0xffff;
54 return INT_MIN
+ random();
64 uint64_t t
= random();
65 t
= (t
<< 32) | random();
69 double rand_double(void)
71 return 1.0 / rand64();
74 void fill_buffer(void *p
, size_t len
)
79 for (i
=0; i
<len
; i
++) {
84 void verify_buffer(void *p1
, void *p2
, size_t len
)
87 assert(memcmp(p1
, p2
, len
) == 0);
91 void fill_string(char *p
, size_t len
)
95 for (i
=0; i
<len
-1; i
++) {
96 p
[i
] = 'A' + rand_int(26);
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
)
112 void verify_ctdb_uint8(uint8_t *p1
, uint8_t *p2
)
117 void fill_ctdb_uint16(uint16_t *p
)
122 void verify_ctdb_uint16(uint16_t *p1
, uint16_t *p2
)
127 void fill_ctdb_int32(int32_t *p
)
132 void verify_ctdb_int32(int32_t *p1
, int32_t *p2
)
137 void fill_ctdb_uint32(uint32_t *p
)
142 void verify_ctdb_uint32(uint32_t *p1
, uint32_t *p2
)
147 void fill_ctdb_uint64(uint64_t *p
)
152 void verify_ctdb_uint64(uint64_t *p1
, uint64_t *p2
)
157 void fill_ctdb_double(double *p
)
162 void verify_ctdb_double(double *p1
, double *p2
)
167 void fill_ctdb_bool(bool *p
)
169 if (rand_int(2) == 0) {
176 void verify_ctdb_bool(bool *p1
, bool *p2
)
181 void fill_ctdb_string(TALLOC_CTX
*mem_ctx
, const char **p
)
186 len
= rand_int(1024) + 2;
187 str
= talloc_size(mem_ctx
, len
+1);
190 fill_string(str
, len
);
194 void verify_ctdb_string(const char **p1
, const char **p2
)
196 if (*p1
== NULL
|| *p2
== NULL
) {
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
)
218 void verify_ctdb_pid(pid_t
*p1
, pid_t
*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
);
249 sigaction(SIGABRT
, &act
, NULL
);
253 void protocol_test_iterate_tag(const char *fmt
, ...)
259 count
= vsnprintf(protocol_test_iterate_buf
,
260 sizeof(protocol_test_iterate_buf
),
266 protocol_test_iterate_buf
[sizeof(protocol_test_iterate_buf
) - 1] = '\0';
269 void protocol_test_iterate(int argc
,
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) {
285 "%s: min must be less than max\n",
294 fprintf(stderr
, "usage: %s min [max]\n", argv
[0]);
298 sigaction(SIGABRT
, &act
, NULL
);
300 for (seed
= min
; seed
<= max
; seed
++) {