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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
|
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "common.h"
#include "common-socket.h"
#define ISO8601_FORMAT "%Y-%m-%dT%H:%M:%S"
/* timestamps for debug and error logs */
static char *get_timestamp(void)
{
struct tm *tm;
time_t t;
size_t len;
char *s;
len = sizeof(char) * 32;
t = time(NULL);
if (t == -1) {
return NULL;
}
tm = gmtime(&t);
if (tm == NULL) {
return NULL;
}
s = (char *) malloc(len);
if (!s) {
perror("malloc");
return NULL;
}
memset(s, '\0', len);
strftime(s, len - 1, ISO8601_FORMAT, tm);
return s;
}
void test_fail_fl(char *msg, char *file, int line)
{
char *timestamp;
int e;
e = errno;
timestamp = get_timestamp();
if (errct == 0) fprintf(stderr, "\n");
errno = e;
fprintf(stderr, "[ERROR][%s] (%s Line %d) %s [pid=%d:errno=%d:%s]\n",
timestamp, file, line, msg, getpid(), errno, strerror(errno));
fflush(stderr);
if (timestamp != NULL) {
free(timestamp);
timestamp = NULL;
}
errno = e;
e(7);
}
#if DEBUG == 1
void debug_fl(char *msg, char *file, int line)
{
char *timestamp;
timestamp = get_timestamp();
fprintf(stdout,"[DEBUG][%s] (%s:%d) %s [pid=%d]\n",
timestamp, __FILE__, __LINE__, msg, getpid());
fflush(stdout);
if (timestamp != NULL) {
free(timestamp);
timestamp = NULL;
}
}
#endif
void test_socket(const struct socket_test_info *info)
{
struct stat statbuf, statbuf2;
int sd, sd2;
int rc;
int i;
debug("entering test_socket()");
debug("Test socket() with an unsupported address family");
errno = 0;
sd = socket(-1, info->type, 0);
if (!(sd == -1 && errno == EAFNOSUPPORT)) {
test_fail("socket");
if (sd != -1) {
CLOSE(sd);
}
}
debug("Test socket() with all available FDs open by this process");
for (i = 3; i < getdtablesize(); i++) {
rc = open("/dev/null", O_RDONLY);
if (rc == -1) {
test_fail("we couldn't open /dev/null for read");
}
}
errno = 0;
sd = socket(info->domain, info->type, 0);
if (!(sd == -1 && errno == EMFILE)) {
test_fail("socket() call with all fds open should fail");
if (sd != -1) {
CLOSE(sd);
}
}
for (i = 3; i < getdtablesize(); i++) {
CLOSE(i);
}
debug("Test socket() with an mismatched protocol");
errno = 0;
sd = socket(info->domain, info->type, 4);
if (!(sd == -1 && errno == EPROTONOSUPPORT)) {
test_fail("socket() should fail with errno = EPROTONOSUPPORT");
if (sd != -1) {
CLOSE(sd);
}
}
debug("Test socket() success");
/*
* open 2 sockets at once and *then* close them.
* This will test that /dev/uds is cloning properly.
*/
SOCKET(sd, info->domain, info->type, 0);
SOCKET(sd2, info->domain, info->type, 0);
rc = fstat(sd, &statbuf);
if (rc == -1) {
test_fail("fstat failed on sd");
}
rc = fstat(sd2, &statbuf2);
if (rc == -1) {
test_fail("fstat failed on sd2");
}
if (statbuf.st_dev == statbuf2.st_dev) {
test_fail("/dev/uds isn't being cloned");
}
CLOSE(sd2);
CLOSE(sd);
debug("leaving test_socket()");
}
void test_getsockname(const struct socket_test_info *info)
{
int sd;
int rc;
int on;
struct sockaddr_storage sock_addr;
socklen_t sock_addr_len;
SOCKET(sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
rc = bind(sd, info->serveraddr, info->serveraddrlen);
if (rc == -1) {
test_fail("bind() should have worked");
}
debug("Test getsockname() success");
memset(&sock_addr, '\0', sizeof(sock_addr));
sock_addr_len = sizeof(sock_addr);
rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
if (rc == -1) {
test_fail("getsockname() should have worked");
}
info->callback_check_sockaddr((struct sockaddr *) &sock_addr,
sock_addr_len, "getsockname", 1);
CLOSE(sd);
}
void test_bind(const struct socket_test_info *info)
{
struct sockaddr_storage sock_addr;
socklen_t sock_addr_len;
int sd;
int sd2;
int rc;
int on;
debug("entering test_bind()");
info->callback_cleanup();
debug("Test bind() success");
SOCKET(sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
rc = bind(sd, info->serveraddr, info->serveraddrlen);
if (rc == -1) {
test_fail("bind() should have worked");
}
debug("Test getsockname() success");
memset(&sock_addr, '\0', sizeof(sock_addr));
sock_addr_len = sizeof(sock_addr);
rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
if (rc == -1) {
test_fail("getsockname() should have worked");
}
info->callback_check_sockaddr((struct sockaddr *) &sock_addr,
sock_addr_len, "getsockname", 1);
debug("Test bind() with a address that has already been bind()'d");
SOCKET(sd2, info->domain, info->type, 0);
errno = 0;
rc = bind(sd2, info->serveraddr, info->serveraddrlen);
if (!((rc == -1) && (errno == EADDRINUSE))) {
test_fail("bind() should have failed with EADDRINUSE");
}
CLOSE(sd2);
CLOSE(sd);
info->callback_cleanup();
debug("Test bind() with a NULL address");
SOCKET(sd, info->domain, info->type, 0);
errno = 0;
rc = bind(sd, (struct sockaddr *) NULL,
sizeof(struct sockaddr_storage));
if (!((rc == -1) && (errno == EFAULT))) {
test_fail("bind() should have failed with EFAULT");
}
CLOSE(sd);
debug("leaving test_bind()");
}
void test_listen(const struct socket_test_info *info)
{
int rc;
debug("entering test_listen()");
debug("Test listen() with a bad file descriptor");
errno = 0;
rc = listen(-1, 0);
if (!(rc == -1 && errno == EBADF)) {
test_fail("listen(-1, 0) should have failed");
}
debug("Test listen() with a non-socket file descriptor");
errno = 0;
rc = listen(0, 0);
/* Test on errno disabled here: there's currently no telling what this
* will return. POSIX says it should be ENOTSOCK, MINIX3 libc returns
* ENOSYS, and we used to test for ENOTTY here..
*/
if (!(rc == -1)) {
test_fail("listen(0, 0) should have failed");
}
debug("leaving test_listen()");
}
void test_shutdown(const struct socket_test_info *info)
{
int how[3] = { SHUT_RD, SHUT_WR, SHUT_RDWR };
int sd;
int rc;
int i;
debug("entering test_shutdown()");
/* test for each direction (read, write, read-write) */
for (i = 0; i < 3; i++) {
debug("test shutdown() with an invalid descriptor");
errno = 0;
rc = shutdown(-1, how[i]);
if (!(rc == -1 && errno == EBADF)) {
test_fail("shutdown(-1, how[i]) should have failed");
}
debug("test shutdown() with a non-socket descriptor");
errno = 0;
rc = shutdown(0, how[i]);
if (!(rc == -1 && errno == ENOTSOCK)) {
test_fail("shutdown() should have failed with "
"ENOTSOCK");
}
debug("test shutdown() with a socket that is not connected");
SOCKET(sd, info->domain, info->type, 0);
errno = 0;
rc = shutdown(sd, how[i]);
if (rc != 0 && !(rc == -1 && errno == ENOTCONN)) {
test_fail("shutdown() should have failed");
}
CLOSE(sd);
}
SOCKET(sd, info->domain, info->type, 0);
errno = 0;
rc = shutdown(sd, -1);
if (!(rc == -1 && errno == EINVAL)) {
test_fail("shutdown(sd, -1) should have failed with EINVAL");
}
CLOSE(sd);
debug("leaving test_shutdown()");
}
void test_close(const struct socket_test_info *info)
{
int sd, sd2;
int rc, i, on;
debug("entering test_close()");
info->callback_cleanup();
debug("Test close() success");
SOCKET(sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
rc = bind(sd, info->serveraddr, info->serveraddrlen);
if (rc != 0) {
test_fail("bind() should have worked");
}
CLOSE(sd);
debug("Close an already closed file descriptor");
errno = 0;
rc = close(sd);
if (!(rc == -1 && errno == EBADF)) {
test_fail("close(sd) should have failed with EBADF");
}
info->callback_cleanup();
debug("dup()'ing a file descriptor and closing both should work");
SOCKET(sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
rc = bind(sd, info->serveraddr, info->serveraddrlen);
if (rc != 0) {
test_fail("bind() should have worked");
}
errno = 0;
sd2 = dup(sd);
if (sd2 == -1) {
test_fail("dup(sd) should have worked");
} else {
CLOSE(sd2);
CLOSE(sd);
}
info->callback_cleanup();
/* Create and close a socket a bunch of times.
* If the implementation doesn't properly free the
* socket during close(), eventually socket() will
* fail when the internal descriptor table is full.
*/
for (i = 0; i < 1024; i++) {
SOCKET(sd, info->domain, info->type, 0);
CLOSE(sd);
}
debug("leaving test_close()");
}
void test_sockopts(const struct socket_test_info *info)
{
int i;
int rc;
int sd;
int option_value;
socklen_t option_len;
debug("entering test_sockopts()");
for (i = 0; i < info->typecount; i++) {
SOCKET(sd, info->domain, info->types[i], 0);
debug("Test setsockopt() works");
option_value = 0;
option_len = sizeof(option_value);
errno = 0;
rc = getsockopt(sd, SOL_SOCKET, SO_TYPE, &option_value,
&option_len);
if (rc != 0) {
test_fail("setsockopt() should have worked");
}
if (option_value != info->types[i]) {
test_fail("SO_TYPE didn't seem to work.");
}
CLOSE(sd);
}
SOCKET(sd, info->domain, info->type, 0);
debug("Test setsockopt() works");
option_value = 0;
option_len = sizeof(option_value);
errno = 0;
rc = getsockopt(sd, SOL_SOCKET, SO_SNDBUF, &option_value, &option_len);
if (info->expected_sndbuf >= 0 &&
option_value != info->expected_sndbuf) {
test_fail("SO_SNDBUF didn't seem to work.");
}
CLOSE(sd);
SOCKET(sd, info->domain, info->type, 0);
debug("Test setsockopt() works");
option_value = 0;
option_len = sizeof(option_value);
errno = 0;
rc = getsockopt(sd, SOL_SOCKET, SO_RCVBUF, &option_value, &option_len);
if (rc != 0) {
test_fail("getsockopt() should have worked");
}
if (info->expected_rcvbuf >= 0 &&
option_value != info->expected_rcvbuf) {
test_fail("SO_RCVBUF didn't seem to work.");
}
CLOSE(sd);
debug("leaving test_sockopts()");
}
void test_read(const struct socket_test_info *info)
{
int rc;
int fd;
char buf[BUFSIZE];
debug("entering test_read()");
errno = 0;
rc = read(-1, buf, sizeof(buf));
if (!(rc == -1 && errno == EBADF)) {
test_fail("read() should have failed with EBADF");
}
fd = open("/tmp", O_RDONLY);
if (fd == -1) {
test_fail("open(\"/tmp\", O_RDONLY) should have worked");
}
CLOSE(fd);
debug("leaving test_read()");
}
void test_write(const struct socket_test_info *info)
{
int rc;
char buf[BUFSIZE];
debug("entering test_write()");
errno = 0;
rc = write(-1, buf, sizeof(buf));
if (!(rc == -1 && errno == EBADF)) {
test_fail("write() should have failed with EBADF");
}
debug("leaving test_write()");
}
void test_dup(const struct socket_test_info *info)
{
struct stat info1;
struct stat info2;
int sd, sd2;
int rc;
int i, on;
debug("entering test_dup()");
info->callback_cleanup();
debug("Test dup()");
SOCKET(sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
rc = bind(sd, info->serveraddr, info->serveraddrlen);
if (rc != 0) {
test_fail("bind() should have worked");
}
errno = 0;
sd2 = dup(sd);
if (sd2 == -1) {
test_fail("dup(sd) should have worked");
}
rc = fstat(sd, &info1);
if (rc == -1) {
test_fail("fstat(fd, &info1) failed");
}
rc = fstat(sd2, &info2);
if (rc == -1) {
test_fail("fstat(sd, &info2) failed");
}
if (info1.st_ino != info2.st_ino) {
test_fail("dup() failed info1.st_ino != info2.st_ino");
}
CLOSE(sd);
CLOSE(sd2);
debug("Test dup() with a closed socket");
errno = 0;
rc = dup(sd);
if (!(rc == -1 && errno == EBADF)) {
test_fail("dup(sd) on a closed socket shouldn't have worked");
}
debug("Test dup() with socket descriptor of -1");
errno = 0;
rc = dup(-1);
if (!(rc == -1 && errno == EBADF)) {
test_fail("dup(-1) shouldn't have worked");
}
debug("Test dup() when all of the file descriptors are taken");
SOCKET(sd, info->domain, info->type, 0);
for (i = 4; i < getdtablesize(); i++) {
rc = open("/dev/null", O_RDONLY);
if (rc == -1) {
test_fail("we couldn't open /dev/null for read");
}
}
errno = 0;
sd2 = dup(sd);
if (!(sd2 == -1 && errno == EMFILE)) {
test_fail("dup(sd) should have failed with errno = EMFILE");
}
for (i = 3; i < getdtablesize(); i++) {
CLOSE(i);
}
info->callback_cleanup();
debug("leaving test_dup()");
}
void test_dup2(const struct socket_test_info *info)
{
struct stat info1;
struct stat info2;
int sd;
int fd;
int rc;
int on;
debug("entering test_dup2()");
info->callback_cleanup();
SOCKET(sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
rc = bind(sd, info->serveraddr, info->serveraddrlen);
if (rc != 0) {
test_fail("bind() should have worked");
}
fd = open("/dev/null", O_RDONLY);
if (fd == -1) {
test_fail("open(\"/dev/null\", O_RDONLY) failed");
}
fd = dup2(sd, fd);
if (fd == -1) {
test_fail("dup2(sd, fd) failed.");
}
memset(&info1, '\0', sizeof(struct stat));
memset(&info2, '\0', sizeof(struct stat));
rc = fstat(fd, &info1);
if (rc == -1) {
test_fail("fstat(fd, &info1) failed");
}
rc = fstat(sd, &info2);
if (rc == -1) {
test_fail("fstat(sd, &info2) failed");
}
if (!(info1.st_ino == info2.st_ino &&
major(info1.st_dev) == major(info2.st_dev) &&
minor(info1.st_dev) == minor(info2.st_dev))) {
test_fail("dup2() failed");
}
CLOSE(fd);
CLOSE(sd);
info->callback_cleanup();
debug("leaving test_dup2()");
}
/*
* A toupper() server. This toy server converts a string to upper case.
*/
static void test_xfer_server(const struct socket_test_info *info, pid_t pid)
{
int i;
struct timeval tv;
fd_set readfds;
int status;
int rc;
int sd;
int on;
unsigned char buf[BUFSIZE];
socklen_t client_addr_size;
int client_sd;
struct sockaddr_storage client_addr;
status = 0;
rc = 0;
sd = 0;
client_sd = 0;
client_addr_size = sizeof(struct sockaddr_storage);
memset(&buf, '\0', sizeof(buf));
memset(&client_addr, '\0', sizeof(client_addr));
SOCKET(sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
rc = bind(sd, info->serveraddr, info->serveraddrlen);
if (rc == -1) {
test_fail("bind() should have worked");
}
rc = listen(sd, 8);
if (rc == -1) {
test_fail("listen(sd, 8) should have worked");
}
/* we're ready for connections, time to tell the client to start
* the test
*/
kill(pid, SIGUSR1);
tv.tv_sec = 10;
tv.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(sd, &readfds);
/* use select() in case the client is really broken and never
* attempts to connect (we don't want to block on accept()
* forever).
*/
rc = select(sd + 1, &readfds, NULL, NULL, &tv);
if (rc == -1) {
test_fail("[server] select() should not have failed");
}
if (rc != 1) {
test_fail("[server] select() should have returned 1");
printf("[server] select returned %d\n", rc);
}
if (!(FD_ISSET(sd, &readfds))) {
test_fail("[server] client didn't connect within 10 seconds");
kill(pid, SIGKILL);
return;
}
client_sd = accept(sd, (struct sockaddr *) &client_addr,
&client_addr_size);
if (client_sd == -1) {
test_fail("accept() should have worked");
kill(pid, SIGKILL);
return;
} else {
debug("[server] client accept()'d");
}
debug("[server] Reading message");
rc = read(client_sd, buf, sizeof(buf));
if (rc == -1) {
test_fail("read() failed unexpectedly");
kill(pid, SIGKILL);
return;
}
debug("[server] we got the following message:");
debug(buf);
for (i = 0; i < rc && i < 127; i++) {
buf[i] = toupper(buf[i]);
}
debug("[server] Writing message...");
rc = write(client_sd, buf, sizeof(buf));
if (rc == -1) {
test_fail("write(client_sd, buf, sizeof(buf)) failed");
kill(pid, SIGKILL);
return;
}
if (rc < strlen((char *)buf)) {
test_fail("[server] write didn't write all the bytes");
}
memset(&buf, '\0', sizeof(buf));
debug("[server] Recv message");
rc = recv(client_sd, buf, sizeof(buf), 0);
if (rc == -1) {
test_fail("recv() failed unexpectedly");
kill(pid, SIGKILL);
return;
}
debug("[server] we got the following message:");
debug(buf);
for (i = 0; i < rc && i < 127; i++) {
buf[i] = toupper(buf[i]);
}
debug("[server] Sending message...");
rc = send(client_sd, buf, sizeof(buf), 0);
if (rc == -1) {
test_fail("send(client_sd, buf, sizeof(buf), 0) failed");
kill(pid, SIGKILL);
return;
}
if (rc < strlen((char *)buf)) {
test_fail("[server] write didn't write all the bytes");
}
memset(&buf, '\0', sizeof(buf));
debug("[server] Recvfrom message");
rc = recvfrom(client_sd, buf, sizeof(buf), 0, NULL, 0);
if (rc == -1) {
test_fail("recvfrom() failed unexpectedly");
kill(pid, SIGKILL);
return;
}
debug("[server] we got the following message:");
debug(buf);
for (i = 0; i < rc && i < 127; i++) {
buf[i] = toupper(buf[i]);
}
debug("[server] Sendto message...");
rc = sendto(client_sd, buf, sizeof(buf), 0, NULL, 0);
if (rc == -1) {
test_fail("sendto() failed");
kill(pid, SIGKILL);
return;
}
if (rc < strlen((char *)buf)) {
test_fail("[server] write didn't write all the bytes");
}
shutdown(client_sd, SHUT_RDWR);
CLOSE(client_sd);
shutdown(sd, SHUT_RDWR);
CLOSE(sd);
/* wait for client to exit */
do {
errno = 0;
rc = waitpid(pid, &status, 0);
} while (rc == -1 && errno == EINTR);
/* we use the exit status to get its error count */
errct += WEXITSTATUS(status);
}
int server_ready = 0;
/* signal handler for the client */
void test_xfer_sighdlr(int sig)
{
debug("entering signal handler");
switch (sig) {
/* the server will send SIGUSR1 when it is time for us
* to start the tests
*/
case SIGUSR1:
server_ready = 1;
debug("got SIGUSR1, the server is ready for the client");
break;
default:
debug("didn't get SIGUSR1");
}
debug("leaving signal handler");
}
/*
* A toupper() client.
*/
static void test_xfer_client(const struct socket_test_info *info)
{
struct timeval tv;
fd_set readfds;
struct sockaddr_storage peer_addr;
socklen_t peer_addr_len;
int sd;
int rc;
char buf[BUFSIZE];
debug("[client] entering test_xfer_client()");
errct = 0; /* reset error count */
memset(&buf, '\0', sizeof(buf));
while (server_ready == 0) {
debug("[client] waiting for the server to signal");
sleep(1);
}
peer_addr_len = sizeof(peer_addr);
if (info->callback_xfer_prepclient) info->callback_xfer_prepclient();
debug("[client] creating client socket");
SOCKET(sd, info->domain, info->type, 0);
debug("[client] connecting to server through the symlink");
rc = connect(sd, info->clientaddrsym, info->clientaddrsymlen);
if (rc == -1) {
test_fail("[client] connect() should have worked");
} else {
debug("[client] connected");
}
debug("[client] testing getpeername()");
memset(&peer_addr, '\0', sizeof(peer_addr));
rc = getpeername(sd, (struct sockaddr *) &peer_addr, &peer_addr_len);
if (rc == -1) {
test_fail("[client] getpeername() should have worked");
}
info->callback_check_sockaddr((struct sockaddr *) &peer_addr,
peer_addr_len, "getpeername", 1);
strncpy(buf, "Hello, World!", sizeof(buf) - 1);
debug("[client] send to server");
rc = write(sd, buf, sizeof(buf));
if (rc == -1) {
test_fail("[client] write() failed unexpectedly");
}
memset(buf, '\0', sizeof(buf));
debug("[client] read from server");
rc = read(sd, buf, sizeof(buf));
if (rc == -1) {
test_fail("[client] read() failed unexpectedly");
} else {
debug("[client] we got the following message:");
debug(buf);
}
if (strncmp(buf, "HELLO, WORLD!", sizeof(buf)) != 0) {
test_fail("[client] We didn't get the correct response");
}
memset(&buf, '\0', sizeof(buf));
strncpy(buf, "Bonjour!", sizeof(buf) - 1);
debug("[client] send to server");
rc = send(sd, buf, sizeof(buf), 0);
if (rc == -1) {
test_fail("[client] send() failed unexpectedly");
}
if (info->callback_xfer_peercred) info->callback_xfer_peercred(sd);
debug("Testing select()");
tv.tv_sec = 2;
tv.tv_usec = 500000;
FD_ZERO(&readfds);
FD_SET(sd, &readfds);
rc = select(sd + 1, &readfds, NULL, NULL, &tv);
if (rc == -1) {
test_fail("[client] select() should not have failed");
}
if (rc != 1) {
test_fail("[client] select() should have returned 1");
}
if (!(FD_ISSET(sd, &readfds))) {
test_fail("The server didn't respond within 2.5 seconds");
}
memset(buf, '\0', sizeof(buf));
debug("[client] recv from server");
rc = recv(sd, buf, sizeof(buf), 0);
if (rc == -1) {
test_fail("[client] recv() failed unexpectedly");
} else {
debug("[client] we got the following message:");
debug(buf);
}
if (strncmp(buf, "BONJOUR!", sizeof(buf)) != 0) {
test_fail("[client] We didn't get the right response.");
}
memset(&buf, '\0', sizeof(buf));
strncpy(buf, "Hola!", sizeof(buf) - 1);
debug("[client] sendto to server");
rc = sendto(sd, buf, sizeof(buf), 0, NULL, 0);
if (rc == -1) {
test_fail("[client] sendto() failed");
}
debug("Testing select()");
tv.tv_sec = 2;
tv.tv_usec = 500000;
FD_ZERO(&readfds);
FD_SET(sd, &readfds);
rc = select(sd + 1, &readfds, NULL, NULL, &tv);
if (rc == -1) {
test_fail("[client] select() should not have failed");
}
if (rc != 1) {
test_fail("[client] select() should have returned 1");
}
if (!(FD_ISSET(sd, &readfds))) {
test_fail("[client] The server didn't respond in 2.5 seconds");
}
memset(buf, '\0', sizeof(buf));
debug("[client] recvfrom from server");
rc = recvfrom(sd, buf, sizeof(buf), 0, NULL, 0);
if (rc == -1) {
test_fail("[cleint] recvfrom() failed unexpectedly");
} else {
debug("[client] we got the following message:");
debug(buf);
}
if (strncmp(buf, "HOLA!", sizeof(buf)) != 0) {
test_fail("[client] We didn't get the right response.");
}
debug("[client] closing socket");
CLOSE(sd);
debug("[client] leaving test_xfer_client()");
exit(errct);
}
void test_xfer(const struct socket_test_info *info)
{
pid_t pid;
debug("entering test_xfer()");
info->callback_cleanup();
/* the signal handler is only used by the client, but we have to
* install it now. if we don't the server may signal the client
* before the handler is installed.
*/
debug("installing signal handler");
if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
}
debug("signal handler installed");
server_ready = 0;
pid = fork();
if (pid == -1) {
test_fail("fork() failed");
return;
} else if (pid == 0) {
debug("child");
errct = 0;
test_xfer_client(info);
test_fail("we should never get here");
exit(1);
} else {
debug("parent");
test_xfer_server(info, pid);
debug("parent done");
}
info->callback_cleanup();
debug("leaving test_xfer()");
}
static void test_simple_client(const struct socket_test_info *info, int type)
{
char buf[BUFSIZE];
int sd, rc;
sd = socket(info->domain, type, 0);
if (sd == -1) {
test_fail("socket");
exit(errct);
}
while (server_ready == 0) {
debug("[client] waiting for the server");
sleep(1);
}
bzero(buf, BUFSIZE);
snprintf(buf, BUFSIZE-1, "Hello, My Name is Client.");
if (type == SOCK_DGRAM) {
rc = sendto(sd, buf, strlen(buf) + 1, 0,
info->clientaddr, info->clientaddrlen);
if (rc == -1) {
test_fail("sendto");
exit(errct);
}
} else {
rc = connect(sd, info->clientaddr, info->clientaddrlen);
if (rc == -1) {
test_fail("connect");
exit(errct);
}
rc = write(sd, buf, strlen(buf) + 1);
if (rc == -1) {
test_fail("write");
}
memset(buf, '\0', BUFSIZE);
rc = read(sd, buf, BUFSIZE);
if (rc == -1) {
test_fail("read");
}
if (strcmp("Hello, My Name is Server.", buf) != 0) {
test_fail("didn't read the correct string");
}
}
rc = close(sd);
if (rc == -1) {
test_fail("close");
}
exit(errct);
}
static void test_simple_server(const struct socket_test_info *info, int type,
pid_t pid)
{
char buf[BUFSIZE];
int sd, rc, client_sd, status, on;
struct sockaddr_storage addr;
socklen_t addr_len;
addr_len = info->clientaddrlen;
sd = socket(info->domain, type, 0);
if (sd == -1) {
test_fail("socket");
}
on = 1;
(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
assert(info->clientaddrlen <= sizeof(addr));
memcpy(&addr, info->clientaddr, info->clientaddrlen);
rc = bind(sd, info->serveraddr, info->serveraddrlen);
if (rc == -1) {
test_fail("bind");
}
if (type == SOCK_DGRAM) {
/* ready for client */
kill(pid, SIGUSR1);
rc = recvfrom(sd, buf, BUFSIZE, 0,
(struct sockaddr *) &addr, &addr_len);
if (rc == -1) {
test_fail("recvfrom");
}
} else {
rc = listen(sd, 5);
if (rc == -1) {
test_fail("listen");
}
/* we're ready for connections, time to tell the client
* to start the test
*/
kill(pid, SIGUSR1);
client_sd = accept(sd, (struct sockaddr *) &addr, &addr_len);
if (client_sd == -1) {
test_fail("accept");
}
memset(buf, '\0', BUFSIZE);
rc = read(client_sd, buf, BUFSIZE);
if (rc == -1) {
test_fail("read");
}
if (strcmp("Hello, My Name is Client.", buf) != 0) {
test_fail("didn't read the correct string");
}
/* added for extra fun to make the client block on read() */
sleep(1);
bzero(buf, BUFSIZE);
snprintf(buf, BUFSIZE-1, "Hello, My Name is Server.");
rc = write(client_sd, buf, strlen(buf) + 1);
if (rc == -1) {
test_fail("write");
}
rc = close(client_sd);
if (rc == -1) {
test_fail("close");
}
}
rc = close(sd);
if (rc == -1) {
test_fail("close");
}
/* wait for client to exit */
do {
errno = 0;
rc = waitpid(pid, &status, 0);
} while (rc == -1 && errno == EINTR);
/* we use the exit status to get its error count */
errct += WEXITSTATUS(status);
}
static void test_abort_client(const struct socket_test_info *info,
int abort_type);
static void test_abort_server(const struct socket_test_info *info,
pid_t pid, int abort_type);
void test_abort_client_server(const struct socket_test_info *info,
int abort_type)
{
pid_t pid;
debug("test_simple_client_server()");
info->callback_cleanup();
/* the signal handler is only used by the client, but we have to
* install it now. if we don't the server may signal the client
* before the handler is installed.
*/
debug("installing signal handler");
if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
}
debug("signal handler installed");
server_ready = 0;
pid = fork();
if (pid == -1) {
test_fail("fork() failed");
return;
} else if (pid == 0) {
debug("child");
errct = 0;
test_abort_client(info, abort_type);
test_fail("we should never get here");
exit(1);
} else {
debug("parent");
test_abort_server(info, pid, abort_type);
debug("parent done");
}
info->callback_cleanup();
}
static void test_abort_client(const struct socket_test_info *info,
int abort_type)
{
char buf[BUFSIZE];
int sd, rc;
sd = socket(info->domain, info->type, 0);
if (sd == -1) {
test_fail("socket");
exit(errct);
}
while (server_ready == 0) {
debug("[client] waiting for the server");
sleep(1);
}
bzero(buf, BUFSIZE);
snprintf(buf, BUFSIZE-1, "Hello, My Name is Client.");
rc = connect(sd, info->clientaddr, info->clientaddrlen);
if (rc == -1) {
test_fail("connect");
exit(errct);
}
if (abort_type == 2) {
/* Give server a chance to close connection */
sleep(2);
rc = write(sd, buf, strlen(buf) + 1);
if (rc != -1) {
if (!info->ignore_write_conn_reset) {
test_fail("write should have failed\n");
}
} else if (errno != EPIPE && errno != ECONNRESET) {
test_fail("errno should've been EPIPE/ECONNRESET\n");
}
}
rc = close(sd);
if (rc == -1) {
test_fail("close");
}
exit(errct);
}
static void test_abort_server(const struct socket_test_info *info,
pid_t pid, int abort_type)
{
char buf[BUFSIZE];
int sd, rc, client_sd, status, on;
struct sockaddr_storage addr;
socklen_t addr_len;
addr_len = info->clientaddrlen;
sd = socket(info->domain, info->type, 0);
if (sd == -1) {
test_fail("socket");
}
on = 1;
(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
assert(sizeof(addr) >= info->clientaddrlen);
memcpy(&addr, info->clientaddr, info->clientaddrlen);
rc = bind(sd, info->serveraddr, info->serveraddrlen);
if (rc == -1) {
test_fail("bind");
}
rc = listen(sd, 5);
if (rc == -1) {
test_fail("listen");
}
/* we're ready for connections, time to tell the client
* to start the test
*/
kill(pid, SIGUSR1);
client_sd = accept(sd, (struct sockaddr *) &addr, &addr_len);
if (client_sd == -1) {
test_fail("accept");
}
if (abort_type == 1) {
memset(buf, '\0', BUFSIZE);
rc = read(client_sd, buf, BUFSIZE);
if (rc != 0 && rc != -1) {
test_fail("read should've failed or returned zero\n");
}
if (rc != 0 && errno != ECONNRESET) {
test_fail("errno should've been ECONNRESET\n");
}
} /* else if (abort_type == 2) { */
rc = close(client_sd);
if (rc == -1) {
test_fail("close");
}
/* } */
rc = close(sd);
if (rc == -1) {
test_fail("close");
}
/* wait for client to exit */
do {
errno = 0;
rc = waitpid(pid, &status, 0);
} while (rc == -1 && errno == EINTR);
/* we use the exit status to get its error count */
errct += WEXITSTATUS(status);
}
void test_simple_client_server(const struct socket_test_info *info, int type)
{
pid_t pid;
debug("entering test_simple_client_server()");
info->callback_cleanup();
/* the signal handler is only used by the client, but we have to
* install it now. if we don't the server may signal the client
* before the handler is installed.
*/
debug("installing signal handler");
if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
}
debug("signal handler installed");
server_ready = 0;
pid = fork();
if (pid == -1) {
test_fail("fork() failed");
return;
} else if (pid == 0) {
debug("child");
errct = 0;
test_simple_client(info, type);
test_fail("we should never get here");
exit(1);
} else {
debug("parent");
test_simple_server(info, type, pid);
debug("parent done");
}
info->callback_cleanup();
debug("leaving test_simple_client_server()");
}
void test_msg_dgram(const struct socket_test_info *info)
{
int rc;
int src;
int dst;
struct sockaddr_storage addr;
struct iovec iov[3];
struct msghdr msg1;
struct msghdr msg2;
char buf1[BUFSIZE];
char buf2[BUFSIZE];
char buf3[BUFSIZE];
debug("entering test_msg_dgram");
info->callback_cleanup();
src = socket(info->domain, SOCK_DGRAM, 0);
if (src == -1) {
test_fail("socket");
}
dst = socket(info->domain, SOCK_DGRAM, 0);
if (dst == -1) {
test_fail("socket");
}
rc = bind(src, info->serveraddr2, info->serveraddr2len);
if (rc == -1) {
test_fail("bind");
}
assert(info->clientaddrlen <= sizeof(addr));
memcpy(&addr, info->clientaddr, info->clientaddrlen);
rc = bind(dst, info->serveraddr, info->serveraddrlen);
if (rc == -1) {
test_fail("bind");
}
memset(&buf1, '\0', BUFSIZE);
memset(&buf2, '\0', BUFSIZE);
memset(&buf3, '\0', BUFSIZE);
strncpy(buf1, "Minix ", BUFSIZE-1);
strncpy(buf2, "is ", BUFSIZE-1);
strncpy(buf3, "great!", BUFSIZE-1);
iov[0].iov_base = buf1;
iov[0].iov_len = 6;
iov[1].iov_base = buf2;
iov[1].iov_len = 3;
iov[2].iov_base = buf3;
iov[2].iov_len = 32;
memset(&msg1, '\0', sizeof(struct msghdr));
msg1.msg_name = &addr;
msg1.msg_namelen = info->clientaddrlen;
msg1.msg_iov = iov;
msg1.msg_iovlen = 3;
msg1.msg_control = NULL;
msg1.msg_controllen = 0;
msg1.msg_flags = 0;
rc = sendmsg(src, &msg1, 0);
if (rc == -1) {
test_fail("sendmsg");
}
memset(&buf1, '\0', BUFSIZE);
memset(&buf2, '\0', BUFSIZE);
iov[0].iov_base = buf1;
iov[0].iov_len = 9;
iov[1].iov_base = buf2;
iov[1].iov_len = 32;
memset(&addr, '\0', sizeof(addr));
memset(&msg2, '\0', sizeof(struct msghdr));
msg2.msg_name = &addr;
msg2.msg_namelen = sizeof(addr);
msg2.msg_iov = iov;
msg2.msg_iovlen = 2;
msg2.msg_control = NULL;
msg2.msg_controllen = 0;
msg2.msg_flags = 0;
rc = recvmsg(dst, &msg2, 0);
if (rc == -1) {
test_fail("recvmsg");
}
if (strncmp(buf1, "Minix is ", 9) || strncmp(buf2, "great!", 6)) {
test_fail("recvmsg");
}
info->callback_check_sockaddr((struct sockaddr *) &addr,
msg2.msg_namelen, "recvmsg", 2);
rc = close(dst);
if (rc == -1) {
test_fail("close");
}
rc = close(src);
if (rc == -1) {
test_fail("close");
}
info->callback_cleanup();
debug("leaving test_msg_dgram");
}
#define check_select(sd, rd, wr, block) \
check_select_internal(sd, rd, wr, block, 1, __LINE__)
#define check_select_cond(sd, rd, wr, block, allchecks) \
check_select_internal(sd, rd, wr, block, allchecks, __LINE__)
static void
check_select_internal(int sd, int rd, int wr, int block, int allchecks, int line)
{
fd_set read_set, write_set;
struct timeval tv;
FD_ZERO(&read_set);
if (rd != -1)
FD_SET(sd, &read_set);
FD_ZERO(&write_set);
if (wr != -1)
FD_SET(sd, &write_set);
tv.tv_sec = block ? 2 : 0;
tv.tv_usec = 0;
errno = 0;
if (select(sd + 1, &read_set, &write_set, NULL, &tv) < 0)
test_fail_fl("select() failed unexpectedly", __FILE__, line);
if (rd != -1 && !!FD_ISSET(sd, &read_set) != rd && allchecks)
test_fail_fl("select() mismatch on read operation",
__FILE__, line);
if (wr != -1 && !!FD_ISSET(sd, &write_set) != wr && allchecks)
test_fail_fl("select() mismatch on write operation",
__FILE__, line);
}
/*
* Verify that:
* - a nonblocking connecting socket for which there is no accepter, will
* return EINPROGRESS and complete in the background later;
* - a nonblocking listening socket will return EAGAIN on accept;
* - connecting a connecting socket yields EALREADY;
* - connecting a connected socket yields EISCONN;
* - selecting for read and write on a connecting socket will only satisfy the
* write only once it is connected;
* - doing a nonblocking write on a connecting socket yields EAGAIN;
* - doing a nonblocking read on a connected socket with no pending data yields
* EAGAIN.
*/
void
test_nonblock(const struct socket_test_info *info)
{
char buf[BUFSIZE];
socklen_t len;
int server_sd, client_sd;
struct sockaddr_storage addr;
int status, on;
debug("entering test_nonblock()");
memset(buf, 0, sizeof(buf));
SOCKET(server_sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
test_fail("bind() should have worked");
if (info->callback_set_listen_opt != NULL)
info->callback_set_listen_opt(server_sd);
if (listen(server_sd, 8) == -1)
test_fail("listen() should have worked");
fcntl(server_sd, F_SETFL, fcntl(server_sd, F_GETFL) | O_NONBLOCK);
check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
len = sizeof(addr);
if (accept(server_sd, (struct sockaddr *) &addr, &len) != -1 ||
errno != EAGAIN)
test_fail("accept() should have yielded EAGAIN");
SOCKET(client_sd, info->domain, info->type, 0);
fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
test_fail("connect() should have failed");
} else if (errno != EINPROGRESS) {
test_fail("connect() should have yielded EINPROGRESS");
}
check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
!info->ignore_select_delay);
if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
test_fail("connect() should have failed");
} else if (errno != EALREADY && errno != EISCONN) {
test_fail("connect() should have yielded EALREADY");
}
if (recv(client_sd, buf, sizeof(buf), 0) != -1 || errno != EAGAIN)
test_fail("recv() should have yielded EAGAIN");
/* This may be an implementation aspect, or even plain wrong (?). */
if (!info->ignore_send_waiting) {
if (send(client_sd, buf, sizeof(buf), 0) != -1) {
test_fail("send() should have failed");
} else if (errno != EAGAIN) {
test_fail("send() should have yielded EAGAIN");
}
}
switch (fork()) {
case 0:
errct = 0;
close(client_sd);
check_select(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
len = sizeof(addr);
client_sd = accept(server_sd, (struct sockaddr *) &addr, &len);
if (client_sd == -1)
test_fail("accept() should have succeeded");
check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
close(server_sd);
/* Let the socket become writable in the parent process. */
sleep(1);
if (write(client_sd, buf, 1) != 1)
test_fail("write() should have succeeded");
/* Wait for the client side to close. */
check_select_cond(client_sd, 0 /*read*/, 1 /*write*/,
0 /*block*/, !info->ignore_select_delay /*allchecks*/);
check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
exit(errct);
case -1:
test_fail("can't fork");
default:
break;
}
close(server_sd);
check_select(client_sd, 0 /*read*/, 1 /*write*/, 1 /*block*/);
check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1 ||
errno != EISCONN)
test_fail("connect() should have yielded EISCONN");
check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
if (read(client_sd, buf, 1) != 1)
test_fail("read() should have succeeded");
check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
if (read(client_sd, buf, 1) != -1 || errno != EAGAIN)
test_fail("read() should have yielded EAGAIN");
/* Let the child process block on the select waiting for the close. */
sleep(1);
close(client_sd);
errno = 0;
if (wait(&status) <= 0)
test_fail("wait() should have succeeded");
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
test_fail("child process failed the test");
info->callback_cleanup();
debug("leaving test_nonblock()");
}
/*
* Verify that a nonblocking connect for which there is an accepter, succeeds
* immediately. A pretty lame test, only here for completeness.
*/
void
test_connect_nb(const struct socket_test_info *info)
{
socklen_t len;
int server_sd, client_sd;
struct sockaddr_storage addr;
int status, on;
debug("entering test_connect_nb()");
SOCKET(server_sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
test_fail("bind() should have worked");
if (listen(server_sd, 8) == -1)
test_fail("listen() should have worked");
switch (fork()) {
case 0:
errct = 0;
len = sizeof(addr);
if (accept(server_sd, (struct sockaddr *) &addr, &len) == -1)
test_fail("accept() should have succeeded");
exit(errct);
case -1:
test_fail("can't fork");
default:
break;
}
close(server_sd);
sleep(1);
SOCKET(client_sd, info->domain, info->type, 0);
fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
if (connect(client_sd, info->clientaddr, info->clientaddrlen) != 0) {
if (!info->ignore_connect_delay) {
test_fail("connect() should have succeeded");
} else if (errno != EINPROGRESS) {
test_fail("connect() should have succeeded or "
"failed with EINPROGRESS");
}
}
close(client_sd);
if (wait(&status) <= 0)
test_fail("wait() should have succeeded");
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
test_fail("child process failed the test");
info->callback_cleanup();
debug("leaving test_connect_nb()");
}
static void
dummy_handler(int sig)
{
/* Nothing. */
}
/*
* Verify that:
* - interrupting a blocking connect will return EINTR but complete in the
* background later;
* - doing a blocking write on an asynchronously connecting socket succeeds
* once the socket is connected.
* - doing a nonblocking write on a connected socket with lots of pending data
* yields EAGAIN.
*/
void
test_intr(const struct socket_test_info *info)
{
struct sigaction act, oact;
char buf[BUFSIZE];
int isconn;
socklen_t len;
int server_sd, client_sd;
struct sockaddr_storage addr;
int r, status, on;
debug("entering test_intr()");
memset(buf, 0, sizeof(buf));
SOCKET(server_sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
test_fail("bind() should have worked");
if (info->callback_set_listen_opt != NULL)
info->callback_set_listen_opt(server_sd);
if (listen(server_sd, 8) == -1)
test_fail("listen() should have worked");
SOCKET(client_sd, info->domain, info->type, 0);
memset(&act, 0, sizeof(act));
act.sa_handler = dummy_handler;
if (sigaction(SIGALRM, &act, &oact) == -1)
test_fail("sigaction() should have succeeded");
if (info->domain != PF_INET) alarm(1);
isconn = 0;
if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
if (!info->ignore_connect_unaccepted) {
test_fail("connect() should have failed");
}
isconn = 1;
} else if (errno != EINTR) {
test_fail("connect() should have yielded EINTR");
}
alarm(0);
check_select(client_sd, 0 /*read*/, isconn /*write*/, 0 /*block*/);
switch (fork()) {
case 0:
errct = 0;
close(client_sd);
/* Ensure that the parent is blocked on the send(). */
sleep(1);
check_select(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
len = sizeof(addr);
client_sd = accept(server_sd, (struct sockaddr *) &addr, &len);
if (client_sd == -1)
test_fail("accept() should have succeeded");
check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
close(server_sd);
check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
if (recv(client_sd, buf, sizeof(buf), 0) != sizeof(buf))
test_fail("recv() should have yielded bytes");
/* No partial transfers should be happening. */
check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
sleep(1);
fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) |
O_NONBLOCK);
/* We can only test nonblocking writes by filling the pipe. */
while ((r = write(client_sd, buf, sizeof(buf))) > 0);
if (r != -1) {
test_fail("write() should have failed");
} else if (errno != EAGAIN) {
test_fail("write() should have yielded EAGAIN");
}
check_select(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/);
if (write(client_sd, buf, 1) != -1) {
test_fail("write() should have failed");
} else if (errno != EAGAIN) {
test_fail("write() should have yielded EAGAIN");
}
exit(errct);
case -1:
test_fail("can't fork");
default:
break;
}
close(server_sd);
if (send(client_sd, buf, sizeof(buf), 0) != sizeof(buf))
test_fail("send() should have succeded");
check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
if (wait(&status) <= 0)
test_fail("wait() should have succeeded");
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
test_fail("child process failed the test");
check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
close(client_sd);
sigaction(SIGALRM, &oact, NULL);
info->callback_cleanup();
debug("leaving test_intr()");
}
/*
* Verify that closing a connecting socket before it is accepted will result in
* no activity on the accepting side later.
*/
void
test_connect_close(const struct socket_test_info *info)
{
int server_sd, client_sd, sd, on;
struct sockaddr_storage addr;
socklen_t len;
debug("entering test_connect_close()");
SOCKET(server_sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
test_fail("bind() should have worked");
if (info->callback_set_listen_opt != NULL)
info->callback_set_listen_opt(server_sd);
if (listen(server_sd, 8) == -1)
test_fail("listen() should have worked");
fcntl(server_sd, F_SETFL, fcntl(server_sd, F_GETFL) | O_NONBLOCK);
check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
SOCKET(client_sd, info->domain, info->type, 0);
fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1 ||
errno != EINPROGRESS)
test_fail("connect() should have yielded EINPROGRESS");
check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
!info->ignore_select_delay);
check_select_cond(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/,
!info->ignore_select_delay);
close(client_sd);
check_select_cond(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/,
!info->ignore_select_delay);
len = sizeof(addr);
errno = 0;
if ((sd = accept(server_sd, (struct sockaddr *) &addr, &len)) != -1) {
if (!info->ignore_accept_delay) {
test_fail("accept() should have failed");
}
close(sd);
} else if (errno != EAGAIN) {
test_fail("accept() should have yielded EAGAIN");
}
close(server_sd);
info->callback_cleanup();
debug("leaving test_connect_close()");
}
/*
* Verify that closing a listening socket will cause a blocking connect to fail
* with ECONNRESET, and that a subsequent write will yield EPIPE. This test
* works only if the connect(2) does not succeed before accept(2) is called at
* all, which means it is limited to UDS with LOCAL_CONNWAIT right now.
*/
void
test_listen_close(const struct socket_test_info *info)
{
int server_sd, client_sd;
int status, on;
char byte;
debug("entering test_listen_close()");
SOCKET(server_sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
test_fail("bind() should have worked");
if (info->callback_set_listen_opt != NULL)
info->callback_set_listen_opt(server_sd);
if (listen(server_sd, 8) == -1)
test_fail("listen() should have worked");
switch (fork()) {
case 0:
sleep(1);
exit(0);
case -1:
test_fail("can't fork");
default:
break;
}
close(server_sd);
SOCKET(client_sd, info->domain, info->type, 0);
byte = 0;
if (write(client_sd, &byte, 1) != -1 || errno != ENOTCONN)
test_fail("write() should have yielded ENOTCONN");
if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
test_fail("connect() should have failed");
} else if (errno != ECONNRESET) {
test_fail("connect() should have yielded ECONNRESET");
}
/*
* The error we get on the next write() depends on whether the socket
* may be reused after a failed connect. For UDS, it may be, so we get
* ENOTCONN. Otherwise we would expect EPIPE.
*/
if (write(client_sd, &byte, 1) != -1 || errno != ENOTCONN)
test_fail("write() should have yielded ENOTCONN");
close(client_sd);
if (wait(&status) <= 0)
test_fail("wait() should have succeeded");
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
test_fail("child process failed the test");
info->callback_cleanup();
debug("leaving test_listen_close()");
}
/*
* Verify that closing a listening socket will cause a nonblocking connect to
* result in the socket becoming readable and writable, and yielding ECONNRESET
* and EPIPE on the next two writes, respectively.
*/
void
test_listen_close_nb(const struct socket_test_info *info)
{
int server_sd, client_sd;
int status, on;
char byte;
debug("entering test_listen_close_nb()");
SOCKET(server_sd, info->domain, info->type, 0);
on = 1;
(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
test_fail("bind() should have worked");
if (info->callback_set_listen_opt != NULL)
info->callback_set_listen_opt(server_sd);
if (listen(server_sd, 8) == -1)
test_fail("listen() should have worked");
switch (fork()) {
case 0:
sleep(1);
exit(0);
case -1:
test_fail("can't fork");
default:
break;
}
close(server_sd);
SOCKET(client_sd, info->domain, info->type, 0);
fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1 ||
errno != EINPROGRESS)
test_fail("connect() should have yielded EINPROGRESS");
check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
!info->ignore_select_delay);
check_select_cond(client_sd, 1 /*read*/, 1 /*write*/, 1 /*block*/,
!info->ignore_select_delay);
byte = 0;
if (write(client_sd, &byte, 1) != -1) {
if (!info->ignore_write_conn_reset) {
test_fail("write() should have failed");
}
} else if (errno != ECONNRESET) {
test_fail("write() should have yielded ECONNRESET");
}
check_select_cond(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/,
!info->ignore_select_delay);
close(client_sd);
if (wait(&status) <= 0)
test_fail("wait() should have succeeded");
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
test_fail("child process failed the test");
info->callback_cleanup();
debug("leaving test_listen_close_nb()");
}
|