File: arg_split_test.c

package info (click to toggle)
cfengine3 3.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,256 kB
  • ctags: 9,613
  • sloc: ansic: 116,129; sh: 12,366; yacc: 1,088; makefile: 1,006; lex: 391; perl: 197; xml: 21; sed: 4
file content (169 lines) | stat: -rw-r--r-- 4,626 bytes parent folder | download | duplicates (3)
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
#include <test.h>

#include <setjmp.h>
#include <cmockery.h>

#include <conversion.h>
#include <exec_tools.h>

static void test_split_empty(void)
{
    char **s = ArgSplitCommand("");

    assert_true(s);
    assert_false(*s);
    ArgFree(s);
}

static void test_split_easy(void)
{
    char **s = ArgSplitCommand("zero one two");

    assert_string_equal(s[0], "zero");
    assert_string_equal(s[1], "one");
    assert_string_equal(s[2], "two");
    assert_false(s[3]);

    ArgFree(s);
}

static void test_split_quoted_beginning(void)
{
    char **s = ArgSplitCommand("\"quoted string\" atbeginning");

    assert_string_equal(s[0], "quoted string");
    assert_string_equal(s[1], "atbeginning");
    assert_false(s[2]);
    ArgFree(s);
}

static void test_split_quoted_end(void)
{
    char **s = ArgSplitCommand("atend 'quoted string'");

    assert_string_equal(s[0], "atend");
    assert_string_equal(s[1], "quoted string");
    assert_false(s[2]);
    ArgFree(s);
}

static void test_split_quoted_middle(void)
{
    char **s = ArgSplitCommand("at `quoted string` middle");

    assert_string_equal(s[0], "at");
    assert_string_equal(s[1], "quoted string");
    assert_string_equal(s[2], "middle");
    assert_false(s[3]);
    ArgFree(s);
}

static void test_complex_quoting(void)
{
    char **s = ArgSplitCommand("\"foo`'bar\"");

    assert_string_equal(s[0], "foo`'bar");
    assert_false(s[1]);
    ArgFree(s);
}

static void test_arguments_resize_for_null(void)
{
/* This test checks that extending returned argument list for NULL terminator
 * works correctly */
    char **s = ArgSplitCommand("0 1 2 3 4 5 6 7");

    assert_string_equal(s[7], "7");
    assert_false(s[8]);
    ArgFree(s);
}

static void test_arguments_resize(void)
{
    char **s = ArgSplitCommand("0 1 2 3 4 5 6 7 8");

    assert_string_equal(s[7], "7");
    assert_string_equal(s[8], "8");
    assert_false(s[9]);
    ArgFree(s);
}

static void test_command_promiser(void)
{
    char *t1 = "/bin/echo";
    assert_string_equal(CommandArg0(t1), "/bin/echo");

    char *t2 = "/bin/rpm -qa --queryformat \"i | repos | %{name} | %{version}-%{release} | %{arch}\n\"";
    assert_string_equal(CommandArg0(t2), "/bin/rpm");
    
    char *t3 = "/bin/mount -va";
    assert_string_equal(CommandArg0(t3), "/bin/mount");

    char *t4 = "\"/bin/echo\"";
    assert_string_equal(CommandArg0(t4), "/bin/echo");
    
    char *t5 = "\"/bin/echo\" 123";
    assert_string_equal(CommandArg0(t5), "/bin/echo");

    char *t6 = "\"/bin/echo with space\" 123";
    assert_string_equal(CommandArg0(t6), "/bin/echo with space");

    char *t7 = "c:\\Windows\\System32\\cmd.exe";
    assert_string_equal(CommandArg0(t7), "c:\\Windows\\System32\\cmd.exe");

    char *t8 = "\"c:\\Windows\\System32\\cmd.exe\"";
    assert_string_equal(CommandArg0(t8), "c:\\Windows\\System32\\cmd.exe");

    char *t9 = "\"c:\\Windows\\System32\\cmd.exe\" /some args here";
    assert_string_equal(CommandArg0(t9), "c:\\Windows\\System32\\cmd.exe");

    char *t10 = "\"c:\\Windows\\System32 with space\\cmd.exe\"";
    assert_string_equal(CommandArg0(t10), "c:\\Windows\\System32 with space\\cmd.exe");

    char *t11 = "\"c:\\Windows\\System32 with space\\cmd.exe\" /some args here";
    assert_string_equal(CommandArg0(t11), "c:\\Windows\\System32 with space\\cmd.exe");

    char *t12 = "\"c:\\Windows\\System32 with space\\cmd.exe\" /some \"args here\"";
    assert_string_equal(CommandArg0(t12), "c:\\Windows\\System32 with space\\cmd.exe");

    char *t13 = "\\\\mycommand";
    assert_string_equal(CommandArg0(t13), "\\\\mycommand");

    char *t14 = "\\\\myhost\\share\\command.exe";
    assert_string_equal(CommandArg0(t14), "\\\\myhost\\share\\command.exe");

    char *t15 = "\"\\\\myhost\\share\\command.exe\"";
    assert_string_equal(CommandArg0(t15), "\\\\myhost\\share\\command.exe");


    /* bad input */

    char *b1 = "\"/bin/echo 123";
    assert_string_equal(CommandArg0(b1), "/bin/echo 123");

    char *b2 = "/bin/echo\" 123";
    assert_string_equal(CommandArg0(b2), "/bin/echo\"");

    char *b3 = "";
    assert_string_equal(CommandArg0(b3), "");
    
}

int main()
{
    PRINT_TEST_BANNER();
    const UnitTest tests[] =
    {
        unit_test(test_split_empty),
        unit_test(test_split_easy),
        unit_test(test_split_quoted_beginning),
        unit_test(test_split_quoted_middle),
        unit_test(test_split_quoted_end),
        unit_test(test_complex_quoting),
        unit_test(test_arguments_resize_for_null),
        unit_test(test_arguments_resize),
        unit_test(test_command_promiser),
    };

    return run_tests(tests);
}