ctdb-protocol: Fix marshalling for tdb_data
[vlendec/samba-autobuild/.git] / ctdb / tests / src / protocol_common.c
1 /*
2    protocol tests - common functions
3
4    Copyright (C) Amitay Isaacs  2015-2017
5
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.
10
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.
15
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/>.
18 */
19
20 #include "replace.h"
21 #include "system/network.h"
22
23 #include <assert.h>
24
25 #include "protocol/protocol_api.h"
26
27 #include "tests/src/protocol_common.h"
28
29 uint8_t BUFFER[1024*1024];
30
31 /*
32  * Functions to generation random data
33  */
34
35 int rand_int(int max)
36 {
37         return random() % max;
38 }
39
40 uint8_t rand8(void)
41 {
42         uint8_t val = rand_int(256) & 0xff;
43         return val;
44 }
45
46 uint16_t rand16(void)
47 {
48         uint16_t val = rand_int(0xffff) & 0xffff;
49         return val;
50 }
51
52 int32_t rand32i(void)
53 {
54         return INT_MIN + random();
55 }
56
57 uint32_t rand32(void)
58 {
59         return random();
60 }
61
62 uint64_t rand64(void)
63 {
64         uint64_t t = random();
65         t = (t << 32) | random();
66         return t;
67 }
68
69 double rand_double(void)
70 {
71         return 1.0 / rand64();
72 }
73
74 void fill_buffer(void *p, size_t len)
75 {
76         int i;
77         uint8_t *ptr = p;
78
79         for (i=0; i<len; i++) {
80                 ptr[i] = rand8();
81         }
82 }
83
84 void verify_buffer(void *p1, void *p2, size_t len)
85 {
86         if (len > 0) {
87                 assert(memcmp(p1, p2, len) == 0);
88         }
89 }
90
91 static void fill_string(char *p, size_t len)
92 {
93         int i;
94
95         for (i=0; i<len-1; i++) {
96                 p[i] = 'A' + rand_int(26);
97         }
98         p[len-1] = '\0';
99 }
100
101 static void verify_string(const char *p1, const char *p2)
102 {
103         assert(strlen(p1) == strlen(p2));
104         assert(strcmp(p1, p2) == 0);
105 }
106
107 void fill_ctdb_uint8(uint8_t *p)
108 {
109         *p = rand8();
110 }
111
112 void verify_ctdb_uint8(uint8_t *p1, uint8_t *p2)
113 {
114         assert(*p1 == *p2);
115 }
116
117 void fill_ctdb_uint16(uint16_t *p)
118 {
119         *p = rand16();
120 }
121
122 void verify_ctdb_uint16(uint16_t *p1, uint16_t *p2)
123 {
124         assert(*p1 == *p2);
125 }
126
127 void fill_ctdb_int32(int32_t *p)
128 {
129         *p = rand32i();
130 }
131
132 void verify_ctdb_int32(int32_t *p1, int32_t *p2)
133 {
134         assert(*p1 == *p2);
135 }
136
137 void fill_ctdb_uint32(uint32_t *p)
138 {
139         *p = rand32();
140 }
141
142 void verify_ctdb_uint32(uint32_t *p1, uint32_t *p2)
143 {
144         assert(*p1 == *p2);
145 }
146
147 void fill_ctdb_uint64(uint64_t *p)
148 {
149         *p = rand64();
150 }
151
152 void verify_ctdb_uint64(uint64_t *p1, uint64_t *p2)
153 {
154         assert(*p1 == *p2);
155 }
156
157 void fill_ctdb_double(double *p)
158 {
159         *p = rand_double();
160 }
161
162 void verify_ctdb_double(double *p1, double *p2)
163 {
164         assert(*p1 == *p2);
165 }
166
167 void fill_ctdb_bool(bool *p)
168 {
169         if (rand_int(2) == 0) {
170                 *p = true;
171         } else {
172                 *p = false;
173         }
174 }
175
176 void verify_ctdb_bool(bool *p1, bool *p2)
177 {
178         assert(*p1 == *p2);
179 }
180
181 void fill_ctdb_string(TALLOC_CTX *mem_ctx, const char **p)
182 {
183         char *str;
184         int len;
185
186         len = rand_int(1024) + 2;
187         str = talloc_size(mem_ctx, len+1);
188         assert(str != NULL);
189
190         fill_string(str, len);
191         *p = str;
192 }
193
194 void verify_ctdb_string(const char **p1, const char **p2)
195 {
196         if (*p1 == NULL || *p2 == NULL) {
197                 assert(*p1 == *p2);
198         } else {
199                 verify_string(*p1, *p2);
200         }
201 }
202
203 void fill_ctdb_stringn(TALLOC_CTX *mem_ctx, const char **p)
204 {
205         fill_ctdb_string(mem_ctx, p);
206 }
207
208 void verify_ctdb_stringn(const char **p1, const char **p2)
209 {
210         verify_ctdb_string(p1, p2);
211 }
212
213 void fill_ctdb_pid(pid_t *p)
214 {
215         *p = rand32();
216 }
217
218 void verify_ctdb_pid(pid_t *p1, pid_t *p2)
219 {
220         assert(*p1 == *p2);
221 }
222
223 void fill_ctdb_timeval(struct timeval *p)
224 {
225         p->tv_sec = rand32();
226         p->tv_usec = rand_int(1000000);
227 }
228
229 void verify_ctdb_timeval(struct timeval *p1, struct timeval *p2)
230 {
231         assert(p1->tv_sec == p2->tv_sec);
232         assert(p1->tv_usec == p2->tv_usec);
233 }
234
235 void fill_tdb_data_nonnull(TALLOC_CTX *mem_ctx, TDB_DATA *p)
236 {
237         p->dsize = rand_int(1024) + 1;
238         p->dptr = talloc_array(mem_ctx, uint8_t, p->dsize);
239         assert(p->dptr != NULL);
240         fill_buffer(p->dptr, p->dsize);
241 }
242
243 void fill_tdb_data(TALLOC_CTX *mem_ctx, TDB_DATA *p)
244 {
245         if (rand_int(5) == 0) {
246                 p->dsize = 0;
247                 p->dptr = NULL;
248         } else {
249                 fill_tdb_data_nonnull(mem_ctx, p);
250         }
251 }
252
253 void verify_tdb_data(TDB_DATA *p1, TDB_DATA *p2)
254 {
255         assert(p1->dsize == p2->dsize);
256         verify_buffer(p1->dptr, p2->dptr, p1->dsize);
257 }
258
259 void fill_ctdb_tdb_data(TALLOC_CTX *mem_ctx, TDB_DATA *p)
260 {
261         fill_tdb_data(mem_ctx, p);
262 }
263
264 void verify_ctdb_tdb_data(TDB_DATA *p1, TDB_DATA *p2)
265 {
266         verify_tdb_data(p1, p2);
267 }
268
269 void fill_ctdb_statistics(TALLOC_CTX *mem_ctx, struct ctdb_statistics *p)
270 {
271         fill_buffer((uint8_t *)p, sizeof(struct ctdb_statistics));
272 }
273
274 void verify_ctdb_statistics(struct ctdb_statistics *p1,
275                             struct ctdb_statistics *p2)
276 {
277         verify_buffer(p1, p2, sizeof(struct ctdb_statistics));
278 }
279
280 void fill_ctdb_vnn_map(TALLOC_CTX *mem_ctx, struct ctdb_vnn_map *p)
281 {
282         int i;
283
284         p->generation = rand32();
285         p->size = rand_int(20);
286         if (p->size > 0) {
287                 p->map = talloc_array(mem_ctx, uint32_t, p->size);
288                 assert(p->map != NULL);
289
290                 for (i=0; i<p->size; i++) {
291                         p->map[i] = rand32();
292                 }
293         } else {
294                 p->map = NULL;
295         }
296 }
297
298 void verify_ctdb_vnn_map(struct ctdb_vnn_map *p1, struct ctdb_vnn_map *p2)
299 {
300         int i;
301
302         assert(p1->generation == p2->generation);
303         assert(p1->size == p2->size);
304         for (i=0; i<p1->size; i++) {
305                 assert(p1->map[i] == p2->map[i]);
306         }
307 }
308
309 void fill_ctdb_dbid(TALLOC_CTX *mem_ctx, struct ctdb_dbid *p)
310 {
311         p->db_id = rand32();
312         p->flags = rand8();
313 }
314
315 void verify_ctdb_dbid(struct ctdb_dbid *p1, struct ctdb_dbid *p2)
316 {
317         assert(p1->db_id == p2->db_id);
318         assert(p1->flags == p2->flags);
319 }
320
321 void fill_ctdb_dbid_map(TALLOC_CTX *mem_ctx, struct ctdb_dbid_map *p)
322 {
323         int i;
324
325         p->num = rand_int(40);
326         if (p->num > 0) {
327                 p->dbs = talloc_array(mem_ctx, struct ctdb_dbid, p->num);
328                 assert(p->dbs != NULL);
329                 for (i=0; i<p->num; i++) {
330                         fill_ctdb_dbid(mem_ctx, &p->dbs[i]);
331                 }
332         } else {
333                 p->dbs = NULL;
334         }
335 }
336
337 void verify_ctdb_dbid_map(struct ctdb_dbid_map *p1, struct ctdb_dbid_map *p2)
338 {
339         int i;
340
341         assert(p1->num == p2->num);
342         for (i=0; i<p1->num; i++) {
343                 verify_ctdb_dbid(&p1->dbs[i], &p2->dbs[i]);
344         }
345 }
346
347 void fill_ctdb_pulldb(TALLOC_CTX *mem_ctx, struct ctdb_pulldb *p)
348 {
349         p->db_id = rand32();
350         p->lmaster = rand32();
351 }
352
353 void verify_ctdb_pulldb(struct ctdb_pulldb *p1, struct ctdb_pulldb *p2)
354 {
355         assert(p1->db_id == p2->db_id);
356         assert(p1->lmaster == p2->lmaster);
357 }
358
359 void fill_ctdb_pulldb_ext(TALLOC_CTX *mem_ctx, struct ctdb_pulldb_ext *p)
360 {
361         p->db_id = rand32();
362         p->lmaster = rand32();
363         p->srvid = rand64();
364 }
365
366 void verify_ctdb_pulldb_ext(struct ctdb_pulldb_ext *p1,
367                             struct ctdb_pulldb_ext *p2)
368 {
369         assert(p1->db_id == p2->db_id);
370         assert(p1->lmaster == p2->lmaster);
371         assert(p1->srvid == p2->srvid);
372 }
373
374 void fill_ctdb_ltdb_header(TALLOC_CTX *mem_ctx, struct ctdb_ltdb_header *p)
375 {
376         p->rsn = rand64();
377         p->dmaster = rand32();
378         p->flags = rand32();
379 }
380
381 void verify_ctdb_ltdb_header(struct ctdb_ltdb_header *p1,
382                              struct ctdb_ltdb_header *p2)
383 {
384         assert(p1->rsn == p2->rsn);
385         assert(p1->dmaster == p2->dmaster);
386         assert(p1->flags == p2->flags);
387 }
388
389 void fill_ctdb_rec_data(TALLOC_CTX *mem_ctx, struct ctdb_rec_data *p)
390 {
391         p->reqid = rand32();
392         if (p->reqid % 5 == 0) {
393                 p->header = talloc(mem_ctx, struct ctdb_ltdb_header);
394                 assert(p->header != NULL);
395                 fill_ctdb_ltdb_header(mem_ctx, p->header);
396         } else {
397                 p->header = NULL;
398         }
399         fill_tdb_data_nonnull(mem_ctx, &p->key);
400         fill_tdb_data(mem_ctx, &p->data);
401 }
402
403 void verify_ctdb_rec_data(struct ctdb_rec_data *p1, struct ctdb_rec_data *p2)
404 {
405         struct ctdb_ltdb_header header;
406
407         assert(p1->reqid == p2->reqid);
408         if (p1->header != NULL) {
409                 assert(ctdb_ltdb_header_extract(&p2->data, &header) == 0);
410                 verify_ctdb_ltdb_header(p1->header, &header);
411         }
412         verify_tdb_data(&p1->key, &p2->key);
413         verify_tdb_data(&p1->data, &p2->data);
414 }
415
416 void fill_ctdb_rec_buffer(TALLOC_CTX *mem_ctx, struct ctdb_rec_buffer *p)
417 {
418         struct ctdb_rec_data rec;
419         int ret, i;
420         int count;
421
422         p->db_id = rand32();
423         p->count = 0;
424         p->buf = NULL;
425         p->buflen = 0;
426
427         count = rand_int(100);
428         if (count > 0) {
429                 for (i=0; i<count; i++) {
430                         fill_ctdb_rec_data(mem_ctx, &rec);
431                         ret = ctdb_rec_buffer_add(mem_ctx, p, rec.reqid,
432                                                   rec.header,
433                                                   rec.key, rec.data);
434                         assert(ret == 0);
435                 }
436         }
437 }
438
439 void verify_ctdb_rec_buffer(struct ctdb_rec_buffer *p1,
440                             struct ctdb_rec_buffer *p2)
441 {
442         assert(p1->db_id == p2->db_id);
443         assert(p1->count == p2->count);
444         assert(p1->buflen == p2->buflen);
445         verify_buffer(p1->buf, p2->buf, p1->buflen);
446 }
447
448 void fill_ctdb_traverse_start(TALLOC_CTX *mem_ctx,
449                               struct ctdb_traverse_start *p)
450 {
451         p->db_id = rand32();
452         p->reqid = rand32();
453         p->srvid = rand64();
454 }
455
456 void verify_ctdb_traverse_start(struct ctdb_traverse_start *p1,
457                                 struct ctdb_traverse_start *p2)
458 {
459         assert(p1->db_id == p2->db_id);
460         assert(p1->reqid == p2->reqid);
461         assert(p1->srvid == p2->srvid);
462 }
463
464 void fill_ctdb_traverse_all(TALLOC_CTX *mem_ctx,
465                             struct ctdb_traverse_all *p)
466 {
467         p->db_id = rand32();
468         p->reqid = rand32();
469         p->pnn = rand32();
470         p->client_reqid = rand32();
471         p->srvid = rand64();
472 }
473
474 void verify_ctdb_traverse_all(struct ctdb_traverse_all *p1,
475                               struct ctdb_traverse_all *p2)
476 {
477         assert(p1->db_id == p2->db_id);
478         assert(p1->reqid == p2->reqid);
479         assert(p1->pnn == p2->pnn);
480         assert(p1->client_reqid == p2->client_reqid);
481         assert(p1->srvid == p2->srvid);
482 }
483
484 void fill_ctdb_traverse_start_ext(TALLOC_CTX *mem_ctx,
485                                   struct ctdb_traverse_start_ext *p)
486 {
487         p->db_id = rand32();
488         p->reqid = rand32();
489         p->srvid = rand64();
490         p->withemptyrecords = rand_int(2);
491 }
492
493 void verify_ctdb_traverse_start_ext(struct ctdb_traverse_start_ext *p1,
494                                     struct ctdb_traverse_start_ext *p2)
495 {
496         assert(p1->db_id == p2->db_id);
497         assert(p1->reqid == p2->reqid);
498         assert(p1->srvid == p2->srvid);
499         assert(p1->withemptyrecords == p2->withemptyrecords);
500 }
501
502 void fill_ctdb_traverse_all_ext(TALLOC_CTX *mem_ctx,
503                                 struct ctdb_traverse_all_ext *p)
504 {
505         p->db_id = rand32();
506         p->reqid = rand32();
507         p->pnn = rand32();
508         p->client_reqid = rand32();
509         p->srvid = rand64();
510         p->withemptyrecords = rand_int(2);
511 }
512
513 void verify_ctdb_traverse_all_ext(struct ctdb_traverse_all_ext *p1,
514                                   struct ctdb_traverse_all_ext *p2)
515 {
516         assert(p1->db_id == p2->db_id);
517         assert(p1->reqid == p2->reqid);
518         assert(p1->pnn == p2->pnn);
519         assert(p1->client_reqid == p2->client_reqid);
520         assert(p1->srvid == p2->srvid);
521         assert(p1->withemptyrecords == p2->withemptyrecords);
522 }
523
524 void fill_ctdb_sock_addr(TALLOC_CTX *mem_ctx, ctdb_sock_addr *p)
525 {
526         if (rand_int(2) == 0) {
527                 p->ip.sin_family = AF_INET;
528                 p->ip.sin_port = rand_int(65535);
529                 fill_buffer(&p->ip.sin_addr, sizeof(struct in_addr));
530         } else {
531                 p->ip6.sin6_family = AF_INET6;
532                 p->ip6.sin6_port = rand_int(65535);
533                 fill_buffer(&p->ip6.sin6_addr, sizeof(struct in6_addr));
534         }
535 }
536
537 void verify_ctdb_sock_addr(ctdb_sock_addr *p1, ctdb_sock_addr *p2)
538 {
539         assert(p1->sa.sa_family == p2->sa.sa_family);
540         if (p1->sa.sa_family == AF_INET) {
541                 assert(p1->ip.sin_port == p2->ip.sin_port);
542                 verify_buffer(&p1->ip.sin_addr, &p2->ip.sin_addr,
543                                    sizeof(struct in_addr));
544         } else {
545                 assert(p1->ip6.sin6_port == p2->ip6.sin6_port);
546                 verify_buffer(&p1->ip6.sin6_addr, &p2->ip6.sin6_addr,
547                                    sizeof(struct in6_addr));
548         }
549 }
550
551 void fill_ctdb_connection(TALLOC_CTX *mem_ctx, struct ctdb_connection *p)
552 {
553         fill_ctdb_sock_addr(mem_ctx, &p->src);
554         fill_ctdb_sock_addr(mem_ctx, &p->dst);
555 }
556
557 void verify_ctdb_connection(struct ctdb_connection *p1,
558                             struct ctdb_connection *p2)
559 {
560         verify_ctdb_sock_addr(&p1->src, &p2->src);
561         verify_ctdb_sock_addr(&p1->dst, &p2->dst);
562 }
563
564 void fill_ctdb_tunable(TALLOC_CTX *mem_ctx, struct ctdb_tunable *p)
565 {
566         fill_ctdb_string(mem_ctx, &p->name);
567         p->value = rand32();
568 }
569
570 void verify_ctdb_tunable(struct ctdb_tunable *p1, struct ctdb_tunable *p2)
571 {
572         verify_ctdb_string(&p1->name, &p2->name);
573         assert(p1->value == p2->value);
574 }
575
576 void fill_ctdb_node_flag_change(TALLOC_CTX *mem_ctx,
577                                 struct ctdb_node_flag_change *p)
578 {
579         p->pnn = rand32();
580         p->new_flags = rand32();
581         p->old_flags = rand32();
582 }
583
584 void verify_ctdb_node_flag_change(struct ctdb_node_flag_change *p1,
585                                   struct ctdb_node_flag_change *p2)
586 {
587         assert(p1->pnn == p2->pnn);
588         assert(p1->new_flags == p2->new_flags);
589         assert(p1->old_flags == p2->old_flags);
590 }
591
592 void fill_ctdb_var_list(TALLOC_CTX *mem_ctx, struct ctdb_var_list *p)
593 {
594         int i;
595
596         p->count = rand_int(100) + 1;
597         p->var = talloc_array(mem_ctx, const char *, p->count);
598         for (i=0; i<p->count; i++) {
599                 fill_ctdb_string(p->var, &p->var[i]);
600         }
601 }
602
603 void verify_ctdb_var_list(struct ctdb_var_list *p1, struct ctdb_var_list *p2)
604 {
605         int i;
606
607         assert(p1->count == p2->count);
608         for (i=0; i<p1->count; i++) {
609                 verify_ctdb_string(&p1->var[i], &p2->var[i]);
610         }
611 }
612
613 void fill_ctdb_tunable_list(TALLOC_CTX *mem_ctx, struct ctdb_tunable_list *p)
614 {
615         fill_buffer(p, sizeof(struct ctdb_tunable_list));
616 }
617
618 void verify_ctdb_tunable_list(struct ctdb_tunable_list *p1,
619                               struct ctdb_tunable_list *p2)
620 {
621         verify_buffer(p1, p2, sizeof(struct ctdb_tunable_list));
622 }
623
624 void fill_ctdb_tickle_list(TALLOC_CTX *mem_ctx, struct ctdb_tickle_list *p)
625 {
626         int i;
627
628         fill_ctdb_sock_addr(mem_ctx, &p->addr);
629         p->num = rand_int(1000);
630         if (p->num > 0) {
631                 p->conn = talloc_array(mem_ctx, struct ctdb_connection, p->num);
632                 assert(p->conn != NULL);
633                 for (i=0; i<p->num; i++) {
634                         fill_ctdb_connection(mem_ctx, &p->conn[i]);
635                 }
636         } else {
637                 p->conn = NULL;
638         }
639 }
640
641 void verify_ctdb_tickle_list(struct ctdb_tickle_list *p1,
642                              struct ctdb_tickle_list *p2)
643 {
644         int i;
645
646         verify_ctdb_sock_addr(&p1->addr, &p2->addr);
647         assert(p1->num == p2->num);
648         for (i=0; i<p1->num; i++) {
649                 verify_ctdb_connection(&p1->conn[i], &p2->conn[i]);
650         }
651 }
652
653 void fill_ctdb_addr_info(TALLOC_CTX *mem_ctx, struct ctdb_addr_info *p)
654 {
655         fill_ctdb_sock_addr(mem_ctx, &p->addr);
656         p->mask = rand_int(33);
657         if (rand_int(2) == 0) {
658                 p->iface = NULL;
659         } else {
660                 fill_ctdb_string(mem_ctx, &p->iface);
661         }
662 }
663
664 void verify_ctdb_addr_info(struct ctdb_addr_info *p1,
665                            struct ctdb_addr_info *p2)
666 {
667         verify_ctdb_sock_addr(&p1->addr, &p2->addr);
668         assert(p1->mask == p2->mask);
669         verify_ctdb_string(&p1->iface, &p2->iface);
670 }
671
672 void fill_ctdb_transdb(TALLOC_CTX *mem_ctx, struct ctdb_transdb *p)
673 {
674         p->db_id = rand32();
675         p->tid = rand32();
676 }
677
678 void verify_ctdb_transdb(struct ctdb_transdb *p1, struct ctdb_transdb *p2)
679 {
680         assert(p1->db_id == p2->db_id);
681         assert(p1->tid == p2->tid);
682 }
683
684 void fill_ctdb_uptime(TALLOC_CTX *mem_ctx, struct ctdb_uptime *p)
685 {
686         fill_buffer(p, sizeof(struct ctdb_uptime));
687 }
688
689 void verify_ctdb_uptime(struct ctdb_uptime *p1, struct ctdb_uptime *p2)
690 {
691         verify_buffer(p1, p2, sizeof(struct ctdb_uptime));
692 }
693
694 void fill_ctdb_public_ip(TALLOC_CTX *mem_ctx, struct ctdb_public_ip *p)
695 {
696         p->pnn = rand32();
697         fill_ctdb_sock_addr(mem_ctx, &p->addr);
698 }
699
700 void verify_ctdb_public_ip(struct ctdb_public_ip *p1,
701                            struct ctdb_public_ip *p2)
702 {
703         assert(p1->pnn == p2->pnn);
704         verify_ctdb_sock_addr(&p1->addr, &p2->addr);
705 }
706
707 void fill_ctdb_public_ip_list(TALLOC_CTX *mem_ctx,
708                               struct ctdb_public_ip_list *p)
709 {
710         int i;
711
712         p->num = rand_int(32);
713         if (p->num > 0) {
714                 p->ip = talloc_array(mem_ctx, struct ctdb_public_ip, p->num);
715                 assert(p->ip != NULL);
716                 for (i=0; i<p->num; i++) {
717                         fill_ctdb_public_ip(mem_ctx, &p->ip[i]);
718                 }
719         } else {
720                 p->ip = NULL;
721         }
722 }
723
724 void verify_ctdb_public_ip_list(struct ctdb_public_ip_list *p1,
725                                 struct ctdb_public_ip_list *p2)
726 {
727         int i;
728
729         assert(p1->num == p2->num);
730         for (i=0; i<p1->num; i++) {
731                 verify_ctdb_public_ip(&p1->ip[i], &p2->ip[i]);
732         }
733 }
734
735 void fill_ctdb_node_and_flags(TALLOC_CTX *mem_ctx,
736                               struct ctdb_node_and_flags *p)
737 {
738         p->pnn = rand32();
739         p->flags = rand32();
740         fill_ctdb_sock_addr(mem_ctx, &p->addr);
741 }
742
743 void verify_ctdb_node_and_flags(struct ctdb_node_and_flags *p1,
744                                 struct ctdb_node_and_flags *p2)
745 {
746         assert(p1->pnn == p2->pnn);
747         assert(p1->flags == p2->flags);
748         verify_ctdb_sock_addr(&p1->addr, &p2->addr);
749 }
750
751 void fill_ctdb_node_map(TALLOC_CTX *mem_ctx, struct ctdb_node_map *p)
752 {
753         int i;
754
755         p->num = rand_int(32);
756         if (p->num > 0) {
757                 p->node = talloc_array(mem_ctx, struct ctdb_node_and_flags,
758                                        p->num);
759                 assert(p->node != NULL);
760                 for (i=0; i<p->num; i++) {
761                         fill_ctdb_node_and_flags(mem_ctx, &p->node[i]);
762                 }
763         } else {
764                 p->node = NULL;
765         }
766 }
767
768 void verify_ctdb_node_map(struct ctdb_node_map *p1, struct ctdb_node_map *p2)
769 {
770         int i;
771
772         assert(p1->num == p2->num);
773         for (i=0; i<p1->num; i++) {
774                 verify_ctdb_node_and_flags(&p1->node[i], &p2->node[i]);
775         }
776 }
777
778 void fill_ctdb_script(TALLOC_CTX *mem_ctx, struct ctdb_script *p)
779 {
780         fill_buffer(p, sizeof(struct ctdb_script));
781 }
782
783 void verify_ctdb_script(struct ctdb_script *p1, struct ctdb_script *p2)
784 {
785         verify_buffer(p1, p2, sizeof(struct ctdb_script));
786 }
787
788 void fill_ctdb_script_list(TALLOC_CTX *mem_ctx, struct ctdb_script_list *p)
789 {
790         int i;
791
792         p->num_scripts = rand_int(32);
793         if (p->num_scripts > 0) {
794                 p->script = talloc_array(mem_ctx, struct ctdb_script,
795                                          p->num_scripts);
796                 assert(p->script != NULL);
797                 for (i=0; i<p->num_scripts; i++) {
798                         fill_ctdb_script(mem_ctx, &p->script[i]);
799                 }
800         } else {
801                 p->script = NULL;
802         }
803 }
804
805 void verify_ctdb_script_list(struct ctdb_script_list *p1,
806                              struct ctdb_script_list *p2)
807 {
808         int i;
809
810         assert(p1->num_scripts == p2->num_scripts);
811         for (i=0; i<p1->num_scripts; i++) {
812                 verify_ctdb_script(&p1->script[i], &p2->script[i]);
813         }
814 }
815
816 void fill_ctdb_ban_state(TALLOC_CTX *mem_ctx, struct ctdb_ban_state *p)
817 {
818         p->pnn = rand32();
819         p->time = rand32();
820 }
821
822 void verify_ctdb_ban_state(struct ctdb_ban_state *p1,
823                            struct ctdb_ban_state *p2)
824 {
825         assert(p1->pnn == p2->pnn);
826         assert(p1->time == p2->time);
827 }
828
829 void fill_ctdb_notify_data(TALLOC_CTX *mem_ctx, struct ctdb_notify_data *p)
830 {
831         p->srvid = rand64();
832         fill_tdb_data(mem_ctx, &p->data);
833 }
834
835 void verify_ctdb_notify_data(struct ctdb_notify_data *p1,
836                              struct ctdb_notify_data *p2)
837 {
838         assert(p1->srvid == p2->srvid);
839         verify_tdb_data(&p1->data, &p2->data);
840 }
841
842 void fill_ctdb_iface(TALLOC_CTX *mem_ctx, struct ctdb_iface *p)
843 {
844         fill_buffer(p, sizeof(struct ctdb_iface));
845 }
846
847 void verify_ctdb_iface(struct ctdb_iface *p1, struct ctdb_iface *p2)
848 {
849         verify_buffer(p1, p2, sizeof(struct ctdb_iface));
850 }
851
852 void fill_ctdb_iface_list(TALLOC_CTX *mem_ctx, struct ctdb_iface_list *p)
853 {
854         int i;
855
856         p->num = rand_int(32);
857         if (p->num > 0) {
858                 p->iface = talloc_array(mem_ctx, struct ctdb_iface, p->num);
859                 assert(p->iface != NULL);
860                 for (i=0; i<p->num; i++) {
861                         fill_ctdb_iface(mem_ctx, &p->iface[i]);
862                 }
863         } else {
864                 p->iface = NULL;
865         }
866 }
867
868 void verify_ctdb_iface_list(struct ctdb_iface_list *p1,
869                             struct ctdb_iface_list *p2)
870 {
871         int i;
872
873         assert(p1->num == p2->num);
874         for (i=0; i<p1->num; i++) {
875                 verify_ctdb_iface(&p1->iface[i], &p2->iface[i]);
876         }
877 }
878
879 void fill_ctdb_public_ip_info(TALLOC_CTX *mem_ctx,
880                               struct ctdb_public_ip_info *p)
881 {
882         fill_ctdb_public_ip(mem_ctx, &p->ip);
883         p->active_idx = rand_int(32) + 1;
884         p->ifaces = talloc(mem_ctx, struct ctdb_iface_list);
885         assert(p->ifaces != NULL);
886         fill_ctdb_iface_list(mem_ctx, p->ifaces);
887 }
888
889 void verify_ctdb_public_ip_info(struct ctdb_public_ip_info *p1,
890                                 struct ctdb_public_ip_info *p2)
891 {
892         verify_ctdb_public_ip(&p1->ip, &p2->ip);
893         assert(p1->active_idx == p2->active_idx);
894         verify_ctdb_iface_list(p1->ifaces, p2->ifaces);
895 }
896
897 void fill_ctdb_statistics_list(TALLOC_CTX *mem_ctx,
898                                struct ctdb_statistics_list *p)
899 {
900         int i;
901
902         p->num = rand_int(10);
903         if (p->num > 0) {
904                 p->stats = talloc_array(mem_ctx, struct ctdb_statistics,
905                                         p->num);
906                 assert(p->stats != NULL);
907
908                 for (i=0; i<p->num; i++) {
909                         fill_ctdb_statistics(mem_ctx, &p->stats[i]);
910                 }
911         } else {
912                 p->stats = NULL;
913         }
914 }
915
916 void verify_ctdb_statistics_list(struct ctdb_statistics_list *p1,
917                                  struct ctdb_statistics_list *p2)
918 {
919         int i;
920
921         assert(p1->num == p2->num);
922         for (i=0; i<p1->num; i++) {
923                 verify_ctdb_statistics(&p1->stats[i], &p2->stats[i]);
924         }
925 }
926
927 void fill_ctdb_key_data(TALLOC_CTX *mem_ctx, struct ctdb_key_data *p)
928 {
929         p->db_id = rand32();
930         fill_ctdb_ltdb_header(mem_ctx, &p->header);
931         fill_tdb_data_nonnull(mem_ctx, &p->key);
932 }
933
934 void verify_ctdb_key_data(struct ctdb_key_data *p1, struct ctdb_key_data *p2)
935 {
936         assert(p1->db_id == p2->db_id);
937         verify_ctdb_ltdb_header(&p1->header, &p2->header);
938         verify_tdb_data(&p1->key, &p2->key);
939 }
940
941 void fill_ctdb_db_statistics(TALLOC_CTX *mem_ctx,
942                              struct ctdb_db_statistics *p)
943 {
944         int i;
945
946         fill_buffer(p, offsetof(struct ctdb_db_statistics, num_hot_keys));
947         p->num_hot_keys = 10;
948         for (i=0; i<p->num_hot_keys; i++) {
949                 p->hot_keys[i].count = rand32();
950                 fill_tdb_data(mem_ctx, &p->hot_keys[i].key);
951         }
952 }
953
954 void verify_ctdb_db_statistics(struct ctdb_db_statistics *p1,
955                                struct ctdb_db_statistics *p2)
956 {
957         int i;
958
959         verify_buffer(p1, p2, offsetof(struct ctdb_db_statistics,
960                                             num_hot_keys));
961         assert(p1->num_hot_keys == p2->num_hot_keys);
962         for (i=0; i<p1->num_hot_keys; i++) {
963                 assert(p1->hot_keys[i].count == p2->hot_keys[i].count);
964                 verify_tdb_data(&p1->hot_keys[i].key, &p2->hot_keys[i].key);
965         }
966 }
967
968 void fill_ctdb_election_message(TALLOC_CTX *mem_ctx,
969                                 struct ctdb_election_message *p)
970 {
971         p->num_connected = rand_int(32);
972         fill_buffer(&p->priority_time, sizeof(struct timeval));
973         p->pnn = rand_int(32);
974         p->node_flags = rand32();
975 }
976
977 void verify_ctdb_election_message(struct ctdb_election_message *p1,
978                                   struct ctdb_election_message *p2)
979 {
980         assert(p1->num_connected == p2->num_connected);
981         verify_buffer(p1, p2, sizeof(struct timeval));
982         assert(p1->pnn == p2->pnn);
983         assert(p1->node_flags == p2->node_flags);
984 }
985
986 void fill_ctdb_srvid_message(TALLOC_CTX *mem_ctx,
987                              struct ctdb_srvid_message *p)
988 {
989         p->pnn = rand_int(32);
990         p->srvid = rand64();
991 }
992
993 void verify_ctdb_srvid_message(struct ctdb_srvid_message *p1,
994                                struct ctdb_srvid_message *p2)
995 {
996         assert(p1->pnn == p2->pnn);
997         assert(p1->srvid == p2->srvid);
998 }
999
1000 void fill_ctdb_disable_message(TALLOC_CTX *mem_ctx,
1001                                struct ctdb_disable_message *p)
1002 {
1003         p->pnn = rand_int(32);
1004         p->srvid = rand64();
1005         p->timeout = rand32();
1006 }
1007
1008 void verify_ctdb_disable_message(struct ctdb_disable_message *p1,
1009                                  struct ctdb_disable_message *p2)
1010 {
1011         assert(p1->pnn == p2->pnn);
1012         assert(p1->srvid == p2->srvid);
1013         assert(p1->timeout == p2->timeout);
1014 }
1015
1016 void fill_ctdb_server_id(TALLOC_CTX *mem_ctx, struct ctdb_server_id *p)
1017 {
1018         p->pid = rand64();
1019         p->task_id = rand32();
1020         p->vnn = rand_int(32);
1021         p->unique_id = rand64();
1022 }
1023
1024 void verify_ctdb_server_id(struct ctdb_server_id *p1,
1025                            struct ctdb_server_id *p2)
1026 {
1027         assert(p1->pid == p2->pid);
1028         assert(p1->task_id == p2->task_id);
1029         assert(p1->vnn == p2->vnn);
1030         assert(p1->unique_id == p2->unique_id);
1031 }
1032
1033 void fill_ctdb_g_lock(TALLOC_CTX *mem_ctx, struct ctdb_g_lock *p)
1034 {
1035         p->type = rand_int(2);
1036         fill_ctdb_server_id(mem_ctx, &p->sid);
1037 }
1038
1039 void verify_ctdb_g_lock(struct ctdb_g_lock *p1, struct ctdb_g_lock *p2)
1040 {
1041         assert(p1->type == p2->type);
1042         verify_ctdb_server_id(&p1->sid, &p2->sid);
1043 }
1044
1045 void fill_ctdb_g_lock_list(TALLOC_CTX *mem_ctx, struct ctdb_g_lock_list *p)
1046 {
1047         int i;
1048
1049         p->num = rand_int(20) + 1;
1050         p->lock = talloc_array(mem_ctx, struct ctdb_g_lock, p->num);
1051         assert(p->lock != NULL);
1052         for (i=0; i<p->num; i++) {
1053                 fill_ctdb_g_lock(mem_ctx, &p->lock[i]);
1054         }
1055 }
1056
1057 void verify_ctdb_g_lock_list(struct ctdb_g_lock_list *p1,
1058                              struct ctdb_g_lock_list *p2)
1059 {
1060         int i;
1061
1062         assert(p1->num == p2->num);
1063         for (i=0; i<p1->num; i++) {
1064                 verify_ctdb_g_lock(&p1->lock[i], &p2->lock[i]);
1065         }
1066 }