winprefs v0.3.2
A registry exporter for programmers.
Loading...
Searching...
No Matches
test_output_reg_command.c
1#include "constants.h"
2#include "reg_command.h"
3
4void test_output_reg_command_dword(void **state) {
5 expect_memory(__wrap_write_output,
6 out,
7 L"reg add \"HKEY_CLASSES_ROOT\\Control Panel\\Desktop\" /v \"DoubleClickHeight\" "
8 L"/t REG_DWORD /d 4 /f",
9 95);
10 will_return(__wrap_write_output, true);
11 DWORD *test_val = malloc(sizeof(DWORD));
12 test_val[0] = 4;
13 bool ret = do_write_reg_command(nullptr,
14 L"HKEY_CLASSES_ROOT\\Control Panel\\Desktop",
15 L"DoubleClickHeight",
16 (const char *)test_val,
17 sizeof(DWORD),
18 REG_DWORD);
19 assert_true(ret);
20 free(test_val);
21}
22
23void test_output_reg_command_qword(void **state) {
24 expect_memory(
25 __wrap_write_output,
26 out,
27 L"reg add \"HKEY_LOCAL_MACHINE\\Control Panel\\Desktop\" /v \"DoubleClickHeight\" "
28 L"/t REG_QWORD /d 4 /f",
29 95);
30 will_return(__wrap_write_output, true);
31 QWORD *test_val = malloc(sizeof(QWORD));
32 test_val[0] = 4;
33 bool ret = do_write_reg_command(nullptr,
34 L"HKEY_LOCAL_MACHINE\\Control Panel\\Desktop",
35 L"DoubleClickHeight",
36 (const char *)test_val,
37 sizeof(QWORD),
38 REG_QWORD);
39 assert_true(ret);
40 free(test_val);
41}
42
43void test_output_reg_command_sz(void **state) {
44 expect_memory(__wrap_write_output,
45 out,
46 L"reg add \"HKEY_CURRENT_CONFIG\\Environment\" /v \"TEMP\" /t REG_SZ /d "
47 L"\"\"\"quoted \\string\"\" fff\" /f",
48 94);
49 will_return(__wrap_write_output, true);
50 bool ret = do_write_reg_command(nullptr,
51 L"HKEY_CURRENT_CONFIG\\Environment",
52 L"TEMP",
53 (const char *)L"\"quoted \\string\" fff",
54 21 * sizeof(wchar_t),
55 REG_SZ);
56 assert_true(ret);
57}
58
59void test_output_reg_command_expand_sz(void **state) {
60 expect_memory(__wrap_write_output,
61 out,
62 L"reg add \"HKEY_CURRENT_USER\\Environment\" /v \"TEMP\" /t REG_EXPAND_SZ /d \"a "
63 L"midsummer night's %%dream\" /f",
64 103);
65 will_return(__wrap_write_output, true);
66 bool ret = do_write_reg_command(nullptr,
67 L"HKEY_CURRENT_USER\\Environment",
68 L"TEMP",
69 (const char *)L"a midsummer night's %dream%",
70 26 * sizeof(wchar_t),
71 REG_EXPAND_SZ);
72 assert_true(ret);
73}
74
75const wchar_t *MULTI_SZ_TEST_DATA = L"\"quoted string\" fff\0test2\0\0";
76
77void test_output_reg_command_multi_sz(void **state) {
78 expect_memory(__wrap_write_output,
79 out,
80 L"reg add \"HKEY_USERS\\Environment\" /v \"TEMP\" /t REG_MULTI_SZ /d "
81 L"\"\"\"quoted string\"\" fff\0test2\\0\" /f",
82 98);
83 will_return(__wrap_write_output, true);
84 bool ret = do_write_reg_command(nullptr,
85 L"HKEY_USERS\\Environment",
86 L"TEMP",
87 (const char *)MULTI_SZ_TEST_DATA,
88 27 * sizeof(wchar_t),
89 REG_MULTI_SZ);
90 assert_true(ret);
91}
92
93#if SIZEOF_WCHAR_T == 4
94const unsigned char MULTI_SZ_TEST_DATA_INVALID[] = {
95 L'e', 0, 0, 0, 'n', 0, 0, 0, '-', 0, 0, 0, 'U', 0, 0, 0, 'S'};
96#else
97const unsigned char MULTI_SZ_TEST_DATA_INVALID[] = {L'e', 0, 'n', 0, '-', 0, 'U', 0, 'S'};
98#endif
99
100void test_output_reg_command_multi_sz_invalid(void **state) {
101 bool ret = do_write_reg_command(nullptr,
102 L"HKEY_USERS\\Environment",
103 L"TEMP",
104 (const char *)MULTI_SZ_TEST_DATA_INVALID,
105 sizeof(MULTI_SZ_TEST_DATA_INVALID),
106 REG_MULTI_SZ);
107 assert_true(ret);
108}
109
110void test_output_reg_command_invalid_type(void **state) {
111 bool ret = do_write_reg_command(nullptr,
112 L"HKEY_USERS\\Environment",
113 L"TEMP",
114 (const char *)MULTI_SZ_TEST_DATA,
115 25 * sizeof(wchar_t),
116 10240);
117 assert_true(ret);
118 assert_int_equal(errno, EINVAL);
119}
120
121void test_output_reg_command_none(void **state) {
122 expect_memory(__wrap_write_output,
123 out,
124 L"reg add \"HKEY_CURRENT_USER\\Environment\" /v \"TEMP\" /t REG_NONE /f",
125 65);
126 will_return(__wrap_write_output, true);
127 bool ret = do_write_reg_command(nullptr,
128 L"HKEY_CURRENT_USER\\Environment",
129 L"TEMP",
130 (const char *)MULTI_SZ_TEST_DATA,
131 0,
132 REG_NONE);
133 assert_true(ret);
134}
135
136const unsigned char BINARY_TEST_DATA[] = {0x30, 0, 0x02, 0x80, 0x12, 0, 0, 0};
137
138void test_output_reg_command_binary(void **state) {
139 expect_memory(__wrap_write_output,
140 out,
141 L"reg add \"HKEY_DYN_DATA\\Control Panel\\Desktop\" /v \"UserPreferencesMask\" "
142 L"/t REG_BINARY /d 300002ff12000000 /f",
143 108);
144 will_return(__wrap_write_output, true);
145 bool ret = do_write_reg_command(nullptr,
146 L"HKEY_DYN_DATA\\Control Panel\\Desktop",
147 L"UserPreferencesMask",
148 (const char *)BINARY_TEST_DATA,
149 sizeof(BINARY_TEST_DATA),
150 REG_BINARY);
151 assert_true(ret);
152}
153
154void test_output_reg_command_default_key(void **state) {
155 expect_memory(__wrap_write_output,
156 out,
157 L"reg add \"HKEY_DYN_DATA\\Control Panel\\Desktop\" /ve "
158 L"/t REG_SZ /d \"out\" /f",
159 108);
160 will_return(__wrap_write_output, true);
161 wchar_t *data = L"out";
162 bool ret = do_write_reg_command(nullptr,
163 L"HKEY_DYN_DATA\\Control Panel\\Desktop",
164 nullptr,
165 (const char *)data,
166 4 * sizeof(wchar_t),
167 REG_SZ);
168 assert_true(ret);
169
170 expect_memory(__wrap_write_output,
171 out,
172 L"reg add \"HKEY_DYN_DATA\\Control Panel\\Desktop\" /ve "
173 L"/t REG_SZ /d \"out\" /f",
174 108);
175 will_return(__wrap_write_output, true);
176 ret = do_write_reg_command(nullptr,
177 L"HKEY_DYN_DATA\\Control Panel\\Desktop",
178 L"",
179 (const char *)data,
180 4 * sizeof(wchar_t),
181 REG_SZ);
182 assert_true(ret);
183
184 expect_memory(__wrap_write_output,
185 out,
186 L"reg add \"HKEY_DYN_DATA\\Control Panel\\Desktop\" /ve "
187 L"/t REG_SZ /d \"out\" /f",
188 108);
189 will_return(__wrap_write_output, true);
190 ret = do_write_reg_command(nullptr,
191 L"HKEY_DYN_DATA\\Control Panel\\Desktop",
192 L"(default)",
193 (const char *)data,
194 4 * sizeof(wchar_t),
195 REG_SZ);
196 assert_true(ret);
197}
198
199void test_output_reg_command_skip_too_big(void **state) {
200 wchar_t *buf = calloc(CMD_MAX_COMMAND_LENGTH + 2000, sizeof(wchar_t));
201 wmemset(buf, L'a', CMD_MAX_COMMAND_LENGTH + 2000);
202 bool ret = do_write_reg_command(nullptr,
203 L"HKEY_CURRENT_USER\\Control Panel\\Desktop",
204 L"UserPreferencesMask",
205 (const char *)buf,
206 CMD_MAX_COMMAND_LENGTH + 2000,
207 REG_BINARY);
208 free(buf);
209 assert_true(ret);
210 assert_int_equal(errno, EKEYREJECTED);
211}
212
213const struct CMUnitTest output_reg_command_tests[] = {
214 cmocka_unit_test(test_output_reg_command_binary),
215 cmocka_unit_test(test_output_reg_command_default_key),
216 cmocka_unit_test(test_output_reg_command_dword),
217 cmocka_unit_test(test_output_reg_command_expand_sz),
218 cmocka_unit_test(test_output_reg_command_invalid_type),
219 cmocka_unit_test(test_output_reg_command_multi_sz),
220#ifndef ASAN_ENABLED
221 cmocka_unit_test(test_output_reg_command_multi_sz_invalid),
222#endif
223 cmocka_unit_test(test_output_reg_command_none),
224 cmocka_unit_test(test_output_reg_command_qword),
225 cmocka_unit_test(test_output_reg_command_skip_too_big),
226 cmocka_unit_test(test_output_reg_command_sz),
227};
228
229int main(int argc, char *argv[]) {
230 return cmocka_run_group_tests(output_reg_command_tests, nullptr, nullptr);
231}