FreeRDP
Loading...
Searching...
No Matches
TestClientCmdLine.c
1#include <freerdp/client.h>
2#include <freerdp/client/cmdline.h>
3#include <freerdp/settings.h>
4#include <winpr/cmdline.h>
5#include <winpr/spec.h>
6#include <winpr/strlst.h>
7#include <winpr/collections.h>
8
9typedef BOOL (*validate_settings_pr)(rdpSettings* settings);
10
11#define printref() printf("%s:%d: in function %-40s:", __FILE__, __LINE__, __func__)
12
13#define TEST_ERROR(format, ...) \
14 do \
15 { \
16 (void)fprintf(stderr, format, ##__VA_ARGS__); \
17 printref(); \
18 (void)printf(format, ##__VA_ARGS__); \
19 (void)fflush(stdout); \
20 } while (0)
21
22#define TEST_FAILURE(format, ...) \
23 do \
24 { \
25 printref(); \
26 (void)printf(" FAILURE "); \
27 (void)printf(format, ##__VA_ARGS__); \
28 (void)fflush(stdout); \
29 } while (0)
30
31static void print_test_title(int argc, char** argv)
32{
33 printf("Running test:");
34
35 for (int i = 0; i < argc; i++)
36 {
37 printf(" %s", argv[i]);
38 }
39
40 printf("\n");
41}
42
43static inline BOOL testcase(const char* name, char** argv, size_t argc, int expected_return,
44 validate_settings_pr validate_settings)
45{
46 int status = 0;
47 BOOL valid_settings = TRUE;
48 rdpSettings* settings = freerdp_settings_new(0);
49
50 WINPR_ASSERT(argc <= INT_MAX);
51
52 print_test_title((int)argc, argv);
53
54 if (!settings)
55 {
56 TEST_ERROR("Test %s could not allocate settings!\n", name);
57 return FALSE;
58 }
59
60 status = freerdp_client_settings_parse_command_line(settings, (int)argc, argv, FALSE);
61
62 if (validate_settings)
63 {
64 valid_settings = validate_settings(settings);
65 }
66
67 freerdp_settings_free(settings);
68
69 if (status == expected_return)
70 {
71 if (!valid_settings)
72 {
73 return FALSE;
74 }
75 }
76 else
77 {
78 TEST_FAILURE("Expected status %d, got status %d\n", expected_return, status);
79 return FALSE;
80 }
81
82 return TRUE;
83}
84
85#if defined(_WIN32)
86#define DRIVE_REDIRECT_PATH "c:\\Windows"
87#else
88#define DRIVE_REDIRECT_PATH "/tmp"
89#endif
90
91static BOOL check_settings_smartcard_no_redirection(rdpSettings* settings)
92{
93 BOOL result = TRUE;
94
95 if (freerdp_settings_get_bool(settings, FreeRDP_RedirectSmartCards))
96 {
97 TEST_FAILURE("Expected RedirectSmartCards = FALSE, but RedirectSmartCards = TRUE!\n");
98 result = FALSE;
99 }
100
101 if (freerdp_device_collection_find_type(settings, RDPDR_DTYP_SMARTCARD))
102 {
103 TEST_FAILURE("Expected no SMARTCARD device, but found at least one!\n");
104 result = FALSE;
105 }
106
107 return result;
108}
109
110typedef struct
111{
112 int expected_status;
113 validate_settings_pr validate_settings;
114 const char* command_line[128];
115 struct
116 {
117 int index;
118 const char* expected_value;
119 } modified_arguments[8];
120} test;
121
122// NOLINTBEGIN(bugprone-suspicious-missing-comma)
123static const test tests[] = {
124 { COMMAND_LINE_STATUS_PRINT_HELP,
125 check_settings_smartcard_no_redirection,
126 { "testfreerdp", "--help", nullptr },
127 { { 0 } } },
128 { COMMAND_LINE_STATUS_PRINT_HELP,
129 check_settings_smartcard_no_redirection,
130 { "testfreerdp", "/help", nullptr },
131 { { 0 } } },
132 { COMMAND_LINE_STATUS_PRINT_HELP,
133 check_settings_smartcard_no_redirection,
134 { "testfreerdp", "-help", nullptr },
135 { { 0 } } },
136 { COMMAND_LINE_STATUS_PRINT_VERSION,
137 check_settings_smartcard_no_redirection,
138 { "testfreerdp", "--version", nullptr },
139 { { 0 } } },
140 { COMMAND_LINE_STATUS_PRINT_VERSION,
141 check_settings_smartcard_no_redirection,
142 { "testfreerdp", "/version", nullptr },
143 { { 0 } } },
144 { COMMAND_LINE_STATUS_PRINT_VERSION,
145 check_settings_smartcard_no_redirection,
146 { "testfreerdp", "-version", nullptr },
147 { { 0 } } },
148 { 0,
149 check_settings_smartcard_no_redirection,
150 { "testfreerdp", "-v", "test.freerdp.com", nullptr },
151 { { 0 } } },
152 { 0,
153 check_settings_smartcard_no_redirection,
154 { "testfreerdp", "--v", "test.freerdp.com", nullptr },
155 { { 0 } } },
156 { 0,
157 check_settings_smartcard_no_redirection,
158 { "testfreerdp", "/v:test.freerdp.com", nullptr },
159 { { 0 } } },
160 { 0,
161 check_settings_smartcard_no_redirection,
162 { "testfreerdp", "/sound", "/drive:media," DRIVE_REDIRECT_PATH, "/v:test.freerdp.com",
163 nullptr },
164 { { 0 } } },
165 { 0,
166 check_settings_smartcard_no_redirection,
167 { "testfreerdp", "-u", "test", "-p", "test", "-v", "test.freerdp.com", nullptr },
168 { { 4, "****" }, { 0 } } },
169 { 0,
170 check_settings_smartcard_no_redirection,
171 { "testfreerdp", "/u:test", "/p:test", "/v:test.freerdp.com", nullptr },
172 { { 2, "/p:****" }, { 0 } } },
173 { COMMAND_LINE_ERROR_NO_KEYWORD,
174 check_settings_smartcard_no_redirection,
175 { "testfreerdp", "-invalid", nullptr },
176 { { 0 } } },
177 { COMMAND_LINE_ERROR_NO_KEYWORD,
178 check_settings_smartcard_no_redirection,
179 { "testfreerdp", "--invalid", nullptr },
180 { { 0 } } },
181#if defined(WITH_FREERDP_DEPRECATED_CMDLINE)
182 { COMMAND_LINE_STATUS_PRINT,
183 check_settings_smartcard_no_redirection,
184 { "testfreerdp", "/kbd-list", 0 },
185 { { 0 } } },
186 { COMMAND_LINE_STATUS_PRINT,
187 check_settings_smartcard_no_redirection,
188 { "testfreerdp", "/monitor-list", 0 },
189 { { 0 } } },
190#endif
191 { COMMAND_LINE_STATUS_PRINT,
192 check_settings_smartcard_no_redirection,
193 { "testfreerdp", "/list:kbd", nullptr },
194 { { 0 } } },
195 { COMMAND_LINE_STATUS_PRINT,
196 check_settings_smartcard_no_redirection,
197 { "testfreerdp", "/list:monitor", nullptr },
198 { { 0 } } },
199 { 0,
200 check_settings_smartcard_no_redirection,
201 { "testfreerdp", "/sound", "/drive:media:" DRIVE_REDIRECT_PATH, "/v:test.freerdp.com",
202 nullptr },
203 { { 0 } } },
204 { 0,
205 check_settings_smartcard_no_redirection,
206 { "testfreerdp", "/sound", "/drive:media,/foo/bar/blabla", "/v:test.freerdp.com", nullptr },
207 { { 0 } } },
208};
209// NOLINTEND(bugprone-suspicious-missing-comma)
210
211static void check_modified_arguments(const test* test, char** command_line, int* rc)
212{
213 const char* expected_argument = nullptr;
214
215 for (int k = 0; (expected_argument = test->modified_arguments[k].expected_value); k++)
216 {
217 int index = test->modified_arguments[k].index;
218 char* actual_argument = command_line[index];
219
220 if (0 != strcmp(actual_argument, expected_argument))
221 {
222 printref();
223 printf("Failure: overridden argument %d is %s but it should be %s\n", index,
224 actual_argument, expected_argument);
225 (void)fflush(stdout);
226 *rc = -1;
227 }
228 }
229}
230
231int TestClientCmdLine(int argc, char* argv[])
232{
233 int rc = 0;
234
235 WINPR_UNUSED(argc);
236 WINPR_UNUSED(argv);
237 for (size_t i = 0; i < ARRAYSIZE(tests); i++)
238 {
239 const test* current = &tests[i];
240 int failure = 0;
241 char** command_line = string_list_copy(current->command_line);
242
243 const int len = string_list_length((const char* const*)command_line);
244 if (!testcase(__func__, command_line, WINPR_ASSERTING_INT_CAST(size_t, len),
245 current->expected_status, current->validate_settings))
246 {
247 TEST_FAILURE("parsing arguments.\n");
248 failure = 1;
249 }
250
251 check_modified_arguments(current, command_line, &failure);
252
253 if (failure)
254 {
255 string_list_print(stdout, (const char* const*)command_line);
256 rc = -1;
257 }
258
259 string_list_free(command_line);
260 }
261
262 return rc;
263}
FREERDP_API void freerdp_settings_free(rdpSettings *settings)
Free a settings struct with all data in it.
WINPR_ATTR_NODISCARD FREERDP_API rdpSettings * freerdp_settings_new(DWORD flags)
creates a new setting struct
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.