lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include "ares-test.h" |
| 2 | #include "dns-proto.h" |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | extern "C" { |
| 7 | // Remove command-line defines of package variables for the test project... |
| 8 | #undef PACKAGE_NAME |
| 9 | #undef PACKAGE_BUGREPORT |
| 10 | #undef PACKAGE_STRING |
| 11 | #undef PACKAGE_TARNAME |
| 12 | // ... so we can include the library's config without symbol redefinitions. |
| 13 | #include "ares_setup.h" |
| 14 | #include "ares_nowarn.h" |
| 15 | #include "ares_inet_net_pton.h" |
| 16 | #include "ares_data.h" |
| 17 | #include "ares_private.h" |
| 18 | #include "bitncmp.h" |
| 19 | |
| 20 | #ifdef HAVE_ARPA_INET_H |
| 21 | #include <arpa/inet.h> |
| 22 | #endif |
| 23 | } |
| 24 | |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace ares { |
| 29 | namespace test { |
| 30 | |
| 31 | #ifndef CARES_SYMBOL_HIDING |
| 32 | void CheckPtoN4(int size, unsigned int value, const char *input) { |
| 33 | struct in_addr a4; |
| 34 | a4.s_addr = 0; |
| 35 | uint32_t expected = htonl(value); |
| 36 | EXPECT_EQ(size, ares_inet_net_pton(AF_INET, input, &a4, sizeof(a4))) |
| 37 | << " for input " << input; |
| 38 | EXPECT_EQ(expected, a4.s_addr) << " for input " << input; |
| 39 | } |
| 40 | #endif |
| 41 | |
| 42 | TEST_F(LibraryTest, InetPtoN) { |
| 43 | struct in_addr a4; |
| 44 | struct in6_addr a6; |
| 45 | |
| 46 | #ifndef CARES_SYMBOL_HIDING |
| 47 | uint32_t expected; |
| 48 | |
| 49 | CheckPtoN4(4 * 8, 0x01020304, "1.2.3.4"); |
| 50 | CheckPtoN4(4 * 8, 0x81010101, "129.1.1.1"); |
| 51 | CheckPtoN4(4 * 8, 0xC0010101, "192.1.1.1"); |
| 52 | CheckPtoN4(4 * 8, 0xE0010101, "224.1.1.1"); |
| 53 | CheckPtoN4(4 * 8, 0xE1010101, "225.1.1.1"); |
| 54 | CheckPtoN4(4, 0xE0000000, "224"); |
| 55 | CheckPtoN4(4 * 8, 0xFD000000, "253"); |
| 56 | CheckPtoN4(4 * 8, 0xF0010101, "240.1.1.1"); |
| 57 | CheckPtoN4(4 * 8, 0x02030405, "02.3.4.5"); |
| 58 | CheckPtoN4(3 * 8, 0x01020304, "1.2.3.4/24"); |
| 59 | CheckPtoN4(3 * 8, 0x01020300, "1.2.3/24"); |
| 60 | CheckPtoN4(2 * 8, 0xa0000000, "0xa"); |
| 61 | CheckPtoN4(0, 0x02030405, "2.3.4.5/000"); |
| 62 | CheckPtoN4(1 * 8, 0x01020000, "1.2/8"); |
| 63 | CheckPtoN4(2 * 8, 0x01020000, "0x0102/16"); |
| 64 | CheckPtoN4(4 * 8, 0x02030405, "02.3.4.5"); |
| 65 | |
| 66 | EXPECT_EQ(16 * 8, ares_inet_net_pton(AF_INET6, "::", &a6, sizeof(a6))); |
| 67 | EXPECT_EQ(16 * 8, ares_inet_net_pton(AF_INET6, "::1", &a6, sizeof(a6))); |
| 68 | EXPECT_EQ(16 * 8, ares_inet_net_pton(AF_INET6, "1234:5678::", &a6, sizeof(a6))); |
| 69 | EXPECT_EQ(16 * 8, ares_inet_net_pton(AF_INET6, "12:34::ff", &a6, sizeof(a6))); |
| 70 | EXPECT_EQ(16 * 8, ares_inet_net_pton(AF_INET6, "12:34::ffff:1.2.3.4", &a6, sizeof(a6))); |
| 71 | EXPECT_EQ(23, ares_inet_net_pton(AF_INET6, "12:34::ffff:1.2.3.4/23", &a6, sizeof(a6))); |
| 72 | EXPECT_EQ(3 * 8, ares_inet_net_pton(AF_INET6, "12:34::ff/24", &a6, sizeof(a6))); |
| 73 | EXPECT_EQ(0, ares_inet_net_pton(AF_INET6, "12:34::ff/0", &a6, sizeof(a6))); |
| 74 | EXPECT_EQ(16 * 8, ares_inet_net_pton(AF_INET6, "12:34::ffff:0.2", &a6, sizeof(a6))); |
| 75 | EXPECT_EQ(16 * 8, ares_inet_net_pton(AF_INET6, "1234:1234:1234:1234:1234:1234:1234:1234", &a6, sizeof(a6))); |
| 76 | |
| 77 | // Various malformed versions |
| 78 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "", &a4, sizeof(a4))); |
| 79 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, " ", &a4, sizeof(a4))); |
| 80 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "0x", &a4, sizeof(a4))); |
| 81 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "0x ", &a4, sizeof(a4))); |
| 82 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "x0", &a4, sizeof(a4))); |
| 83 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "0xXYZZY", &a4, sizeof(a4))); |
| 84 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "xyzzy", &a4, sizeof(a4))); |
| 85 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET+AF_INET6, "1.2.3.4", &a4, sizeof(a4))); |
| 86 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "257.2.3.4", &a4, sizeof(a4))); |
| 87 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "002.3.4.x", &a4, sizeof(a4))); |
| 88 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "00.3.4.x", &a4, sizeof(a4))); |
| 89 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "2.3.4.x", &a4, sizeof(a4))); |
| 90 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "2.3.4.5.6", &a4, sizeof(a4))); |
| 91 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "2.3.4.5.6/12", &a4, sizeof(a4))); |
| 92 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "2.3.4:5", &a4, sizeof(a4))); |
| 93 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "2.3.4.5/120", &a4, sizeof(a4))); |
| 94 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "2.3.4.5/1x", &a4, sizeof(a4))); |
| 95 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "2.3.4.5/x", &a4, sizeof(a4))); |
| 96 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ff/240", &a6, sizeof(a6))); |
| 97 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ff/02", &a6, sizeof(a6))); |
| 98 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ff/2y", &a6, sizeof(a6))); |
| 99 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ff/y", &a6, sizeof(a6))); |
| 100 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ff/", &a6, sizeof(a6))); |
| 101 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "", &a6, sizeof(a6))); |
| 102 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, ":x", &a6, sizeof(a6))); |
| 103 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, ":", &a6, sizeof(a6))); |
| 104 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, ": :1234", &a6, sizeof(a6))); |
| 105 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "::12345", &a6, sizeof(a6))); |
| 106 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "1234::2345:3456::0011", &a6, sizeof(a6))); |
| 107 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "1234:1234:1234:1234:1234:1234:1234:1234:", &a6, sizeof(a6))); |
| 108 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "1234:1234:1234:1234:1234:1234:1234:1234::", &a6, sizeof(a6))); |
| 109 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "1234:1234:1234:1234:1234:1234:1234:1.2.3.4", &a6, sizeof(a6))); |
| 110 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, ":1234:1234:1234:1234:1234:1234:1234:1234", &a6, sizeof(a6))); |
| 111 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, ":1234:1234:1234:1234:1234:1234:1234:1234:", &a6, sizeof(a6))); |
| 112 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "1234:1234:1234:1234:1234:1234:1234:1234:5678", &a6, sizeof(a6))); |
| 113 | // TODO(drysdale): check whether the next two tests should give -1. |
| 114 | EXPECT_EQ(0, ares_inet_net_pton(AF_INET6, "1234:1234:1234:1234:1234:1234:1234:1234:5678:5678", &a6, sizeof(a6))); |
| 115 | EXPECT_EQ(0, ares_inet_net_pton(AF_INET6, "1234:1234:1234:1234:1234:1234:1234:1234:5678:5678:5678", &a6, sizeof(a6))); |
| 116 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ffff:257.2.3.4", &a6, sizeof(a6))); |
| 117 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ffff:002.2.3.4", &a6, sizeof(a6))); |
| 118 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ffff:1.2.3.4.5.6", &a6, sizeof(a6))); |
| 119 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ffff:1.2.3.4.5", &a6, sizeof(a6))); |
| 120 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ffff:1.2.3.z", &a6, sizeof(a6))); |
| 121 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ffff:1.2.3001.4", &a6, sizeof(a6))); |
| 122 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ffff:1.2.3..4", &a6, sizeof(a6))); |
| 123 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ffff:1.2.3.", &a6, sizeof(a6))); |
| 124 | |
| 125 | // Hex constants are allowed. |
| 126 | EXPECT_EQ(4 * 8, ares_inet_net_pton(AF_INET, "0x01020304", &a4, sizeof(a4))); |
| 127 | expected = htonl(0x01020304); |
| 128 | EXPECT_EQ(expected, a4.s_addr); |
| 129 | EXPECT_EQ(4 * 8, ares_inet_net_pton(AF_INET, "0x0a0b0c0d", &a4, sizeof(a4))); |
| 130 | expected = htonl(0x0a0b0c0d); |
| 131 | EXPECT_EQ(expected, a4.s_addr); |
| 132 | EXPECT_EQ(4 * 8, ares_inet_net_pton(AF_INET, "0x0A0B0C0D", &a4, sizeof(a4))); |
| 133 | expected = htonl(0x0a0b0c0d); |
| 134 | EXPECT_EQ(expected, a4.s_addr); |
| 135 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "0x0xyz", &a4, sizeof(a4))); |
| 136 | EXPECT_EQ(4 * 8, ares_inet_net_pton(AF_INET, "0x1122334", &a4, sizeof(a4))); |
| 137 | expected = htonl(0x11223340); |
| 138 | EXPECT_EQ(expected, a4.s_addr); // huh? |
| 139 | |
| 140 | // No room, no room. |
| 141 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "1.2.3.4", &a4, sizeof(a4) - 1)); |
| 142 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET6, "12:34::ff", &a6, sizeof(a6) - 1)); |
| 143 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "0x01020304", &a4, 2)); |
| 144 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "0x01020304", &a4, 0)); |
| 145 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "0x0a0b0c0d", &a4, 0)); |
| 146 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "0x0xyz", &a4, 0)); |
| 147 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "0x1122334", &a4, sizeof(a4) - 1)); |
| 148 | EXPECT_EQ(-1, ares_inet_net_pton(AF_INET, "253", &a4, sizeof(a4) - 1)); |
| 149 | #endif |
| 150 | |
| 151 | EXPECT_EQ(1, ares_inet_pton(AF_INET, "1.2.3.4", &a4)); |
| 152 | EXPECT_EQ(1, ares_inet_pton(AF_INET6, "12:34::ff", &a6)); |
| 153 | EXPECT_EQ(1, ares_inet_pton(AF_INET6, "12:34::ffff:1.2.3.4", &a6)); |
| 154 | EXPECT_EQ(0, ares_inet_pton(AF_INET, "xyzzy", &a4)); |
| 155 | EXPECT_EQ(-1, ares_inet_pton(AF_INET+AF_INET6, "1.2.3.4", &a4)); |
| 156 | } |
| 157 | |
| 158 | TEST_F(LibraryTest, FreeCorruptData) { |
| 159 | // ares_free_data(p) expects that there is a type field and a marker |
| 160 | // field in the memory before p. Feed it incorrect versions of each. |
| 161 | struct ares_data *data = (struct ares_data *)malloc(sizeof(struct ares_data)); |
| 162 | void* p = &(data->data); |
| 163 | |
| 164 | // Invalid type |
| 165 | data->type = (ares_datatype)99; |
| 166 | data->mark = ARES_DATATYPE_MARK; |
| 167 | ares_free_data(p); |
| 168 | |
| 169 | // Invalid marker |
| 170 | data->type = (ares_datatype)ARES_DATATYPE_MX_REPLY; |
| 171 | data->mark = ARES_DATATYPE_MARK + 1; |
| 172 | ares_free_data(p); |
| 173 | |
| 174 | // Null pointer |
| 175 | ares_free_data(nullptr); |
| 176 | |
| 177 | free(data); |
| 178 | } |
| 179 | |
| 180 | #ifndef CARES_SYMBOL_HIDING |
| 181 | TEST(LibraryInit, StrdupFailures) { |
| 182 | EXPECT_EQ(ARES_SUCCESS, ares_library_init(ARES_LIB_INIT_ALL)); |
| 183 | char* copy = ares_strdup("string"); |
| 184 | EXPECT_NE(nullptr, copy); |
| 185 | free(copy); |
| 186 | ares_library_cleanup(); |
| 187 | } |
| 188 | |
| 189 | TEST_F(LibraryTest, StrdupFailures) { |
| 190 | SetAllocFail(1); |
| 191 | char* copy = ares_strdup("string"); |
| 192 | EXPECT_EQ(nullptr, copy); |
| 193 | } |
| 194 | |
| 195 | TEST_F(LibraryTest, MallocDataFail) { |
| 196 | EXPECT_EQ(nullptr, ares_malloc_data((ares_datatype)99)); |
| 197 | SetAllocSizeFail(sizeof(struct ares_data)); |
| 198 | EXPECT_EQ(nullptr, ares_malloc_data(ARES_DATATYPE_MX_REPLY)); |
| 199 | } |
| 200 | |
| 201 | TEST(Misc, Bitncmp) { |
| 202 | byte a[4] = {0x80, 0x01, 0x02, 0x03}; |
| 203 | byte b[4] = {0x80, 0x01, 0x02, 0x04}; |
| 204 | byte c[4] = {0x01, 0xFF, 0x80, 0x02}; |
| 205 | EXPECT_GT(0, ares__bitncmp(a, b, sizeof(a)*8)); |
| 206 | EXPECT_LT(0, ares__bitncmp(b, a, sizeof(a)*8)); |
| 207 | EXPECT_EQ(0, ares__bitncmp(a, a, sizeof(a)*8)); |
| 208 | |
| 209 | for (int ii = 1; ii < (3*8+5); ii++) { |
| 210 | EXPECT_EQ(0, ares__bitncmp(a, b, ii)); |
| 211 | EXPECT_EQ(0, ares__bitncmp(b, a, ii)); |
| 212 | EXPECT_LT(0, ares__bitncmp(a, c, ii)); |
| 213 | EXPECT_GT(0, ares__bitncmp(c, a, ii)); |
| 214 | } |
| 215 | |
| 216 | // Last byte differs at 5th bit |
| 217 | EXPECT_EQ(0, ares__bitncmp(a, b, 3*8 + 3)); |
| 218 | EXPECT_EQ(0, ares__bitncmp(a, b, 3*8 + 4)); |
| 219 | EXPECT_EQ(0, ares__bitncmp(a, b, 3*8 + 5)); |
| 220 | EXPECT_GT(0, ares__bitncmp(a, b, 3*8 + 6)); |
| 221 | EXPECT_GT(0, ares__bitncmp(a, b, 3*8 + 7)); |
| 222 | } |
| 223 | |
| 224 | TEST_F(LibraryTest, Casts) { |
| 225 | ssize_t ssz = 100; |
| 226 | unsigned int u = 100; |
| 227 | int i = 100; |
| 228 | long l = 100; |
| 229 | |
| 230 | unsigned int ru = aresx_sztoui(ssz); |
| 231 | EXPECT_EQ(u, ru); |
| 232 | int ri = aresx_sztosi(ssz); |
| 233 | EXPECT_EQ(i, ri); |
| 234 | |
| 235 | ri = aresx_sltosi(l); |
| 236 | EXPECT_EQ(l, (long)ri); |
| 237 | } |
| 238 | |
| 239 | TEST_F(LibraryTest, ReadLine) { |
| 240 | TempFile temp("abcde\n0123456789\nXYZ\n012345678901234567890\n\n"); |
| 241 | FILE *fp = fopen(temp.filename(), "r"); |
| 242 | size_t bufsize = 4; |
| 243 | char *buf = (char *)malloc(bufsize); |
| 244 | |
| 245 | EXPECT_EQ(ARES_SUCCESS, ares__read_line(fp, &buf, &bufsize)); |
| 246 | EXPECT_EQ("abcde", std::string(buf)); |
| 247 | EXPECT_EQ(ARES_SUCCESS, ares__read_line(fp, &buf, &bufsize)); |
| 248 | EXPECT_EQ("0123456789", std::string(buf)); |
| 249 | EXPECT_EQ(ARES_SUCCESS, ares__read_line(fp, &buf, &bufsize)); |
| 250 | EXPECT_EQ("XYZ", std::string(buf)); |
| 251 | SetAllocFail(1); |
| 252 | EXPECT_EQ(ARES_ENOMEM, ares__read_line(fp, &buf, &bufsize)); |
| 253 | EXPECT_EQ(nullptr, buf); |
| 254 | |
| 255 | fclose(fp); |
| 256 | free(buf); |
| 257 | } |
| 258 | |
| 259 | TEST_F(LibraryTest, ReadLineNoBuf) { |
| 260 | TempFile temp("abcde\n0123456789\nXYZ\n012345678901234567890"); |
| 261 | FILE *fp = fopen(temp.filename(), "r"); |
| 262 | size_t bufsize = 0; |
| 263 | char *buf = nullptr; |
| 264 | |
| 265 | SetAllocFail(1); |
| 266 | EXPECT_EQ(ARES_ENOMEM, ares__read_line(fp, &buf, &bufsize)); |
| 267 | |
| 268 | EXPECT_EQ(ARES_SUCCESS, ares__read_line(fp, &buf, &bufsize)); |
| 269 | EXPECT_EQ("abcde", std::string(buf)); |
| 270 | EXPECT_EQ(ARES_SUCCESS, ares__read_line(fp, &buf, &bufsize)); |
| 271 | EXPECT_EQ("0123456789", std::string(buf)); |
| 272 | EXPECT_EQ(ARES_SUCCESS, ares__read_line(fp, &buf, &bufsize)); |
| 273 | EXPECT_EQ("XYZ", std::string(buf)); |
| 274 | EXPECT_EQ(ARES_SUCCESS, ares__read_line(fp, &buf, &bufsize)); |
| 275 | EXPECT_EQ("012345678901234567890", std::string(buf)); |
| 276 | |
| 277 | fclose(fp); |
| 278 | free(buf); |
| 279 | } |
| 280 | |
| 281 | TEST(Misc, GetHostent) { |
| 282 | TempFile hostsfile("1.2.3.4 example.com \n" |
| 283 | " 2.3.4.5\tgoogle.com www.google.com\twww2.google.com\n" |
| 284 | "#comment\n" |
| 285 | "4.5.6.7\n" |
| 286 | "1.3.5.7 \n" |
| 287 | "::1 ipv6.com"); |
| 288 | struct hostent *host = nullptr; |
| 289 | FILE *fp = fopen(hostsfile.filename(), "r"); |
| 290 | ASSERT_NE(nullptr, fp); |
| 291 | EXPECT_EQ(ARES_EBADFAMILY, ares__get_hostent(fp, AF_INET+AF_INET6, &host)); |
| 292 | rewind(fp); |
| 293 | |
| 294 | EXPECT_EQ(ARES_SUCCESS, ares__get_hostent(fp, AF_INET, &host)); |
| 295 | ASSERT_NE(nullptr, host); |
| 296 | std::stringstream ss1; |
| 297 | ss1 << HostEnt(host); |
| 298 | EXPECT_EQ("{'example.com' aliases=[] addrs=[1.2.3.4]}", ss1.str()); |
| 299 | ares_free_hostent(host); |
| 300 | host = nullptr; |
| 301 | |
| 302 | EXPECT_EQ(ARES_SUCCESS, ares__get_hostent(fp, AF_INET, &host)); |
| 303 | ASSERT_NE(nullptr, host); |
| 304 | std::stringstream ss2; |
| 305 | ss2 << HostEnt(host); |
| 306 | EXPECT_EQ("{'google.com' aliases=[www.google.com, www2.google.com] addrs=[2.3.4.5]}", ss2.str()); |
| 307 | ares_free_hostent(host); |
| 308 | host = nullptr; |
| 309 | |
| 310 | EXPECT_EQ(ARES_EOF, ares__get_hostent(fp, AF_INET, &host)); |
| 311 | |
| 312 | rewind(fp); |
| 313 | EXPECT_EQ(ARES_SUCCESS, ares__get_hostent(fp, AF_INET6, &host)); |
| 314 | ASSERT_NE(nullptr, host); |
| 315 | std::stringstream ss3; |
| 316 | ss3 << HostEnt(host); |
| 317 | EXPECT_EQ("{'ipv6.com' aliases=[] addrs=[0000:0000:0000:0000:0000:0000:0000:0001]}", ss3.str()); |
| 318 | ares_free_hostent(host); |
| 319 | host = nullptr; |
| 320 | EXPECT_EQ(ARES_EOF, ares__get_hostent(fp, AF_INET6, &host)); |
| 321 | fclose(fp); |
| 322 | } |
| 323 | |
| 324 | TEST_F(LibraryTest, GetHostentAllocFail) { |
| 325 | TempFile hostsfile("1.2.3.4 example.com alias1 alias2\n"); |
| 326 | struct hostent *host = nullptr; |
| 327 | FILE *fp = fopen(hostsfile.filename(), "r"); |
| 328 | ASSERT_NE(nullptr, fp); |
| 329 | |
| 330 | for (int ii = 1; ii <= 8; ii++) { |
| 331 | rewind(fp); |
| 332 | ClearFails(); |
| 333 | SetAllocFail(ii); |
| 334 | host = nullptr; |
| 335 | EXPECT_EQ(ARES_ENOMEM, ares__get_hostent(fp, AF_INET, &host)) << ii; |
| 336 | } |
| 337 | fclose(fp); |
| 338 | } |
| 339 | #endif |
| 340 | |
| 341 | #ifdef CARES_EXPOSE_STATICS |
| 342 | // These tests access internal static functions from the library, which |
| 343 | // are only exposed when CARES_EXPOSE_STATICS has been configured. As such |
| 344 | // they are tightly couple to the internal library implementation details. |
| 345 | extern "C" char *ares_striendstr(const char*, const char*); |
| 346 | TEST_F(LibraryTest, Striendstr) { |
| 347 | EXPECT_EQ(nullptr, ares_striendstr("abc", "12345")); |
| 348 | EXPECT_NE(nullptr, ares_striendstr("abc12345", "12345")); |
| 349 | EXPECT_NE(nullptr, ares_striendstr("abcxyzzy", "XYZZY")); |
| 350 | EXPECT_NE(nullptr, ares_striendstr("xyzzy", "XYZZY")); |
| 351 | EXPECT_EQ(nullptr, ares_striendstr("xyxzy", "XYZZY")); |
| 352 | EXPECT_NE(nullptr, ares_striendstr("", "")); |
| 353 | const char *str = "plugh"; |
| 354 | EXPECT_NE(nullptr, ares_striendstr(str, str)); |
| 355 | } |
| 356 | extern "C" int single_domain(ares_channel, const char*, char**); |
| 357 | TEST_F(DefaultChannelTest, SingleDomain) { |
| 358 | TempFile aliases("www www.google.com\n"); |
| 359 | EnvValue with_env("HOSTALIASES", aliases.filename()); |
| 360 | |
| 361 | SetAllocSizeFail(128); |
| 362 | char *ptr = nullptr; |
| 363 | EXPECT_EQ(ARES_ENOMEM, single_domain(channel_, "www", &ptr)); |
| 364 | |
| 365 | channel_->flags |= ARES_FLAG_NOSEARCH|ARES_FLAG_NOALIASES; |
| 366 | EXPECT_EQ(ARES_SUCCESS, single_domain(channel_, "www", &ptr)); |
| 367 | EXPECT_EQ("www", std::string(ptr)); |
| 368 | free(ptr); |
| 369 | ptr = nullptr; |
| 370 | |
| 371 | SetAllocFail(1); |
| 372 | EXPECT_EQ(ARES_ENOMEM, single_domain(channel_, "www", &ptr)); |
| 373 | EXPECT_EQ(nullptr, ptr); |
| 374 | } |
| 375 | #endif |
| 376 | |
| 377 | TEST_F(DefaultChannelTest, SaveInvalidChannel) { |
| 378 | int saved = channel_->nservers; |
| 379 | channel_->nservers = -1; |
| 380 | struct ares_options opts; |
| 381 | int optmask = 0; |
| 382 | EXPECT_EQ(ARES_ENODATA, ares_save_options(channel_, &opts, &optmask)); |
| 383 | channel_->nservers = saved; |
| 384 | } |
| 385 | |
| 386 | } // namespace test |
| 387 | } // namespace ares |