winprefs v0.3.2
A registry exporter for programmers.
Loading...
Searching...
No Matches
test_output_c.c
1#include "constants.h"
2#include "reg_code.h"
3
4void test_output_c_null_escaped_reg_key(void **state) {
5 DWORD *test_val = malloc(sizeof(DWORD));
6 test_val[0] = 4;
7 bool ret = do_write_c_reg_code(nullptr,
8 L"HKEY_CURRENT_USER", // invalid argument
9 L"DoubleClickHeight",
10 (const char *)test_val,
11 sizeof(DWORD),
12 REG_DWORD);
13 assert_false(ret);
14 free(test_val);
15}
16
17void test_output_c_dword(void **state) {
18 expect_memory(__wrap_write_output,
19 out,
20 L"dnum = 4; RegSetKeyValue(HKEY_CLASSES_ROOT, TEXT(\"Control Panel\\Desktop\"), "
21 L"TEXT(\"DoubleClickHeight\"), REG_DWORD, (LPCVOID)&dnum, sizeof(DWORD));",
22 64 * sizeof(wchar_t));
23 will_return(__wrap_write_output, true);
24 DWORD *test_val = malloc(sizeof(DWORD));
25 test_val[0] = 4;
26 bool ret = do_write_c_reg_code(nullptr,
27 L"HKEY_CLASSES_ROOT\\Control Panel\\Desktop",
28 L"DoubleClickHeight",
29 (const char *)test_val,
30 sizeof(DWORD),
31 REG_DWORD);
32 assert_true(ret);
33 free(test_val);
34}
35
36void test_output_c_qword(void **state) {
37 expect_memory(__wrap_write_output,
38 out,
39 L"qnum = 4; RegSetKeyValue(HKEY_LOCAL_MACHINE, TEXT(\"Control Panel\\Desktop\"), "
40 L"TEXT(\"DoubleClickHeight\"), REG_QWORD, (LPCVOID)&qnum, sizeof(QWORD));",
41 64 * sizeof(wchar_t));
42 will_return(__wrap_write_output, true);
43 QWORD *test_val = malloc(sizeof(QWORD));
44 test_val[0] = 4;
45 bool ret = do_write_c_reg_code(nullptr,
46 L"HKEY_LOCAL_MACHINE\\Control Panel\\Desktop",
47 L"DoubleClickHeight",
48 (const char *)test_val,
49 sizeof(QWORD),
50 REG_QWORD);
51 assert_true(ret);
52 free(test_val);
53}
54
55void test_output_c_sz(void **state) {
56 expect_memory(
57 __wrap_write_output,
58 out,
59 L"RegSetKeyValue(HKEY_CURRENT_CONFIG, TEXT(\"Environment\"), TEXT(\"TEMP\"), REG_SZ, "
60 L"TEXT(\"\\\"quoted \\\\string\\\" fff\"), 104);",
61 50 * sizeof(wchar_t));
62 will_return(__wrap_write_output, true);
63 bool ret = do_write_c_reg_code(nullptr,
64 L"HKEY_CURRENT_CONFIG\\Environment",
65 L"TEMP",
66 (const char *)L"\"quoted \\string\" fff",
67 26 * sizeof(wchar_t),
68 REG_SZ);
69 assert_true(ret);
70}
71
72void test_output_c_expand_sz(void **state) {
73 expect_memory(
74 __wrap_write_output,
75 out,
76 L"RegSetKeyValue(HKEY_CURRENT_USER, TEXT(\"Environment\"), TEXT(\"TEMP\"), REG_EXPAND_SZ, "
77 L"TEXT(\"a midsummer night's %dream%\"), 52);",
78 50 * sizeof(wchar_t));
79 will_return(__wrap_write_output, true);
80 bool ret = do_write_c_reg_code(nullptr,
81 L"HKEY_CURRENT_USER\\Environment",
82 L"TEMP",
83 (const char *)L"a midsummer night's %dream%",
84 26 * sizeof(wchar_t),
85 REG_EXPAND_SZ);
86 assert_true(ret);
87}
88
89const wchar_t *MULTI_SZ_TEST_DATA = L"\"quoted string\" fff\0test2\0\0";
90
91void test_output_c_multi_sz(void **state) {
92 expect_memory(__wrap_write_output,
93 out,
94 L"RegSetKeyValue(HKEY_USERS, TEXT(\"Environment\"), TEXT(\"TEMP\"), "
95 L"REG_MULTI_SZ, TEXT(\"\\\"quoted string\\\" fff\\0test2\\0\\0\"), 54);",
96 50 * sizeof(wchar_t));
97 will_return(__wrap_write_output, true);
98 bool ret = do_write_c_reg_code(nullptr,
99 L"HKEY_USERS\\Environment",
100 L"TEMP",
101 (const char *)MULTI_SZ_TEST_DATA,
102 28 * sizeof(wchar_t),
103 REG_MULTI_SZ);
104 assert_true(ret);
105}
106
107const wchar_t *MULTI_SZ_TEST_DATA_INVALID = L"\"quoted string\" fff\0test2";
108
109void test_output_c_multi_sz_invalid(void **state) {
110 bool ret = do_write_c_reg_code(nullptr,
111 L"HKEY_USERS\\Environment",
112 L"TEMP",
113 (const char *)MULTI_SZ_TEST_DATA,
114 25 * sizeof(wchar_t),
115 REG_MULTI_SZ);
116 assert_true(ret);
117}
118
119void test_output_c_top_key_invalid(void **state) {
120 bool ret = do_write_c_reg_code(nullptr,
121 L"HKEY_USER_Z\\Environment",
122 L"TEMP",
123 (const char *)MULTI_SZ_TEST_DATA,
124 25 * sizeof(wchar_t),
125 REG_MULTI_SZ);
126 assert_false(ret);
127}
128
129void test_output_c_invalid_type(void **state) {
130 bool ret = do_write_c_reg_code(nullptr,
131 L"HKEY_USERS\\Environment",
132 L"TEMP",
133 (const char *)MULTI_SZ_TEST_DATA,
134 25 * sizeof(wchar_t),
135 10240);
136 assert_true(ret);
137 assert_int_equal(errno, EINVAL);
138}
139
140void test_output_c_none(void **state) {
141 expect_memory(__wrap_write_output,
142 out,
143 L"RegSetKeyValue(HKEY_CURRENT_USER, TEXT(\"Environment\"), TEXT(\"TEMP\"), "
144 L"REG_NONE, NULL, 0);",
145 89 * sizeof(wchar_t));
146 will_return(__wrap_write_output, true);
147 bool ret = do_write_c_reg_code(nullptr,
148 L"HKEY_CURRENT_USER\\Environment",
149 L"TEMP",
150 (const char *)MULTI_SZ_TEST_DATA,
151 0,
152 REG_NONE);
153 assert_true(ret);
154}
155
156const unsigned char BINARY_TEST_DATA[] = {0x30, 0, 0x02, 0x80, 0x12, 0, 0, 0};
157
158void test_output_c_binary(void **state) {
159 expect_memory(__wrap_write_output,
160 out,
161 L"data = { 0x30, 0x00, 0x02, 0x80, 0x12, 0x00, 0x00, 0x00 }; "
162 L"RegSetKeyValue(HKEY_DYN_DATA, TEXT(\"Control Panel\\\\Desktop\"), "
163 L"TEXT(\"UserPreferencesMask\"), REG_BINARY, (LPCVOID)&data, 8);",
164 182 * sizeof(wchar_t));
165 will_return(__wrap_write_output, true);
166 bool ret = do_write_c_reg_code(nullptr,
167 L"HKEY_DYN_DATA\\Control Panel\\Desktop",
168 L"UserPreferencesMask",
169 (const char *)BINARY_TEST_DATA,
170 sizeof(BINARY_TEST_DATA),
171 REG_BINARY);
172 assert_true(ret);
173}
174
175const struct CMUnitTest output_c_tests[] = {
176 cmocka_unit_test(test_output_c_binary),
177 cmocka_unit_test(test_output_c_dword),
178 cmocka_unit_test(test_output_c_expand_sz),
179 cmocka_unit_test(test_output_c_multi_sz),
180 cmocka_unit_test(test_output_c_multi_sz_invalid),
181 cmocka_unit_test(test_output_c_none),
182 cmocka_unit_test(test_output_c_null_escaped_reg_key),
183 cmocka_unit_test(test_output_c_qword),
184 cmocka_unit_test(test_output_c_sz),
185 cmocka_unit_test(test_output_c_top_key_invalid),
186 cmocka_unit_test(test_output_c_invalid_type),
187};
188
189int main(int argc, char *argv[]) {
190 return cmocka_run_group_tests(output_c_tests, nullptr, nullptr);
191}