tst-network.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Test for inet_network.
  2. Copyright (C) 2000, 2002 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Andreas Jaeger <aj@suse.de>, 2000.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <arpa/inet.h>
  20. struct
  21. {
  22. const char *network;
  23. uint32_t number;
  24. } tests [] =
  25. {
  26. {"1.0.0.0", 0x1000000},
  27. {"1.0.0", 0x10000},
  28. {"1.0", 0x100},
  29. {"1", 0x1},
  30. {"192.168.0.0", 0xC0A80000},
  31. /* Now some invalid addresses. */
  32. {"141.30.225.2800", INADDR_NONE},
  33. {"141.76.1.1.1", INADDR_NONE},
  34. {"141.76.1.11.", INADDR_NONE},
  35. {"1410", INADDR_NONE},
  36. {"1.1410", INADDR_NONE},
  37. {"1.1410.", INADDR_NONE},
  38. {"1.1410", INADDR_NONE},
  39. {"141.76.1111", INADDR_NONE},
  40. {"141.76.1111.", INADDR_NONE},
  41. {"1.1.1.257", INADDR_NONE},
  42. /* Now some from BSD */
  43. {"0x12", 0x00000012},
  44. {"127.1", 0x00007f01},
  45. {"127.1.2.3", 0x7f010203},
  46. {"0x123456", INADDR_NONE},
  47. {"0x12.0x34", 0x00001234},
  48. {"0x12.0x345", INADDR_NONE},
  49. {"1.2.3.4.5", INADDR_NONE},
  50. {"1..3.4", INADDR_NONE},
  51. {".", INADDR_NONE},
  52. {"1.", INADDR_NONE},
  53. {".1", INADDR_NONE},
  54. {"x", INADDR_NONE},
  55. {"0x", INADDR_NONE},
  56. {"0", 0x00000000},
  57. {"0x0", 0x00000000},
  58. {"01.02.07.077", 0x0102073f},
  59. {"0x1.23.045.0", 0x01172500},
  60. {"", INADDR_NONE},
  61. {" ", INADDR_NONE},
  62. {"bar", INADDR_NONE},
  63. {"1.2bar", INADDR_NONE},
  64. {"1.", INADDR_NONE},
  65. {"ÊÃÕËÅÎ", INADDR_NONE},
  66. {"255.255.255.255", INADDR_NONE},
  67. {"x", INADDR_NONE},
  68. {"0X12", 0x00000012},
  69. {"078", INADDR_NONE},
  70. {"1 bar", INADDR_NONE},
  71. {"127.0xfff", INADDR_NONE},
  72. };
  73. int
  74. main (void)
  75. {
  76. int errors = 0;
  77. size_t i;
  78. uint32_t res;
  79. for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
  80. {
  81. printf ("Testing: %s\n", tests[i].network);
  82. res = inet_network (tests[i].network);
  83. if (res != tests[i].number)
  84. {
  85. ++errors;
  86. printf ("Test failed for inet_network (\"%s\"):\n",
  87. tests[i].network);
  88. printf ("Expected return value %u (0x%x) but got %u (0x%x).\n",
  89. tests[i].number, tests[i].number, res, res);
  90. }
  91. }
  92. return errors != 0;
  93. }