FreeRDP
TestAddinArgv.c
1 #include <winpr/crt.h>
2 #include <winpr/crypto.h>
3 
4 #include <freerdp/settings.h>
5 
6 static BOOL test_alloc(void)
7 {
8  BOOL rc = FALSE;
9  int rng = 0;
10  const char* param[] = { "foo:", "bar", "bla", "rdp", NULL };
11  ADDIN_ARGV* arg1 = NULL;
12  ADDIN_ARGV* arg2 = NULL;
13  ADDIN_ARGV* arg3 = NULL;
14  ADDIN_ARGV* arg4 = NULL;
15 
16  /* Test empty allocation */
17  arg1 = freerdp_addin_argv_new(0, NULL);
18  if (!arg1 || (arg1->argc != 0) || (arg1->argv))
19  goto fail;
20 
21  /* Test allocation without initializing arguments of random size > 0 */
22  winpr_RAND(&rng, sizeof(rng));
23  rng = abs(rng % 8192) + 1;
24 
25  arg2 = freerdp_addin_argv_new(rng, NULL);
26  if (!arg2 || (arg2->argc != rng) || (!arg2->argv))
27  goto fail;
28  for (int x = 0; x < arg2->argc; x++)
29  {
30  if (arg2->argv[x])
31  goto fail;
32  }
33 
34  /* Test allocation with initializing arguments of size > 0 */
35  arg3 = freerdp_addin_argv_new(ARRAYSIZE(param) - 1, param);
36  if (!arg3 || (arg3->argc != ARRAYSIZE(param) - 1) || (!arg3->argv))
37  goto fail;
38 
39  for (int x = 0; x < arg3->argc; x++)
40  {
41  if (strcmp(arg3->argv[x], param[x]) != 0)
42  goto fail;
43  }
44 
45  /* Input lists with NULL elements are not allowed */
46  arg4 = freerdp_addin_argv_new(ARRAYSIZE(param), param);
47  if (arg4)
48  goto fail;
49  rc = TRUE;
50 fail:
51  freerdp_addin_argv_free(arg1);
52  freerdp_addin_argv_free(arg2);
53  freerdp_addin_argv_free(arg3);
54  freerdp_addin_argv_free(arg4);
55  printf("%s: %d\n", __func__, rc);
56  return rc;
57 }
58 
59 static BOOL test_clone(void)
60 {
61  BOOL rc = FALSE;
62  const char* param[] = { "foo:", "bar", "bla", "rdp" };
63  ADDIN_ARGV* arg = NULL;
64  ADDIN_ARGV* clone = NULL;
65  ADDIN_ARGV* clone2 = NULL;
66 
67  arg = freerdp_addin_argv_new(ARRAYSIZE(param), param);
68  if (!arg || (arg->argc != ARRAYSIZE(param)))
69  goto fail;
70  clone = freerdp_addin_argv_clone(arg);
71  if (!clone || (clone->argc != arg->argc))
72  goto fail;
73 
74  for (int x = 0; x < arg->argc; x++)
75  {
76  if (strcmp(param[x], arg->argv[x]) != 0)
77  goto fail;
78  if (strcmp(param[x], clone->argv[x]) != 0)
79  goto fail;
80  }
81 
82  clone2 = freerdp_addin_argv_clone(NULL);
83  if (clone2)
84  goto fail;
85  rc = TRUE;
86 fail:
87  freerdp_addin_argv_free(arg);
88  freerdp_addin_argv_free(clone);
89  freerdp_addin_argv_free(clone2);
90  printf("%s: %d\n", __func__, rc);
91  return rc;
92 }
93 
94 static BOOL test_add_remove(void)
95 {
96  const char* args[] = { "foo", "bar", "bla", "gaga" };
97  BOOL rc = FALSE;
98  ADDIN_ARGV* arg = freerdp_addin_argv_new(0, NULL);
99 
100  if (!arg || (arg->argc != 0) || arg->argv)
101  goto fail;
102  for (size_t y = 0; y < ARRAYSIZE(args); y++)
103  {
104  const char* param = args[y];
105  if (!freerdp_addin_argv_add_argument(arg, param))
106  goto fail;
107  if (arg->argc != (int)y + 1)
108  goto fail;
109  if (!arg->argv)
110  goto fail;
111  if (strcmp(arg->argv[y], param) != 0)
112  goto fail;
113  }
114 
115  /* Try to remove non existing element, must not return TRUE */
116  if (freerdp_addin_argv_del_argument(arg, "foobar"))
117  goto fail;
118 
119  /* Invalid parameters, must return FALSE */
120  if (freerdp_addin_argv_del_argument(NULL, "foobar"))
121  goto fail;
122 
123  /* Invalid parameters, must return FALSE */
124  if (freerdp_addin_argv_del_argument(arg, NULL))
125  goto fail;
126 
127  /* Remove elements one by one to test argument index move */
128  for (size_t y = 0; y < ARRAYSIZE(args); y++)
129  {
130  const char* param = args[y];
131  if (!freerdp_addin_argv_del_argument(arg, param))
132  goto fail;
133  for (size_t x = y + 1; x < ARRAYSIZE(args); x++)
134  {
135  if (strcmp(arg->argv[x - y - 1], args[x]) != 0)
136  goto fail;
137  }
138  }
139  rc = TRUE;
140 fail:
141  freerdp_addin_argv_free(arg);
142  printf("%s: %d\n", __func__, rc);
143  return rc;
144 }
145 
146 static BOOL test_set_argument(void)
147 {
148  int ret = 0;
149  const char* newarg = "foobar";
150  const char* args[] = { "foo", "bar", "bla", "gaga" };
151  BOOL rc = FALSE;
152  ADDIN_ARGV* arg = NULL;
153 
154  arg = freerdp_addin_argv_new(ARRAYSIZE(args), args);
155  if (!arg || (arg->argc != ARRAYSIZE(args)) || !arg->argv)
156  goto fail;
157 
158  /* Check invalid parameters */
159  ret = freerdp_addin_set_argument(NULL, "foo");
160  if (ret >= 0)
161  goto fail;
162  ret = freerdp_addin_set_argument(arg, NULL);
163  if (ret >= 0)
164  goto fail;
165 
166  /* Try existing parameter */
167  ret = freerdp_addin_set_argument(arg, "foo");
168  if ((ret != 1) || (arg->argc != ARRAYSIZE(args)))
169  goto fail;
170 
171  /* Try new parameter */
172  ret = freerdp_addin_set_argument(arg, newarg);
173  if ((ret != 0) || (arg->argc != ARRAYSIZE(args) + 1) ||
174  (strcmp(newarg, arg->argv[ARRAYSIZE(args)]) != 0))
175  goto fail;
176 
177  rc = TRUE;
178 fail:
179  freerdp_addin_argv_free(arg);
180  printf("%s: %d\n", __func__, rc);
181  return rc;
182 }
183 
184 static BOOL test_replace_argument(void)
185 {
186  int ret = 0;
187  const char* newarg = "foobar";
188  const char* args[] = { "foo", "bar", "bla", "gaga" };
189  BOOL rc = FALSE;
190  ADDIN_ARGV* arg = NULL;
191 
192  arg = freerdp_addin_argv_new(ARRAYSIZE(args), args);
193  if (!arg || (arg->argc != ARRAYSIZE(args)) || !arg->argv)
194  goto fail;
195 
196  /* Check invalid parameters */
197  ret = freerdp_addin_replace_argument(NULL, "foo", newarg);
198  if (ret >= 0)
199  goto fail;
200  ret = freerdp_addin_replace_argument(arg, NULL, newarg);
201  if (ret >= 0)
202  goto fail;
203  ret = freerdp_addin_replace_argument(arg, "foo", NULL);
204  if (ret >= 0)
205  goto fail;
206 
207  /* Try existing parameter */
208  ret = freerdp_addin_replace_argument(arg, "foo", newarg);
209  if ((ret != 1) || (arg->argc != ARRAYSIZE(args)) || (strcmp(arg->argv[0], newarg) != 0))
210  goto fail;
211 
212  /* Try new parameter */
213  ret = freerdp_addin_replace_argument(arg, "lalala", newarg);
214  if ((ret != 0) || (arg->argc != ARRAYSIZE(args) + 1) ||
215  (strcmp(newarg, arg->argv[ARRAYSIZE(args)]) != 0))
216  goto fail;
217 
218  rc = TRUE;
219 fail:
220  freerdp_addin_argv_free(arg);
221  printf("%s: %d\n", __func__, rc);
222  return rc;
223 }
224 
225 static BOOL test_set_argument_value(void)
226 {
227  int ret = 0;
228  const char* newarg1 = "foobar";
229  const char* newarg2 = "lalala";
230  const char* fullnewarg1 = "foo:foobar";
231  const char* fullnewarg2 = "foo:lalala";
232  const char* fullnewvalue = "lalala:foobar";
233  const char* args[] = { "foo", "foo:", "bar", "bla", "gaga" };
234  BOOL rc = FALSE;
235  ADDIN_ARGV* arg = NULL;
236 
237  arg = freerdp_addin_argv_new(ARRAYSIZE(args), args);
238  if (!arg || (arg->argc != ARRAYSIZE(args)) || !arg->argv)
239  goto fail;
240 
241  /* Check invalid parameters */
242  ret = freerdp_addin_set_argument_value(NULL, "foo", newarg1);
243  if (ret >= 0)
244  goto fail;
245  ret = freerdp_addin_set_argument_value(arg, NULL, newarg1);
246  if (ret >= 0)
247  goto fail;
248  ret = freerdp_addin_set_argument_value(arg, "foo", NULL);
249  if (ret >= 0)
250  goto fail;
251 
252  /* Try existing parameter */
253  ret = freerdp_addin_set_argument_value(arg, "foo", newarg1);
254  if ((ret != 1) || (arg->argc != ARRAYSIZE(args)) || (strcmp(arg->argv[1], fullnewarg1) != 0))
255  goto fail;
256  ret = freerdp_addin_set_argument_value(arg, "foo", newarg2);
257  if ((ret != 1) || (arg->argc != ARRAYSIZE(args)) || (strcmp(arg->argv[1], fullnewarg2) != 0))
258  goto fail;
259 
260  /* Try new parameter */
261  ret = freerdp_addin_set_argument_value(arg, newarg2, newarg1);
262  if ((ret != 0) || (arg->argc != ARRAYSIZE(args) + 1) ||
263  (strcmp(fullnewvalue, arg->argv[ARRAYSIZE(args)]) != 0))
264  goto fail;
265 
266  rc = TRUE;
267 fail:
268  freerdp_addin_argv_free(arg);
269  printf("%s: %d\n", __func__, rc);
270  return rc;
271 }
272 
273 static BOOL test_replace_argument_value(void)
274 {
275  int ret = 0;
276  const char* newarg1 = "foobar";
277  const char* newarg2 = "lalala";
278  const char* fullnewarg1 = "foo:foobar";
279  const char* fullnewarg2 = "foo:lalala";
280  const char* fullnewvalue = "lalala:foobar";
281  const char* args[] = { "foo", "foo:", "bar", "bla", "gaga" };
282  BOOL rc = FALSE;
283  ADDIN_ARGV* arg = NULL;
284 
285  arg = freerdp_addin_argv_new(ARRAYSIZE(args), args);
286  if (!arg || (arg->argc != ARRAYSIZE(args)) || !arg->argv)
287  goto fail;
288 
289  /* Check invalid parameters */
290  ret = freerdp_addin_replace_argument_value(NULL, "bar", "foo", newarg1);
291  if (ret >= 0)
292  goto fail;
293  ret = freerdp_addin_replace_argument_value(arg, NULL, "foo", newarg1);
294  if (ret >= 0)
295  goto fail;
296  ret = freerdp_addin_replace_argument_value(arg, "foo", NULL, newarg1);
297  if (ret >= 0)
298  goto fail;
299  ret = freerdp_addin_replace_argument_value(arg, "bar", "foo", NULL);
300  if (ret >= 0)
301  goto fail;
302 
303  /* Try existing parameter */
304  ret = freerdp_addin_replace_argument_value(arg, "bla", "foo", newarg1);
305  if ((ret != 1) || (arg->argc != ARRAYSIZE(args)) || (strcmp(arg->argv[3], fullnewarg1) != 0))
306  goto fail;
307  ret = freerdp_addin_replace_argument_value(arg, "foo", "foo", newarg2);
308  if ((ret != 1) || (arg->argc != ARRAYSIZE(args)) || (strcmp(arg->argv[0], fullnewarg2) != 0))
309  goto fail;
310 
311  /* Try new parameter */
312  ret = freerdp_addin_replace_argument_value(arg, "hahaha", newarg2, newarg1);
313  if ((ret != 0) || (arg->argc != ARRAYSIZE(args) + 1) ||
314  (strcmp(fullnewvalue, arg->argv[ARRAYSIZE(args)]) != 0))
315  goto fail;
316 
317  rc = TRUE;
318 fail:
319  freerdp_addin_argv_free(arg);
320  printf("%s: %d\n", __func__, rc);
321  return rc;
322 }
323 
324 int TestAddinArgv(int argc, char* argv[])
325 {
326 
327  WINPR_UNUSED(argc);
328  WINPR_UNUSED(argv);
329 
330  if (!test_alloc())
331  return -1;
332  if (!test_clone())
333  return -1;
334  if (!test_add_remove())
335  return -1;
336  if (!test_set_argument())
337  return -1;
338  if (!test_replace_argument())
339  return -1;
340  if (!test_set_argument_value())
341  return -1;
342  if (!test_replace_argument_value())
343  return -1;
344  return 0;
345 }