tst-aton.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Test legacy IPv4 text-to-address function inet_aton.
  2. Copyright (C) 1998-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 <array_length.h>
  16. #include <stdio.h>
  17. #include <stdint.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. static const struct tests
  22. {
  23. const char *input;
  24. int valid;
  25. uint32_t result;
  26. } tests[] =
  27. {
  28. { "", 0, 0 },
  29. { "-1", 0, 0 },
  30. { "256", 1, 0x00000100 },
  31. { "256.", 0, 0 },
  32. { "255a", 0, 0 },
  33. { "256a", 0, 0 },
  34. { "0x100", 1, 0x00000100 },
  35. { "0200.0x123456", 1, 0x80123456 },
  36. { "0300.0x89123456.", 0 ,0 },
  37. { "0100.-0xffff0000", 0, 0 },
  38. { "0.0xffffff", 1, 0x00ffffff },
  39. { "0.0x1000000", 0, 0 },
  40. { "0377.16777215", 1, 0xffffffff },
  41. { "0377.16777216", 0, 0 },
  42. { "0x87.077777777", 1, 0x87ffffff },
  43. { "0x87.0100000000", 0, 0 },
  44. { "0.1.3", 1, 0x00010003 },
  45. { "0.256.3", 0, 0 },
  46. { "256.1.3", 0, 0 },
  47. { "0.1.0x10000", 0, 0 },
  48. { "0.1.0xffff", 1, 0x0001ffff },
  49. { "0.1a.3", 0, 0 },
  50. { "0.1.a3", 0, 0 },
  51. { "1.2.3.4", 1, 0x01020304 },
  52. { "0400.2.3.4", 0, 0 },
  53. { "1.0x100.3.4", 0, 0 },
  54. { "1.2.256.4", 0, 0 },
  55. { "1.2.3.0x100", 0, 0 },
  56. { "323543357756889", 0, 0 },
  57. { "10.1.2.3.4", 0, 0 },
  58. { "192.0.2.1", 1, 0xc0000201 },
  59. { "192.0.2.2\nX", 1, 0xc0000202 },
  60. { "192.0.2.3 Y", 1, 0xc0000203 },
  61. { "192.0.2.3Z", 0, 0 },
  62. { "192.000.002.010", 1, 0xc0000208 },
  63. };
  64. static int
  65. do_test (void)
  66. {
  67. int result = 0;
  68. size_t cnt;
  69. for (cnt = 0; cnt < array_length (tests); ++cnt)
  70. {
  71. struct in_addr addr;
  72. if ((int) inet_aton (tests[cnt].input, &addr) != tests[cnt].valid)
  73. {
  74. if (tests[cnt].valid)
  75. printf ("\"%s\" not seen as valid IP address\n", tests[cnt].input);
  76. else
  77. printf ("\"%s\" seen as valid IP address\n", tests[cnt].input);
  78. result = 1;
  79. }
  80. else if (tests[cnt].valid && addr.s_addr != ntohl (tests[cnt].result))
  81. {
  82. printf ("\"%s\" not converted correctly: is %08x, should be %08x\n",
  83. tests[cnt].input, addr.s_addr, tests[cnt].result);
  84. result = 1;
  85. }
  86. }
  87. return result;
  88. }
  89. #include <support/test-driver.c>