winprefs v0.3.2
A registry exporter for programmers.
Loading...
Searching...
No Matches
test_save_prefs.c
1#include "constants.h"
2#include "registry.h"
3
4static wchar_t *OUTPUT_DIR = L"\\c\\users\\name\\AppData\\Roaming\\prefs-export";
5
6static void test_save_prefs_wfullpath_error(void **state) {
7 will_return(__wrap__wfullpath, (void *)0);
8 will_return(__wrap__wfullpath, false);
9
10 bool ret = save_preferences(false,
11 nullptr,
12 OUTPUT_DIR,
13 L"exec-reg.bat",
14 2,
15 HKEY_CURRENT_USER,
16 L"HKCU:\\",
17 OUTPUT_FORMAT_REG,
18 nullptr);
19
20 assert_false(ret);
21}
22
23static void test_save_prefs_fail_to_create_storage_dir(void **state) {
24 will_return(__wrap__wfullpath, OUTPUT_DIR);
25 will_return(__wrap__wfullpath, true);
26 will_return(__wrap_CreateDirectory, false);
27 will_return(__wrap_GetLastError, 999);
28
29 bool ret = save_preferences(false,
30 nullptr,
31 OUTPUT_DIR,
32 L"exec-reg.bat",
33 2,
34 HKEY_CURRENT_USER,
35 L"HKCU:\\",
36 OUTPUT_FORMAT_REG,
37 nullptr);
38
39 assert_false(ret);
40}
41
42static void test_save_prefs_fail_to_create_storage_dir_alt(void **state) {
43 will_return(__wrap__wfullpath, OUTPUT_DIR);
44 will_return(__wrap__wfullpath, true);
45 will_return(__wrap_CreateDirectory, true);
46 will_return(__wrap_CreateDirectory, true);
47 will_return(__wrap_CreateDirectory, true);
48 will_return(__wrap_CreateDirectory, true);
49 will_return(__wrap_CreateDirectory, true);
50 will_return(__wrap_CreateDirectory, true);
51 will_return(__wrap_CreateDirectory, false);
52 will_return(__wrap_GetLastError, 999);
53
54 bool ret = save_preferences(false,
55 nullptr,
56 OUTPUT_DIR,
57 L"exec-reg.bat",
58 2,
59 HKEY_CURRENT_USER,
60 L"HKCU:\\",
61 OUTPUT_FORMAT_REG,
62 nullptr);
63
64 assert_false(ret);
65}
66
67static void test_save_prefs_CreateFile_returns_invalid_handle(void **state) {
68 will_return(__wrap__wfullpath, OUTPUT_DIR);
69 will_return(__wrap__wfullpath, true);
70 will_return(__wrap_PathAppend, L"exec-reg.bat");
71 will_return(__wrap_PathAppend, true);
72 will_return_always(__wrap_CreateDirectory, true);
73 will_return(__wrap_CreateFile, INVALID_HANDLE_VALUE);
74
75 bool ret = save_preferences(false,
76 nullptr,
77 OUTPUT_DIR,
78 L"exec-reg.bat",
79 2,
80 HKEY_CURRENT_USER,
81 L"HKCU:\\",
82 OUTPUT_FORMAT_REG,
83 nullptr);
84
85 assert_false(ret);
86}
87
88void test_save_prefs_GetStdHandle_returns_invalid_handle(void **state) {
89 will_return(__wrap__wfullpath, OUTPUT_DIR);
90 will_return(__wrap__wfullpath, true);
91 will_return(__wrap_PathAppend, L"exec-reg.bat");
92 will_return(__wrap_PathAppend, true);
93 will_return(__wrap_GetStdHandle, INVALID_HANDLE_VALUE);
94
95 bool ret = save_preferences(false,
96 nullptr,
97 OUTPUT_DIR,
98 L"-",
99 2,
100 HKEY_CURRENT_USER,
101 L"HKCU:\\",
102 OUTPUT_FORMAT_REG,
103 nullptr);
104
105 assert_false(ret);
106}
107
108void test_save_prefs_c_preamble(void **state) {
109 will_return(__wrap__wfullpath, OUTPUT_DIR);
110 will_return(__wrap__wfullpath, true);
111 will_return(__wrap_PathAppend, L"exec-reg.bat");
112 will_return(__wrap_PathAppend, true);
113 will_return(__wrap_GetStdHandle, STD_OUTPUT_HANDLE);
114 expect_value(__wrap_WriteFile, nNumberOfBytesToWrite, SIZEOF_C_PREAMBLE);
115 will_return(__wrap_WriteFile, 10);
116 will_return(__wrap_WriteFile, true);
117 will_return(__wrap_write_key_filtered_recursive, true);
118
119 bool ret = save_preferences(
120 false, nullptr, OUTPUT_DIR, L"-", 2, HKEY_DYN_DATA, L"HKDD:\\", OUTPUT_FORMAT_C, nullptr);
121
122 assert_true(ret);
123}
124
125void test_save_prefs_commit(void **state) {
126 will_return(__wrap__wfullpath, OUTPUT_DIR);
127 will_return(__wrap__wfullpath, true);
128 will_return(__wrap_PathAppend, L"exec-reg.bat");
129 will_return(__wrap_PathAppend, true);
130 will_return_always(__wrap_CreateDirectory, true);
131 will_return(__wrap_write_key_filtered_recursive, true);
132 will_return(__wrap_CreateFile, (HANDLE)1);
133 will_return(__wrap_CloseHandle, true);
134 will_return(__wrap_git_commit, true);
135
136 bool ret = save_preferences(true,
137 nullptr,
138 OUTPUT_DIR,
139 L"exec-reg.bat",
140 2,
141 HKEY_DYN_DATA,
142 L"HKDD:\\",
143 OUTPUT_FORMAT_REG,
144 nullptr);
145
146 assert_true(ret);
147}
148
149const struct CMUnitTest save_prefs_tests[] = {
150 cmocka_unit_test(test_save_prefs_CreateFile_returns_invalid_handle),
151 cmocka_unit_test(test_save_prefs_GetStdHandle_returns_invalid_handle),
152 cmocka_unit_test(test_save_prefs_c_preamble),
153 cmocka_unit_test(test_save_prefs_fail_to_create_storage_dir),
154 cmocka_unit_test(test_save_prefs_fail_to_create_storage_dir_alt),
155 cmocka_unit_test(test_save_prefs_wfullpath_error),
156 cmocka_unit_test(test_save_prefs_commit),
157};
158
159int main(int argc, char *argv[]) {
160 (void)argc;
161 (void)argv;
162 return cmocka_run_group_tests(save_prefs_tests, nullptr, nullptr);
163}