tst-inet_ntop.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <arpa/inet.h>
  2. #include <errno.h>
  3. #include <netinet/in.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. static int
  7. do_test (void)
  8. {
  9. struct in_addr addr4;
  10. struct in6_addr addr6;
  11. char buf[64];
  12. int result = 0;
  13. addr4.s_addr = 0xe0e0e0e0;
  14. addr6.s6_addr16[0] = 0;
  15. addr6.s6_addr16[1] = 0;
  16. addr6.s6_addr16[2] = 0;
  17. addr6.s6_addr16[3] = 0;
  18. addr6.s6_addr16[4] = 0;
  19. addr6.s6_addr16[5] = 0xffff;
  20. addr6.s6_addr32[3] = 0xe0e0e0e0;
  21. memset (buf, 'x', sizeof buf);
  22. if (inet_ntop (AF_INET, &addr4, buf, 15) != NULL)
  23. {
  24. puts ("1st inet_ntop returned non-NULL");
  25. result++;
  26. }
  27. else if (errno != ENOSPC)
  28. {
  29. puts ("1st inet_ntop didn't fail with ENOSPC");
  30. result++;
  31. }
  32. if (buf[15] != 'x')
  33. {
  34. puts ("1st inet_ntop wrote past the end of buffer");
  35. result++;
  36. }
  37. if (inet_ntop (AF_INET, &addr4, buf, 16) != buf)
  38. {
  39. puts ("2nd inet_ntop did not return buf");
  40. result++;
  41. }
  42. if (memcmp (buf, "224.224.224.224\0" "xxxxxxxx", 24) != 0)
  43. {
  44. puts ("2nd inet_ntop wrote past the end of buffer");
  45. result++;
  46. }
  47. if (inet_ntop (AF_INET6, &addr6, buf, 22) != NULL)
  48. {
  49. puts ("3rd inet_ntop returned non-NULL");
  50. result++;
  51. }
  52. else if (errno != ENOSPC)
  53. {
  54. puts ("3rd inet_ntop didn't fail with ENOSPC");
  55. result++;
  56. }
  57. if (buf[22] != 'x')
  58. {
  59. puts ("3rd inet_ntop wrote past the end of buffer");
  60. result++;
  61. }
  62. if (inet_ntop (AF_INET6, &addr6, buf, 23) != buf)
  63. {
  64. puts ("4th inet_ntop did not return buf");
  65. result++;
  66. }
  67. if (memcmp (buf, "::ffff:224.224.224.224\0" "xxxxxxxx", 31) != 0)
  68. {
  69. puts ("4th inet_ntop wrote past the end of buffer");
  70. result++;
  71. }
  72. memset (&addr6.s6_addr, 0xe0, sizeof (addr6.s6_addr));
  73. if (inet_ntop (AF_INET6, &addr6, buf, 39) != NULL)
  74. {
  75. puts ("5th inet_ntop returned non-NULL");
  76. result++;
  77. }
  78. else if (errno != ENOSPC)
  79. {
  80. puts ("5th inet_ntop didn't fail with ENOSPC");
  81. result++;
  82. }
  83. if (buf[39] != 'x')
  84. {
  85. puts ("5th inet_ntop wrote past the end of buffer");
  86. result++;
  87. }
  88. if (inet_ntop (AF_INET6, &addr6, buf, 40) != buf)
  89. {
  90. puts ("6th inet_ntop did not return buf");
  91. result++;
  92. }
  93. if (memcmp (buf, "e0e0:e0e0:e0e0:e0e0:e0e0:e0e0:e0e0:e0e0\0"
  94. "xxxxxxxx", 48) != 0)
  95. {
  96. puts ("6th inet_ntop wrote past the end of buffer");
  97. result++;
  98. }
  99. return result;
  100. }
  101. #define TEST_FUNCTION do_test ()
  102. #include "../test-skeleton.c"