tst-inet_pton.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* Test inet_pton functions.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <arpa/inet.h>
  16. #include <resolv/resolv-internal.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <support/check.h>
  21. #include <support/next_to_fault.h>
  22. #include <support/xunistd.h>
  23. #include <unistd.h>
  24. struct test_case
  25. {
  26. /* The input data. */
  27. const char *input;
  28. /* True if AF_INET parses successfully. */
  29. bool ipv4_ok;
  30. /* True if AF_INET6 parses successfully. */
  31. bool ipv6_ok;
  32. /* Expected result for AF_INET. */
  33. unsigned char ipv4_expected[4];
  34. /* Expected result for AF_INET6. */
  35. unsigned char ipv6_expected[16];
  36. };
  37. static void
  38. check_result (const char *what, const struct test_case *t, int family,
  39. void *result_buffer, int inet_ret)
  40. {
  41. TEST_VERIFY_EXIT (inet_ret >= -1);
  42. TEST_VERIFY_EXIT (inet_ret <= 1);
  43. int ok;
  44. const unsigned char *expected;
  45. size_t result_size;
  46. switch (family)
  47. {
  48. case AF_INET:
  49. ok = t->ipv4_ok;
  50. expected = t->ipv4_expected;
  51. result_size = 4;
  52. break;
  53. case AF_INET6:
  54. ok = t->ipv6_ok;
  55. expected = t->ipv6_expected;
  56. result_size = 16;
  57. break;
  58. default:
  59. FAIL_EXIT1 ("invalid address family %d", family);
  60. }
  61. if (inet_ret != ok)
  62. {
  63. support_record_failure ();
  64. printf ("error: %s return value mismatch for [[%s]], family %d\n"
  65. " expected: %d\n"
  66. " actual: %d\n",
  67. what, t->input, family, ok, inet_ret);
  68. return;
  69. }
  70. if (memcmp (result_buffer, expected, result_size) != 0)
  71. {
  72. support_record_failure ();
  73. printf ("error: %s result mismatch for [[%s]], family %d\n",
  74. what, t->input, family);
  75. }
  76. }
  77. static void
  78. run_one_test (const struct test_case *t)
  79. {
  80. size_t test_len = strlen (t->input);
  81. struct support_next_to_fault ntf_out4 = support_next_to_fault_allocate (4);
  82. struct support_next_to_fault ntf_out6 = support_next_to_fault_allocate (16);
  83. /* inet_pton requires NUL termination. */
  84. {
  85. struct support_next_to_fault ntf_in
  86. = support_next_to_fault_allocate (test_len + 1);
  87. memcpy (ntf_in.buffer, t->input, test_len + 1);
  88. memset (ntf_out4.buffer, 0, 4);
  89. check_result ("inet_pton", t, AF_INET, ntf_out4.buffer,
  90. inet_pton (AF_INET, ntf_in.buffer, ntf_out4.buffer));
  91. memset (ntf_out6.buffer, 0, 16);
  92. check_result ("inet_pton", t, AF_INET6, ntf_out6.buffer,
  93. inet_pton (AF_INET6, ntf_in.buffer, ntf_out6.buffer));
  94. support_next_to_fault_free (&ntf_in);
  95. }
  96. /* __inet_pton_length does not require NUL termination. */
  97. {
  98. struct support_next_to_fault ntf_in
  99. = support_next_to_fault_allocate (test_len);
  100. memcpy (ntf_in.buffer, t->input, test_len);
  101. memset (ntf_out4.buffer, 0, 4);
  102. check_result ("__inet_pton_length", t, AF_INET, ntf_out4.buffer,
  103. __inet_pton_length (AF_INET, ntf_in.buffer, ntf_in.length,
  104. ntf_out4.buffer));
  105. memset (ntf_out6.buffer, 0, 16);
  106. check_result ("__inet_pton_length", t, AF_INET6, ntf_out6.buffer,
  107. __inet_pton_length (AF_INET6, ntf_in.buffer, ntf_in.length,
  108. ntf_out6.buffer));
  109. support_next_to_fault_free (&ntf_in);
  110. }
  111. support_next_to_fault_free (&ntf_out4);
  112. support_next_to_fault_free (&ntf_out6);
  113. }
  114. /* The test cases were manually crafted and the set enhanced with
  115. American Fuzzy Lop. */
  116. const struct test_case test_cases[] =
  117. {
  118. {.input = ".:", },
  119. {.input = "0.0.0.0",
  120. .ipv4_ok = true,
  121. .ipv4_expected = {0, 0, 0, 0},
  122. },
  123. {.input = "0.:", },
  124. {.input = "00", },
  125. {.input = "0000000", },
  126. {.input = "00000000000000000", },
  127. {.input = "092.", },
  128. {.input = "10.0.301.2", },
  129. {.input = "127.0.0.1",
  130. .ipv4_ok = true,
  131. .ipv4_expected = {127, 0, 0, 1},
  132. },
  133. {.input = "19..", },
  134. {.input = "192.0.2.-1", },
  135. {.input = "192.0.2.01", },
  136. {.input = "192.0.2.1.", },
  137. {.input = "192.0.2.1192.", },
  138. {.input = "192.0.2.192.\377..", },
  139. {.input = "192.0.2.256", },
  140. {.input = "192.0.2.27",
  141. .ipv4_ok = true,
  142. .ipv4_expected = {192, 0, 2, 27},
  143. },
  144. {.input = "192.0.201.", },
  145. {.input = "192.0.261.", },
  146. {.input = "192.0.2\256", },
  147. {.input = "192.0.\262.", },
  148. {.input = "192.062.", },
  149. {.input = "192.092.\256", },
  150. {.input = "192.0\2562.", },
  151. {.input = "192.192.0.2661\031", },
  152. {.input = "192.192.00n2.1.", },
  153. {.input = "192.192.2.190.", },
  154. {.input = "192.255.255.2555", },
  155. {.input = "192.92.219\023.", },
  156. {.input = "192.\260.2.", },
  157. {.input = "1:1::1:1",
  158. .ipv6_ok = true,
  159. .ipv6_expected = {
  160. 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0,
  161. 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
  162. },
  163. },
  164. {.input = "2", },
  165. {.input = "2.", },
  166. {.input = "2001:db8:00001::f", },
  167. {.input = "2001:db8:10000::f", },
  168. {.input = "2001:db8:1234:5678:abcd:ef01:2345:67",
  169. .ipv6_ok = true,
  170. .ipv6_expected = {
  171. 0x20, 0x1, 0xd, 0xb8, 0x12, 0x34, 0x56, 0x78,
  172. 0xab, 0xcd, 0xef, 0x1, 0x23, 0x45, 0x0, 0x67
  173. },
  174. },
  175. {.input = "2001:db8:1234:5678:abcd:ef01:2345:6789:1", },
  176. {.input = "2001:db8:1234:5678:abcd:ef01:2345::6789", },
  177. {.input = "2001:db8::0",
  178. .ipv6_ok = true,
  179. .ipv6_expected = {
  180. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  181. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
  182. },
  183. },
  184. {.input = "2001:db8::00",
  185. .ipv6_ok = true,
  186. .ipv6_expected = {
  187. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  188. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
  189. },
  190. },
  191. {.input = "2001:db8::1",
  192. .ipv6_ok = true,
  193. .ipv6_expected = {
  194. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  195. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
  196. },
  197. },
  198. {.input = "2001:db8::10",
  199. .ipv6_ok = true,
  200. .ipv6_expected = {
  201. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  202. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10
  203. },
  204. },
  205. {.input = "2001:db8::19",
  206. .ipv6_ok = true,
  207. .ipv6_expected = {
  208. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  209. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x19
  210. },
  211. },
  212. {.input = "2001:db8::1::\012", },
  213. {.input = "2001:db8::1::2\012", },
  214. {.input = "2001:db8::2",
  215. .ipv6_ok = true,
  216. .ipv6_expected = {
  217. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  218. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2
  219. },
  220. },
  221. {.input = "2001:db8::3",
  222. .ipv6_ok = true,
  223. .ipv6_expected = {
  224. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  225. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3
  226. },
  227. },
  228. {.input = "2001:db8::4",
  229. .ipv6_ok = true,
  230. .ipv6_expected = {
  231. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  232. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4
  233. },
  234. },
  235. {.input = "2001:db8::5",
  236. .ipv6_ok = true,
  237. .ipv6_expected = {
  238. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  239. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5
  240. },
  241. },
  242. {.input = "2001:db8::6",
  243. .ipv6_ok = true,
  244. .ipv6_expected = {
  245. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  246. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6
  247. },
  248. },
  249. {.input = "2001:db8::7",
  250. .ipv6_ok = true,
  251. .ipv6_expected = {
  252. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  253. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7
  254. },
  255. },
  256. {.input = "2001:db8::8",
  257. .ipv6_ok = true,
  258. .ipv6_expected = {
  259. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  260. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8
  261. },
  262. },
  263. {.input = "2001:db8::9",
  264. .ipv6_ok = true,
  265. .ipv6_expected = {
  266. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  267. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9
  268. },
  269. },
  270. {.input = "2001:db8::A",
  271. .ipv6_ok = true,
  272. .ipv6_expected = {
  273. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  274. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
  275. },
  276. },
  277. {.input = "2001:db8::B",
  278. .ipv6_ok = true,
  279. .ipv6_expected = {
  280. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  281. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
  282. },
  283. },
  284. {.input = "2001:db8::C",
  285. .ipv6_ok = true,
  286. .ipv6_expected = {
  287. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  288. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
  289. },
  290. },
  291. {.input = "2001:db8::D",
  292. .ipv6_ok = true,
  293. .ipv6_expected = {
  294. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  295. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
  296. },
  297. },
  298. {.input = "2001:db8::E",
  299. .ipv6_ok = true,
  300. .ipv6_expected = {
  301. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  302. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
  303. },
  304. },
  305. {.input = "2001:db8::F",
  306. .ipv6_ok = true,
  307. .ipv6_expected = {
  308. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  309. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
  310. },
  311. },
  312. {.input = "2001:db8::a",
  313. .ipv6_ok = true,
  314. .ipv6_expected = {
  315. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  316. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
  317. },
  318. },
  319. {.input = "2001:db8::b",
  320. .ipv6_ok = true,
  321. .ipv6_expected = {
  322. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  323. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
  324. },
  325. },
  326. {.input = "2001:db8::c",
  327. .ipv6_ok = true,
  328. .ipv6_expected = {
  329. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  330. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
  331. },
  332. },
  333. {.input = "2001:db8::d",
  334. .ipv6_ok = true,
  335. .ipv6_expected = {
  336. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  337. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
  338. },
  339. },
  340. {.input = "2001:db8::e",
  341. .ipv6_ok = true,
  342. .ipv6_expected = {
  343. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  344. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
  345. },
  346. },
  347. {.input = "2001:db8::f",
  348. .ipv6_ok = true,
  349. .ipv6_expected = {
  350. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  351. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
  352. },
  353. },
  354. {.input = "2001:db8::ff",
  355. .ipv6_ok = true,
  356. .ipv6_expected = {
  357. 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
  358. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff
  359. },
  360. },
  361. {.input = "2001:db8::ffff:2\012", },
  362. {.input = "22", },
  363. {.input = "2222@", },
  364. {.input = "255.255.255.255",
  365. .ipv4_ok = true,
  366. .ipv4_expected = {255, 255, 255, 255},
  367. },
  368. {.input = "255.255.255.255\001", },
  369. {.input = "255.255.255.25555", },
  370. {.input = "2:", },
  371. {.input = "2:a:8:EEEE::EEEE:F:EEE8:EEEE\034*:", },
  372. {.input = "2:ff:1:1:7:ff:1:1:7.", },
  373. {.input = "2f:0000000000000000000000000000000000000000000000000000000000"
  374. "0000000000000000000000000000000000000000000000000000000000000000000000"
  375. "0G01",
  376. },
  377. {.input = "429495", },
  378. {.input = "5::5::", },
  379. {.input = "6.6.", },
  380. {.input = "992.", },
  381. {.input = "::",
  382. .ipv6_ok = true,
  383. .ipv6_expected = {
  384. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  385. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
  386. },
  387. },
  388. {.input = "::00001", },
  389. {.input = "::1",
  390. .ipv6_ok = true,
  391. .ipv6_expected = {
  392. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  393. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
  394. },
  395. },
  396. {.input = "::10000", },
  397. {.input = "::1:1",
  398. .ipv6_ok = true,
  399. .ipv6_expected = {
  400. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  401. 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
  402. },
  403. },
  404. {.input = "::ff:1:1:7.0.0.1",
  405. .ipv6_ok = true,
  406. .ipv6_expected = {
  407. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff,
  408. 0x0, 0x1, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
  409. },
  410. },
  411. {.input = "::ff:1:1:7:ff:1:1:7.", },
  412. {.input = "::ff:1:1:7ff:1:8:7.0.0.1", },
  413. {.input = "::ff:1:1:7ff:1:8f:1:1:71", },
  414. {.input = "::ffff:02fff:127.0.S1", },
  415. {.input = "::ffff:127.0.0.1",
  416. .ipv6_ok = true,
  417. .ipv6_expected = {
  418. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  419. 0x0, 0x0, 0xff, 0xff, 0x7f, 0x0, 0x0, 0x1
  420. },
  421. },
  422. {.input = "::ffff:1:7.0.0.1",
  423. .ipv6_ok = true,
  424. .ipv6_expected = {
  425. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  426. 0xff, 0xff, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
  427. },
  428. },
  429. {.input = ":\272", },
  430. {.input = "A:f:ff:1:1:D:ff:1:1::7.", },
  431. {.input = "AAAAA.", },
  432. {.input = "D:::", },
  433. {.input = "DF8F", },
  434. {.input = "F::",
  435. .ipv6_ok = true,
  436. .ipv6_expected = {
  437. 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  438. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
  439. },
  440. },
  441. {.input = "F:A:8:EEEE:8:EEEE\034*:", },
  442. {.input = "F:a:8:EEEE:8:EEEE\034*:", },
  443. {.input = "F:ff:100:7ff:1:8:7.0.10.1",
  444. .ipv6_ok = true,
  445. .ipv6_expected = {
  446. 0x0, 0xf, 0x0, 0xff, 0x1, 0x0, 0x7, 0xff,
  447. 0x0, 0x1, 0x0, 0x8, 0x7, 0x0, 0xa, 0x1
  448. },
  449. },
  450. {.input = "d92.", },
  451. {.input = "ff:00000000000000000000000000000000000000000000000000000000000"
  452. "00000000000000000000000000000000000000000000000000000000000000000001",
  453. },
  454. {.input = "fff2:2::ff2:2:f7",
  455. .ipv6_ok = true,
  456. .ipv6_expected = {
  457. 0xff, 0xf2, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0,
  458. 0x0, 0x0, 0xf, 0xf2, 0x0, 0x2, 0x0, 0xf7
  459. },
  460. },
  461. {.input = "ffff:ff:ff:fff:ff:ff:ff:", },
  462. {.input = "\272:", },
  463. {NULL}
  464. };
  465. static int
  466. do_test (void)
  467. {
  468. for (size_t i = 0; test_cases[i].input != NULL; ++i)
  469. run_one_test (test_cases + i);
  470. return 0;
  471. }
  472. #include <support/test-driver.c>