tst-network.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <stdio.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. struct
  22. {
  23. const char *network;
  24. uint32_t number;
  25. } tests [] =
  26. {
  27. {"1.0.0.0", 0x1000000},
  28. {"1.0.0", 0x10000},
  29. {"1.0", 0x100},
  30. {"1", 0x1},
  31. {"192.168.0.0", 0xC0A80000},
  32. /* Now some invalid addresses. */
  33. {"141.30.225.2800", INADDR_NONE},
  34. {"141.76.1.1.1", INADDR_NONE},
  35. {"141.76.1.11.", INADDR_NONE},
  36. {"1410", INADDR_NONE},
  37. {"1.1410", INADDR_NONE},
  38. {"1.1410.", INADDR_NONE},
  39. {"1.1410", INADDR_NONE},
  40. {"141.76.1111", INADDR_NONE},
  41. {"141.76.1111.", INADDR_NONE},
  42. {"1.1.1.257", INADDR_NONE},
  43. /* Now some from BSD */
  44. {"0x12", 0x00000012},
  45. {"127.1", 0x00007f01},
  46. {"127.1.2.3", 0x7f010203},
  47. {"0x123456", INADDR_NONE},
  48. {"0x12.0x34", 0x00001234},
  49. {"0x12.0x345", INADDR_NONE},
  50. {"1.2.3.4.5", INADDR_NONE},
  51. {"1..3.4", INADDR_NONE},
  52. {".", INADDR_NONE},
  53. {"1.", INADDR_NONE},
  54. {".1", INADDR_NONE},
  55. {"x", INADDR_NONE},
  56. {"0x", INADDR_NONE},
  57. {"0", 0x00000000},
  58. {"0x0", 0x00000000},
  59. {"01.02.07.077", 0x0102073f},
  60. {"0x1.23.045.0", 0x01172500},
  61. {"", INADDR_NONE},
  62. {" ", INADDR_NONE},
  63. {"bar", INADDR_NONE},
  64. {"1.2bar", INADDR_NONE},
  65. {"1.", INADDR_NONE},
  66. {"ÊÃÕËÅÎ", INADDR_NONE},
  67. {"255.255.255.255", INADDR_NONE},
  68. {"x", INADDR_NONE},
  69. {"0X12", 0x00000012},
  70. {"078", INADDR_NONE},
  71. {"1 bar", INADDR_NONE},
  72. {"127.0xfff", INADDR_NONE},
  73. };
  74. int
  75. main (void)
  76. {
  77. int errors = 0;
  78. size_t i;
  79. uint32_t res;
  80. for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
  81. {
  82. printf ("Testing: %s\n", tests[i].network);
  83. res = inet_network (tests[i].network);
  84. if (res != tests[i].number)
  85. {
  86. ++errors;
  87. printf ("Test failed for inet_network (\"%s\"):\n",
  88. tests[i].network);
  89. printf ("Expected return value %u (0x%x) but got %u (0x%x).\n",
  90. tests[i].number, tests[i].number, res, res);
  91. }
  92. }
  93. return errors != 0;
  94. }