2
0

testargs.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_errno.h"
  17. #include "fspr_general.h"
  18. #include "fspr_getopt.h"
  19. #include "fspr_strings.h"
  20. #include "testutil.h"
  21. static void format_arg(char *str, char option, const char *arg)
  22. {
  23. if (arg) {
  24. fspr_snprintf(str, 8196, "%soption: %c with %s\n", str, option, arg);
  25. }
  26. else {
  27. fspr_snprintf(str, 8196, "%soption: %c\n", str, option);
  28. }
  29. }
  30. static void unknown_arg(void *str, const char *err, ...)
  31. {
  32. va_list va;
  33. va_start(va, err);
  34. fspr_vsnprintf(str, 8196, err, va);
  35. va_end(va);
  36. }
  37. static void no_options_found(abts_case *tc, void *data)
  38. {
  39. int largc = 5;
  40. const char * const largv[] = {"testprog", "-a", "-b", "-c", "-d"};
  41. fspr_getopt_t *opt;
  42. fspr_status_t rv;
  43. char ch;
  44. const char *optarg;
  45. char str[8196];
  46. str[0] = '\0';
  47. rv = fspr_getopt_init(&opt, p, largc, largv);
  48. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  49. while (fspr_getopt(opt, "abcd", &ch, &optarg) == APR_SUCCESS) {
  50. switch (ch) {
  51. case 'a':
  52. case 'b':
  53. case 'c':
  54. case 'd':
  55. default:
  56. format_arg(str, ch, optarg);
  57. }
  58. }
  59. ABTS_STR_EQUAL(tc, "option: a\n"
  60. "option: b\n"
  61. "option: c\n"
  62. "option: d\n", str);
  63. }
  64. static void no_options(abts_case *tc, void *data)
  65. {
  66. int largc = 5;
  67. const char * const largv[] = {"testprog", "-a", "-b", "-c", "-d"};
  68. fspr_getopt_t *opt;
  69. fspr_status_t rv;
  70. char ch;
  71. const char *optarg;
  72. char str[8196];
  73. str[0] = '\0';
  74. rv = fspr_getopt_init(&opt, p, largc, largv);
  75. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  76. opt->errfn = unknown_arg;
  77. opt->errarg = str;
  78. while (fspr_getopt(opt, "efgh", &ch, &optarg) == APR_SUCCESS) {
  79. switch (ch) {
  80. case 'a':
  81. case 'b':
  82. case 'c':
  83. case 'd':
  84. format_arg(str, ch, optarg);
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. ABTS_STR_EQUAL(tc, "testprog: illegal option -- a\n", str);
  91. }
  92. static void required_option(abts_case *tc, void *data)
  93. {
  94. int largc = 3;
  95. const char * const largv[] = {"testprog", "-a", "foo"};
  96. fspr_getopt_t *opt;
  97. fspr_status_t rv;
  98. char ch;
  99. const char *optarg;
  100. char str[8196];
  101. str[0] = '\0';
  102. rv = fspr_getopt_init(&opt, p, largc, largv);
  103. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  104. opt->errfn = unknown_arg;
  105. opt->errarg = str;
  106. while (fspr_getopt(opt, "a:", &ch, &optarg) == APR_SUCCESS) {
  107. switch (ch) {
  108. case 'a':
  109. format_arg(str, ch, optarg);
  110. break;
  111. default:
  112. break;
  113. }
  114. }
  115. ABTS_STR_EQUAL(tc, "option: a with foo\n", str);
  116. }
  117. static void required_option_notgiven(abts_case *tc, void *data)
  118. {
  119. int largc = 2;
  120. const char * const largv[] = {"testprog", "-a"};
  121. fspr_getopt_t *opt;
  122. fspr_status_t rv;
  123. char ch;
  124. const char *optarg;
  125. char str[8196];
  126. str[0] = '\0';
  127. rv = fspr_getopt_init(&opt, p, largc, largv);
  128. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  129. opt->errfn = unknown_arg;
  130. opt->errarg = str;
  131. while (fspr_getopt(opt, "a:", &ch, &optarg) == APR_SUCCESS) {
  132. switch (ch) {
  133. case 'a':
  134. format_arg(str, ch, optarg);
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. ABTS_STR_EQUAL(tc, "testprog: option requires an argument -- a\n", str);
  141. }
  142. static void optional_option(abts_case *tc, void *data)
  143. {
  144. int largc = 3;
  145. const char * const largv[] = {"testprog", "-a", "foo"};
  146. fspr_getopt_t *opt;
  147. fspr_status_t rv;
  148. char ch;
  149. const char *optarg;
  150. char str[8196];
  151. str[0] = '\0';
  152. rv = fspr_getopt_init(&opt, p, largc, largv);
  153. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  154. opt->errfn = unknown_arg;
  155. opt->errarg = str;
  156. while (fspr_getopt(opt, "a::", &ch, &optarg) == APR_SUCCESS) {
  157. switch (ch) {
  158. case 'a':
  159. format_arg(str, ch, optarg);
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. ABTS_STR_EQUAL(tc, "option: a with foo\n", str);
  166. }
  167. static void optional_option_notgiven(abts_case *tc, void *data)
  168. {
  169. int largc = 2;
  170. const char * const largv[] = {"testprog", "-a"};
  171. fspr_getopt_t *opt;
  172. fspr_status_t rv;
  173. char ch;
  174. const char *optarg;
  175. char str[8196];
  176. str[0] = '\0';
  177. rv = fspr_getopt_init(&opt, p, largc, largv);
  178. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  179. opt->errfn = unknown_arg;
  180. opt->errarg = str;
  181. while (fspr_getopt(opt, "a::", &ch, &optarg) == APR_SUCCESS) {
  182. switch (ch) {
  183. case 'a':
  184. format_arg(str, ch, optarg);
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. #if 0
  191. /* Our version of getopt doesn't allow for optional arguments. */
  192. ABTS_STR_EQUAL(tc, "option: a\n", str);
  193. #endif
  194. ABTS_STR_EQUAL(tc, "testprog: option requires an argument -- a\n", str);
  195. }
  196. abts_suite *testgetopt(abts_suite *suite)
  197. {
  198. suite = ADD_SUITE(suite)
  199. abts_run_test(suite, no_options, NULL);
  200. abts_run_test(suite, no_options_found, NULL);
  201. abts_run_test(suite, required_option, NULL);
  202. abts_run_test(suite, required_option_notgiven, NULL);
  203. abts_run_test(suite, optional_option, NULL);
  204. abts_run_test(suite, optional_option_notgiven, NULL);
  205. return suite;
  206. }