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 BOOL test_command_line_parsing_case(const char* line, const char** list, size_t expect)
35{
36 BOOL rc = FALSE;
37 int numArgs = 0;
38
39 printf("Parsing: %s\n", line);
40
41 LPSTR* pArgs = CommandLineToArgvA(line, &numArgs);
42 if (numArgs < 0)
43 {
44 (void)fprintf(stderr, "expected %" PRIuz " arguments, got %d return\n", expect, numArgs);
45 goto fail;
46 }
47 if (numArgs != expect)
48 {
49 (void)fprintf(stderr, "expected %" PRIuz " arguments, got %d from '%s'\n", expect, numArgs,
50 line);
51 goto fail;
52 }
53
54 if ((numArgs > 0) && !pArgs)
55 {
56 (void)fprintf(stderr, "expected %d arguments, got NULL return\n", numArgs);
57 goto fail;
58 }
59
60 printf("pNumArgs: %d\n", numArgs);
61
62 for (int i = 0; i < numArgs; i++)
63 {
64 printf("argv[%d] = %s\n", i, pArgs[i]);
65 if (strcmp(pArgs[i], list[i]) != 0)
66 {
67 (void)fprintf(stderr, "failed at argument %d: got '%s' but expected '%s'\n", i,
68 pArgs[i], list[i]);
69 goto fail;
70 }
71 }
72
73 rc = TRUE;
74fail:
75 free((void*)pArgs);
76
77 return rc;
78}
79
80int TestThreadCommandLineToArgv(int argc, char* argv[])
81{
82
83 WINPR_UNUSED(argc);
84 WINPR_UNUSED(argv);
85
86 if (!test_command_line_parsing_case(test_args_line_1, test_args_list_1,
87 ARRAYSIZE(test_args_list_1)))
88 return -1;
89 if (!test_command_line_parsing_case(test_args_line_2, test_args_list_2,
90 ARRAYSIZE(test_args_list_2)))
91 return -1;
92 if (!test_command_line_parsing_case(test_args_line_3, test_args_list_3,
93 ARRAYSIZE(test_args_list_3)))
94 return -1;
95 if (!test_command_line_parsing_case(test_args_line_4, test_args_list_4,
96 ARRAYSIZE(test_args_list_4)))
97 return -1;
98 if (!test_command_line_parsing_case(test_args_line_5, test_args_list_5,
99 ARRAYSIZE(test_args_list_5)))
100 return -1;
101 if (!test_command_line_parsing_case(test_args_line_6, test_args_list_6,
102 ARRAYSIZE(test_args_list_6)))
103 return -1;
104 if (!test_command_line_parsing_case(test_args_line_7, test_args_list_7,
105 ARRAYSIZE(test_args_list_7)))
106 return -1;
107
108 return 0;
109}