test_getopt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/test_getopt.c
  18. * @brief testcase for util/getopt.c
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. static int
  23. testMinimal ()
  24. {
  25. char *const emptyargv[] = { "test", NULL };
  26. const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = {
  27. GNUNET_GETOPT_OPTION_END
  28. };
  29. if (1 != GNUNET_GETOPT_run ("test", emptyoptionlist, 1, emptyargv))
  30. return 1;
  31. return 0;
  32. }
  33. static int
  34. testVerbose ()
  35. {
  36. char *const myargv[] = { "test", "-V", "-V", "more", NULL };
  37. unsigned int vflags = 0;
  38. const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] =
  39. { GNUNET_GETOPT_option_verbose (&vflags), GNUNET_GETOPT_OPTION_END };
  40. if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv))
  41. {
  42. GNUNET_break (0);
  43. return 1;
  44. }
  45. if (vflags != 2)
  46. {
  47. GNUNET_break (0);
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. static int
  53. testVersion ()
  54. {
  55. char *const myargv[] = { "test_getopt", "-v", NULL };
  56. const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] =
  57. { GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION),
  58. GNUNET_GETOPT_OPTION_END };
  59. if (0 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv))
  60. {
  61. GNUNET_break (0);
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. static int
  67. testAbout ()
  68. {
  69. char *const myargv[] = { "test_getopt", "-h", NULL };
  70. const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] =
  71. { GNUNET_GETOPT_option_help ("Testing"), GNUNET_GETOPT_OPTION_END };
  72. if (0 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv))
  73. {
  74. GNUNET_break (0);
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. static int
  80. testLogOpts ()
  81. {
  82. char *const myargv[] =
  83. { "test_getopt", "-l", "filename", "-L", "WARNING", NULL };
  84. char *level = GNUNET_strdup ("stuff");
  85. char *fn = NULL;
  86. const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] =
  87. { GNUNET_GETOPT_option_logfile (&fn),
  88. GNUNET_GETOPT_option_loglevel (&level),
  89. GNUNET_GETOPT_OPTION_END };
  90. if (5 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 5, myargv))
  91. {
  92. GNUNET_break (0);
  93. return 1;
  94. }
  95. GNUNET_assert (NULL != fn);
  96. if ((0 != strcmp (level, "WARNING")) || (NULL == strstr (fn, "/filename")))
  97. {
  98. GNUNET_break (0);
  99. GNUNET_free (level);
  100. GNUNET_free (fn);
  101. return 1;
  102. }
  103. GNUNET_free (level);
  104. GNUNET_free (fn);
  105. return 0;
  106. }
  107. static int
  108. testFlagNum ()
  109. {
  110. char *const myargv[] = { "test_getopt", "-f", "-n", "42", "-N", "42", NULL };
  111. int flag = 0;
  112. unsigned int num = 0;
  113. unsigned long long lnum = 0;
  114. const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] =
  115. { GNUNET_GETOPT_option_flag ('f', "--flag", "helptext", &flag),
  116. GNUNET_GETOPT_option_uint ('n', "--num", "ARG", "helptext", &num),
  117. GNUNET_GETOPT_option_ulong ('N', "--lnum", "ARG", "helptext", &lnum),
  118. GNUNET_GETOPT_OPTION_END };
  119. if (6 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 6, myargv))
  120. {
  121. GNUNET_break (0);
  122. return 1;
  123. }
  124. if ((1 != flag) || (42 != num) || (42 != lnum))
  125. {
  126. GNUNET_break (0);
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. int
  132. main (int argc, char *argv[])
  133. {
  134. int errCnt = 0;
  135. GNUNET_log_setup ("test_getopt", "WARNING", NULL);
  136. /* suppress output from -h, -v options */
  137. GNUNET_break (0 == close (1));
  138. if (0 != testMinimal ())
  139. errCnt++;
  140. if (0 != testVerbose ())
  141. errCnt++;
  142. if (0 != testVersion ())
  143. errCnt++;
  144. if (0 != testAbout ())
  145. errCnt++;
  146. if (0 != testLogOpts ())
  147. errCnt++;
  148. if (0 != testFlagNum ())
  149. errCnt++;
  150. return errCnt;
  151. }