winprefs v0.3.2
A registry exporter for programmers.
Loading...
Searching...
No Matches
test_io.c
1#include "io.h"
2#include "io_default_writer.h"
3
4void test_write_output_returns_false_when_WideCharToMultiByte_returns_req_size_0(void **state) {
5 will_return(__wrap_WideCharToMultiByte, 0);
6 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
7 writer_t *default_writer = get_default_writer();
8 default_writer->setup(default_writer, true, nullptr);
9 assert_false(write_output(nullptr, false, default_writer));
10 default_writer->teardown(default_writer);
11 free(default_writer);
12}
13
14void test_write_output_returns_true_when_WideCharToMultiByte_returns_0(void **state) {
15 will_return(__wrap_WideCharToMultiByte, 10);
16 will_return(__wrap_WideCharToMultiByte, 0);
17 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
18 writer_t *default_writer = get_default_writer();
19 default_writer->setup(default_writer, true, nullptr);
20 assert_true(write_output(nullptr, false, default_writer));
21 default_writer->teardown(default_writer);
22 free(default_writer);
23}
24
25void test_write_output_adds_cr(void **state) {
26 expect_value(__wrap_WriteFile, nNumberOfBytesToWrite, 11);
27 will_return(__wrap_WideCharToMultiByte, 10);
28 will_return(__wrap_WideCharToMultiByte, 10);
29 will_return(__wrap_WriteFile, 11);
30 will_return(__wrap_WriteFile, true);
31 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
32 writer_t *default_writer = get_default_writer();
33 default_writer->setup(default_writer, true, nullptr);
34 assert_true(write_output(nullptr, true, default_writer));
35 default_writer->teardown(default_writer);
36 free(default_writer);
37}
38
39void test_write_key_filtered_recursive_does_not_exceed_max_depth_inclusive(void **state) {
40 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
41 writer_t *default_writer = get_default_writer();
42 default_writer->setup(default_writer, true, nullptr);
43 assert_true(write_key_filtered_recursive(
44 nullptr, nullptr, 20, 20, nullptr, OUTPUT_FORMAT_REG, default_writer));
45 default_writer->teardown(default_writer);
46 free(default_writer);
47 assert_int_equal(errno, EDOM);
48}
49
50void test_write_key_filtered_recursive_does_not_exceed_length_limitations(void **state) {
51 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
52 writer_t *default_writer = get_default_writer();
53 default_writer->setup(default_writer, true, nullptr);
54 assert_true(write_key_filtered_recursive(
55 nullptr,
56 L"HKCR\\Software\\Microsoft\\Windows\\CurrentVersion\\Microsoft\\Microsoft\\Microsoft"
57 L"\\Microsoft\\Microsoft\\Windows\\Windows\\Windows\\Windows\\Windows\\Windows\\Windows"
58 L"\\Windows\\Windows\\Windows\\Windows\\Windows\\Windows\\Windows\\Windows\\Windows"
59 L"\\Windows\\Windows\\Windows\\Windows\\Windows\\Windows\\Windows\\Windows\\Windows"
60 L"\\Windows\\Windows",
61 20,
62 4,
63 L"HKCR\\Software\\Microsoft\\Windows\\Windows\\CurrentVersion\\Windows\\CurrentVersion"
64 L"\\Windows\\CurrentVersion\\Windows\\CurrentVersion",
65 OUTPUT_FORMAT_REG,
66 default_writer));
67 default_writer->teardown(default_writer);
68 free(default_writer);
69 assert_int_equal(errno, E2BIG);
70}
71
72void test_write_key_filtered_has_filters(void **state) {
73 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
74 writer_t *default_writer = get_default_writer();
75 default_writer->setup(default_writer, true, nullptr);
76 assert_true(write_key_filtered_recursive(
77 nullptr, L"MuiCache", 20, 4, L"HKCR\\Windows\\Shell", OUTPUT_FORMAT_REG, default_writer));
78 assert_int_equal(errno, EKEYREJECTED);
79 default_writer->teardown(default_writer);
80 free(default_writer);
81}
82
83void test_write_key_filtered_RegOpenKeyEx_fail(void **state) {
84 will_return(__wrap_RegOpenKeyEx, -1);
85 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
86 writer_t *default_writer = get_default_writer();
87 default_writer->setup(default_writer, true, nullptr);
88 assert_true(write_key_filtered_recursive(
89 nullptr, nullptr, 20, 4, L"HKCR\\Windows\\Shell", OUTPUT_FORMAT_REG, default_writer));
90 default_writer->teardown(default_writer);
91 free(default_writer);
92}
93
94void test_write_key_filtered_RegQueryInfoKey_fail(void **state) {
95 will_return(__wrap_RegOpenKeyEx, ERROR_SUCCESS);
96 will_return(__wrap_RegQueryInfoKey, 0);
97 will_return(__wrap_RegQueryInfoKey, -1);
98 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
99 writer_t *default_writer = get_default_writer();
100 default_writer->setup(default_writer, true, nullptr);
101 assert_false(write_key_filtered_recursive(
102 nullptr, nullptr, 20, 4, L"HKCR\\Windows\\Shell", OUTPUT_FORMAT_REG, default_writer));
103 default_writer->teardown(default_writer);
104 free(default_writer);
105}
106
107void test_write_key_filtered_RegQueryInfoKey_0_subkeys_returns_true(void **state) {
108 will_return(__wrap_RegOpenKeyEx, ERROR_SUCCESS);
109 will_return(__wrap_RegQueryInfoKey, 0);
110 will_return(__wrap_RegQueryInfoKey, ERROR_SUCCESS);
111 will_return(__wrap_RegCloseKey, 0);
112 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
113 writer_t *default_writer = get_default_writer();
114 default_writer->setup(default_writer, true, nullptr);
115 assert_true(write_key_filtered_recursive(
116 nullptr, nullptr, 20, 4, L"HKCR\\Windows\\Shell", OUTPUT_FORMAT_REG, default_writer));
117 default_writer->teardown(default_writer);
118 free(default_writer);
119}
120
121void test_write_key_filtered_RegEnumKeyEx_fail_returns_true(void **state) {
122 will_return(__wrap_RegOpenKeyEx, ERROR_SUCCESS);
123 will_return(__wrap_RegQueryInfoKey, 1);
124 will_return(__wrap_RegQueryInfoKey, ERROR_SUCCESS);
125 will_return(__wrap_RegEnumKeyEx, 0);
126 will_return(__wrap_RegEnumKeyEx, 0);
127 will_return(__wrap_RegEnumKeyEx, -1);
128 will_return(__wrap_RegCloseKey, 0);
129 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
130 writer_t *default_writer = get_default_writer();
131 default_writer->setup(default_writer, true, nullptr);
132 assert_true(write_key_filtered_recursive(
133 nullptr, nullptr, 20, 4, L"HKCR\\Windows\\Shell", OUTPUT_FORMAT_REG, default_writer));
134 default_writer->teardown(default_writer);
135 free(default_writer);
136}
137
138void test_write_key_filtered_recursion_fail(void **state) {
139 will_return(__wrap_RegOpenKeyEx, ERROR_SUCCESS);
140 will_return(__wrap_RegQueryInfoKey, 1);
141 will_return(__wrap_RegQueryInfoKey, ERROR_SUCCESS);
142 will_return(__wrap_RegEnumKeyEx, L"main");
143 will_return(__wrap_RegEnumKeyEx, 4);
144 will_return(__wrap_RegEnumKeyEx, ERROR_SUCCESS);
145 will_return(__wrap_RegOpenKeyEx, ERROR_SUCCESS);
146 will_return(__wrap_RegQueryInfoKey, 0);
147 will_return(__wrap_RegQueryInfoKey, -1);
148 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
149 writer_t *default_writer = get_default_writer();
150 default_writer->setup(default_writer, true, nullptr);
151 assert_false(write_key_filtered_recursive(
152 nullptr, nullptr, 20, 4, L"HKCR\\Windows\\Shell", OUTPUT_FORMAT_REG, default_writer));
153 default_writer->teardown(default_writer);
154 free(default_writer);
155}
156
157void test_do_writes_n_values_0_returns_true(void **state) {
158 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
159 writer_t *default_writer = get_default_writer();
160 default_writer->setup(default_writer, true, nullptr);
161 assert_true(do_writes(nullptr, 0, nullptr, OUTPUT_FORMAT_REG, default_writer));
162 default_writer->teardown(default_writer);
163 free(default_writer);
164}
165
166void test_do_writes_nullptr_args(void **state) {
167 assert_false(do_writes(nullptr, 1, L"full path", OUTPUT_FORMAT_REG, nullptr));
168 assert_false(do_writes(nullptr, 1, nullptr, OUTPUT_FORMAT_REG, nullptr));
169}
170
171void test_do_writes_unknown_format_arg(void **state) {
172 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
173 writer_t *default_writer = get_default_writer();
174 default_writer->setup(default_writer, true, nullptr);
175 assert_false(do_writes(nullptr, 1, L"full path", OUTPUT_FORMAT_UNKNOWN, default_writer));
176 default_writer->teardown(default_writer);
177 free(default_writer);
178}
179
180void test_do_writes_callbacks_fail(void **state) {
181 will_return_always(__wrap_RegEnumValue, ERROR_SUCCESS);
182 will_return(__wrap_do_write_reg_command, false);
183 will_return(__wrap_do_write_c_reg_code, false);
184 will_return(__wrap_do_write_c_sharp_reg_code, false);
185 will_return(__wrap_do_write_powershell_reg_code, false);
186 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
187 writer_t *default_writer = get_default_writer();
188 default_writer->setup(default_writer, true, nullptr);
189 assert_false(do_writes(nullptr, 1, L"full path", OUTPUT_FORMAT_C, default_writer));
190 assert_false(do_writes(nullptr, 1, L"full path", OUTPUT_FORMAT_C_SHARP, default_writer));
191 assert_false(do_writes(nullptr, 1, L"full path", OUTPUT_FORMAT_POWERSHELL, default_writer));
192 assert_false(do_writes(nullptr, 1, L"full path", OUTPUT_FORMAT_REG, default_writer));
193 default_writer->teardown(default_writer);
194 free(default_writer);
195}
196
197void test_do_writes(void **state) {
198 will_return_always(__wrap_RegEnumValue, ERROR_SUCCESS);
199 will_return(__wrap_do_write_c_reg_code, true);
200 will_return_always(__wrap_GetStdHandle, (HANDLE)0);
201 writer_t *default_writer = get_default_writer();
202 default_writer->setup(default_writer, true, nullptr);
203 assert_true(do_writes(nullptr, 1, L"full path", OUTPUT_FORMAT_C, default_writer));
204 default_writer->teardown(default_writer);
205 free(default_writer);
206}
207
208const struct CMUnitTest io_tests[] = {
209 cmocka_unit_test(test_do_writes),
210 cmocka_unit_test(test_do_writes_callbacks_fail),
211 cmocka_unit_test(test_do_writes_n_values_0_returns_true),
212 cmocka_unit_test(test_do_writes_nullptr_args),
213 cmocka_unit_test(test_do_writes_unknown_format_arg),
214 cmocka_unit_test(test_write_key_filtered_RegEnumKeyEx_fail_returns_true),
215 cmocka_unit_test(test_write_key_filtered_RegOpenKeyEx_fail),
216 cmocka_unit_test(test_write_key_filtered_RegQueryInfoKey_0_subkeys_returns_true),
217 cmocka_unit_test(test_write_key_filtered_RegQueryInfoKey_fail),
218 cmocka_unit_test(test_write_key_filtered_has_filters),
219 cmocka_unit_test(test_write_key_filtered_recursion_fail),
220 cmocka_unit_test(test_write_key_filtered_recursive_does_not_exceed_length_limitations),
221 cmocka_unit_test(test_write_key_filtered_recursive_does_not_exceed_max_depth_inclusive),
222 cmocka_unit_test(test_write_output_adds_cr),
223 cmocka_unit_test(test_write_output_returns_true_when_WideCharToMultiByte_returns_0),
224 cmocka_unit_test(test_write_output_returns_false_when_WideCharToMultiByte_returns_req_size_0),
225};
226
227int main(int argc, char *argv[]) {
228 return cmocka_run_group_tests(io_tests, nullptr, nullptr);
229}