OpenVPN
test_argv.c
Go to the documentation of this file.
1 #include "config.h"
2 #include "syshead.h"
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <string.h>
8 #include <setjmp.h>
9 #include <cmocka.h>
10 #include <assert.h>
11 #include <stdbool.h>
12 
13 #include "argv.h"
14 #include "buffer.h"
15 
16 /* Defines for use in the tests and the mock parse_line() */
17 #define PATH1 "/s p a c e"
18 #define PATH2 "/foo bar/baz"
19 #define PARAM1 "param1"
20 #define PARAM2 "param two"
21 #define SCRIPT_CMD "\"" PATH1 PATH2 "\"" PARAM1 "\"" PARAM2 "\""
22 
23 int
24 __wrap_parse_line(const char *line, char **p, const int n, const char *file,
25  const int line_num, int msglevel, struct gc_arena *gc)
26 {
27  p[0] = PATH1 PATH2;
28  p[1] = PARAM1;
29  p[2] = PARAM2;
30  return 3;
31 }
32 
33 static void
35 {
36  struct argv a = argv_new();
37 
38  argv_printf(&a, " %s %s %d ", PATH1, PATH2, 42);
39  assert_int_equal(a.argc, 3);
40 
41  argv_free(&a);
42 }
43 
44 static void
46 {
47  struct argv a = argv_new();
48 
49  argv_printf(&a, "%s ", PATH1);
50  argv_printf_cat(&a, " %s %s", PATH2, PARAM1);
51  assert_int_equal(a.argc, 3);
52 
53  argv_free(&a);
54 }
55 
56 static void
58 {
59  struct argv a = argv_new();
60 
61  argv_printf(&a, "<p1:%s>", PATH1);
62  assert_int_equal(a.argc, 1);
63  assert_string_equal(a.argv[0], "<p1:" PATH1 ">");
64 
65  argv_free(&a);
66 }
67 
68 static void
70 {
71  struct argv a = argv_new();
72 
73  assert_false(argv_printf(&a, "tool --do %s", "this\035--harmful"));
74  assert_int_equal(a.argc, 0);
75 
76  argv_free(&a);
77 }
78 
79 static void
81 {
82  struct argv a = argv_new();
83 
84  argv_printf(&a, "%s%s", PATH1, PATH2);
85  assert_int_equal(a.argc, 1);
86 
87  argv_printf(&a, "%s%s %d", PATH1, PATH2, 42);
88  assert_int_equal(a.argc, 2);
89 
90  argv_printf(&a, "foo %s%s %s x y", PATH2, PATH1, "foo");
91  assert_int_equal(a.argc, 5);
92 
93  argv_free(&a);
94 }
95 
96 static void
98 {
99  struct argv a = argv_new();
100 
101  argv_printf(&a, "%s", "");
102  assert_int_equal(a.argc, 1);
103 
104  argv_printf(&a, "%s %s", PATH1, "");
105  assert_int_equal(a.argc, 2);
106 
107  argv_printf(&a, "%s %s %s", PATH1, "", PARAM1);
108  assert_int_equal(a.argc, 3);
109 
110  argv_printf(&a, "%s %s %s %s", PATH1, "", "", PARAM1);
111  assert_int_equal(a.argc, 4);
112 
113  argv_printf(&a, "%s %s", "", PARAM1);
114  assert_int_equal(a.argc, 2);
115 
116  argv_free(&a);
117 }
118 
119 static void
121 {
122  int i;
123  struct argv a = argv_new();
124  const char *args[] = {
125  "good_tools_have_good_names_even_though_it_might_impair_typing",
126  "--long-opt=looooooooooooooooooooooooooooooooooooooooooooooooong",
127  "--long-cat=loooooooooooooooooooooooooooooooooooooooooooooooooooonger",
128  "file_with_very_descriptive_filename_that_leaves_no_questions_open.jpg.exe"
129  };
130 
131  argv_printf(&a, "%s %s %s %s", args[0], args[1], args[2], args[3]);
132  assert_int_equal(a.argc, 4);
133  for (i = 0; i < a.argc; i++)
134  {
135  assert_string_equal(a.argv[i], args[i]);
136  }
137 
138  argv_free(&a);
139 }
140 
141 static void
143 {
144  struct argv a = argv_new();
145 
147  assert_int_equal(a.argc, 3);
148 
149  argv_free(&a);
150 }
151 
152 static void
154 {
155  struct argv a = argv_new();
156 
158  argv_printf_cat(&a, "bar baz %d %s", 42, PATH1);
159  assert_int_equal(a.argc, 7);
160 
161  argv_free(&a);
162 }
163 
164 static void
166 {
167  struct argv a = argv_new();
168 
169  argv_printf(&a, "%s %s %s", PATH1, PATH2, PARAM1);
170  argv_printf_cat(&a, "%s", PARAM2);
171  argv_printf_cat(&a, "foo");
172  assert_int_equal(a.argc, 5);
173 
174  argv_free(&a);
175 }
176 
177 static void
179 {
180  struct argv a = argv_new();
181  struct gc_arena gc = gc_new();
182  const char *output;
183 
184  output = argv_str(&a, &gc, PA_BRACKET);
185  assert_string_equal(output, "");
186 
187  argv_free(&a);
188  gc_free(&gc);
189 }
190 
191 static void
193 {
194  struct argv a = argv_new();
195  struct gc_arena gc = gc_new();
196  const char *output;
197 
198  argv_printf(&a, "%s%s", PATH1, PATH2);
199  argv_printf_cat(&a, "%s", PARAM1);
200  argv_printf_cat(&a, "%s", PARAM2);
201  argv_printf_cat(&a, "%d", -1);
202  argv_printf_cat(&a, "%u", -1);
203  argv_printf_cat(&a, "%lu", 1L );
204  output = argv_str(&a, &gc, PA_BRACKET);
205  assert_string_equal(output, "[" PATH1 PATH2 "] [" PARAM1 "] [" PARAM2 "]"
206  " [-1] [4294967295] [1]");
207 
208  argv_free(&a);
209  gc_free(&gc);
210 }
211 
212 static void
214 {
215  struct argv a = argv_new();
216  struct argv b;
217 
218  b = argv_insert_head(&a, PATH1);
219  assert_int_equal(b.argc, 1);
220  assert_string_equal(b.argv[0], PATH1);
221  argv_free(&b);
222 
223  argv_free(&a);
224 }
225 
226 static void
228 {
229  struct argv a = argv_new();
230  struct argv b;
231  int i;
232 
233  argv_printf(&a, "%s", PATH2);
234  b = argv_insert_head(&a, PATH1);
235  assert_int_equal(b.argc, a.argc + 1);
236  for (i = 0; i < b.argc; i++)
237  {
238  if (i == 0)
239  {
241  }
242  else
243  {
244  assert_string_equal(b.argv[i], a.argv[i - 1]);
245  }
246  }
247  argv_free(&b);
248 
249  argv_free(&a);
250 }
251 
252 int
253 main(void)
254 {
255  const struct CMUnitTest tests[] = {
269  };
270 
271  return cmocka_run_group_tests_name("argv", tests, NULL, NULL);
272 }
static void argv_printf__empty_parameter__argc_correct(void **state)
Definition: test_argv.c:97
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
Definition: argv.c:466
static void argv_printf__combined_path_with_spaces__argc_correct(void **state)
Definition: test_argv.c:80
#define cmocka_unit_test(f)
Initializes a CMUnitTest structure.
Definition: cmocka.h:1653
static void argv_printf_cat__used_twice__argc_correct(void **state)
Definition: test_argv.c:165
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition: argv.c:90
static void argv_insert_head__non_empty_argv__head_added(void **state)
Definition: test_argv.c:227
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1023
const char * argv_str(const struct argv *a, struct gc_arena *gc, const unsigned int flags)
Generate a single string with all the arguments in a struct argv concatenated.
Definition: argv.c:233
static void argv_printf_cat__multiple_spaces_in_format__parsed_as_one(void **state)
Definition: test_argv.c:45
static void argv_parse_cmd__command_and_extra_options__argc_correct(void **state)
Definition: test_argv.c:153
int main(void)
Definition: test_argv.c:253
#define PARAM2
Definition: test_argv.c:20
static void argv_printf__group_sep_in_arg__fail_no_ouput(void **state)
Definition: test_argv.c:69
#define PATH2
Definition: test_argv.c:18
#define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown)
Definition: cmocka.h:1818
static void argv_str__multiple_argv__correct_output(void **state)
Definition: test_argv.c:192
#define assert_string_equal(a, b)
Definition: cmocka.h:1214
static struct gc_arena gc_new(void)
Definition: buffer.h:1015
static void argv_str__empty_argv__empty_output(void **state)
Definition: test_argv.c:178
char ** argv
Definition: argv.h:39
static void argv_printf__long_args__data_correct(void **state)
Definition: test_argv.c:120
static void argv_insert_head__empty_argv__head_only(void **state)
Definition: test_argv.c:213
static void argv_printf__embedded_format_directive__replaced_in_output(void **state)
Definition: test_argv.c:57
size_t argc
Definition: argv.h:38
#define PARAM1
Definition: test_argv.c:19
static void argv_printf__multiple_spaces_in_format__parsed_as_one(void **state)
Definition: test_argv.c:34
#define SCRIPT_CMD
Definition: test_argv.c:21
static void argv_parse_cmd__command_string__argc_correct(void **state)
Definition: test_argv.c:142
#define PA_BRACKET
Definition: buffer.h:144
int __wrap_parse_line(const char *line, char **p, const int n, const char *file, const int line_num, int msglevel, struct gc_arena *gc)
Definition: test_argv.c:24
void argv_parse_cmd(struct argv *argres, const char *cmdstr)
Parses a command string, tokenizes it and puts each element into a separate struct argv argument slot...
Definition: argv.c:485
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition: argv.c:104
#define PATH1
Definition: test_argv.c:17
Definition: argv.h:35
#define assert_false(c)
Definition: cmocka.h:1063
struct argv argv_insert_head(const struct argv *a, const char *head)
Inserts an argument string in front of all other argument slots.
Definition: argv.c:210
#define assert_int_equal(a, b)
Definition: cmocka.h:1174
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition: argv.c:442