test_getopt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2003, 2004, 2005, 2006, 2009 GNUnet e.V.
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @file util/test_getopt.c
  19. * @brief testcase for util/getopt.c
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. static int
  24. testMinimal ()
  25. {
  26. char *const emptyargv[] = {
  27. "test",
  28. NULL
  29. };
  30. const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = {
  31. GNUNET_GETOPT_OPTION_END
  32. };
  33. if (1 != GNUNET_GETOPT_run ("test", emptyoptionlist, 1, emptyargv))
  34. return 1;
  35. return 0;
  36. }
  37. static int
  38. testVerbose ()
  39. {
  40. char *const myargv[] = {
  41. "test",
  42. "-V",
  43. "-V",
  44. "more",
  45. NULL
  46. };
  47. unsigned int vflags = 0;
  48. const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] = {
  49. GNUNET_GETOPT_option_verbose (&vflags),
  50. GNUNET_GETOPT_OPTION_END
  51. };
  52. if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv))
  53. {
  54. GNUNET_break (0);
  55. return 1;
  56. }
  57. if (vflags != 2)
  58. {
  59. GNUNET_break (0);
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. static int
  65. testVersion ()
  66. {
  67. char *const myargv[] = {
  68. "test_getopt",
  69. "-v",
  70. NULL
  71. };
  72. const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] = {
  73. GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION),
  74. GNUNET_GETOPT_OPTION_END
  75. };
  76. if (0 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv))
  77. {
  78. GNUNET_break (0);
  79. return 1;
  80. }
  81. return 0;
  82. }
  83. static int
  84. testAbout ()
  85. {
  86. char *const myargv[] = {
  87. "test_getopt",
  88. "-h",
  89. NULL
  90. };
  91. const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] = {
  92. GNUNET_GETOPT_option_help ("Testing"),
  93. GNUNET_GETOPT_OPTION_END
  94. };
  95. if (0 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv))
  96. {
  97. GNUNET_break (0);
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. static int
  103. testLogOpts ()
  104. {
  105. char *const myargv[] = {
  106. "test_getopt",
  107. "-l", "filename",
  108. "-L", "WARNING",
  109. NULL
  110. };
  111. char *level = GNUNET_strdup ("stuff");
  112. char *fn = NULL;
  113. const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
  114. GNUNET_GETOPT_option_logfile (&fn),
  115. GNUNET_GETOPT_option_loglevel (&level),
  116. GNUNET_GETOPT_OPTION_END
  117. };
  118. if (5 != GNUNET_GETOPT_run ("test_getopt",
  119. logoptionlist,
  120. 5, myargv))
  121. {
  122. GNUNET_break (0);
  123. return 1;
  124. }
  125. GNUNET_assert (NULL != fn);
  126. if ( (0 != strcmp (level, "WARNING")) ||
  127. (NULL == strstr (fn, "/filename")) )
  128. {
  129. GNUNET_break (0);
  130. GNUNET_free (level);
  131. GNUNET_free (fn);
  132. return 1;
  133. }
  134. GNUNET_free (level);
  135. GNUNET_free (fn);
  136. return 0;
  137. }
  138. static int
  139. testFlagNum ()
  140. {
  141. char *const myargv[] = {
  142. "test_getopt",
  143. "-f",
  144. "-n", "42",
  145. "-N", "42",
  146. NULL
  147. };
  148. int flag = 0;
  149. unsigned int num = 0;
  150. unsigned long long lnum = 0;
  151. const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
  152. GNUNET_GETOPT_option_flag ('f',
  153. "--flag",
  154. "helptext",
  155. &flag),
  156. GNUNET_GETOPT_option_uint ('n',
  157. "--num",
  158. "ARG",
  159. "helptext",
  160. &num),
  161. GNUNET_GETOPT_option_ulong ('N',
  162. "--lnum",
  163. "ARG",
  164. "helptext",
  165. &lnum),
  166. GNUNET_GETOPT_OPTION_END
  167. };
  168. if (6 !=
  169. GNUNET_GETOPT_run ("test_getopt",
  170. logoptionlist,
  171. 6,
  172. myargv))
  173. {
  174. GNUNET_break (0);
  175. return 1;
  176. }
  177. if ( (1 != flag) ||
  178. (42 != num) ||
  179. (42 != lnum))
  180. {
  181. GNUNET_break (0);
  182. return 1;
  183. }
  184. return 0;
  185. }
  186. int
  187. main (int argc, char *argv[])
  188. {
  189. int errCnt = 0;
  190. GNUNET_log_setup ("test_getopt",
  191. "WARNING",
  192. NULL);
  193. /* suppress output from -h, -v options */
  194. #ifndef MINGW
  195. GNUNET_break (0 == CLOSE (1));
  196. #endif
  197. if (0 != testMinimal ())
  198. errCnt++;
  199. if (0 != testVerbose ())
  200. errCnt++;
  201. if (0 != testVersion ())
  202. errCnt++;
  203. if (0 != testAbout ())
  204. errCnt++;
  205. if (0 != testLogOpts ())
  206. errCnt++;
  207. if (0 != testFlagNum ())
  208. errCnt++;
  209. return errCnt;
  210. }