summaryrefslogtreecommitdiff
path: root/tests/unit_tests/openvpn/test_argv.c
blob: 3dc470a52d1e03986184a6ea6f3beae426632267 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "config.h"
#include "syshead.h"

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <setjmp.h>
#include <cmocka.h>
#include <assert.h>
#include <stdbool.h>

#include "argv.h"
#include "buffer.h"

/* Defines for use in the tests and the mock parse_line() */
#define PATH1       "/s p a c e"
#define PATH2       "/foo bar/baz"
#define PARAM1      "param1"
#define PARAM2      "param two"
#define SCRIPT_CMD  "\"" PATH1 PATH2 "\"" PARAM1 "\"" PARAM2 "\""

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)
{
    p[0] = PATH1 PATH2;
    p[1] = PARAM1;
    p[2] = PARAM2;
    return 3;
}

static void
argv_printf__multiple_spaces_in_format__parsed_as_one(void **state)
{
    struct argv a = argv_new();

    argv_printf(&a, "    %s     %s  %d   ", PATH1, PATH2, 42);
    assert_int_equal(a.argc, 3);

    argv_free(&a);
}

static void
argv_printf_cat__multiple_spaces_in_format__parsed_as_one(void **state)
{
    struct argv a = argv_new();

    argv_printf(&a, "%s ", PATH1);
    argv_printf_cat(&a, " %s  %s", PATH2, PARAM1);
    assert_int_equal(a.argc, 3);

    argv_free(&a);
}

static void
argv_printf__embedded_format_directive__replaced_in_output(void **state)
{
    struct argv a = argv_new();

    argv_printf(&a, "<p1:%s>", PATH1);
    assert_int_equal(a.argc, 1);
    assert_string_equal(a.argv[0], "<p1:" PATH1 ">");

    argv_free(&a);
}

static void
argv_printf__group_sep_in_arg__fail_no_ouput(void **state)
{
    struct argv a = argv_new();

    assert_false(argv_printf(&a, "tool --do %s", "this\035--harmful"));
    assert_int_equal(a.argc, 0);

    argv_free(&a);
}

static void
argv_printf__combined_path_with_spaces__argc_correct(void **state)
{
    struct argv a = argv_new();

    argv_printf(&a, "%s%s", PATH1, PATH2);
    assert_int_equal(a.argc, 1);

    argv_printf(&a, "%s%s %d", PATH1, PATH2, 42);
    assert_int_equal(a.argc, 2);

    argv_printf(&a, "foo %s%s %s x y", PATH2, PATH1, "foo");
    assert_int_equal(a.argc, 5);

    argv_free(&a);
}

static void
argv_printf__empty_parameter__argc_correct(void **state)
{
    struct argv a = argv_new();

    argv_printf(&a, "%s", "");
    assert_int_equal(a.argc, 1);

    argv_printf(&a, "%s %s", PATH1, "");
    assert_int_equal(a.argc, 2);

    argv_printf(&a, "%s %s %s", PATH1, "", PARAM1);
    assert_int_equal(a.argc, 3);

    argv_printf(&a, "%s %s %s %s", PATH1, "", "", PARAM1);
    assert_int_equal(a.argc, 4);

    argv_printf(&a, "%s %s", "", PARAM1);
    assert_int_equal(a.argc, 2);

    argv_free(&a);
}

static void
argv_printf__long_args__data_correct(void **state)
{
    int i;
    struct argv a = argv_new();
    const char *args[] = {
        "good_tools_have_good_names_even_though_it_might_impair_typing",
        "--long-opt=looooooooooooooooooooooooooooooooooooooooooooooooong",
        "--long-cat=loooooooooooooooooooooooooooooooooooooooooooooooooooonger",
        "file_with_very_descriptive_filename_that_leaves_no_questions_open.jpg.exe"
    };

    argv_printf(&a, "%s %s %s %s", args[0], args[1], args[2], args[3]);
    assert_int_equal(a.argc, 4);
    for (i = 0; i < a.argc; i++)
    {
        assert_string_equal(a.argv[i], args[i]);
    }

    argv_free(&a);
}

static void
argv_parse_cmd__command_string__argc_correct(void **state)
{
    struct argv a = argv_new();

    argv_parse_cmd(&a, SCRIPT_CMD);
    assert_int_equal(a.argc, 3);

    argv_free(&a);
}

static void
argv_parse_cmd__command_and_extra_options__argc_correct(void **state)
{
    struct argv a = argv_new();

    argv_parse_cmd(&a, SCRIPT_CMD);
    argv_printf_cat(&a, "bar baz %d %s", 42, PATH1);
    assert_int_equal(a.argc, 7);

    argv_free(&a);
}

static void
argv_printf_cat__used_twice__argc_correct(void **state)
{
    struct argv a = argv_new();

    argv_printf(&a, "%s %s %s", PATH1, PATH2, PARAM1);
    argv_printf_cat(&a, "%s", PARAM2);
    argv_printf_cat(&a, "foo");
    assert_int_equal(a.argc, 5);

    argv_free(&a);
}

static void
argv_str__empty_argv__empty_output(void **state)
{
    struct argv a = argv_new();
    struct gc_arena gc = gc_new();
    const char *output;

    output = argv_str(&a, &gc, PA_BRACKET);
    assert_string_equal(output, "");

    argv_free(&a);
    gc_free(&gc);
}

static void
argv_str__multiple_argv__correct_output(void **state)
{
    struct argv a = argv_new();
    struct gc_arena gc = gc_new();
    const char *output;

    argv_printf(&a, "%s%s", PATH1, PATH2);
    argv_printf_cat(&a, "%s", PARAM1);
    argv_printf_cat(&a, "%s", PARAM2);
    argv_printf_cat(&a, "%d", -1);
    argv_printf_cat(&a, "%u", -1);
    argv_printf_cat(&a, "%lu", 1L );
    output = argv_str(&a, &gc, PA_BRACKET);
    assert_string_equal(output, "[" PATH1 PATH2 "] [" PARAM1 "] [" PARAM2 "]"
                        " [-1] [4294967295] [1]");

    argv_free(&a);
    gc_free(&gc);
}

static void
argv_insert_head__empty_argv__head_only(void **state)
{
    struct argv a = argv_new();
    struct argv b;

    b = argv_insert_head(&a, PATH1);
    assert_int_equal(b.argc, 1);
    assert_string_equal(b.argv[0], PATH1);
    argv_free(&b);

    argv_free(&a);
}

static void
argv_insert_head__non_empty_argv__head_added(void **state)
{
    struct argv a = argv_new();
    struct argv b;
    int i;

    argv_printf(&a, "%s", PATH2);
    b = argv_insert_head(&a, PATH1);
    assert_int_equal(b.argc, a.argc + 1);
    for (i = 0; i < b.argc; i++)
    {
        if (i == 0)
        {
            assert_string_equal(b.argv[i], PATH1);
        }
        else
        {
            assert_string_equal(b.argv[i], a.argv[i - 1]);
        }
    }
    argv_free(&b);

    argv_free(&a);
}

int
main(void)
{
    const struct CMUnitTest tests[] = {
        cmocka_unit_test(argv_printf__multiple_spaces_in_format__parsed_as_one),
        cmocka_unit_test(argv_printf_cat__multiple_spaces_in_format__parsed_as_one),
        cmocka_unit_test(argv_printf__embedded_format_directive__replaced_in_output),
        cmocka_unit_test(argv_printf__group_sep_in_arg__fail_no_ouput),
        cmocka_unit_test(argv_printf__combined_path_with_spaces__argc_correct),
        cmocka_unit_test(argv_printf__empty_parameter__argc_correct),
        cmocka_unit_test(argv_printf__long_args__data_correct),
        cmocka_unit_test(argv_parse_cmd__command_string__argc_correct),
        cmocka_unit_test(argv_parse_cmd__command_and_extra_options__argc_correct),
        cmocka_unit_test(argv_printf_cat__used_twice__argc_correct),
        cmocka_unit_test(argv_str__empty_argv__empty_output),
        cmocka_unit_test(argv_str__multiple_argv__correct_output),
        cmocka_unit_test(argv_insert_head__non_empty_argv__head_added),
    };

    return cmocka_run_group_tests_name("argv", tests, NULL, NULL);
}