blob: 8ba0d7a9796ce9e918bc587e679d9575f012e9b0 [file] [log] [blame]
Elliott Hughes6a41b0f2014-05-13 16:05:51 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <arpa/inet.h>
20
21TEST(arpa_inet, inet_addr) {
22 ASSERT_EQ((htonl)(0x7f000001), inet_addr("127.0.0.1"));
23}
24
25TEST(arpa_inet, inet_aton) {
26 in_addr a;
Elliott Hughes04786662015-10-08 18:04:49 -070027
28 // a.b.c.d
29 a.s_addr = 0;
30 ASSERT_EQ(1, inet_aton("127.1.2.3", &a));
31 ASSERT_EQ((htonl)(0x7f010203), a.s_addr);
32
33 // a.b.c
34 a.s_addr = 0;
35 ASSERT_EQ(1, inet_aton("127.1.2", &a));
36 ASSERT_EQ((htonl)(0x7f010002), a.s_addr);
37
38 // a.b
39 a.s_addr = 0;
40 ASSERT_EQ(1, inet_aton("127.1", &a));
Elliott Hughes6a41b0f2014-05-13 16:05:51 -070041 ASSERT_EQ((htonl)(0x7f000001), a.s_addr);
Elliott Hughes04786662015-10-08 18:04:49 -070042
43 // a
44 a.s_addr = 0;
45 ASSERT_EQ(1, inet_aton("0x7f000001", &a));
46 ASSERT_EQ((htonl)(0x7f000001), a.s_addr);
47
48 // Hex (0x) and mixed-case hex digits.
49 a.s_addr = 0;
50 ASSERT_EQ(1, inet_aton("0xFf.0.0.1", &a));
51 ASSERT_EQ((htonl)(0xff000001), a.s_addr);
52
53 // Hex (0X) and mixed-case hex digits.
54 a.s_addr = 0;
55 ASSERT_EQ(1, inet_aton("0XfF.0.0.1", &a));
56 ASSERT_EQ((htonl)(0xff000001), a.s_addr);
57
58 // Octal.
59 a.s_addr = 0;
60 ASSERT_EQ(1, inet_aton("0177.0.0.1", &a));
61 ASSERT_EQ((htonl)(0x7f000001), a.s_addr);
62
63 a.s_addr = 0;
64 ASSERT_EQ(1, inet_aton("036", &a));
65 ASSERT_EQ((htonl)(036U), a.s_addr);
66}
67
68TEST(arpa_inet, inet_aton_nullptr) {
69 ASSERT_EQ(0, inet_aton("", nullptr));
70 ASSERT_EQ(1, inet_aton("127.0.0.1", nullptr));
71}
72
73TEST(arpa_inet, inet_aton_invalid) {
74 ASSERT_EQ(0, inet_aton("", nullptr)); // Empty.
75 ASSERT_EQ(0, inet_aton("x", nullptr)); // Leading junk.
76 ASSERT_EQ(0, inet_aton("127.0.0.1x", nullptr)); // Trailing junk.
77 ASSERT_EQ(0, inet_aton("09.0.0.1", nullptr)); // Invalid octal.
78 ASSERT_EQ(0, inet_aton("0xg.0.0.1", nullptr)); // Invalid hex.
79
80 ASSERT_EQ(0, inet_aton("1.2.3.4.5", nullptr)); // Too many dots.
81 ASSERT_EQ(0, inet_aton("1.2.3.4.", nullptr)); // Trailing dot.
82
83 // Out of range a.b.c.d form.
84 ASSERT_EQ(0, inet_aton("999.0.0.1", nullptr));
85 ASSERT_EQ(0, inet_aton("0.999.0.1", nullptr));
86 ASSERT_EQ(0, inet_aton("0.0.999.1", nullptr));
87 ASSERT_EQ(0, inet_aton("0.0.0.999", nullptr));
88
89 // Out of range a.b.c form.
90 ASSERT_EQ(0, inet_aton("256.0.0", nullptr));
91 ASSERT_EQ(0, inet_aton("0.256.0", nullptr));
92 ASSERT_EQ(0, inet_aton("0.0.0x10000", nullptr));
93
94 // Out of range a.b form.
95 ASSERT_EQ(0, inet_aton("256.0", nullptr));
96 ASSERT_EQ(0, inet_aton("0.0x1000000", nullptr));
97
98 // Out of range a form.
99 ASSERT_EQ(0, inet_aton("0x100000000", nullptr));
100
101 ASSERT_EQ(0, inet_aton("0400.0.0.1", nullptr)); // Out of range octal.
Elliott Hughes6a41b0f2014-05-13 16:05:51 -0700102}
103
104TEST(arpa_inet, inet_lnaof) {
105 in_addr a = { htonl(0x12345678) };
106 ASSERT_EQ(0x00345678U, inet_lnaof(a));
107}
108
109TEST(arpa_inet, inet_makeaddr) {
110 in_addr a = inet_makeaddr(0x12U, 0x345678);
111 ASSERT_EQ((htonl)(0x12345678), a.s_addr);
112}
113
114TEST(arpa_inet, inet_netof) {
115 in_addr a = { htonl(0x12345678) };
116 ASSERT_EQ(0x12U, inet_netof(a));
117}
118
119TEST(arpa_inet, inet_network) {
120 ASSERT_EQ(0x7f000001U, inet_network("127.0.0.1"));
Elliott Hughes04786662015-10-08 18:04:49 -0700121 ASSERT_EQ(0x7fU, inet_network("0x7f"));
122 ASSERT_EQ(~0U, inet_network(""));
Elliott Hughes6a41b0f2014-05-13 16:05:51 -0700123}
124
125TEST(arpa_inet, inet_ntoa) {
126 in_addr a = { (htonl)(0x7f000001) };
127 ASSERT_STREQ("127.0.0.1", inet_ntoa(a));
128}
129
130TEST(arpa_inet, inet_pton__inet_ntop) {
131 sockaddr_storage ss;
132 ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss));
133
134 char s[INET_ADDRSTRLEN];
135 ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss, s, INET_ADDRSTRLEN));
136}
Elliott Hughes5c8c88d2014-05-13 19:17:46 -0700137
138TEST(arpa_inet, inet_ntop_overflow) {
139 // OpenBSD's inet_ntop had a bug where passing a 'size' larger than INET_ADDRSTRLEN
140 // for AF_INET or INET6_ADDRSTRLEN for AF_INET6 would cause inet_ntop to overflow an
141 // internal buffer.
142
143 sockaddr_storage ss4;
144 ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss4));
145
146 sockaddr_storage ss6;
147 ASSERT_EQ(1, inet_pton(AF_INET6, "::1", &ss6));
148
149 char s4[INET_ADDRSTRLEN];
150 char s6[INET6_ADDRSTRLEN];
151 ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss4, s4, INET_ADDRSTRLEN));
152 ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss4, s4, 2*INET_ADDRSTRLEN));
153 ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, INET_ADDRSTRLEN));
154 ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, INET6_ADDRSTRLEN));
155 ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, 2*INET6_ADDRSTRLEN));
156}