FreeRDP
Loading...
Searching...
No Matches
TestThreadCommandLineToArgv.c
1
2#include <stdio.h>
3#include <winpr/crt.h>
4#include <winpr/thread.h>
5
6static const char* test_args_line_1 = "app.exe abc d e";
7
8static const char* test_args_list_1[] = { "app.exe", "abc", "d", "e" };
9
10static const char* test_args_line_2 = "app.exe abc \t def";
11
12static const char* test_args_list_2[] = { "app.exe", "abc", "def" };
13
14static const char* test_args_line_3 = "app.exe \"abc\" d e";
15
16static const char* test_args_list_3[] = { "app.exe", "abc", "d", "e" };
17
18static const char* test_args_line_4 = "app.exe a\\\\b d\"e f\"g h";
19
20static const char* test_args_list_4[] = { "app.exe", "a\\\\b", "de fg", "h" };
21
22static const char* test_args_line_5 = "app.exe a\\\\\\\"b c d";
23
24static const char* test_args_list_5[] = { "app.exe", "a\\\"b", "c", "d" };
25
26static const char* test_args_line_6 = "app.exe a\\\\\\\\\"b c\" d e";
27
28static const char* test_args_list_6[] = { "app.exe", "a\\\\b c", "d", "e" };
29
30static const char* test_args_line_7 = "app.exe a\\\\\\\\\"b c\" d e f\\\\\\\\\"g h\" i j";
31
32static const char* test_args_list_7[] = { "app.exe", "a\\\\b c", "d", "e", "f\\\\g h", "i", "j" };
33
34static const char* test_args_line_8 = "app.exe arg1 \"arg2\"";
35
36static const char* test_args_list_8[] = { "app.exe", "arg1", "arg2" };
37
38static BOOL test_command_line_parsing_case(const char* line, const char** list, size_t expect)
39{
40 BOOL rc = FALSE;
41 int numArgs = 0;
42
43 printf("Parsing: %s\n", line);
44
45 LPSTR* pArgs = CommandLineToArgvA(line, &numArgs);
46 if (numArgs < 0)
47 {
48 (void)fprintf(stderr, "expected %" PRIuz " arguments, got %d return\n", expect, numArgs);
49 goto fail;
50 }
51 if (numArgs != expect)
52 {
53 (void)fprintf(stderr, "expected %" PRIuz " arguments, got %d from '%s'\n", expect, numArgs,
54 line);
55 goto fail;
56 }
57
58 if ((numArgs > 0) && !pArgs)
59 {
60 (void)fprintf(stderr, "expected %d arguments, got NULL return\n", numArgs);
61 goto fail;
62 }
63
64 printf("pNumArgs: %d\n", numArgs);
65
66 for (int i = 0; i < numArgs; i++)
67 {
68 printf("argv[%d] = %s\n", i, pArgs[i]);
69 if (strcmp(pArgs[i], list[i]) != 0)
70 {
71 (void)fprintf(stderr, "failed at argument %d: got '%s' but expected '%s'\n", i,
72 pArgs[i], list[i]);
73 goto fail;
74 }
75 }
76
77 rc = TRUE;
78fail:
79 free((void*)pArgs);
80
81 return rc;
82}
83
84int TestThreadCommandLineToArgv(int argc, char* argv[])
85{
86
87 WINPR_UNUSED(argc);
88 WINPR_UNUSED(argv);
89
90 if (!test_command_line_parsing_case(test_args_line_1, test_args_list_1,
91 ARRAYSIZE(test_args_list_1)))
92 return -1;
93 if (!test_command_line_parsing_case(test_args_line_2, test_args_list_2,
94 ARRAYSIZE(test_args_list_2)))
95 return -1;
96 if (!test_command_line_parsing_case(test_args_line_3, test_args_list_3,
97 ARRAYSIZE(test_args_list_3)))
98 return -1;
99 if (!test_command_line_parsing_case(test_args_line_4, test_args_list_4,
100 ARRAYSIZE(test_args_list_4)))
101 return -1;
102 if (!test_command_line_parsing_case(test_args_line_5, test_args_list_5,
103 ARRAYSIZE(test_args_list_5)))
104 return -1;
105 if (!test_command_line_parsing_case(test_args_line_6, test_args_list_6,
106 ARRAYSIZE(test_args_list_6)))
107 return -1;
108 if (!test_command_line_parsing_case(test_args_line_7, test_args_list_7,
109 ARRAYSIZE(test_args_list_7)))
110 return -1;
111 if (!test_command_line_parsing_case(test_args_line_8, test_args_list_8,
112 ARRAYSIZE(test_args_list_8)))
113 return -1;
114
115 return 0;
116}