winprefs v0.3.2
A registry exporter for programmers.
Loading...
Searching...
No Matches
test_output_powershell.c
1#include "constants.h"
2#include "powershell.h"
3
4void test_powershell_null_escaped_reg_key(void **state) {
5 DWORD *test_val = malloc(sizeof(DWORD));
6 test_val[0] = 4;
7 bool ret = do_write_powershell_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_powershell_dword(void **state) {
18 expect_memory(
19 __wrap_write_output,
20 out,
21 L"if (!(Test-Path 'HKEY_CURRENT_USER:\\Control Panel\\Desktop')) { New-Item -Path "
22 L"'HKEY_CURRENT_USER:\\Control Panel\\Desktop' -Force | Out-Null } "
23 L"New-ItemProperty -LiteralPath 'HKEY_CURRENT_USER:\\Control Panel\\Desktop' -Name "
24 L"'DoubleClickHeight' -PropertyType DWord -Force -Value 4",
25 275);
26 will_return(__wrap_write_output, true);
27 DWORD *test_val = malloc(sizeof(DWORD));
28 test_val[0] = 4;
29 bool ret = do_write_powershell_reg_code(nullptr,
30 L"HKEY_CURRENT_USER\\Control Panel\\Desktop",
31 L"DoubleClickHeight",
32 (const char *)test_val,
33 sizeof(DWORD),
34 REG_DWORD);
35 assert_true(ret);
36 free(test_val);
37}
38
39void test_powershell_qword(void **state) {
40 expect_memory(
41 __wrap_write_output,
42 out,
43 L"if (!(Test-Path 'HKEY_CURRENT_USER:\\Control Panel\\Desktop')) { New-Item -Path "
44 L"'HKEY_CURRENT_USER:\\Control Panel\\Desktop' -Force | Out-Null } "
45 L"New-ItemProperty -LiteralPath 'HKEY_CURRENT_USER:\\Control Panel\\Desktop' -Name "
46 L"'DoubleClickHeight' -PropertyType QWord -Force -Value 4",
47 275);
48 will_return(__wrap_write_output, true);
49 QWORD *test_val = malloc(sizeof(QWORD));
50 test_val[0] = 4;
51 bool ret = do_write_powershell_reg_code(nullptr,
52 L"HKEY_CURRENT_USER\\Control Panel\\Desktop",
53 L"DoubleClickHeight",
54 (const char *)test_val,
55 sizeof(QWORD),
56 REG_QWORD);
57 assert_true(ret);
58 free(test_val);
59}
60
61void test_powershell_sz(void **state) {
62 expect_memory(__wrap_write_output,
63 out,
64 L"if (!(Test-Path 'HKEY_CURRENT_USER:\\Environment')) { New-Item -Path "
65 L"'HKEY_CURRENT_USER:\\Environment' -Force | Out-Null } "
66 L"New-ItemProperty -LiteralPath 'HKEY_CURRENT_USER:\\Environment' -Name "
67 L"'TEMP' -PropertyType String -Force -Value 'a midsummer night''s dream'",
68 275);
69 will_return(__wrap_write_output, true);
70 bool ret = do_write_powershell_reg_code(nullptr,
71 L"HKEY_CURRENT_USER\\Environment",
72 L"TEMP",
73 (const char *)L"a midsummer night's dream",
74 26 * sizeof(wchar_t),
75 REG_SZ);
76 assert_true(ret);
77}
78
79void test_powershell_expand_sz(void **state) {
80 expect_memory(__wrap_write_output,
81 out,
82 L"if (!(Test-Path 'HKEY_CURRENT_USER:\\Environment')) { New-Item -Path "
83 L"'HKEY_CURRENT_USER:\\Environment' -Force | Out-Null } "
84 L"New-ItemProperty -LiteralPath 'HKEY_CURRENT_USER:\\Environment' -Name "
85 L"'TEMP' -PropertyType ExpandString -Force -Value 'a midsummer night''s %dream%'",
86 275);
87 will_return(__wrap_write_output, true);
88 bool ret = do_write_powershell_reg_code(nullptr,
89 L"HKEY_CURRENT_USER\\Environment",
90 L"TEMP",
91 (const char *)L"a midsummer night's %dream%",
92 26 * sizeof(wchar_t),
93 REG_EXPAND_SZ);
94 assert_true(ret);
95}
96
97const wchar_t *MULTI_SZ_TEST_DATA = L"a midsummer night's dream\0test2\0";
98
99void test_powershell_multi_sz(void **state) {
100 expect_memory(
101 __wrap_write_output,
102 out,
103 L"if (!(Test-Path 'HKEY_CURRENT_USER:\\Environment')) { New-Item -Path "
104 L"'HKEY_CURRENT_USER:\\Environment' -Force | Out-Null } "
105 L"New-ItemProperty -LiteralPath 'HKEY_CURRENT_USER:\\Environment' -Name "
106 L"'TEMP' -PropertyType String -Force -Value @\"\na midsummer night''s dream\ntest2\n)\n\"@",
107 50 * sizeof(wchar_t));
108 will_return(__wrap_write_output, true);
109 bool ret = do_write_powershell_reg_code(nullptr,
110 L"HKEY_CURRENT_USER\\Environment",
111 L"TEMP",
112 (const char *)MULTI_SZ_TEST_DATA,
113 33 * sizeof(wchar_t),
114 REG_MULTI_SZ);
115 assert_true(ret);
116}
117
118void test_powershell_none(void **state) {
119 expect_memory(__wrap_write_output,
120 out,
121 L"if (!(Test-Path 'HKEY_CURRENT_USER:\\Environment')) { New-Item -Path "
122 L"'HKEY_CURRENT_USER:\\Environment' -Force | Out-Null } "
123 L"New-ItemProperty -LiteralPath 'HKEY_CURRENT_USER:\\Environment' -Name "
124 L"'TEMP' -PropertyType None -Force -Value $null",
125 237);
126 will_return(__wrap_write_output, true);
127 bool ret = do_write_powershell_reg_code(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 int BINARY_TEST_DATA[] = {0x30, 0, 0x02, 0x80, 0x12, 0, 0, 0};
137
138void test_powershell_binary(void **state) {
139 expect_memory(
140 __wrap_write_output,
141 out,
142 L"if (!(Test-Path 'HKEY_CURRENT_USER:\\Control Panel\\Desktop')) { New-Item -Path "
143 L"'HKEY_CURRENT_USER:\\Control Panel\\Desktop' -Force | Out-Null } New-ItemProperty "
144 L"-LiteralPath 'HKEY_CURRENT_USER:\\Control Panel\\Desktop' -Name 'UserPreferencesMask' "
145 L"-PropertyType Binary -Force -Value "
146 L"(byte[]](0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x80,0x00,0x00,0x00,"
147 L"0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0)",
148 445);
149 will_return(__wrap_write_output, true);
150 bool ret = do_write_powershell_reg_code(nullptr,
151 L"HKEY_CURRENT_USER\\Control Panel\\Desktop",
152 L"UserPreferencesMask",
153 (const char *)BINARY_TEST_DATA,
154 sizeof(BINARY_TEST_DATA),
155 REG_BINARY);
156 assert_true(ret);
157}
158
159void test_powershell_skip_too_big(void **state) {
160 wchar_t *buf = calloc(POWERSHELL_MAX_COMMAND_LENGTH + 500, sizeof(wchar_t));
161 wmemset(buf, L'a', POWERSHELL_MAX_COMMAND_LENGTH + 500);
162 bool ret = do_write_powershell_reg_code(nullptr,
163 L"HKEY_CURRENT_USER\\Control Panel\\Desktop",
164 L"UserPreferencesMask",
165 (const char *)buf,
166 POWERSHELL_MAX_COMMAND_LENGTH + 500,
167 REG_BINARY);
168 assert_true(ret);
169 free(buf);
170}
171
172const unsigned char MULTI_SZ_TEST_DATA_INVALID[] = {'e', 0, 'n', 0, '-', 0, 'U', 0, 'S'};
173
174void test_powershell_multi_sz_invalid(void **state) {
175 bool ret = do_write_powershell_reg_code(nullptr,
176 L"HKEY_USERS\\Environment",
177 L"TEMP",
178 (const char *)MULTI_SZ_TEST_DATA_INVALID,
179 9,
180 REG_MULTI_SZ);
181 assert_true(ret);
182}
183
184const struct CMUnitTest powershell_tests[] = {
185 cmocka_unit_test(test_powershell_binary),
186 cmocka_unit_test(test_powershell_dword),
187 cmocka_unit_test(test_powershell_expand_sz),
188 cmocka_unit_test(test_powershell_multi_sz),
189#ifndef ASAN_ENABLED
190 cmocka_unit_test(test_powershell_multi_sz_invalid),
191#endif
192 cmocka_unit_test(test_powershell_none),
193 cmocka_unit_test(test_powershell_null_escaped_reg_key),
194 cmocka_unit_test(test_powershell_qword),
195 cmocka_unit_test(test_powershell_skip_too_big),
196 cmocka_unit_test(test_powershell_sz),
197};
198
199int main(int argc, char *argv[]) {
200 return cmocka_run_group_tests(powershell_tests, nullptr, nullptr);
201}