-
Notifications
You must be signed in to change notification settings - Fork 55
/
selftest.c
1920 lines (1674 loc) · 64.6 KB
/
selftest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Selftest. To be run for every commit.
*/
#include <ZeroTierSockets.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <time.h>
#define LIBZT_DEBUG 1
#include "../src/Debug.hpp"
#pragma GCC diagnostic ignored "-Wunused-value"
int random32()
{
const int BITS_PER_RAND = (int)(log2(RAND_MAX / 2 + 1) + 1.0);
int ret = 0;
for (int i = 0; i < sizeof(int) * CHAR_BIT; i += BITS_PER_RAND) {
ret <<= BITS_PER_RAND;
ret |= rand();
}
return ret;
}
uint64_t random64()
{
return ((uint64_t)random32() << 32) | random32();
}
//----------------------------------------------------------------------------//
// Test parameters and variables //
//----------------------------------------------------------------------------//
int verbosity = 2;
int callback_was_called_flag = 0;
// Used throughout the selftest
char keypair_i[ZTS_ID_STR_BUF_LEN];
//----------------------------------------------------------------------------//
// Event handler //
//----------------------------------------------------------------------------//
void print_node_details(const char* msg, zts_node_info_t* d)
{
DEBUG_INFO(" %s", msg);
if (verbosity < 2) {
return;
}
DEBUG_INFO(" msg->node->node_id : %10llx", d->node_id);
DEBUG_INFO(" msg->node->port_primary : %10d", d->port_primary);
DEBUG_INFO(" msg->node->port_secondary : %10d", d->port_secondary);
DEBUG_INFO(" msg->node->port_tertiary : %10d", d->port_tertiary);
DEBUG_INFO(" msg->node->ver_major : %10d", d->ver_major);
DEBUG_INFO(" msg->node->ver_minor : %10d", d->ver_minor);
DEBUG_INFO(" msg->node->ver_rev : %10d", d->ver_rev);
}
void print_net_details(const char* msg, zts_net_info_t* d)
{
DEBUG_INFO(" %s", msg);
if (verbosity < 2) {
return;
}
DEBUG_INFO(" net_id : %16llx", d->net_id);
DEBUG_INFO(" mac : %llx", d->mac);
DEBUG_INFO(" name : %s", d->name);
DEBUG_INFO(" type : %d", d->type);
DEBUG_INFO(" mtu : %d", d->mtu);
DEBUG_INFO(" dhcp : %d", d->dhcp);
DEBUG_INFO(" bridge : %d", d->bridge);
DEBUG_INFO(" broadcast_enabled : %d", d->broadcast_enabled);
DEBUG_INFO(" port_error : %d", d->port_error);
DEBUG_INFO(" netconf_rev : %lu", d->netconf_rev);
DEBUG_INFO(" route_count : %d", d->route_count);
DEBUG_INFO(" multicast_sub_count : %d", d->multicast_sub_count);
for (int i = 0; i < d->multicast_sub_count; i++) {
DEBUG_INFO("\t - mac=%llx, adi=%x", d->multicast_subs[i].mac, d->multicast_subs[i].adi);
}
DEBUG_INFO("\t- addresses:");
for (int i = 0; i < d->assigned_addr_count; i++) {
if (d->assigned_addrs[i].ss_family == ZTS_AF_INET) {
char ipstr[ZTS_INET_ADDRSTRLEN] = { 0 };
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(d->assigned_addrs[i]);
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
DEBUG_INFO("\t - %s", ipstr);
}
if (d->assigned_addrs[i].ss_family == ZTS_AF_INET6) {
char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 };
struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&(d->assigned_addrs[i]);
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN);
DEBUG_INFO("\t - %s", ipstr);
}
}
char target[ZTS_INET6_ADDRSTRLEN] = { 0 };
char via[ZTS_INET6_ADDRSTRLEN] = { 0 };
DEBUG_INFO("\t- routes:");
for (int i = 0; i < d->route_count; i++) {
if (d->routes[i].target.ss_family == ZTS_AF_INET) {
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(d->routes[i].target);
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), target, ZTS_INET6_ADDRSTRLEN);
in4 = (struct zts_sockaddr_in*)&(d->routes[i].via);
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), via, ZTS_INET6_ADDRSTRLEN);
}
if (d->routes[i].target.ss_family == ZTS_AF_INET6) {
struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&(d->routes[i].target);
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), target, ZTS_INET6_ADDRSTRLEN);
in6 = (struct zts_sockaddr_in6*)&(d->routes[i].via);
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), via, ZTS_INET6_ADDRSTRLEN);
}
DEBUG_INFO("\t - target : %s", target);
DEBUG_INFO("\t - via : %s", via);
DEBUG_INFO("\t - flags : %d", d->routes[i].flags);
DEBUG_INFO("\t - metric : %d", d->routes[i].metric);
}
}
void print_peer_details(const char* msg, zts_peer_info_t* d)
{
DEBUG_INFO(" %s", msg);
if (verbosity < 2) {
return;
}
DEBUG_INFO("\t- peer : %llx", d->peer_id);
DEBUG_INFO("\t- role : %d", d->role);
DEBUG_INFO("\t- latency : %d", d->latency);
DEBUG_INFO("\t- version : %d.%d.%d", d->ver_major, d->ver_minor, d->ver_rev);
DEBUG_INFO("\t- path_count : %d", d->path_count);
DEBUG_INFO("\t- paths:");
// Print all known paths for each peer
for (unsigned int j = 0; j < d->path_count; j++) {
char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 };
int port = 0;
struct zts_sockaddr* sa = (struct zts_sockaddr*)&(d->paths[j].address);
if (sa->sa_family == ZTS_AF_INET) {
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)sa;
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
port = ntohs(in4->sin_port);
}
if (sa->sa_family == ZTS_AF_INET6) {
struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)sa;
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN);
}
DEBUG_INFO("\t - %15s : %6d", ipstr, port);
}
DEBUG_INFO("");
}
void print_netif_details(const char* msg, zts_netif_info_t* d)
{
DEBUG_INFO(" %s", msg);
if (verbosity < 2) {
return;
}
DEBUG_INFO("\t- net_id : %llx", d->net_id);
DEBUG_INFO("\t- mac : %llx", d->mac);
DEBUG_INFO("\t- mtu : %d", d->mtu);
}
void print_route_details(const char* msg, zts_route_info_t* d)
{
DEBUG_INFO("%s", msg);
if (verbosity < 2) {
return;
}
}
void print_address_details(const char* msg, zts_addr_info_t* d)
{
DEBUG_INFO(" %s", msg);
if (verbosity < 2) {
return;
}
char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 };
struct zts_sockaddr* sa = (struct zts_sockaddr*)&(d->addr);
if (sa->sa_family == ZTS_AF_INET) {
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(d->addr);
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
}
if (sa->sa_family == ZTS_AF_INET6) {
struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&(d->addr);
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN);
}
DEBUG_INFO(" network : %llx", d->net_id);
DEBUG_INFO(" addr : %s", ipstr);
}
//----------------------------------------------------------------------------//
// Event Handler //
//----------------------------------------------------------------------------//
#define ZTS_NODE_EVENT(code) code >= ZTS_EVENT_NODE_UP&& code <= ZTS_EVENT_NODE_FATAL_ERROR
#define ZTS_NETWORK_EVENT(code) code >= ZTS_EVENT_NETWORK_NOT_FOUND&& code <= ZTS_EVENT_NETWORK_UPDATE
#define ZTS_STACK_EVENT(code) code >= ZTS_EVENT_STACK_UP&& code <= ZTS_EVENT_STACK_DOWN
#define ZTS_NETIF_EVENT(code) code >= ZTS_EVENT_NETIF_UP&& code <= ZTS_EVENT_NETIF_LINK_DOWN
#define ZTS_PEER_EVENT(code) code >= ZTS_EVENT_PEER_DIRECT&& code <= ZTS_EVENT_PEER_PATH_DEAD
#define ZTS_ROUTE_EVENT(code) code >= ZTS_EVENT_ROUTE_ADDED&& code <= ZTS_EVENT_ROUTE_REMOVED
#define ZTS_ADDR_EVENT(code) code >= ZTS_EVENT_ADDR_ADDED_IP4&& code <= ZTS_EVENT_ADDR_REMOVED_IP6
#define ZTS_STORE_EVENT(code) code >= ZTS_EVENT_STORE_IDENTITY_SECRET&& code <= ZTS_EVENT_STORE_NETWORK
void on_zts_event(void* msgPtr)
{
/* Used to signal that a callback was sent to the user, we test for this
value later */
callback_was_called_flag = 1;
assert(("msgPtr is null", msgPtr != NULL));
zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr;
assert(("Invalid callback event code", msg->event_code > 0));
DEBUG_INFO(" msg->event_code = %d", msg->event_code);
if (msg->node) {
DEBUG_INFO(" msg->node = %p", (void*)(msg->node));
}
if (msg->network) {
DEBUG_INFO(" msg->network = %p", (void*)(msg->network));
}
if (msg->netif) {
DEBUG_INFO(" msg->netif = %p", (void*)(msg->netif));
}
if (msg->route) {
DEBUG_INFO(" msg->route = %p", (void*)(msg->route));
}
if (msg->peer) {
DEBUG_INFO(" msg->peer = %p", (void*)(msg->peer));
}
if (msg->addr) {
DEBUG_INFO(" msg->addr = %p", (void*)(msg->addr));
}
// Ensure references to structures are valid when needed
assert((msg->node == NULL) ^ (msg->node && ZTS_NODE_EVENT(msg->event_code)));
assert((msg->network == NULL) ^ (msg->network && ZTS_NETWORK_EVENT(msg->event_code)));
assert((msg->netif == NULL) ^ (msg->netif && ZTS_NETIF_EVENT(msg->event_code)));
assert((msg->peer == NULL) ^ (msg->peer && ZTS_PEER_EVENT(msg->event_code)));
assert((msg->route == NULL) ^ (msg->route && ZTS_ROUTE_EVENT(msg->event_code)));
assert((msg->addr == NULL) ^ (msg->addr && ZTS_ADDR_EVENT(msg->event_code)));
assert((msg->cache == NULL) ^ (msg->cache && ZTS_STORE_EVENT(msg->event_code)));
// Node events
if (msg->event_code == ZTS_EVENT_NODE_UP) {
print_node_details("ZTS_EVENT_NODE_UP", msg->node);
}
if (msg->event_code == ZTS_EVENT_NODE_ONLINE) {
print_node_details("ZTS_EVENT_NODE_ONLINE", msg->node);
}
if (msg->event_code == ZTS_EVENT_NODE_OFFLINE) {
print_node_details("ZTS_EVENT_NODE_OFFLINE", msg->node);
}
if (msg->event_code == ZTS_EVENT_NODE_DOWN) {
print_node_details("ZTS_EVENT_NODE_DOWN", msg->node);
}
if (msg->event_code == ZTS_EVENT_NODE_FATAL_ERROR) {
print_node_details("ZTS_EVENT_NODE_FATAL_ERROR", msg->node);
}
// Network events
if (msg->event_code == ZTS_EVENT_NETWORK_NOT_FOUND) {
print_net_details("ZTS_EVENT_NETWORK_NOT_FOUND", msg->network);
}
if (msg->event_code == ZTS_EVENT_NETWORK_CLIENT_TOO_OLD) {
print_net_details("ZTS_EVENT_NETWORK_CLIENT_TOO_OLD", msg->network);
}
if (msg->event_code == ZTS_EVENT_NETWORK_REQ_CONFIG) {
print_net_details("ZTS_EVENT_NETWORK_REQ_CONFIG", msg->network);
}
if (msg->event_code == ZTS_EVENT_NETWORK_OK) {
print_net_details("ZTS_EVENT_NETWORK_OK", msg->network);
}
if (msg->event_code == ZTS_EVENT_NETWORK_ACCESS_DENIED) {
print_net_details("ZTS_EVENT_NETWORK_ACCESS_DENIED", msg->network);
}
if (msg->event_code == ZTS_EVENT_NETWORK_READY_IP6) {
print_net_details("ZTS_EVENT_NETWORK_READY_IP6", msg->network);
}
if (msg->event_code == ZTS_EVENT_NETWORK_READY_IP4) {
print_net_details("ZTS_EVENT_NETWORK_READY_IP4", msg->network);
}
if (msg->event_code == ZTS_EVENT_NETWORK_READY_IP4_IP6) {
print_net_details("ZTS_EVENT_NETWORK_READY_IP4_IP6", msg->network);
}
if (msg->event_code == ZTS_EVENT_NETWORK_DOWN) {
print_net_details("ZTS_EVENT_NETWORK_DOWN", msg->network);
}
if (msg->event_code == ZTS_EVENT_NETWORK_UPDATE) {
print_net_details("ZTS_EVENT_NETWORK_UPDATE", msg->network);
}
// Stack events
if (msg->event_code == ZTS_EVENT_STACK_UP) {
// print_stack_details("ZTS_EVENT_STACK_UP", msg->stack);
}
if (msg->event_code == ZTS_EVENT_STACK_DOWN) {
// print_stack_details("ZTS_EVENT_STACK_DOWN", msg->stack);
}
// Netif events
if (msg->event_code == ZTS_EVENT_NETIF_UP) {
print_netif_details("ZTS_EVENT_NETIF_UP", msg->netif);
}
if (msg->event_code == ZTS_EVENT_NETIF_DOWN) {
print_netif_details("ZTS_EVENT_NETIF_DOWN", msg->netif);
}
if (msg->event_code == ZTS_EVENT_NETIF_REMOVED) {
print_netif_details("ZTS_EVENT_NETIF_REMOVED", msg->netif);
}
if (msg->event_code == ZTS_EVENT_NETIF_LINK_UP) {
print_netif_details("ZTS_EVENT_NETIF_LINK_UP", msg->netif);
}
if (msg->event_code == ZTS_EVENT_NETIF_LINK_DOWN) {
print_netif_details("ZTS_EVENT_NETIF_LINK_DOWN", msg->netif);
}
// Peer events
if (msg->peer) {
if (msg->peer->role == ZTS_PEER_ROLE_PLANET) {
/* Safe to ignore, these are our roots. They orchestrate the P2P
connection. You might also see other unknown peers, these are our
network controllers. */
return;
}
}
if (msg->event_code == ZTS_EVENT_PEER_DIRECT) {
print_peer_details("ZTS_EVENT_PEER_DIRECT", msg->peer);
}
if (msg->event_code == ZTS_EVENT_PEER_RELAY) {
print_peer_details("ZTS_EVENT_PEER_RELAY", msg->peer);
}
if (msg->event_code == ZTS_EVENT_PEER_UNREACHABLE) {
print_peer_details("ZTS_EVENT_PEER_UNREACHABLE", msg->peer);
}
if (msg->event_code == ZTS_EVENT_PEER_PATH_DISCOVERED) {
print_peer_details("ZTS_EVENT_PEER_PATH_DISCOVERED", msg->peer);
}
if (msg->event_code == ZTS_EVENT_PEER_PATH_DEAD) {
print_peer_details("ZTS_EVENT_PEER_PATH_DEAD", msg->peer);
}
// Route events
if (msg->event_code == ZTS_EVENT_ROUTE_ADDED) {
print_route_details("ZTS_EVENT_ROUTE_ADDED", msg->route);
}
if (msg->event_code == ZTS_EVENT_ROUTE_REMOVED) {
print_route_details("ZTS_EVENT_ROUTE_REMOVED", msg->route);
}
// Address events
if (msg->event_code == ZTS_EVENT_ADDR_ADDED_IP4) {
print_address_details("ZTS_EVENT_ADDR_ADDED_IP4", msg->addr);
}
if (msg->event_code == ZTS_EVENT_ADDR_ADDED_IP6) {
print_address_details("ZTS_EVENT_ADDR_ADDED_IP6", msg->addr);
}
if (msg->event_code == ZTS_EVENT_ADDR_REMOVED_IP4) {
print_address_details("ZTS_EVENT_ADDR_REMOVED_IP4", msg->addr);
}
if (msg->event_code == ZTS_EVENT_ADDR_REMOVED_IP6) {
print_address_details("ZTS_EVENT_ADDR_REMOVED_IP6", msg->addr);
}
// Cache events
if (msg->event_code == ZTS_EVENT_STORE_IDENTITY_PUBLIC) {
DEBUG_INFO("ZTS_EVENT_STORE_IDENTITY_PUBLIC (len=%d)", msg->len);
}
if (msg->event_code == ZTS_EVENT_STORE_IDENTITY_SECRET) {
DEBUG_INFO("ZTS_EVENT_STORE_IDENTITY_SECRET (len=%d)", msg->len);
}
if (msg->event_code == ZTS_EVENT_STORE_PLANET) {
DEBUG_INFO("ZTS_EVENT_STORE_PLANET (len=%d)", msg->len);
}
if (msg->event_code == ZTS_EVENT_STORE_PEER) {
DEBUG_INFO("ZTS_EVENT_STORE_PEER (len=%d)", msg->len);
}
if (msg->event_code == ZTS_EVENT_STORE_NETWORK) {
DEBUG_INFO("ZTS_EVENT_STORE_NETWORK (len=%d)", msg->len);
}
DEBUG_INFO("");
}
void api_value_arg_test(int tid, uint8_t num, int8_t i8, int16_t i16, int32_t i32, int64_t i64, void* nullable)
{
DEBUG_INFO(
"fuzzing values: thr = %10d, func [ %3d ] (%4d, %7d, %12d, %32lld, %p)",
tid,
num,
i8,
i16,
i32,
i64,
nullable);
int res = ZTS_ERR_OK;
// Test uninitialized Network Stack API usage
/*
res = zts_get_all_stats((struct zts_stats *)nullable);
assert(("pre-init call to zts_get_all_stats(): res != ZTS_ERR_SERVICE",
res == ZTS_ERR_SERVICE));
res = zts_get_protocol_stats(i32, nullable);
assert(("pre-init call to zts_get_protocol_stats(): res !=
ZTS_ERR_SERVICE", res == ZTS_ERR_SERVICE));
*/
res = zts_dns_set_server(i8, (const zts_ip_addr*)nullable);
assert(("pre-init call to zts_add_dns_nameserver(): res != ZTS_ERR_SERVICE", res == ZTS_ERR_SERVICE));
const zts_ip_addr* res_ptr = zts_dns_get_server(i8);
assert(("pre-init call to zts_del_dns_nameserver(): res != ZTS_ERR_SERVICE", res == ZTS_ERR_SERVICE));
struct zts_sockaddr* null_addr = (struct zts_sockaddr*)nullable;
struct zts_sockaddr_storage* null_addr_ss = (struct zts_sockaddr_storage*)nullable;
zts_socklen_t* null_len = (zts_socklen_t*)nullable;
zts_fd_set* null_fd_set = (zts_fd_set*)nullable;
zts_timeval* null_timeval = (zts_timeval*)nullable;
// Test uninitialized control API usage (Node)
switch (num) {
//
// Central
//
/*
case 1:
assert(zts_central_set_access_mode(i8) == ZTS_ERR_SERVICE);
break;
case 2:
assert(zts_central_set_verbose(i8) == ZTS_ERR_SERVICE);
break;
case 3:
assert(zts_central_clear_resp_buf() == ZTS_ERR_SERVICE);
break;
case 4:
assert(zts_central_init(const NULL, const NULL, NULL, i32) == ZTS_ERR_SERVICE);
break;
case 5:
assert(zts_central_cleanup() == ZTS_ERR_SERVICE);
break;
case 6:
assert(zts_central_get_last_resp_buf(NULL, i32) == ZTS_ERR_SERVICE);
break;
case 7:
assert(zts_central_status_get(NULL) == ZTS_ERR_SERVICE);
break;
case 8:
assert(zts_central_self_get(NULL) == ZTS_ERR_SERVICE);
break;
case 9:
assert(zts_central_net_get(NULL, i64) == ZTS_ERR_SERVICE);
break;
case 10:
assert(zts_central_net_update(NULL, i64) == ZTS_ERR_SERVICE);
break;
case 11:
assert(zts_central_net_delete(NULL, i64) == ZTS_ERR_SERVICE);
break;
case 12:
assert(zts_central_net_get_all(NULL) == ZTS_ERR_SERVICE);
break;
case 13:
assert(zts_central_member_get(NULL, i64, i64) == ZTS_ERR_SERVICE);
break;
case 14:
assert(zts_central_member_update(NULL, i64, i64, NULL) == ZTS_ERR_SERVICE);
break;
case 15:
assert(zts_central_node_auth(NULL, i64, i64, i8) == ZTS_ERR_SERVICE);
break;
case 16:
assert(zts_central_net_get_members(NULL, i64) == ZTS_ERR_SERVICE);
break;
*/
//
// Id (tested in separate section)
//
/*
case 20:
assert(zts_id_new(NULL, NULL) == ZTS_ERR_SERVICE);
break;
case 21:
assert(zts_id_pair_is_valid(NULL, i32) == ZTS_ERR_SERVICE);
break;
*/
//
// Init
//
/*
int res = 0;
case 30:
assert(zts_init_from_storage(NULL) == ZTS_ERR_OK);
break;
case 31:
assert(zts_init_from_memory(NULL, i16) == ZTS_ERR_OK);
break;
case 32:
assert(zts_init_set_event_handler(NULL) == ZTS_ERR_SERVICE);
break;
case 33:
assert(zts_init_blacklist_if(NULL, i32) == ZTS_ERR_SERVICE);
break;
case 34:
assert(zts_init_set_roots(NULL, i32) == ZTS_ERR_SERVICE);
break;
case 35:
assert(zts_init_set_port(i16) == ZTS_ERR_SERVICE);
break;
case 36:
assert(zts_init_allow_net_cache(i32) == ZTS_ERR_SERVICE);
break;
case 37:
assert(zts_init_allow_peer_cache(i32) == ZTS_ERR_SERVICE);
break;
*/
//
// Address
//
case 40:
assert(zts_addr_is_assigned(i64, i32) == 0);
break;
case 41:
assert(zts_addr_get(i64, i32, null_addr_ss) == ZTS_ERR_SERVICE);
break;
/*
case 42:
assert(zts_addr_get_str(i64, i32, NULL, i32) == ZTS_ERR_SERVICE);
break;
*/
case 43:
assert(zts_addr_get_all(i64, null_addr_ss, NULL) == ZTS_ERR_SERVICE);
break;
/*
case 44:
assert(zts_addr_compute_6plane(i64, i64, null_addr) == ZTS_ERR_SERVICE);
break;
case 45:
assert(zts_addr_compute_rfc4193(i64, i64, null_addr) == ZTS_ERR_SERVICE);
break;
case 46:
assert(zts_addr_compute_rfc4193_str(i64, i64, NULL, i32) == ZTS_ERR_SERVICE);
break;
case 47:
assert(zts_addr_compute_6plane_str(i64, i64, NULL, i32) == ZTS_ERR_SERVICE);
break;
//
// Network
//
case 50:
assert(zts_net_compute_adhoc_id(i16, i16) == ZTS_ERR_SERVICE);
break;
*/
case 51:
assert(zts_net_join(i64) == ZTS_ERR_SERVICE);
break;
case 52:
assert(zts_net_leave(i64) == ZTS_ERR_SERVICE);
break;
// case 53:
// assert(zts_net_count() == ZTS_ERR_SERVICE);
// break;
case 54:
assert(zts_net_get_mac(i64) == ZTS_ERR_SERVICE);
break;
case 55:
assert(zts_net_get_mac_str(i64, NULL, i32) == ZTS_ERR_SERVICE);
break;
case 56:
assert(zts_net_get_broadcast(i64) == ZTS_ERR_SERVICE);
break;
case 57:
assert(zts_net_get_mtu(i64) == ZTS_ERR_SERVICE);
break;
case 58:
assert(zts_net_get_name(i64, NULL, i32) == ZTS_ERR_SERVICE);
break;
case 59:
assert(zts_net_get_status(i64) == ZTS_ERR_SERVICE);
break;
case 60:
assert(zts_net_get_type(i64) == ZTS_ERR_SERVICE);
break;
// Route
case 80:
assert(zts_route_is_assigned(i64, i32) == ZTS_ERR_SERVICE);
break;
// Node
/*
case 90:
assert(zts_node_start() == ZTS_ERR_SERVICE);
break;
*/
case 91:
assert(zts_node_is_online() == 0);
break;
case 92:
assert(zts_node_get_id() == ZTS_ERR_SERVICE);
break;
case 93:
assert(zts_node_get_id_pair(NULL, NULL) == ZTS_ERR_SERVICE);
break;
case 94:
assert(zts_node_get_port() == ZTS_ERR_SERVICE);
break;
case 95:
assert(zts_node_stop() == ZTS_ERR_SERVICE);
break;
// case 96:
// assert(zts_node_restart() == ZTS_ERR_SERVICE);
break;
case 97:
assert(zts_node_free() == ZTS_ERR_SERVICE);
break;
//
// Moon
//
/*
case 100:
assert(zts_moon_orbit(i64, i64) == ZTS_ERR_SERVICE);
break;
case 101:
assert(zts_moon_deorbit(i64) == ZTS_ERR_SERVICE);
break;
*/
//
// Utility
//
// case 110:
// assert(zts_util_delay(i64) == ZTS_ERR_SERVICE);
// break;
// Socket
case 120:
assert(zts_bsd_socket(i32, i32, i32) == ZTS_ERR_SERVICE);
break;
case 121:
assert(zts_bsd_connect(i32, null_addr, i32) == ZTS_ERR_SERVICE);
break;
case 122:
assert(zts_connect(i32, NULL, i32, i32) == ZTS_ERR_SERVICE);
break;
case 123:
assert(zts_bsd_bind(i32, null_addr, i32) == ZTS_ERR_SERVICE);
break;
case 124:
assert(zts_bind(i32, NULL, i32) == ZTS_ERR_SERVICE);
break;
case 125:
assert(zts_bsd_listen(i32, i32) == ZTS_ERR_SERVICE);
break;
case 126:
assert(zts_bsd_accept(i32, null_addr, NULL) == ZTS_ERR_SERVICE);
break;
case 127:
assert(zts_accept(i32, NULL, i32, NULL) == ZTS_ERR_SERVICE);
break;
case 128:
assert(zts_bsd_setsockopt(i32, i32, i32, NULL, i32) == ZTS_ERR_SERVICE);
break;
case 129:
assert(zts_bsd_getsockopt(i32, i32, i32, NULL, NULL) == ZTS_ERR_SERVICE);
break;
case 130:
assert(zts_bsd_getsockname(i32, null_addr, NULL) == ZTS_ERR_SERVICE);
break;
case 131:
assert(zts_bsd_getpeername(i32, null_addr, NULL) == ZTS_ERR_SERVICE);
break;
case 132:
assert(zts_bsd_close(i32) == ZTS_ERR_SERVICE);
break;
case 133:
assert(zts_bsd_select(i32, NULL, NULL, NULL, NULL) == ZTS_ERR_SERVICE);
break;
case 134:
assert(zts_bsd_fcntl(i32, i32, i32) == ZTS_ERR_SERVICE);
break;
case 135:
assert(zts_bsd_poll(NULL, (zts_nfds_t)NULL, i32) == ZTS_ERR_SERVICE);
break;
case 136:
assert(zts_bsd_ioctl(i32, i64, NULL) == ZTS_ERR_SERVICE);
break;
case 137:
assert(zts_bsd_send(i32, NULL, i32, i32) == ZTS_ERR_SERVICE);
break;
case 138:
assert(zts_bsd_sendto(i32, NULL, i32, i32, null_addr, i32) == ZTS_ERR_SERVICE);
break;
case 139:
assert(zts_bsd_sendmsg(i32, NULL, i32) == ZTS_ERR_SERVICE);
break;
case 140:
assert(zts_bsd_recv(i32, NULL, i32, i32) == ZTS_ERR_SERVICE);
break;
case 141:
assert(zts_bsd_recvfrom(i32, NULL, i32, i32, null_addr, NULL) == ZTS_ERR_SERVICE);
break;
case 142:
assert(zts_bsd_recvmsg(i32, NULL, i32) == ZTS_ERR_SERVICE);
break;
case 143:
assert(zts_bsd_read(i32, NULL, i32) == ZTS_ERR_SERVICE);
break;
case 144:
assert(zts_bsd_readv(i32, NULL, i32) == ZTS_ERR_SERVICE);
break;
case 145:
assert(zts_bsd_write(i32, NULL, i32) == ZTS_ERR_SERVICE);
break;
case 146:
assert(zts_bsd_writev(i32, nullable, i32) == ZTS_ERR_SERVICE);
break;
case 147:
assert(zts_bsd_shutdown(i32, i32) == ZTS_ERR_SERVICE);
break;
case 148:
assert(zts_set_no_delay(i32, i32) == ZTS_ERR_SERVICE);
break;
case 149:
assert(zts_get_no_delay(i32) == ZTS_ERR_SERVICE);
break;
case 150:
assert(zts_set_linger(i32, i32, i32) == ZTS_ERR_SERVICE);
break;
case 151:
assert(zts_get_linger_value(i32) == ZTS_ERR_SERVICE);
break;
case 152:
assert(zts_get_linger_value(i32) == ZTS_ERR_SERVICE);
break;
case 153:
assert(zts_set_reuse_addr(i32, i32) == ZTS_ERR_SERVICE);
break;
case 154:
assert(zts_get_reuse_addr(i32) == ZTS_ERR_SERVICE);
break;
case 155:
assert(zts_set_recv_timeout(i32, i32, i32) == ZTS_ERR_SERVICE);
break;
case 156:
assert(zts_get_recv_timeout(i32) == ZTS_ERR_SERVICE);
break;
case 157:
assert(zts_set_send_timeout(i32, i32, i32) == ZTS_ERR_SERVICE);
break;
case 158:
assert(zts_get_send_timeout(i32) == ZTS_ERR_SERVICE);
break;
case 159:
assert(zts_set_send_buf_size(i32, i32) == ZTS_ERR_SERVICE);
break;
case 160:
assert(zts_get_send_buf_size(i32) == ZTS_ERR_SERVICE);
break;
case 161:
assert(zts_set_recv_buf_size(i32, i32) == ZTS_ERR_SERVICE);
break;
case 162:
assert(zts_get_recv_buf_size(i32) == ZTS_ERR_SERVICE);
break;
case 163:
assert(zts_set_ttl(i32, i32) == ZTS_ERR_SERVICE);
break;
case 164:
assert(zts_get_ttl(i32) == ZTS_ERR_SERVICE);
break;
case 165:
assert(zts_set_blocking(i32, i32) == ZTS_ERR_SERVICE);
break;
case 166:
assert(zts_get_blocking(i32) == ZTS_ERR_SERVICE);
break;
case 167:
assert(zts_set_keepalive(i32, i32) == ZTS_ERR_SERVICE);
break;
case 168:
assert(zts_get_keepalive(i32) == ZTS_ERR_SERVICE);
break;
case 169:
assert(zts_bsd_gethostbyname(NULL) == NULL);
break;
case 170:
assert(zts_dns_set_server(i8, null_addr) == ZTS_ERR_SERVICE);
break;
case 171:
assert(zts_dns_get_server(i8) == NULL);
break;
/*
case 172:
assert(zts_ipaddr_ntoa(null_addr) == NULL);
break;
case 173:
assert(zts_ipaddr_aton(NULL, NULL) == ZTS_ERR_SERVICE);
break;
case 174:
assert(zts_inet_ntop(i32, NULL, NULL, i32) == NULL);
break;
case 175:
assert(zts_inet_pton(i32, NULL, NULL) == ZTS_ERR_SERVICE);
break;
case 176:
assert(zts_util_ipstr_to_saddr(i32, NULL, i32, null_addr, NULL) == ZTS_ERR_SERVICE);
break;
*/
default:
break;
}
}
#define FUZZ_NUM 128
void test_pre_service_fuzz()
{
DEBUG_INFO("\n\n***\ttest_pre_service_fuzz");
unsigned int tid = (unsigned int)pthread_self();
// Test service-related API functions before initializing service
// Test null values in sequential order
for (int i = 0; i < FUZZ_NUM; i++) {
api_value_arg_test(tid, i, 0, 0, 0, 0, NULL);
}
// Test all null values in random order (delayed)
for (int i = 0; i < FUZZ_NUM; i++) {
uint8_t delay = (uint8_t)random64();
uint8_t num = (uint8_t)random64();
zts_util_delay(delay / 16);
api_value_arg_test(tid, num, 0, 0, 0, 0, NULL);
}
// Test random values in random order (delayed)
for (int i = 0; i < FUZZ_NUM; i++) {
uint8_t delay = (uint8_t)random64();
uint8_t num = (uint8_t)random64();
int8_t i8 = (uint8_t)random64();
int16_t i16 = (uint16_t)random64();
int32_t i32 = (uint32_t)random64();
int64_t i64 = (uint64_t)random64();
int x;
void* nullable = &x;
zts_util_delay(delay / 16);
api_value_arg_test(tid, num, i8, i16, i32, i64, nullable);
}
// Test all null values in random order (no delay)
for (int i = 0; i < FUZZ_NUM; i++) {
uint8_t num = (uint8_t)random64();
api_value_arg_test(tid, num, 0, 0, 0, 0, NULL);
}
// Test random values in random order (no delay)
for (int i = 0; i < FUZZ_NUM; i++) {
uint8_t num = (uint8_t)random64();
int8_t i8 = (uint8_t)random64();
int16_t i16 = (uint16_t)random64();
int32_t i32 = (uint32_t)random64();
int64_t i64 = (uint64_t)random64();
int x;
void* nullable = &x;
api_value_arg_test(tid, num, i8, i16, i32, i64, nullable);
}
// Test non-service helper functions
// (B) Test zts_inet_ntop
char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 };
int16_t port = 0;
struct zts_sockaddr_in in4;
in4.sin_port = htons(8080);
#if defined(_WIN32)
zts_inet_pton(ZTS_AF_INET, "192.168.22.1", &(in4.sin_addr.S_addr));
#else
zts_inet_pton(ZTS_AF_INET, "192.168.22.1", &(in4.sin_addr.s_addr));
#endif
in4.sin_family = ZTS_AF_INET;
struct zts_sockaddr* sa = (struct zts_sockaddr*)&in4;
if (sa->sa_family == ZTS_AF_INET) {
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)sa;
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
port = ntohs(in4->sin_port);
}
if (sa->sa_family == ZTS_AF_INET6) {
struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)sa;
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN);
}
assert(port == 8080);
assert(! strcmp(ipstr, "192.168.22.1"));
// (C) Test zts_inet_pton
uint8_t buf[sizeof(struct zts_in6_addr)] = { 0 };
char str[ZTS_INET6_ADDRSTRLEN] = { 0 };
zts_inet_pton(ZTS_AF_INET, "192.168.22.2", buf);
zts_inet_ntop(ZTS_AF_INET, buf, str, ZTS_INET6_ADDRSTRLEN);
assert(! strcmp(str, "192.168.22.2"));
}
void test_sockets()
{
int res = ZTS_ERR_OK;
// Test simplified API, proxy for setsockopt/getsockopt/ioctl etc.
int s4 = zts_bsd_socket(ZTS_AF_INET6, ZTS_SOCK_STREAM, 0);
assert(s4 >= 0);
// TCP_NODELAY
// Check value before doing anything
assert(zts_get_no_delay(s4) == 0);
// Turn on
assert(zts_set_no_delay(s4, 1) == ZTS_ERR_OK);
// Should return value instead of error code
assert(zts_get_no_delay(s4) == 1);
// Turn off
assert(zts_set_no_delay(s4, 0) == ZTS_ERR_OK);
assert(zts_get_no_delay(s4) == ZTS_ERR_OK);
// SO_LINGER
// Check value before doing anything
assert(zts_get_linger_enabled(s4) == 0);
assert(zts_get_linger_value(s4) == 0);
// Turn on, set to 7 seconds
assert(zts_set_linger(s4, 1, 7) == ZTS_ERR_OK);
assert(zts_get_linger_enabled(s4) == 1);
assert(zts_get_linger_value(s4) == 7);
assert(zts_set_linger(s4, 0, 0) == ZTS_ERR_OK);
// Turn off
assert(zts_get_linger_enabled(s4) == 0);
assert(zts_get_linger_value(s4) == 0);
// SO_REUSEADDR
// Check value before doing anything
assert(zts_get_reuse_addr(s4) == 0);
// Turn on
assert(zts_set_reuse_addr(s4, 1) == ZTS_ERR_OK);
// Should return value instead of error code
assert(zts_get_reuse_addr(s4) == 1);
// Turn off
assert(zts_set_reuse_addr(s4, 0) == ZTS_ERR_OK);
assert(zts_get_reuse_addr(s4) == ZTS_ERR_OK);
// SO_RCVTIMEO
// Check value before doing anything
assert(zts_get_recv_timeout(s4) == 0);
// Set to value
assert(zts_set_recv_timeout(s4, 3, 0) == ZTS_ERR_OK);
assert(zts_get_recv_timeout(s4) == 3);
assert(zts_set_recv_timeout(s4, 0, 0) == ZTS_ERR_OK);
// Set to zero
assert(zts_get_recv_timeout(s4) == 0);
// SO_SNDTIMEO
// Check value before doing anything
assert(zts_get_send_timeout(s4) == 0);
// Set to value
assert(zts_set_send_timeout(s4, 4, 0) == ZTS_ERR_OK);
assert(zts_get_send_timeout(s4) == 4);
assert(zts_set_send_timeout(s4, 0, 0) == ZTS_ERR_OK);
// Set to zero
assert(zts_get_send_timeout(s4) == 0);
// SO_SNDBUF
// Check value before doing anything
assert(zts_get_send_buf_size(s4) == -1); // Unimplemented
// Set to 7 seconds
assert(zts_set_send_buf_size(s4, 1024) == ZTS_ERR_OK);
assert(zts_get_send_buf_size(s4) == -1); // Unimplemented
assert(zts_set_send_buf_size(s4, 0) == ZTS_ERR_OK);
// Set to zero
assert(zts_get_send_buf_size(s4) == -1); // Unimplemented
// SO_RCVBUF