portabtest.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 *str, *ptr, *last;
  92. ptr = strdup("12,abc");
  93. str = ptr;
  94. ptr = strtok_r(ptr, ",", &last);
  95. if (!ptr)
  96. Panic("strtok_r result #1");
  97. if (strcmp(ptr, "12") != 0)
  98. Panic("strtok_r token #1");
  99. ptr = strtok_r(NULL, ",", &last);
  100. if (!ptr)
  101. Panic("strtok_r result #2");
  102. if (strcmp(ptr, "abc") != 0)
  103. Panic("strtok_r token #2");
  104. ptr = strtok_r(NULL, ",", &last);
  105. if (ptr)
  106. Panic("strtok_r result #3");
  107. free(str);
  108. }
  109. #ifdef PROTOTYPES
  110. static void
  111. Check_vsnprintf(const int Len, const char *Format, ...)
  112. #else
  113. static void
  114. Check_vsnprintf(Len, Format, va_alist)
  115. const int Len;
  116. const char *Format;
  117. va_dcl
  118. #endif
  119. {
  120. char str[5];
  121. va_list ap;
  122. int r;
  123. #ifdef PROTOTYPES
  124. va_start(ap, Format);
  125. #else
  126. va_start(ap);
  127. #endif
  128. r = vsnprintf(str, sizeof(str), Format, ap);
  129. va_end(ap);
  130. if (r != Len) {
  131. /* C99 states that vsnprintf() "returns the number of
  132. * characters that would have been printed if the n were
  133. * unlimited", but according to the Linux manual page "glibc
  134. * until 2.0.6 would return -1 when the output was truncated",
  135. * and other implementations (libUTIL on A/UX) even return the
  136. * number of characters processed ... so we only test our own
  137. * implementation and warn on errors otherwise :-/ */
  138. #ifdef HAVE_VSNPRINTF
  139. fprintf(stderr,
  140. "\n ** WARNING: The vsnprintf() function of this system isn't standard\n");
  141. fprintf(stderr,
  142. " ** conformant and returns a WRONG result: %d (should be %d)! The test\n",
  143. r, Len);
  144. fprintf(stderr,
  145. " ** result has been ignored but may lead to errors during execution!\n\n");
  146. #else
  147. Panic("vsnprintf return code");
  148. #endif
  149. }
  150. if (str[4] != '\0')
  151. Panic("vsnprintf NULL byte");
  152. if (strlen(str) != 4)
  153. Panic("vsnprintf string length");
  154. }
  155. GLOBAL int
  156. main(void)
  157. {
  158. /* validate datatypes */
  159. if (false != 0)
  160. Panic("false");
  161. if (true != 1)
  162. Panic("true");
  163. if (sizeof(UINT8) != 1)
  164. Panic("UINT8");
  165. if (sizeof(UINT16) != 2)
  166. Panic("UINT16");
  167. if (sizeof(UINT32) != 4)
  168. Panic("UINT32");
  169. /* check functions */
  170. Check_snprintf();
  171. Check_strdup();
  172. Check_strndup();
  173. Check_strlcpy();
  174. Check_strlcat();
  175. Check_strtok_r();
  176. Check_vsnprintf(2+10, "%s%s", "ab", "1234567890");
  177. return 0;
  178. }
  179. /* -eof- */