22#define PATH1 "/s p a c e"
23#define PATH2 "/foo bar/baz"
24#define PARAM1 "param1"
25#define PARAM2 "param two"
26#define SCRIPT_CMD "\"" PATH1 PATH2 "\"" PARAM1 "\"" PARAM2 "\""
29parse_line(
const char *line,
char **p,
const int n,
const char *file,
const int line_num,
44 assert_int_equal(a.
argc, 3);
56 assert_int_equal(a.
argc, 3);
67 assert_int_equal(a.
argc, 1);
68 assert_string_equal(a.
argv[0],
"<p1:" PATH1 ">");
78 assert_false(
argv_printf(&a,
"tool --do %s",
"this\035--harmful"));
79 assert_int_equal(a.
argc, 0);
90 assert_int_equal(a.
argc, 1);
93 assert_int_equal(a.
argc, 2);
96 assert_int_equal(a.
argc, 5);
107 assert_int_equal(a.
argc, 1);
110 assert_int_equal(a.
argc, 2);
113 assert_int_equal(a.
argc, 3);
116 assert_int_equal(a.
argc, 4);
119 assert_int_equal(a.
argc, 2);
128 const char *args[] = {
129 "good_tools_have_good_names_even_though_it_might_impair_typing",
130 "--long-opt=looooooooooooooooooooooooooooooooooooooooooooooooong",
131 "--long-cat=loooooooooooooooooooooooooooooooooooooooooooooooooooonger",
132 "file_with_very_descriptive_filename_that_leaves_no_questions_open.jpg.exe"
135 argv_printf(&a,
"%s %s %s %s", args[0], args[1], args[2], args[3]);
136 assert_int_equal(a.
argc, 4);
137 for (
size_t i = 0; i < a.
argc; i++)
139 assert_string_equal(a.
argv[i], args[i]);
151 assert_int_equal(a.
argc, 3);
163 assert_int_equal(a.
argc, 7);
176 assert_int_equal(a.
argc, 5);
189 assert_string_equal(output,
"");
210 " [-1] [4294967295] [1]");
223 assert_int_equal(b.
argc, 1);
238 assert_int_equal(b.
argc, a.
argc + 1);
239 for (
size_t i = 0; i < b.
argc; i++)
247 assert_string_equal(b.
argv[i], a.
argv[i - 1]);
266 const WCHAR *expected;
269 {
"CN=user1", L
"script.bat 0 CN=user1" },
271 {
"CN=a,b;c=d", L
"script.bat 0 CN=a,b;c=d" },
273 {
"O=Ctrl, CN=y", L
"script.bat 0 \"O=Ctrl, CN=y\"" },
275 {
"CN=x&ver", L
"script.bat 0 \"CN=x&ver\"" },
276 {
"CN=x|ver", L
"script.bat 0 \"CN=x|ver\"" },
277 {
"CN=x>f", L
"script.bat 0 \"CN=x>f\"" },
278 {
"CN=x<f", L
"script.bat 0 \"CN=x<f\"" },
279 {
"CN=x^f", L
"script.bat 0 \"CN=x^f\"" },
280 {
"CN=x%f", L
"script.bat 0 \"CN=x%f\"" },
281 {
"CN=x(f)", L
"script.bat 0 \"CN=x(f)\"" },
282 {
"CN=x!f", L
"script.bat 0 \"CN=x!f\"" },
284 {
"CN=a\"b", L
"script.bat 0 CN=a_b" },
287 for (
size_t i = 0; i <
SIZE(cases); i++)
292 argv_printf(&a,
"%s %d %s",
"script.bat", 0, cases[i].arg);
293 assert_int_equal(a.
argc, 3);
296 assert_non_null(cmd_line);
297 assert_int_equal(wcscmp(cmd_line, cases[i].expected), 0);
309 const struct CMUnitTest tests[] = {
329 return cmocka_run_group_tests_name(
"argv", tests, NULL, NULL);
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.
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...
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
struct argv argv_insert_head(const struct argv *a, const char *head)
Inserts an argument string in front of all other argument slots.
Buffer management functions and garbage collection.
#define PA_BRACKET
Flag for print_argv(): wrap each argument in square brackets.
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Garbage collection arena used to keep track of dynamically allocated memory.
static void argv_parse_cmd__command_and_extra_options__argc_correct(void **state)
int parse_line(const char *line, char **p, const int n, const char *file, const int line_num, msglvl_t msglevel, struct gc_arena *gc)
static void argv_printf_cat__used_twice__argc_correct(void **state)
static void argv_str__multiple_argv__correct_output(void **state)
static void argv_printf__group_sep_in_arg__fail_no_ouput(void **state)
static void argv_printf_cat__multiple_spaces_in_format__parsed_as_one(void **state)
static void argv_str__empty_argv__empty_output(void **state)
static void argv_insert_head__empty_argv__head_only(void **state)
static void argv_printf__embedded_format_directive__replaced_in_output(void **state)
static void argv_printf__empty_parameter__argc_correct(void **state)
static void argv_printf__multiple_spaces_in_format__parsed_as_one(void **state)
static void argv_parse_cmd__command_string__argc_correct(void **state)
static void argv_printf__combined_path_with_spaces__argc_correct(void **state)
static void wide_cmd_line__quotes_only_what_cmd_would_reinterpret(void **state)
static void argv_printf__long_args__data_correct(void **state)
static void argv_insert_head__non_empty_argv__head_added(void **state)
static void openvpn_unit_test_setup(void)
Sets up the environment for unit tests like making both stderr and stdout non-buffered to avoid messa...
WCHAR * wide_cmd_line(const struct argv *a, struct gc_arena *gc)