portabtest.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * Test program for portab.h and friends ;-)
  15. */
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. int allow_severity = 0, deny_severity = 0;
  21. static void
  22. Panic(char *Reason)
  23. {
  24. /* Oops, something failed!? */
  25. fprintf(stderr, "Oops, test for %s failed!?\n", Reason);
  26. exit(1);
  27. } /* Panic */
  28. static void
  29. Check_snprintf(void)
  30. {
  31. char str[5];
  32. snprintf(str, sizeof(str), "%s", "1234567890");
  33. if (str[4] != '\0')
  34. Panic("snprintf NULL byte");
  35. if (strlen(str) != 4)
  36. Panic("snprintf string length");
  37. }
  38. static void
  39. Check_strdup(void)
  40. {
  41. char *ptr;
  42. ptr = strdup("1234567890");
  43. if (!ptr)
  44. Panic("strdup");
  45. if (ptr[10] != '\0')
  46. Panic("strdup NULL byte");
  47. if (strlen(ptr) != 10)
  48. Panic("strdup string length");
  49. free(ptr);
  50. }
  51. static void
  52. Check_strndup(void)
  53. {
  54. char *ptr;
  55. ptr = strndup("1234567890", 5);
  56. if (!ptr)
  57. Panic("strndup");
  58. if (ptr[5] != '\0')
  59. Panic("strndup NULL byte");
  60. if (strlen(ptr) != 5)
  61. Panic("strndup string length");
  62. free(ptr);
  63. }
  64. static void
  65. Check_strlcpy(void)
  66. {
  67. char str[5];
  68. if (strlcpy(str, "1234567890", sizeof(str)) != 10)
  69. Panic("strlcpy return code");
  70. if (str[4] != '\0')
  71. Panic("strlcpy NULL byte");
  72. if (strlen(str) != 4)
  73. Panic("strlcpy string length");
  74. }
  75. static void
  76. Check_strlcat(void)
  77. {
  78. char str[5];
  79. if (strlcpy(str, "12", sizeof(str)) != 2)
  80. Panic("strlcpy for strlcat");
  81. if (strlcat(str, "1234567890", sizeof(str)) != 12)
  82. Panic("strlcat return code");
  83. if (str[4] != '\0')
  84. Panic("strlcat NULL byte");
  85. if (strlen(str) != 4)
  86. Panic("strlcat string length");
  87. }
  88. static void
  89. Check_strtok_r(void)
  90. {
  91. char *ptr, *last;
  92. ptr = strdup("12,abc");
  93. ptr = strtok_r(ptr, ",", &last);
  94. if (!ptr)
  95. Panic("strtok_r result #1");
  96. if (strcmp(ptr, "12") != 0)
  97. Panic("strtok_r token #1");
  98. ptr = strtok_r(NULL, ",", &last);
  99. if (!ptr)
  100. Panic("strtok_r result #2");
  101. if (strcmp(ptr, "abc") != 0)
  102. Panic("strtok_r token #2");
  103. ptr = strtok_r(NULL, ",", &last);
  104. if (ptr)
  105. Panic("strtok_r result #3");
  106. }
  107. #ifdef PROTOTYPES
  108. static void
  109. Check_vsnprintf(const int Len, const char *Format, ...)
  110. #else
  111. static void
  112. Check_vsnprintf(Len, Format, va_alist)
  113. const int Len;
  114. const char *Format;
  115. va_dcl
  116. #endif
  117. {
  118. char str[5];
  119. va_list ap;
  120. int r;
  121. #ifdef PROTOTYPES
  122. va_start(ap, Format);
  123. #else
  124. va_start(ap);
  125. #endif
  126. r = vsnprintf(str, sizeof(str), Format, ap);
  127. va_end(ap);
  128. if (r != Len) {
  129. /* C99 states that vsnprintf() "returns the number of
  130. * characters that would have been printed if the n were
  131. * unlimited", but according to the Linux manual page "glibc
  132. * until 2.0.6 would return -1 when the output was truncated",
  133. * and other implementations (libUTIL on A/UX) even return the
  134. * number of characters processed ... so we only test our own
  135. * implementation and warn on errors otherwise :-/ */
  136. #ifdef HAVE_VSNPRINTF
  137. fprintf(stderr,
  138. "\n ** WARNING: The vsnprintf() function of this system isn't standard\n");
  139. fprintf(stderr,
  140. " ** conformant and returns a WRONG result: %d (should be %d)! The test\n",
  141. r, Len);
  142. fprintf(stderr,
  143. " ** result has been ignored but may lead to errors during execution!\n\n");
  144. #else
  145. Panic("vsnprintf return code");
  146. #endif
  147. }
  148. if (str[4] != '\0')
  149. Panic("vsnprintf NULL byte");
  150. if (strlen(str) != 4)
  151. Panic("vsnprintf string length");
  152. }
  153. GLOBAL int
  154. main(void)
  155. {
  156. /* validate datatypes */
  157. if (false != 0)
  158. Panic("false");
  159. if (true != 1)
  160. Panic("true");
  161. if (sizeof(UINT8) != 1)
  162. Panic("UINT8");
  163. if (sizeof(UINT16) != 2)
  164. Panic("UINT16");
  165. if (sizeof(UINT32) != 4)
  166. Panic("UINT32");
  167. /* check functions */
  168. Check_snprintf();
  169. Check_strdup();
  170. Check_strndup();
  171. Check_strlcpy();
  172. Check_strlcat();
  173. Check_strtok_r();
  174. Check_vsnprintf(2+10, "%s%s", "ab", "1234567890");
  175. return 0;
  176. }
  177. /* -eof- */