tst-network.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. };
  43. int
  44. main (void)
  45. {
  46. int errors = 0;
  47. size_t i;
  48. uint32_t res;
  49. for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
  50. {
  51. printf ("Testing: %s\n", tests[i].network);
  52. res = inet_network (tests[i].network);
  53. if (res != tests[i].number)
  54. {
  55. printf ("Test failed for inet_network (\"%s\"):\n",
  56. tests[i].network);
  57. printf ("Expected return value %u (0x%x) but got %u (0x%x).\n",
  58. tests[i].number, tests[i].number, res, res);
  59. }
  60. }
  61. return errors != 0;
  62. }