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 <unistd.h>
6 #include <stdlib.h>
7 #include <stdarg.h>
8 #include <string.h>
9 #include <setjmp.h>
10 #include <cmocka.h>
11 #include <assert.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_reset(&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_reset(&a);
54 }
55 
56 static void
58 {
59  struct argv a = argv_new();
60 
61  argv_printf(&a, "%s%sc", PATH1, PATH2);
62  assert_int_equal(a.argc, 1);
63 
64  argv_printf(&a, "%s%sc %d", PATH1, PATH2, 42);
65  assert_int_equal(a.argc, 2);
66 
67  argv_printf(&a, "foo %s%sc %s x y", PATH2, PATH1, "foo");
68  assert_int_equal(a.argc, 5);
69 
70  argv_reset(&a);
71 }
72 
73 static void
75 {
76  struct argv a = argv_new();
77 
79  assert_int_equal(a.argc, 3);
80 
81  argv_reset(&a);
82 }
83 
84 static void
86 {
87  struct argv a = argv_new();
88 
90  argv_printf_cat(&a, "bar baz %d %s", 42, PATH1);
91  assert_int_equal(a.argc, 7);
92 
93  argv_reset(&a);
94 }
95 
96 static void
98 {
99  struct argv a = argv_new();
100 
101  argv_printf(&a, "%s %s %s", PATH1, PATH2, PARAM1);
102  argv_printf_cat(&a, "%s", PARAM2);
103  argv_printf_cat(&a, "foo");
104  assert_int_equal(a.argc, 5);
105 
106  argv_reset(&a);
107 }
108 
109 static void
111 {
112  struct argv a = argv_new();
113  struct gc_arena gc = gc_new();
114  const char *output;
115 
116  argv_printf(&a, "%s%sc", PATH1, PATH2);
117  argv_printf_cat(&a, "%s", PARAM1);
118  argv_printf_cat(&a, "%s", PARAM2);
119  argv_printf_cat(&a, "%d", -1);
120  argv_printf_cat(&a, "%u", -1);
121  argv_printf_cat(&a, "%lu", 1L );
122  output = argv_str(&a, &gc, PA_BRACKET);
123  assert_string_equal(output, "[" PATH1 PATH2 "] [" PARAM1 "] [" PARAM2 "]"
124  " [-1] [4294967295] [1]");
125 
126  argv_reset(&a);
127  gc_free(&gc);
128 }
129 
130 static void
132 {
133  struct argv a = argv_new();
134  struct argv b;
135 
136  b = argv_insert_head(&a, PATH1);
137  assert_int_equal(b.argc, 1);
138  assert_string_equal(b.argv[0], PATH1);
139  argv_reset(&b);
140 
141  argv_reset(&a);
142 }
143 
144 static void
146 {
147  struct argv a = argv_new();
148  struct argv b;
149  int i;
150 
151  argv_printf(&a, "%s", PATH2);
152  b = argv_insert_head(&a, PATH1);
153  assert_int_equal(b.argc, a.argc + 1);
154  for (i = 0; i < b.argc; i++) {
155  if (i == 0)
156  {
158  }
159  else
160  {
161  assert_string_equal(b.argv[i], a.argv[i - 1]);
162  }
163  }
164  argv_reset(&b);
165 
166  argv_reset(&a);
167 }
168 
169 int
170 main(void)
171 {
172  const struct CMUnitTest tests[] = {
181  };
182 
183  return cmocka_run_group_tests_name("argv", tests, NULL, NULL);
184 }
static void argv_printf__combined_path_with_spaces__argc_correct(void **state)
Definition: test_argv.c:57
#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:97
struct argv argv_new(void)
Definition: argv.c:52
static void argv_insert_head__non_empty_argv__head_added(void **state)
Definition: test_argv.c:145
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)
Definition: argv.c:193
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:85
int main(void)
Definition: test_argv.c:170
#define PARAM2
Definition: test_argv.c:20
#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:110
#define assert_string_equal(a, b)
Definition: cmocka.h:1214
static struct gc_arena gc_new(void)
Definition: buffer.h:1015
char ** argv
Definition: argv.h:38
static void argv_insert_head__empty_argv__head_only(void **state)
Definition: test_argv.c:131
void argv_parse_cmd(struct argv *a, const char *s)
Definition: argv.c:337
size_t argc
Definition: argv.h:37
#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:74
void argv_printf_cat(struct argv *a, const char *format,...)
Definition: argv.c:328
#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
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
void argv_reset(struct argv *a)
Definition: argv.c:60
#define PATH1
Definition: test_argv.c:17
Definition: argv.h:35
struct argv argv_insert_head(const struct argv *a, const char *head)
Definition: argv.c:126
#define assert_int_equal(a, b)
Definition: cmocka.h:1174
void argv_printf(struct argv *a, const char *format,...)
Definition: argv.c:318