winprefs v0.3.2
A registry exporter for programmers.
Loading...
Searching...
No Matches
test_export_single.c
1#include "registry.h"
2
3const enum OUTPUT_FORMAT formats[] = {
4 OUTPUT_FORMAT_C, OUTPUT_FORMAT_C_SHARP, OUTPUT_FORMAT_POWERSHELL, OUTPUT_FORMAT_REG};
5
6void test_export_single_value_invalid_format(void **state) {
7 will_return(__wrap_RegOpenKeyEx, ERROR_SUCCESS);
8 will_return(__wrap_RegQueryValueEx, ERROR_SUCCESS);
9 will_return(__wrap_GetStdHandle, 0);
10 bool ret = export_single_value(
11 HKEY_CURRENT_USER, L"HKEY_CURRENT_USER\\A\\B\\C", OUTPUT_FORMAT_UNKNOWN, nullptr);
12 assert_false(ret);
13 assert_int_equal(errno, EINVAL);
14}
15
16void test_export_single_value_formats(void **state) {
17 will_return_always(__wrap_RegOpenKeyEx, ERROR_SUCCESS);
18 will_return_always(__wrap_RegQueryValueEx, ERROR_SUCCESS);
19 will_return_always(__wrap_GetStdHandle, 0);
20 will_return(__wrap_do_write_reg_command, true);
21 will_return(__wrap_do_write_c_reg_code, true);
22 will_return(__wrap_do_write_c_sharp_reg_code, true);
23 will_return(__wrap_do_write_powershell_reg_code, true);
24 size_t i;
25 for (i = 0; i < 4; i++) {
26 bool ret = export_single_value(
27 HKEY_CURRENT_USER, L"HKEY_CURRENT_USER\\A\\B\\C", formats[i], nullptr);
28 assert_true(ret);
29 }
30}
31
32void test_export_single_value_formats_fail(void **state) {
33 will_return_always(__wrap_RegOpenKeyEx, ERROR_SUCCESS);
34 will_return_always(__wrap_RegQueryValueEx, ERROR_SUCCESS);
35 will_return_always(__wrap_GetStdHandle, 0);
36 will_return(__wrap_do_write_reg_command, false);
37 will_return(__wrap_do_write_c_reg_code, false);
38 will_return(__wrap_do_write_c_sharp_reg_code, false);
39 will_return(__wrap_do_write_powershell_reg_code, false);
40 size_t i;
41 for (i = 0; i < 4; i++) {
42 bool ret = export_single_value(
43 HKEY_CURRENT_USER, L"HKEY_CURRENT_USER\\A\\B\\C", formats[i], nullptr);
44 assert_false(ret);
45 }
46}
47
48void test_export_single_value_RegOpenKeyEx_fail(void **state) {
49 will_return(__wrap_RegOpenKeyEx, ERROR_ACCESS_DENIED);
50 bool ret = export_single_value(
51 HKEY_CURRENT_USER, L"HKEY_CURRENT_USER\\A\\B\\C", OUTPUT_FORMAT_REG, nullptr);
52 assert_false(ret);
53}
54
55void test_export_single_value_RegQueryValueEx_too_large(void **state) {
56 will_return(__wrap_RegOpenKeyEx, ERROR_SUCCESS);
57 will_return(__wrap_RegQueryValueEx, ERROR_MORE_DATA);
58 bool ret = export_single_value(
59 HKEY_CURRENT_USER, L"HKEY_CURRENT_USER\\A\\B\\C", OUTPUT_FORMAT_REG, nullptr);
60 assert_false(ret);
61}
62
63void test_export_single_value_RegQueryValueEx_fail(void **state) {
64 will_return(__wrap_RegOpenKeyEx, ERROR_SUCCESS);
65 will_return(__wrap_RegQueryValueEx, ERROR_ACCESS_DENIED);
66 bool ret = export_single_value(
67 HKEY_CURRENT_USER, L"HKEY_CURRENT_USER\\A\\B\\C", OUTPUT_FORMAT_REG, nullptr);
68 assert_false(ret);
69}
70
71void test_export_single_value_no_backslash(void **state) {
72 bool ret = export_single_value(HKEY_CURRENT_USER, L"", OUTPUT_FORMAT_REG, nullptr);
73 assert_false(ret);
74 assert_int_equal(errno, EINVAL);
75}
76
77void test_export_single_value_null_reg_path(void **state) {
78 bool ret = export_single_value(HKEY_CURRENT_USER, nullptr, OUTPUT_FORMAT_REG, nullptr);
79 assert_false(ret);
80 assert_int_equal(errno, EINVAL);
81}
82
83const struct CMUnitTest export_single_value_tests[] = {
84 cmocka_unit_test(test_export_single_value_RegOpenKeyEx_fail),
85 cmocka_unit_test(test_export_single_value_RegQueryValueEx_fail),
86 cmocka_unit_test(test_export_single_value_RegQueryValueEx_too_large),
87 cmocka_unit_test(test_export_single_value_formats),
88 cmocka_unit_test(test_export_single_value_formats_fail),
89 cmocka_unit_test(test_export_single_value_no_backslash),
90 cmocka_unit_test(test_export_single_value_null_reg_path),
91 cmocka_unit_test(test_export_single_value_invalid_format),
92};
93
94int main(int argc, char *argv[]) {
95 return cmocka_run_group_tests(export_single_value_tests, nullptr, nullptr);
96}