]> git.saurik.com Git - apple/libc.git/blob - tests/net.c
Libc-1158.1.2.tar.gz
[apple/libc.git] / tests / net.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <net/if_dl.h>
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <darwintest.h>
9
10 T_DECL(link_ntoa_basic, "link_ntoa converts to proper string")
11 {
12 // u_char sdl_len; /* Total length of sockaddr */
13 // u_char sdl_family; /* AF_LINK */
14 // u_short sdl_index; /* if != 0, system given index for interface */
15 // u_char sdl_type; /* interface type */
16 // u_char sdl_nlen; /* interface name length, no trailing 0 reqd. */
17 // u_char sdl_alen; /* link level address length */
18 // u_char sdl_slen; /* link layer selector length */
19 // char sdl_data[12]; /* minimum work area, can be larger;
20 // contains both if name and ll address */
21
22 struct sockaddr_dl sad;
23 bzero(&sad, sizeof(sad));
24 sad.sdl_nlen = 3;
25 sad.sdl_len = 0;
26 sad.sdl_data[0] = 'l';
27 sad.sdl_data[1] = 'e';
28 sad.sdl_data[2] = '0';
29 sad.sdl_data[3] = 0x01;
30 sad.sdl_data[4] = 0x80;
31 sad.sdl_data[5] = 0xc2;
32 sad.sdl_data[6] = 0x00;
33 sad.sdl_data[7] = 0x00;
34 sad.sdl_data[8] = 0x02;
35 sad.sdl_data[9] = 0xaa;
36 sad.sdl_data[10] = 0xbb;
37 sad.sdl_data[11] = 0xcc;
38 sad.sdl_alen = 6;
39
40 char *foo = link_ntoa(&sad);
41
42 T_EXPECT_EQ_STR("le0:1.80.c2.0.0.2", foo, NULL);
43 }
44
45 T_DECL(link_ntoa_overflow, "link_ntoa try to overflow")
46 {
47 char sockraw[64];
48 struct sockaddr_dl *sad;
49 sad = (struct sockaddr_dl *)&sockraw;
50 bzero(sad, sizeof(*sad));
51 sad->sdl_nlen = 3;
52 sad->sdl_len = 0;
53 sad->sdl_data[0] = 'l';
54 sad->sdl_data[1] = 'e';
55 sad->sdl_data[2] = '0';
56 sad->sdl_data[3] = 0x11;
57 sad->sdl_data[4] = 0x80;
58 sad->sdl_data[5] = 0xc2;
59 sad->sdl_data[6] = 0x11;
60 sad->sdl_data[7] = 0x11;
61 sad->sdl_data[8] = 0xa2;
62 sad->sdl_data[9] = 0xaa;
63 sad->sdl_data[10] = 0xbb;
64 sad->sdl_data[11] = 0xcc;
65 sockraw[20] = 0xdd;
66 sockraw[21] = 0xee;
67 sockraw[22] = 0xff;
68 sockraw[23] = 0x1a;
69 sockraw[24] = 0x1b;
70 sockraw[25] = 0x1c;
71 sockraw[26] = 0x1d;
72 sockraw[27] = 0x1e;
73 sockraw[28] = 0x1f;
74 sockraw[29] = 0x2a;
75 sockraw[30] = 0x2b;
76 sockraw[31] = 0x2c;
77
78 /* set the length to something that will fit in the buffer */
79 sad->sdl_alen = 20;
80
81 char *foo = link_ntoa(sad);
82
83 char over = foo[64];
84 char over2 = foo[65];
85
86 /* this string should be 66 bytes long and exceed the buffer */
87 sad->sdl_alen = 21;
88
89 foo = link_ntoa(sad);
90
91 T_EXPECT_EQ_STR("", foo, NULL);
92
93 T_EXPECT_EQ(over, foo[64], "did not overflow");
94 T_EXPECT_EQ(over2, foo[65], "did not overflow either");
95 }
96
97 T_DECL(inet_ntop, "inet_ntop")
98 {
99 char *addresses4[] = { "1.2.3.4", "10.0.0.1", "2.2.2.2" };
100 char *addresses6[] = {
101 "2001:db8:85a3::8a2e:370:7334",
102 "::1", "::"
103 };
104 for (int i = 0; i < sizeof(addresses4)/sizeof(addresses4[0]); i++){
105 struct in_addr addr4;
106 char buf[64];
107 T_EXPECT_EQ(inet_pton(AF_INET, addresses4[i], &addr4), 1, "inet_pton(AF_INET, %s)", addresses4[i]);
108 char *str = inet_ntop(AF_INET, &addr4, buf, sizeof(buf));
109 T_EXPECT_NOTNULL(str, "inet_ntop(AF_INET) of %s", addresses4[i]);
110 T_EXPECT_EQ_STR(str, addresses4[i], "round-trip of %s", addresses4[i]);
111 }
112 for (int i = 0; i < sizeof(addresses6)/sizeof(addresses6[0]); i++){
113 struct in6_addr addr6;
114 char buf[64];
115 T_EXPECT_EQ(inet_pton(AF_INET6, addresses6[i], &addr6), 1, "inet_pton(AF_INET6, %s)", addresses6[i]);
116 char *str = inet_ntop(AF_INET6, &addr6, buf, sizeof(buf));
117 T_EXPECT_NOTNULL(str, "inet_ntop(AF_INET) of %s", addresses6[i]);
118 T_EXPECT_EQ_STR(str, addresses6[i], "round-trip of %s", addresses6[i]);
119 }
120 }