• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
TEST(arpa_inet,inet_addr)21 TEST(arpa_inet, inet_addr) {
22   ASSERT_EQ((htonl)(0x7f000001), inet_addr("127.0.0.1"));
23 }
24 
TEST(arpa_inet,inet_aton)25 TEST(arpa_inet, inet_aton) {
26   in_addr a;
27 
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));
41   ASSERT_EQ((htonl)(0x7f000001), a.s_addr);
42 
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 
TEST(arpa_inet,inet_aton_nullptr)68 TEST(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 
TEST(arpa_inet,inet_aton_invalid)73 TEST(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   // 64-bit overflow.
102   ASSERT_EQ(0, inet_aton("0x10000000000000000", nullptr));
103 
104   // Out of range octal.
105   ASSERT_EQ(0, inet_aton("0400.0.0.1", nullptr));
106 }
107 
TEST(arpa_inet,inet_lnaof)108 TEST(arpa_inet, inet_lnaof) {
109   in_addr a = { htonl(0x12345678) };
110   ASSERT_EQ(0x00345678U, inet_lnaof(a));
111 }
112 
TEST(arpa_inet,inet_makeaddr)113 TEST(arpa_inet, inet_makeaddr) {
114   in_addr a = inet_makeaddr(0x12U, 0x345678);
115   ASSERT_EQ((htonl)(0x12345678), a.s_addr);
116 }
117 
TEST(arpa_inet,inet_netof)118 TEST(arpa_inet, inet_netof) {
119   in_addr a = { htonl(0x12345678) };
120   ASSERT_EQ(0x12U, inet_netof(a));
121 }
122 
TEST(arpa_inet,inet_network)123 TEST(arpa_inet, inet_network) {
124   ASSERT_EQ(0x7f000001U, inet_network("127.0.0.1"));
125   ASSERT_EQ(0x7fU, inet_network("0x7f"));
126   ASSERT_EQ(~0U, inet_network(""));
127 }
128 
TEST(arpa_inet,inet_ntoa)129 TEST(arpa_inet, inet_ntoa) {
130   in_addr a = { (htonl)(0x7f000001) };
131   ASSERT_STREQ("127.0.0.1", inet_ntoa(a));
132 }
133 
TEST(arpa_inet,inet_pton__inet_ntop)134 TEST(arpa_inet, inet_pton__inet_ntop) {
135   sockaddr_storage ss;
136   ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss));
137 
138   char s[INET_ADDRSTRLEN];
139   ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss, s, INET_ADDRSTRLEN));
140 }
141 
TEST(arpa_inet,inet_ntop_overflow)142 TEST(arpa_inet, inet_ntop_overflow) {
143   // OpenBSD's inet_ntop had a bug where passing a 'size' larger than INET_ADDRSTRLEN
144   // for AF_INET or INET6_ADDRSTRLEN for AF_INET6 would cause inet_ntop to overflow an
145   // internal buffer.
146 
147   sockaddr_storage ss4;
148   ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss4));
149 
150   sockaddr_storage ss6;
151   ASSERT_EQ(1, inet_pton(AF_INET6, "::1", &ss6));
152 
153   char s4[INET_ADDRSTRLEN];
154   char s6[INET6_ADDRSTRLEN];
155   ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss4, s4, INET_ADDRSTRLEN));
156   ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss4, s4, 2*INET_ADDRSTRLEN));
157   ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, INET_ADDRSTRLEN));
158   ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, INET6_ADDRSTRLEN));
159   ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, 2*INET6_ADDRSTRLEN));
160 }
161