aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_getopt.c
blob: e5b854658153adef561a1b33b665ee0e0b106d5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
     This file is part of GNUnet.
     Copyright (C) 2003, 2004, 2005, 2006, 2009 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.
    
     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @file util/test_getopt.c
 * @brief testcase for util/getopt.c
 */
#include "platform.h"
#include "gnunet_util_lib.h"


static int
testMinimal ()
{
  char *const emptyargv[] = {
    "test",
    NULL
  };
  const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = {
    GNUNET_GETOPT_OPTION_END
  };

  if (1 != GNUNET_GETOPT_run ("test", emptyoptionlist, 1, emptyargv))
    return 1;

  return 0;
}


static int
testVerbose ()
{
  char *const myargv[] = {
    "test",
    "-V",
    "-V",
    "more",
    NULL
  };
  unsigned int vflags = 0;

  const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] = {
    GNUNET_GETOPT_option_verbose (&vflags),
    GNUNET_GETOPT_OPTION_END
  };

  if (3 != GNUNET_GETOPT_run ("test", verboseoptionlist, 4, myargv))
  {
    GNUNET_break (0);
    return 1;
  }
  if (vflags != 2)
  {
    GNUNET_break (0);
    return 1;
  }
  return 0;
}


static int
testVersion ()
{
  char *const myargv[] = {
    "test_getopt",
    "-v",
    NULL
  };
  const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] = {
    GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION),
    GNUNET_GETOPT_OPTION_END
  };

  if (0 != GNUNET_GETOPT_run ("test_getopt", versionoptionlist, 2, myargv))
  {
    GNUNET_break (0);
    return 1;
  }
  return 0;
}


static int
testAbout ()
{
  char *const myargv[] = {
    "test_getopt",
    "-h",
    NULL
  };
  const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] = {
    GNUNET_GETOPT_option_help ("Testing"),
    GNUNET_GETOPT_OPTION_END
  };

  if (0 != GNUNET_GETOPT_run ("test_getopt", aboutoptionlist, 2, myargv))
  {
    GNUNET_break (0);
    return 1;
  }
  return 0;
}


static int
testLogOpts ()
{
  char *const myargv[] = {
    "test_getopt",
    "-l", "filename",
    "-L", "WARNING",
    NULL
  };
  char *level = GNUNET_strdup ("stuff");
  char *fn = NULL;

  const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
    GNUNET_GETOPT_option_logfile (&fn),
    GNUNET_GETOPT_option_loglevel (&level),
    GNUNET_GETOPT_OPTION_END
  };

  if (5 != GNUNET_GETOPT_run ("test_getopt",
                              logoptionlist,
                              5, myargv))
  {
    GNUNET_break (0);
    return 1;
  }
  GNUNET_assert (NULL != fn);
  if ( (0 != strcmp (level, "WARNING")) ||
       (NULL == strstr (fn, "/filename")) )
  {
    GNUNET_break (0);
    GNUNET_free (level);
    GNUNET_free (fn);
    return 1;
  }
  GNUNET_free (level);
  GNUNET_free (fn);
  return 0;
}


static int
testFlagNum ()
{
  char *const myargv[] = {
    "test_getopt",
    "-f",
    "-n", "42",
    "-N", "42",
    NULL
  };
  int flag = 0;
  unsigned int num = 0;
  unsigned long long lnum = 0;

  const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
    GNUNET_GETOPT_option_flag ('f',
                                  "--flag",
                                  "helptext",
                                  &flag),
    GNUNET_GETOPT_option_uint ('n',
                                   "--num",
                                   "ARG",
                                   "helptext",
                                   &num),
    GNUNET_GETOPT_option_ulong ('N',
                                    "--lnum",
                                    "ARG",
                                    "helptext",
                                    &lnum),
    GNUNET_GETOPT_OPTION_END
  };

  if (6 !=
      GNUNET_GETOPT_run ("test_getopt",
                         logoptionlist,
                         6,
                         myargv))
  {
    GNUNET_break (0);
    return 1;
  }
  if ( (1 != flag) ||
       (42 != num) ||
       (42 != lnum))
  {
    GNUNET_break (0);
    return 1;
  }
  return 0;
}


int
main (int argc, char *argv[])
{
  int errCnt = 0;

  GNUNET_log_setup ("test_getopt",
                    "WARNING",
                    NULL);
  /* suppress output from -h, -v options */
#ifndef MINGW
  GNUNET_break (0 == CLOSE (1));
#endif
  if (0 != testMinimal ())
    errCnt++;
  if (0 != testVerbose ())
    errCnt++;
  if (0 != testVersion ())
    errCnt++;
  if (0 != testAbout ())
    errCnt++;
  if (0 != testLogOpts ())
    errCnt++;
  if (0 != testFlagNum ())
    errCnt++;
  return errCnt;
}