Elliott Hughes | 6a41b0f | 2014-05-13 16:05:51 -0700 | [diff] [blame] | 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 | |
| 21 | TEST(arpa_inet, inet_addr) { |
| 22 | ASSERT_EQ((htonl)(0x7f000001), inet_addr("127.0.0.1")); |
| 23 | } |
| 24 | |
| 25 | TEST(arpa_inet, inet_aton) { |
| 26 | in_addr a; |
Elliott Hughes | 0478666 | 2015-10-08 18:04:49 -0700 | [diff] [blame] | 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)); |
Elliott Hughes | 6a41b0f | 2014-05-13 16:05:51 -0700 | [diff] [blame] | 41 | ASSERT_EQ((htonl)(0x7f000001), a.s_addr); |
Elliott Hughes | 0478666 | 2015-10-08 18:04:49 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
Elliott Hughes | 7b77cb3 | 2015-10-09 17:27:41 -0700 | [diff] [blame] | 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)); |
Elliott Hughes | 6a41b0f | 2014-05-13 16:05:51 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TEST(arpa_inet, inet_lnaof) { |
| 109 | in_addr a = { htonl(0x12345678) }; |
| 110 | ASSERT_EQ(0x00345678U, inet_lnaof(a)); |
| 111 | } |
| 112 | |
| 113 | TEST(arpa_inet, inet_makeaddr) { |
| 114 | in_addr a = inet_makeaddr(0x12U, 0x345678); |
| 115 | ASSERT_EQ((htonl)(0x12345678), a.s_addr); |
| 116 | } |
| 117 | |
| 118 | TEST(arpa_inet, inet_netof) { |
| 119 | in_addr a = { htonl(0x12345678) }; |
| 120 | ASSERT_EQ(0x12U, inet_netof(a)); |
| 121 | } |
| 122 | |
| 123 | TEST(arpa_inet, inet_network) { |
| 124 | ASSERT_EQ(0x7f000001U, inet_network("127.0.0.1")); |
Elliott Hughes | 0478666 | 2015-10-08 18:04:49 -0700 | [diff] [blame] | 125 | ASSERT_EQ(0x7fU, inet_network("0x7f")); |
| 126 | ASSERT_EQ(~0U, inet_network("")); |
Elliott Hughes | 6a41b0f | 2014-05-13 16:05:51 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | TEST(arpa_inet, inet_ntoa) { |
| 130 | in_addr a = { (htonl)(0x7f000001) }; |
| 131 | ASSERT_STREQ("127.0.0.1", inet_ntoa(a)); |
| 132 | } |
| 133 | |
| 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 | } |
Elliott Hughes | 5c8c88d | 2014-05-13 19:17:46 -0700 | [diff] [blame] | 141 | |
| 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 | } |
Elliott Hughes | c30a1c0 | 2021-04-09 13:32:55 -0700 | [diff] [blame] | 161 | |
| 162 | TEST(arpa_inet, inet_nsap_addr) { |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 163 | #if !defined(MUSL) |
Elliott Hughes | c30a1c0 | 2021-04-09 13:32:55 -0700 | [diff] [blame] | 164 | // inet_nsap_addr() doesn't seem to be documented anywhere, but it's basically |
| 165 | // text to binary for arbitrarily-long strings like "0xdeadbeef". Any |
| 166 | // '.', '+', or '/' characters are ignored as punctuation. The return value is |
| 167 | // the length in bytes, or 0 for all errors. |
| 168 | u_char buf[32]; |
| 169 | |
| 170 | // Missing "0x" prefix. |
| 171 | ASSERT_EQ(0U, inet_nsap_addr("123", buf, sizeof(buf))); |
| 172 | ASSERT_EQ(0U, inet_nsap_addr("012", buf, sizeof(buf))); |
| 173 | |
| 174 | // 1 byte. |
| 175 | ASSERT_EQ(1U, inet_nsap_addr("0x12", buf, sizeof(buf))); |
| 176 | ASSERT_EQ(0x12, buf[0]); |
| 177 | |
| 178 | // 10 bytes. |
| 179 | ASSERT_EQ(10U, inet_nsap_addr("0x1234567890abcdef0011", buf, sizeof(buf))); |
| 180 | ASSERT_EQ(0x12, buf[0]); |
| 181 | ASSERT_EQ(0x34, buf[1]); |
| 182 | ASSERT_EQ(0x56, buf[2]); |
| 183 | ASSERT_EQ(0x78, buf[3]); |
| 184 | ASSERT_EQ(0x90, buf[4]); |
| 185 | ASSERT_EQ(0xab, buf[5]); |
| 186 | ASSERT_EQ(0xcd, buf[6]); |
| 187 | ASSERT_EQ(0xef, buf[7]); |
| 188 | ASSERT_EQ(0x00, buf[8]); |
| 189 | ASSERT_EQ(0x11, buf[9]); |
| 190 | |
| 191 | // Ignored punctuation. |
| 192 | ASSERT_EQ(10U, inet_nsap_addr("0x1122.3344+5566/7788/99aa", buf, sizeof(buf))); |
| 193 | ASSERT_EQ(0x11, buf[0]); |
| 194 | ASSERT_EQ(0x22, buf[1]); |
| 195 | ASSERT_EQ(0x33, buf[2]); |
| 196 | ASSERT_EQ(0x44, buf[3]); |
| 197 | ASSERT_EQ(0x55, buf[4]); |
| 198 | ASSERT_EQ(0x66, buf[5]); |
| 199 | ASSERT_EQ(0x77, buf[6]); |
| 200 | ASSERT_EQ(0x88, buf[7]); |
| 201 | ASSERT_EQ(0x99, buf[8]); |
| 202 | ASSERT_EQ(0xaa, buf[9]); |
| 203 | |
| 204 | // Truncated. |
| 205 | ASSERT_EQ(4U, inet_nsap_addr("0xdeadbeef666666666666", buf, 4)); |
| 206 | // Overwritten... |
| 207 | ASSERT_EQ(0xde, buf[0]); |
| 208 | ASSERT_EQ(0xad, buf[1]); |
| 209 | ASSERT_EQ(0xbe, buf[2]); |
| 210 | ASSERT_EQ(0xef, buf[3]); |
| 211 | // Same as before... |
| 212 | ASSERT_EQ(0x55, buf[4]); |
| 213 | ASSERT_EQ(0x66, buf[5]); |
| 214 | ASSERT_EQ(0x77, buf[6]); |
| 215 | ASSERT_EQ(0x88, buf[7]); |
| 216 | ASSERT_EQ(0x99, buf[8]); |
| 217 | ASSERT_EQ(0xaa, buf[9]); |
| 218 | |
| 219 | // Case insensitivity. |
| 220 | ASSERT_EQ(6U, inet_nsap_addr("0xaAbBcCdDeEfF", buf, 6)); |
| 221 | ASSERT_EQ(0xaa, buf[0]); |
| 222 | ASSERT_EQ(0xbb, buf[1]); |
| 223 | ASSERT_EQ(0xcc, buf[2]); |
| 224 | ASSERT_EQ(0xdd, buf[3]); |
| 225 | ASSERT_EQ(0xee, buf[4]); |
| 226 | ASSERT_EQ(0xff, buf[5]); |
| 227 | |
| 228 | // Punctuation isn't allowed within a byte. |
| 229 | ASSERT_EQ(0U, inet_nsap_addr("0x1.122", buf, sizeof(buf))); |
| 230 | // Invalid punctuation. |
| 231 | ASSERT_EQ(0U, inet_nsap_addr("0x11,22", buf, sizeof(buf))); |
| 232 | // Invalid hex digit. |
| 233 | ASSERT_EQ(0U, inet_nsap_addr("0x11.g2", buf, sizeof(buf))); |
| 234 | ASSERT_EQ(0U, inet_nsap_addr("0x11.2g", buf, sizeof(buf))); |
| 235 | // Invalid half-byte. |
| 236 | ASSERT_EQ(0U, inet_nsap_addr("0x11.2", buf, sizeof(buf))); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 237 | #else |
| 238 | GTEST_SKIP() << "musl doesn't have inet_nsap_addr"; |
| 239 | #endif |
Elliott Hughes | c30a1c0 | 2021-04-09 13:32:55 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | TEST(arpa_inet, inet_nsap_ntoa) { |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 243 | #if !defined(MUSL) |
Elliott Hughes | c30a1c0 | 2021-04-09 13:32:55 -0700 | [diff] [blame] | 244 | // inet_nsap_ntoa() doesn't seem to be documented anywhere, but it's basically |
| 245 | // binary to text for arbitrarily-long byte buffers. |
| 246 | // The return value is a pointer to the buffer. No errors are possible. |
| 247 | const unsigned char bytes[] = {0x01, 0x00, 0x02, 0x0e, 0xf0, 0x20}; |
| 248 | char dst[32]; |
| 249 | ASSERT_EQ(dst, inet_nsap_ntoa(6, bytes, dst)); |
| 250 | ASSERT_STREQ(dst, "0x01.0002.0EF0.20"); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 251 | #else |
| 252 | GTEST_SKIP() << "musl doesn't have inet_nsap_ntoa"; |
| 253 | #endif |
Elliott Hughes | c30a1c0 | 2021-04-09 13:32:55 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | TEST(arpa_inet, inet_nsap_ntoa__nullptr) { |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 257 | #if !defined(MUSL) |
Elliott Hughes | c30a1c0 | 2021-04-09 13:32:55 -0700 | [diff] [blame] | 258 | // If you don't provide a destination, a static buffer is provided for you. |
| 259 | const unsigned char bytes[] = {0x01, 0x00, 0x02, 0x0e, 0xf0, 0x20}; |
| 260 | ASSERT_STREQ("0x01.0002.0EF0.20", inet_nsap_ntoa(6, bytes, nullptr)); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 261 | #else |
| 262 | GTEST_SKIP() << "musl doesn't have inet_nsap_ntoa"; |
| 263 | #endif |
Elliott Hughes | c30a1c0 | 2021-04-09 13:32:55 -0700 | [diff] [blame] | 264 | } |