FreeRDP
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 
9 typedef 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 
31 static 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 
43 static 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 
91 static 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 
110 typedef 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)
123 static const test tests[] = {
124  { COMMAND_LINE_STATUS_PRINT_HELP,
125  check_settings_smartcard_no_redirection,
126  { "testfreerdp", "--help", 0 },
127  { { 0 } } },
128  { COMMAND_LINE_STATUS_PRINT_HELP,
129  check_settings_smartcard_no_redirection,
130  { "testfreerdp", "/help", 0 },
131  { { 0 } } },
132  { COMMAND_LINE_STATUS_PRINT_HELP,
133  check_settings_smartcard_no_redirection,
134  { "testfreerdp", "-help", 0 },
135  { { 0 } } },
136  { COMMAND_LINE_STATUS_PRINT_VERSION,
137  check_settings_smartcard_no_redirection,
138  { "testfreerdp", "--version", 0 },
139  { { 0 } } },
140  { COMMAND_LINE_STATUS_PRINT_VERSION,
141  check_settings_smartcard_no_redirection,
142  { "testfreerdp", "/version", 0 },
143  { { 0 } } },
144  { COMMAND_LINE_STATUS_PRINT_VERSION,
145  check_settings_smartcard_no_redirection,
146  { "testfreerdp", "-version", 0 },
147  { { 0 } } },
148  { 0,
149  check_settings_smartcard_no_redirection,
150  { "testfreerdp", "-v", "test.freerdp.com", 0 },
151  { { 0 } } },
152  { 0,
153  check_settings_smartcard_no_redirection,
154  { "testfreerdp", "--v", "test.freerdp.com", 0 },
155  { { 0 } } },
156  { 0,
157  check_settings_smartcard_no_redirection,
158  { "testfreerdp", "/v:test.freerdp.com", 0 },
159  { { 0 } } },
160  { 0,
161  check_settings_smartcard_no_redirection,
162  { "testfreerdp", "/sound", "/drive:media," DRIVE_REDIRECT_PATH, "/v:test.freerdp.com", 0 },
163  { { 0 } } },
164  { 0,
165  check_settings_smartcard_no_redirection,
166  { "testfreerdp", "-u", "test", "-p", "test", "-v", "test.freerdp.com", 0 },
167  { { 4, "****" }, { 0 } } },
168  { 0,
169  check_settings_smartcard_no_redirection,
170  { "testfreerdp", "/u:test", "/p:test", "/v:test.freerdp.com", 0 },
171  { { 2, "/p:****" }, { 0 } } },
172  { COMMAND_LINE_ERROR_NO_KEYWORD,
173  check_settings_smartcard_no_redirection,
174  { "testfreerdp", "-invalid", 0 },
175  { { 0 } } },
176  { COMMAND_LINE_ERROR_NO_KEYWORD,
177  check_settings_smartcard_no_redirection,
178  { "testfreerdp", "--invalid", 0 },
179  { { 0 } } },
180 #if defined(WITH_FREERDP_DEPRECATED_CMDLINE)
181  { COMMAND_LINE_STATUS_PRINT,
182  check_settings_smartcard_no_redirection,
183  { "testfreerdp", "/kbd-list", 0 },
184  { { 0 } } },
185  { COMMAND_LINE_STATUS_PRINT,
186  check_settings_smartcard_no_redirection,
187  { "testfreerdp", "/monitor-list", 0 },
188  { { 0 } } },
189 #endif
190  { COMMAND_LINE_STATUS_PRINT,
191  check_settings_smartcard_no_redirection,
192  { "testfreerdp", "/list:kbd", 0 },
193  { { 0 } } },
194  { COMMAND_LINE_STATUS_PRINT,
195  check_settings_smartcard_no_redirection,
196  { "testfreerdp", "/list:monitor", 0 },
197  { { 0 } } },
198  { 0,
199  check_settings_smartcard_no_redirection,
200  { "testfreerdp", "/sound", "/drive:media:" DRIVE_REDIRECT_PATH, "/v:test.freerdp.com", 0 },
201  { { 0 } } },
202  { 0,
203  check_settings_smartcard_no_redirection,
204  { "testfreerdp", "/sound", "/drive:media,/foo/bar/blabla", "/v:test.freerdp.com", 0 },
205  { { 0 } } },
206 
207 #if 0
208  {
209  COMMAND_LINE_STATUS_PRINT, check_settings_smartcard_no_redirection,
210  {"testfreerdp", "-z", "--plugin", "cliprdr", "--plugin", "rdpsnd", "--data", "alsa", "latency:100", "--", "--plugin", "rdpdr", "--data", "disk:w7share:/home/w7share", "--", "--plugin", "drdynvc", "--data", "tsmf:decoder:gstreamer", "--", "-u", "test", "host.example.com", 0},
211  {{0}}
212  },
213 #endif
214 };
215 // NOLINTEND(bugprone-suspicious-missing-comma)
216 
217 static void check_modified_arguments(const test* test, char** command_line, int* rc)
218 {
219  const char* expected_argument = NULL;
220 
221  for (int k = 0; (expected_argument = test->modified_arguments[k].expected_value); k++)
222  {
223  int index = test->modified_arguments[k].index;
224  char* actual_argument = command_line[index];
225 
226  if (0 != strcmp(actual_argument, expected_argument))
227  {
228  printref();
229  printf("Failure: overridden argument %d is %s but it should be %s\n", index,
230  actual_argument, expected_argument);
231  (void)fflush(stdout);
232  *rc = -1;
233  }
234  }
235 }
236 
237 int TestClientCmdLine(int argc, char* argv[])
238 {
239  int rc = 0;
240 
241  WINPR_UNUSED(argc);
242  WINPR_UNUSED(argv);
243  for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
244  {
245  const test* current = &tests[i];
246  int failure = 0;
247  char** command_line = string_list_copy(current->command_line);
248 
249  if (!testcase(__func__, command_line, string_list_length((const char* const*)command_line),
250  current->expected_status, current->validate_settings))
251  {
252  TEST_FAILURE("parsing arguments.\n");
253  failure = 1;
254  }
255 
256  check_modified_arguments(current, command_line, &failure);
257 
258  if (failure)
259  {
260  string_list_print(stdout, (const char* const*)command_line);
261  rc = -1;
262  }
263 
264  string_list_free(command_line);
265  }
266 
267  return rc;
268 }
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API rdpSettings * freerdp_settings_new(DWORD flags)
creates a new setting struct
FREERDP_API void freerdp_settings_free(rdpSettings *settings)
Free a settings struct with all data in it.