winprefs v0.3.2
A registry exporter for programmers.
Loading...
Searching...
No Matches
test_main.c
1#include "io.h"
2
3int wmain(int argc, wchar_t *argv[]);
4
5static void test_main_help(void **state) {
6 wchar_t **buf = calloc(2, sizeof(wchar_t *));
7 buf[0] = calloc(8, sizeof(wchar_t));
8 buf[1] = calloc(10, sizeof(wchar_t));
9 wmemset(buf[0], L'\0', 8);
10 wmemset(buf[1], L'\0', 10);
11 wmemcpy(buf[0], L"winprefs", 7);
12 wmemcpy(buf[1], L"--help", 6);
13 int ret = wmain(2, buf);
14 free(buf[0]);
15 free(buf[1]);
16 free(buf);
17
18 assert_int_equal(ret, EXIT_SUCCESS);
19}
20
21static void test_main_invalid_option(void **state) {
22 wchar_t **buf = calloc(2, sizeof(wchar_t *));
23 buf[0] = calloc(8, sizeof(wchar_t));
24 buf[1] = calloc(16, sizeof(wchar_t));
25 wmemset(buf[0], L'\0', 8);
26 wmemset(buf[1], L'\0', 16);
27 wmemcpy(buf[0], L"winprefs", 7);
28 wmemcpy(buf[1], L"--invalid-option", 16);
29 int ret = wmain(2, buf);
30 free(buf[0]);
31 free(buf[1]);
32 free(buf);
33
34 assert_int_equal(EXIT_FAILURE, ret);
35}
36
37static void test_main_reg_path_invalid(void **state) {
38 wchar_t **buf = calloc(3, sizeof(wchar_t *));
39 buf[0] = calloc(8, sizeof(wchar_t));
40 buf[1] = calloc(8, sizeof(wchar_t));
41 buf[2] = nullptr;
42 wmemset(buf[0], L'\0', 8);
43 wmemcpy(buf[0], L"winprefs", 7);
44 wmemset(buf[1], L'\0', 7);
45 wmemcpy(buf[1], L"ZZZZ:\\", 7);
46 int ret = wmain(2, buf);
47
48 assert_return_code(ret, EXIT_FAILURE);
49
50 free(buf[1]);
51 free(buf[0]);
52 free(buf);
53}
54
55static void test_main_reg_path_invalid_alt(void **state) {
56 wchar_t **buf = calloc(3, sizeof(wchar_t *));
57 buf[0] = calloc(8, sizeof(wchar_t));
58 buf[1] = calloc(6, sizeof(wchar_t));
59 buf[2] = nullptr;
60 wmemset(buf[0], L'\0', 8);
61 wmemcpy(buf[0], L"winprefs", 7);
62 wmemset(buf[1], L'\0', 6);
63 wmemcpy(buf[1], L"ZZZZ:", 5);
64 int ret = wmain(2, buf);
65
66 assert_return_code(ret, EXIT_FAILURE);
67
68 free(buf[1]);
69 free(buf[0]);
70 free(buf);
71}
72
73static void test_main_max_depth_conversion(void **state) {
74 wchar_t *app_data_folder = calloc(30, sizeof(wchar_t));
75 wmemset(app_data_folder, L'\0', 30);
76 wmemcpy(app_data_folder, L"\\c\\users\\name\\AppData\\Roaming", 29);
77 will_return(__wrap_SHGetFolderPath, app_data_folder);
78 will_return(__wrap_SHGetFolderPath, 0);
79 wchar_t *prefs_export_path = calloc(43, sizeof(wchar_t));
80 wmemset(prefs_export_path, L'\0', 43);
81 wmemcpy(prefs_export_path, app_data_folder, 29);
82 wcsncat(prefs_export_path, L"prefs-export", 12);
83 will_return(__wrap_PathAppend, prefs_export_path);
84 will_return(__wrap_PathAppend, true);
85
86 expect_value(__wrap_save_preferences, max_depth, 50);
87 expect_any(__wrap_save_preferences, commit);
88 expect_any(__wrap_save_preferences, format);
89 expect_any(__wrap_save_preferences, deploy_key);
90 expect_any(__wrap_save_preferences, hk);
91 expect_any(__wrap_save_preferences, output_dir);
92 expect_any(__wrap_save_preferences, output_file);
93 expect_any(__wrap_save_preferences, specified_path);
94 will_return(__wrap_save_preferences, true);
95
96 wchar_t **buf = calloc(4, sizeof(wchar_t *));
97 buf[0] = calloc(8, sizeof(wchar_t));
98 buf[1] = calloc(3, sizeof(wchar_t));
99 buf[2] = calloc(3, sizeof(wchar_t));
100 buf[3] = nullptr;
101 wmemset(buf[0], L'\0', 8);
102 wmemcpy(buf[0], L"winprefs", 7);
103 wmemset(buf[1], L'\0', 3);
104 wmemcpy(buf[1], L"-m", 2);
105 wmemset(buf[2], L'\0', 3);
106 wmemcpy(buf[2], L"50", 2);
107 int ret = wmain(3, buf);
108
109 assert_return_code(ret, EXIT_SUCCESS);
110
111 free(buf[0]);
112 free(buf[1]);
113 free(buf[2]);
114 free(buf);
115 free(app_data_folder);
116 free(prefs_export_path);
117}
118
119static void test_main_commit_arg(void **state) {
120 wchar_t *app_data_folder = calloc(30, sizeof(wchar_t));
121 wmemset(app_data_folder, L'\0', 30);
122 wmemcpy(app_data_folder, L"\\c\\users\\name\\AppData\\Roaming", 29);
123 will_return(__wrap_SHGetFolderPath, app_data_folder);
124 will_return(__wrap_SHGetFolderPath, 0);
125 wchar_t *prefs_export_path = calloc(43, sizeof(wchar_t));
126 wmemset(prefs_export_path, L'\0', 43);
127 wmemcpy(prefs_export_path, app_data_folder, 29);
128 wcsncat(prefs_export_path, L"prefs-export", 12);
129 will_return(__wrap_PathAppend, prefs_export_path);
130 will_return(__wrap_PathAppend, true);
131
132 expect_any(__wrap_save_preferences, max_depth);
133 expect_value(__wrap_save_preferences, commit, true);
134 expect_any(__wrap_save_preferences, format);
135 expect_any(__wrap_save_preferences, deploy_key);
136 expect_any(__wrap_save_preferences, hk);
137 expect_any(__wrap_save_preferences, output_dir);
138 expect_any(__wrap_save_preferences, output_file);
139 expect_any(__wrap_save_preferences, specified_path);
140 will_return(__wrap_save_preferences, true);
141
142 wchar_t **buf = calloc(3, sizeof(wchar_t *));
143 buf[0] = calloc(8, sizeof(wchar_t));
144 buf[1] = calloc(3, sizeof(wchar_t));
145 buf[2] = nullptr;
146 wmemset(buf[0], L'\0', 8);
147 wmemcpy(buf[0], L"winprefs", 7);
148 wmemset(buf[1], L'\0', 3);
149 wmemcpy(buf[1], L"-c", 2);
150 int ret = wmain(2, buf);
151
152 assert_return_code(ret, EXIT_SUCCESS);
153
154 free(buf[0]);
155 free(buf[1]);
156 free(buf);
157 free(app_data_folder);
158 free(prefs_export_path);
159}
160
161static void test_main_output_dir_arg(void **state) {
162 expect_any(__wrap_save_preferences, max_depth);
163 expect_any(__wrap_save_preferences, commit);
164 expect_any(__wrap_save_preferences, format);
165 expect_any(__wrap_save_preferences, deploy_key);
166 expect_any(__wrap_save_preferences, hk);
167 expect_any(__wrap_save_preferences, output_file);
168 expect_any(__wrap_save_preferences, specified_path);
169 will_return(__wrap_save_preferences, true);
170
171 wchar_t **buf = calloc(4, sizeof(wchar_t *));
172 buf[0] = calloc(8, sizeof(wchar_t));
173 buf[1] = calloc(3, sizeof(wchar_t));
174 buf[2] = calloc(7, sizeof(wchar_t));
175 buf[3] = nullptr;
176 wmemset(buf[0], L'\0', 8);
177 wmemcpy(buf[0], L"winprefs", 7);
178 wmemset(buf[1], L'\0', 3);
179 wmemcpy(buf[1], L"-o", 2);
180 wmemset(buf[2], L'\0', 7);
181 wmemcpy(buf[2], L"output", 6);
182 expect_memory(__wrap_save_preferences, output_dir, buf[2], 6 * sizeof(wchar_t));
183 int ret = wmain(3, buf);
184
185 assert_return_code(ret, EXIT_SUCCESS);
186
187 free(buf[0]);
188 free(buf[1]);
189 free(buf[2]);
190 free(buf);
191}
192
193static void test_main_debug_arg(void **state) {
194 wchar_t *app_data_folder = calloc(30, sizeof(wchar_t));
195 wmemset(app_data_folder, L'\0', 30);
196 wmemcpy(app_data_folder, L"\\c\\users\\name\\AppData\\Roaming", 29);
197 will_return(__wrap_SHGetFolderPath, app_data_folder);
198 will_return(__wrap_SHGetFolderPath, 0);
199 wchar_t *prefs_export_path = calloc(43, sizeof(wchar_t));
200 wmemset(prefs_export_path, L'\0', 43);
201 wmemcpy(prefs_export_path, app_data_folder, 29);
202 wcsncat(prefs_export_path, L"prefs-export", 12);
203 will_return(__wrap_PathAppend, prefs_export_path);
204 will_return(__wrap_PathAppend, true);
205
206 expect_any(__wrap_save_preferences, max_depth);
207 expect_any(__wrap_save_preferences, commit);
208 expect_any(__wrap_save_preferences, format);
209 expect_any(__wrap_save_preferences, deploy_key);
210 expect_any(__wrap_save_preferences, hk);
211 expect_any(__wrap_save_preferences, output_dir);
212 expect_any(__wrap_save_preferences, output_file);
213 expect_any(__wrap_save_preferences, specified_path);
214 will_return(__wrap_save_preferences, true);
215
216 wchar_t **buf = calloc(3, sizeof(wchar_t *));
217 buf[0] = calloc(8, sizeof(wchar_t));
218 buf[1] = calloc(3, sizeof(wchar_t));
219 buf[2] = nullptr;
220 wmemset(buf[0], L'\0', 8);
221 wmemcpy(buf[0], L"winprefs", 7);
222 wmemset(buf[1], L'\0', 3);
223 wmemcpy(buf[1], L"-d", 2);
224 debug_print_enabled = false;
225 int ret = wmain(2, buf);
226
227 assert_return_code(ret, EXIT_SUCCESS);
228 assert_true(debug_print_enabled);
229 debug_print_enabled = false;
230
231 free(buf[0]);
232 free(buf[1]);
233 free(buf);
234 free(app_data_folder);
235 free(prefs_export_path);
236}
237
238void test_main_deploy_key_arg(void **state) {
239 wchar_t *app_data_folder = calloc(30, sizeof(wchar_t));
240 wmemset(app_data_folder, L'\0', 30);
241 wmemcpy(app_data_folder, L"\\c\\users\\name\\AppData\\Roaming", 29);
242 will_return(__wrap_SHGetFolderPath, app_data_folder);
243 will_return(__wrap_SHGetFolderPath, 0);
244 wchar_t *prefs_export_path = calloc(43, sizeof(wchar_t));
245 wmemset(prefs_export_path, L'\0', 43);
246 wmemcpy(prefs_export_path, app_data_folder, 29);
247 wcsncat(prefs_export_path, L"prefs-export", 12);
248 will_return(__wrap_PathAppend, prefs_export_path);
249 will_return(__wrap_PathAppend, true);
250
251 expect_any(__wrap_save_preferences, max_depth);
252 expect_any(__wrap_save_preferences, commit);
253 expect_any(__wrap_save_preferences, format);
254 expect_memory(__wrap_save_preferences, deploy_key, L"file.key", 8);
255 expect_any(__wrap_save_preferences, hk);
256 expect_any(__wrap_save_preferences, output_dir);
257 expect_any(__wrap_save_preferences, output_file);
258 expect_any(__wrap_save_preferences, specified_path);
259 will_return(__wrap_save_preferences, true);
260
261 wchar_t **buf = calloc(4, sizeof(wchar_t *));
262 buf[0] = calloc(8, sizeof(wchar_t));
263 buf[1] = calloc(3, sizeof(wchar_t));
264 buf[2] = calloc(9, sizeof(wchar_t));
265 buf[3] = nullptr;
266 wmemset(buf[0], L'\0', 8);
267 wmemcpy(buf[0], L"winprefs", 7);
268 wmemset(buf[1], L'\0', 3);
269 wmemcpy(buf[1], L"-K", 2);
270 wmemset(buf[2], L'\0', 9);
271 wmemcpy(buf[2], L"file.key", 8);
272 int ret = wmain(3, buf);
273
274 assert_return_code(ret, EXIT_SUCCESS);
275 debug_print_enabled = false;
276
277 free(buf[0]);
278 free(buf[1]);
279 free(buf[2]);
280 free(buf);
281 free(app_data_folder);
282 free(prefs_export_path);
283}
284
285void test_main_output_file_arg(void **state) {
286 wchar_t *app_data_folder = calloc(30, sizeof(wchar_t));
287 wmemset(app_data_folder, L'\0', 30);
288 wmemcpy(app_data_folder, L"\\c\\users\\name\\AppData\\Roaming", 29);
289 will_return(__wrap_SHGetFolderPath, app_data_folder);
290 will_return(__wrap_SHGetFolderPath, 0);
291 wchar_t *prefs_export_path = calloc(43, sizeof(wchar_t));
292 wmemset(prefs_export_path, L'\0', 43);
293 wmemcpy(prefs_export_path, app_data_folder, 29);
294 wcsncat(prefs_export_path, L"prefs-export", 12);
295 will_return(__wrap_PathAppend, prefs_export_path);
296 will_return(__wrap_PathAppend, true);
297
298 expect_any(__wrap_save_preferences, max_depth);
299 expect_any(__wrap_save_preferences, commit);
300 expect_any(__wrap_save_preferences, format);
301 expect_any(__wrap_save_preferences, deploy_key);
302 expect_any(__wrap_save_preferences, hk);
303 expect_any(__wrap_save_preferences, output_dir);
304 expect_memory(__wrap_save_preferences, output_file, L"exec.reg", 8);
305 expect_any(__wrap_save_preferences, specified_path);
306 will_return(__wrap_save_preferences, true);
307
308 wchar_t **buf = calloc(4, sizeof(wchar_t *));
309 buf[0] = calloc(8, sizeof(wchar_t));
310 buf[1] = calloc(3, sizeof(wchar_t));
311 buf[2] = calloc(9, sizeof(wchar_t));
312 buf[3] = nullptr;
313 wmemset(buf[0], L'\0', 8);
314 wmemcpy(buf[0], L"winprefs", 7);
315 wmemset(buf[1], L'\0', 3);
316 wmemcpy(buf[1], L"-f", 2);
317 wmemset(buf[2], L'\0', 9);
318 wmemcpy(buf[2], L"exec.reg", 8);
319 int ret = wmain(3, buf);
320
321 assert_return_code(ret, EXIT_SUCCESS);
322 debug_print_enabled = false;
323
324 free(buf[0]);
325 free(buf[1]);
326 free(buf[2]);
327 free(buf);
328 free(app_data_folder);
329 free(prefs_export_path);
330}
331
332static const wchar_t *C_SHARP_FORMAT_NAMES[] = {L"cs", L"c#", L"csharp"};
333
334static void test_main_format_arg_c_sharp(void **state) {
335 wchar_t *app_data_folder = calloc(30, sizeof(wchar_t));
336 wmemset(app_data_folder, L'\0', 30);
337 wmemcpy(app_data_folder, L"\\c\\users\\name\\AppData\\Roaming", 29);
338 wchar_t *prefs_export_path = calloc(43, sizeof(wchar_t));
339 wmemset(prefs_export_path, L'\0', 43);
340 wmemcpy(prefs_export_path, app_data_folder, 29);
341 wcsncat(prefs_export_path, L"prefs-export", 12);
342
343 expect_any_always(__wrap_save_preferences, max_depth);
344 expect_any_always(__wrap_save_preferences, commit);
345 expect_any_always(__wrap_save_preferences, deploy_key);
346 expect_any_always(__wrap_save_preferences, hk);
347 expect_any_always(__wrap_save_preferences, output_dir);
348 expect_any_always(__wrap_save_preferences, output_file);
349 expect_any_always(__wrap_save_preferences, specified_path);
350 will_return_always(__wrap_save_preferences, true);
351
352 long unsigned int i;
353 wchar_t **buf = calloc(4, sizeof(wchar_t *));
354 buf[0] = calloc(8, sizeof(wchar_t));
355 buf[1] = calloc(3, sizeof(wchar_t));
356 buf[2] = calloc(7, sizeof(wchar_t));
357 buf[3] = nullptr;
358 wmemset(buf[0], L'\0', 8);
359 wmemcpy(buf[0], L"winprefs", 7);
360 wmemset(buf[1], L'\0', 3);
361 wmemcpy(buf[1], L"-F", 2);
362 for (i = 0; i < ARRAY_SIZE(C_SHARP_FORMAT_NAMES); i++) {
363 will_return(__wrap_SHGetFolderPath, app_data_folder);
364 will_return(__wrap_SHGetFolderPath, 0);
365 will_return(__wrap_PathAppend, prefs_export_path);
366 will_return(__wrap_PathAppend, true);
367
368 expect_value(__wrap_save_preferences, format, OUTPUT_FORMAT_C_SHARP);
369 wmemset(buf[2], L'\0', 7);
370 wmemcpy(buf[2], C_SHARP_FORMAT_NAMES[i], wcslen(C_SHARP_FORMAT_NAMES[i]));
371 int ret = wmain(3, buf);
372 assert_return_code(ret, EXIT_SUCCESS);
373 }
374
375 free(buf[0]);
376 free(buf[1]);
377 free(buf[2]);
378 free(buf);
379 free(app_data_folder);
380 free(prefs_export_path);
381}
382
383static void test_main_format_arg_invalid(void **state) {
384 wchar_t **buf = calloc(4, sizeof(wchar_t *));
385 buf[0] = calloc(8, sizeof(wchar_t));
386 buf[1] = calloc(3, sizeof(wchar_t));
387 buf[2] = calloc(8, sizeof(wchar_t));
388 buf[3] = nullptr;
389 wmemset(buf[0], L'\0', 8);
390 wmemcpy(buf[0], L"winprefs", 10);
391 wmemset(buf[1], L'\0', 3);
392 wmemcpy(buf[1], L"-F", 2);
393 wmemset(buf[2], L'\0', 8);
394 wmemcpy(buf[2], L"unknown", 7);
395 int ret = wmain(3, buf);
396 assert_return_code(ret, EXIT_FAILURE);
397
398 free(buf[0]);
399 free(buf[1]);
400 free(buf);
401}
402
403static const wchar_t *FULL_REG_PATH_SUFFIX = L":\\Software\\Microsoft\\Windows NT\\some value name";
404static const HKEY possible_hkey[] = {HKEY_CLASSES_ROOT,
405 HKEY_CURRENT_CONFIG,
406 HKEY_CURRENT_USER,
407 HKEY_DYN_DATA,
408 HKEY_LOCAL_MACHINE,
409 HKEY_USERS};
410static const wchar_t *possible_short_paths[] = {
411 L"HKCR", L"HKCC", L"HKCU", L"HKDD", L"HKLM", L"HKU"};
412
413static void test_main_format_arg_full_reg_path(void **state) {
414 will_return_always(__wrap_RegOpenKeyEx, 9999999);
415
416 expect_value_count(__wrap_export_single_value, format, OUTPUT_FORMAT_REG, -1);
417 will_return_count(__wrap_export_single_value, true, 3);
418 will_return_count(__wrap_export_single_value, false, 3);
419
420 size_t full_reg_path_len = wcslen(FULL_REG_PATH_SUFFIX);
421 wchar_t **buf = calloc(3, sizeof(wchar_t *));
422 buf[0] = calloc(9, sizeof(wchar_t));
423 buf[1] = calloc(full_reg_path_len + 6, sizeof(wchar_t));
424 buf[2] = nullptr;
425 wmemset(buf[0], L'\0', 9);
426 wmemcpy(buf[0], L"winprefs", 8);
427
428 long unsigned int i;
429 for (i = 0; i < ARRAY_SIZE(possible_hkey); i++) {
430 expect_value(__wrap_export_single_value, top_key, possible_hkey[i]);
431 wmemset(buf[1], L'\0', full_reg_path_len + 6);
432 _snwprintf(buf[1],
433 full_reg_path_len + 5,
434 L"%ls%ls",
435 possible_short_paths[i],
436 FULL_REG_PATH_SUFFIX);
437 expect_string(__wrap_export_single_value, reg_path, buf[1]);
438 int ret = wmain(2, buf);
439 assert_return_code(ret, i > 2 ? EXIT_FAILURE : EXIT_SUCCESS);
440 }
441
442 free(buf[0]);
443 free(buf[1]);
444 free(buf);
445}
446
447static void test_main_format_arg_reg(void **state) {
448 wchar_t *app_data_folder = calloc(30, sizeof(wchar_t));
449 wmemset(app_data_folder, L'\0', 30);
450 wmemcpy(app_data_folder, L"\\c\\users\\name\\AppData\\Roaming", 29);
451 wchar_t *prefs_export_path = calloc(43, sizeof(wchar_t));
452 wmemset(prefs_export_path, L'\0', 43);
453 wmemcpy(prefs_export_path, app_data_folder, 29);
454 wcsncat(prefs_export_path, L"prefs-export", 12);
455 will_return(__wrap_SHGetFolderPath, app_data_folder);
456 will_return(__wrap_SHGetFolderPath, 0);
457 will_return(__wrap_PathAppend, prefs_export_path);
458 will_return(__wrap_PathAppend, true);
459
460 expect_any_always(__wrap_save_preferences, max_depth);
461 expect_any_always(__wrap_save_preferences, commit);
462 expect_any_always(__wrap_save_preferences, deploy_key);
463 expect_any_always(__wrap_save_preferences, hk);
464 expect_any_always(__wrap_save_preferences, output_dir);
465 expect_any_always(__wrap_save_preferences, output_file);
466 expect_any_always(__wrap_save_preferences, specified_path);
467 will_return_always(__wrap_save_preferences, true);
468
469 wchar_t **buf = calloc(4, sizeof(wchar_t *));
470 buf[0] = calloc(8, sizeof(wchar_t));
471 buf[1] = calloc(3, sizeof(wchar_t));
472 buf[2] = calloc(10, sizeof(wchar_t));
473 buf[3] = nullptr;
474 wmemset(buf[0], L'\0', 8);
475 wmemcpy(buf[0], L"winprefs", 10);
476 wmemset(buf[1], L'\0', 3);
477 wmemcpy(buf[1], L"-F", 2);
478
479 expect_value(__wrap_save_preferences, format, OUTPUT_FORMAT_REG);
480 wmemset(buf[2], L'\0', 10);
481 wmemcpy(buf[2], L"reg", 3);
482 int ret = wmain(3, buf);
483 assert_return_code(ret, EXIT_SUCCESS);
484
485 free(buf[0]);
486 free(buf[1]);
487 free(buf[2]);
488 free(buf);
489 free(app_data_folder);
490 free(prefs_export_path);
491}
492
493static void test_main_format_arg_default(void **state) {
494 wchar_t *app_data_folder = calloc(30, sizeof(wchar_t));
495 wmemset(app_data_folder, L'\0', 30);
496 wmemcpy(app_data_folder, L"\\c\\users\\name\\AppData\\Roaming", 29);
497 wchar_t *prefs_export_path = calloc(43, sizeof(wchar_t));
498 wmemset(prefs_export_path, L'\0', 43);
499 wmemcpy(prefs_export_path, app_data_folder, 29);
500 wcsncat(prefs_export_path, L"prefs-export", 12);
501 will_return(__wrap_SHGetFolderPath, app_data_folder);
502 will_return(__wrap_SHGetFolderPath, 0);
503 will_return(__wrap_PathAppend, prefs_export_path);
504 will_return(__wrap_PathAppend, true);
505
506 expect_any_always(__wrap_save_preferences, max_depth);
507 expect_any_always(__wrap_save_preferences, commit);
508 expect_any_always(__wrap_save_preferences, deploy_key);
509 expect_any_always(__wrap_save_preferences, hk);
510 expect_any_always(__wrap_save_preferences, output_dir);
511 expect_any_always(__wrap_save_preferences, output_file);
512 expect_any_always(__wrap_save_preferences, specified_path);
513 will_return_always(__wrap_save_preferences, true);
514
515 wchar_t **buf = calloc(4, sizeof(wchar_t *));
516 buf[0] = calloc(8, sizeof(wchar_t));
517 buf[1] = nullptr;
518 wmemset(buf[0], L'\0', 8);
519 wmemcpy(buf[0], L"winprefs", 10);
520
521 expect_value(__wrap_save_preferences, format, OUTPUT_FORMAT_REG);
522 int ret = wmain(1, buf);
523 assert_return_code(ret, EXIT_SUCCESS);
524
525 free(buf[0]);
526 free(buf);
527 free(app_data_folder);
528 free(prefs_export_path);
529}
530
531static void test_main_save_prefs_failed(void **state) {
532 wchar_t *app_data_folder = calloc(30, sizeof(wchar_t));
533 wmemset(app_data_folder, L'\0', 30);
534 wmemcpy(app_data_folder, L"\\c\\users\\name\\AppData\\Roaming", 29);
535 wchar_t *prefs_export_path = calloc(43, sizeof(wchar_t));
536 wmemset(prefs_export_path, L'\0', 43);
537 wmemcpy(prefs_export_path, app_data_folder, 29);
538 wcsncat(prefs_export_path, L"prefs-export", 12);
539 will_return(__wrap_SHGetFolderPath, app_data_folder);
540 will_return(__wrap_SHGetFolderPath, 0);
541 will_return(__wrap_PathAppend, prefs_export_path);
542 will_return(__wrap_PathAppend, true);
543
544 expect_any_always(__wrap_save_preferences, max_depth);
545 expect_any_always(__wrap_save_preferences, commit);
546 expect_any_always(__wrap_save_preferences, deploy_key);
547 expect_any_always(__wrap_save_preferences, hk);
548 expect_any_always(__wrap_save_preferences, output_dir);
549 expect_any_always(__wrap_save_preferences, output_file);
550 expect_any_always(__wrap_save_preferences, specified_path);
551 will_return_always(__wrap_save_preferences, false);
552
553 will_return(__wrap_GetLastError, 0);
554 will_return(__wrap_FormatMessage, 10);
555
556 wchar_t **buf = calloc(4, sizeof(wchar_t *));
557 buf[0] = calloc(8, sizeof(wchar_t));
558 buf[1] = nullptr;
559 wmemset(buf[0], L'\0', 8);
560 wmemcpy(buf[0], L"winprefs", 10);
561
562 expect_value(__wrap_save_preferences, format, OUTPUT_FORMAT_REG);
563 int ret = wmain(1, buf);
564 assert_return_code(ret, EXIT_FAILURE);
565
566 free(buf[0]);
567 free(buf);
568 free(app_data_folder);
569 free(prefs_export_path);
570}
571
572static const wchar_t *POWERSHELL_FORMAT_NAMES[] = {L"powershell", L"ps", L"ps1"};
573
574static void test_main_format_arg_powershell(void **state) {
575 wchar_t *app_data_folder = calloc(30, sizeof(wchar_t));
576 wmemset(app_data_folder, L'\0', 30);
577 wmemcpy(app_data_folder, L"\\c\\users\\name\\AppData\\Roaming", 29);
578 wchar_t *prefs_export_path = calloc(43, sizeof(wchar_t));
579 wmemset(prefs_export_path, L'\0', 43);
580 wmemcpy(prefs_export_path, app_data_folder, 29);
581 wcsncat(prefs_export_path, L"prefs-export", 12);
582
583 expect_any_always(__wrap_save_preferences, max_depth);
584 expect_any_always(__wrap_save_preferences, commit);
585 expect_any_always(__wrap_save_preferences, deploy_key);
586 expect_any_always(__wrap_save_preferences, hk);
587 expect_any_always(__wrap_save_preferences, output_dir);
588 expect_any_always(__wrap_save_preferences, output_file);
589 expect_any_always(__wrap_save_preferences, specified_path);
590 will_return_always(__wrap_save_preferences, true);
591
592 long unsigned int i;
593 wchar_t **buf = calloc(4, sizeof(wchar_t *));
594 buf[0] = calloc(8, sizeof(wchar_t));
595 buf[1] = calloc(3, sizeof(wchar_t));
596 buf[2] = calloc(11, sizeof(wchar_t));
597 buf[3] = nullptr;
598 wmemset(buf[0], L'\0', 8);
599 wmemcpy(buf[0], L"winprefs", 10);
600 wmemset(buf[1], L'\0', 3);
601 wmemcpy(buf[1], L"-F", 2);
602 for (i = 0; i < ARRAY_SIZE(POWERSHELL_FORMAT_NAMES); i++) {
603 will_return(__wrap_SHGetFolderPath, app_data_folder);
604 will_return(__wrap_SHGetFolderPath, 0);
605 will_return(__wrap_PathAppend, prefs_export_path);
606 will_return(__wrap_PathAppend, true);
607
608 expect_value(__wrap_save_preferences, format, OUTPUT_FORMAT_POWERSHELL);
609 wmemset(buf[2], L'\0', 11);
610 wmemcpy(buf[2], POWERSHELL_FORMAT_NAMES[i], wcslen(POWERSHELL_FORMAT_NAMES[i]));
611 int ret = wmain(3, buf);
612 assert_return_code(ret, EXIT_SUCCESS);
613 }
614
615 free(buf[0]);
616 free(buf[1]);
617 free(buf[2]);
618 free(buf);
619 free(app_data_folder);
620 free(prefs_export_path);
621}
622
623static const wchar_t *C_FORMAT_NAMES[] = {L"c", L"C"};
624
625static void test_main_format_arg_c(void **state) {
626 wchar_t *app_data_folder = calloc(30, sizeof(wchar_t));
627 wmemset(app_data_folder, L'\0', 30);
628 wmemcpy(app_data_folder, L"\\c\\users\\name\\AppData\\Roaming", 29);
629 wchar_t *prefs_export_path = calloc(43, sizeof(wchar_t));
630 wmemset(prefs_export_path, L'\0', 43);
631 wmemcpy(prefs_export_path, app_data_folder, 29);
632 wcsncat(prefs_export_path, L"prefs-export", 12);
633
634 expect_any_always(__wrap_save_preferences, max_depth);
635 expect_any_always(__wrap_save_preferences, commit);
636 expect_any_always(__wrap_save_preferences, deploy_key);
637 expect_any_always(__wrap_save_preferences, hk);
638 expect_any_always(__wrap_save_preferences, output_dir);
639 expect_any_always(__wrap_save_preferences, output_file);
640 expect_any_always(__wrap_save_preferences, specified_path);
641 will_return_always(__wrap_save_preferences, true);
642
643 long unsigned int i;
644 wchar_t **buf = calloc(4, sizeof(wchar_t *));
645 buf[0] = calloc(8, sizeof(wchar_t));
646 buf[1] = calloc(3, sizeof(wchar_t));
647 buf[2] = calloc(7, sizeof(wchar_t));
648 buf[3] = nullptr;
649 wmemset(buf[0], L'\0', 8);
650 wmemcpy(buf[0], L"winprefs", 7);
651 wmemset(buf[1], L'\0', 3);
652 wmemcpy(buf[1], L"-F", 2);
653 for (i = 0; i < ARRAY_SIZE(C_FORMAT_NAMES); i++) {
654 will_return(__wrap_SHGetFolderPath, app_data_folder);
655 will_return(__wrap_SHGetFolderPath, 0);
656 will_return(__wrap_PathAppend, prefs_export_path);
657 will_return(__wrap_PathAppend, true);
658
659 expect_value(__wrap_save_preferences, format, OUTPUT_FORMAT_C);
660 wmemset(buf[2], L'\0', 7);
661 wmemcpy(buf[2], C_FORMAT_NAMES[i], wcslen(C_FORMAT_NAMES[i]));
662 int ret = wmain(3, buf);
663 assert_return_code(ret, EXIT_SUCCESS);
664 }
665
666 free(buf[0]);
667 free(buf[1]);
668 free(buf[2]);
669 free(buf);
670 free(app_data_folder);
671 free(prefs_export_path);
672}
673
674void test_main_top_key_only(void **state) {
675 expect_any_always(__wrap_save_preferences, max_depth);
676 expect_any_always(__wrap_save_preferences, commit);
677 expect_any_always(__wrap_save_preferences, deploy_key);
678 expect_any_always(__wrap_save_preferences, format);
679 expect_any_always(__wrap_save_preferences, hk);
680 expect_any_always(__wrap_save_preferences, output_dir);
681 expect_any_always(__wrap_save_preferences, output_file);
682 expect_any_always(__wrap_save_preferences, specified_path);
683 will_return_always(__wrap_save_preferences, true);
684
685 wchar_t **buf = calloc(5, sizeof(wchar_t *));
686 buf[0] = calloc(9, sizeof(wchar_t));
687 buf[1] = calloc(3, sizeof(wchar_t));
688 buf[2] = calloc(4, sizeof(wchar_t));
689 buf[3] = calloc(6, sizeof(wchar_t));
690 buf[4] = nullptr;
691 wmemset(buf[0], L'\0', 9);
692 wmemcpy(buf[0], L"winprefs", 8);
693 wmemset(buf[1], L'\0', 3);
694 wmemcpy(buf[1], L"-o", 2);
695 wmemset(buf[2], L'\0', 4);
696 wmemcpy(buf[2], L"out", 3);
697 wmemset(buf[3], L'\0', 6);
698 wmemcpy(buf[3], L"HKCU:", 5);
699
700 int ret = wmain(4, buf);
701 assert_return_code(ret, EXIT_SUCCESS);
702
703 free(buf[0]);
704 free(buf[1]);
705 free(buf[2]);
706 free(buf[3]);
707 free(buf);
708}
709
710void test_main_top_key_only_2(void **state) {
711 expect_any_always(__wrap_save_preferences, max_depth);
712 expect_any_always(__wrap_save_preferences, commit);
713 expect_any_always(__wrap_save_preferences, deploy_key);
714 expect_any_always(__wrap_save_preferences, format);
715 expect_any_always(__wrap_save_preferences, hk);
716 expect_any_always(__wrap_save_preferences, output_dir);
717 expect_any_always(__wrap_save_preferences, output_file);
718 expect_any_always(__wrap_save_preferences, specified_path);
719 will_return_always(__wrap_save_preferences, true);
720
721 wchar_t **buf = calloc(5, sizeof(wchar_t *));
722 buf[0] = calloc(9, sizeof(wchar_t));
723 buf[1] = calloc(3, sizeof(wchar_t));
724 buf[2] = calloc(4, sizeof(wchar_t));
725 buf[3] = calloc(7, sizeof(wchar_t));
726 buf[4] = nullptr;
727 wmemset(buf[0], L'\0', 9);
728 wmemcpy(buf[0], L"winprefs", 8);
729 wmemset(buf[1], L'\0', 3);
730 wmemcpy(buf[1], L"-o", 2);
731 wmemset(buf[2], L'\0', 4);
732 wmemcpy(buf[2], L"out", 3);
733 wmemset(buf[3], L'\0', 6);
734 wmemcpy(buf[3], L"HKCU:\\", 6);
735
736 int ret = wmain(4, buf);
737 assert_return_code(ret, EXIT_SUCCESS);
738
739 free(buf[0]);
740 free(buf[1]);
741 free(buf[2]);
742 free(buf[3]);
743 free(buf);
744}
745
746const struct CMUnitTest main_tests[] = {
747 cmocka_unit_test(test_main_commit_arg),
748 cmocka_unit_test(test_main_debug_arg),
749 cmocka_unit_test(test_main_deploy_key_arg),
750 cmocka_unit_test(test_main_format_arg_c),
751 cmocka_unit_test(test_main_format_arg_c_sharp),
752 cmocka_unit_test(test_main_format_arg_default),
753 cmocka_unit_test(test_main_format_arg_full_reg_path),
754 cmocka_unit_test(test_main_format_arg_invalid),
755 cmocka_unit_test(test_main_format_arg_powershell),
756 cmocka_unit_test(test_main_format_arg_reg),
757 cmocka_unit_test(test_main_help),
758 cmocka_unit_test(test_main_invalid_option),
759 cmocka_unit_test(test_main_max_depth_conversion),
760 cmocka_unit_test(test_main_output_dir_arg),
761 cmocka_unit_test(test_main_output_file_arg),
762 cmocka_unit_test(test_main_reg_path_invalid),
763 cmocka_unit_test(test_main_reg_path_invalid_alt),
764 cmocka_unit_test(test_main_save_prefs_failed),
765 cmocka_unit_test(test_main_top_key_only),
766 cmocka_unit_test(test_main_top_key_only_2),
767};
768
769int main(int argc, char *argv[]) {
770 (void)argc;
771 (void)argv;
772 return cmocka_run_group_tests(main_tests, nullptr, nullptr);
773}
int wmain(int argc, wchar_t *argv[])
Entry point.
Definition main.c:50