regfio tests: Update comment style to match README.Coding
[metze/samba/wip.git] / source3 / registry / tests / test_regfio.c
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * Copyright (C) 2019      Michael Hanselmann <public@hansmi.ch>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdarg.h>
21 #include <stddef.h>
22 #include <setjmp.h>
23 #include <cmocka.h>
24
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30
31 #include "includes.h"
32 #include "lib/replace/replace.h"
33 #include "lib/util/samba_util.h"
34 #include "registry/regfio.h"
35
36 struct test_ctx {
37         char *tmp_regfile;
38         int tmp_regfile_fd;
39         REGF_FILE *rb;
40 };
41
42 static int setup_context(void **state)
43 {
44         struct test_ctx *test_ctx;
45
46         test_ctx = talloc_zero(NULL, struct test_ctx);
47         assert_non_null(test_ctx);
48
49         test_ctx->tmp_regfile_fd  = -1;
50
51         *state = test_ctx;
52
53         return 0;
54 }
55
56 static int setup_context_tempfile(void **state)
57 {
58         struct test_ctx *test_ctx;
59         int ret;
60
61         ret = setup_context(state);
62
63         if (ret == 0) {
64                 test_ctx = talloc_get_type_abort(*state, struct test_ctx);
65
66                 test_ctx->tmp_regfile = talloc_strdup(test_ctx, "/tmp/regfio.XXXXXX");
67                 assert_non_null(test_ctx->tmp_regfile);
68
69                 test_ctx->tmp_regfile_fd = mkstemp(test_ctx->tmp_regfile);
70                 assert_return_code(test_ctx->tmp_regfile_fd, errno);
71         }
72
73         return ret;
74 }
75
76 static int teardown_context(void **state)
77 {
78         struct test_ctx *test_ctx =
79                 talloc_get_type_abort(*state, struct test_ctx);
80
81         if (test_ctx->rb) {
82                 regfio_close(test_ctx->rb);
83         }
84
85         if (test_ctx->tmp_regfile) {
86                 unlink(test_ctx->tmp_regfile);
87         }
88
89         if (test_ctx->tmp_regfile_fd != -1) {
90                 close(test_ctx->tmp_regfile_fd);
91         }
92
93         talloc_free(test_ctx);
94
95         return 0;
96 }
97
98 static void open_testfile(struct test_ctx *test_ctx, const char *filename)
99 {
100         char *path;
101
102         path = talloc_asprintf(test_ctx, "%s/testdata/samba3/%s", SRCDIR, filename);
103         assert_non_null(path);
104
105         test_ctx->rb = regfio_open(path, O_RDONLY, 0600);
106         assert_non_null(test_ctx->rb);
107 }
108
109 static void test_regfio_open_new_file(void **state)
110 {
111         struct test_ctx *test_ctx =
112                 talloc_get_type_abort(*state, struct test_ctx);
113         REGF_NK_REC *root;
114         struct regval_ctr *values;
115         struct regsubkey_ctr *subkeys;
116         WERROR werr;
117
118         test_ctx->rb = regfio_open(test_ctx->tmp_regfile,
119                                    O_RDWR | O_CREAT | O_TRUNC, 0600);
120         assert_non_null(test_ctx->rb);
121
122         root = regfio_rootkey(test_ctx->rb);
123         assert_null(root);
124
125         werr = regsubkey_ctr_init(NULL, &subkeys);
126         assert_true(W_ERROR_IS_OK(werr));
127
128         werr = regval_ctr_init(subkeys, &values);
129         assert_true(W_ERROR_IS_OK(werr));
130
131         /* Write root key */
132         regfio_write_key(test_ctx->rb, "", values, subkeys, NULL, NULL);
133
134         root = regfio_rootkey(test_ctx->rb);
135         assert_non_null(root);
136         assert_memory_equal(root->header, "nk", sizeof(root->header));
137         assert_int_equal(root->key_type, NK_TYPE_ROOTKEY);
138 }
139
140 static void test_regfio_corrupt_hbin(void **state)
141 {
142         struct test_ctx *test_ctx =
143                 talloc_get_type_abort(*state, struct test_ctx);
144         REGF_NK_REC *root;
145
146         open_testfile(test_ctx, "regfio_corrupt_hbin1.dat");
147
148         root = regfio_rootkey(test_ctx->rb);
149         assert_null(root);
150 }
151
152 static void test_regfio_corrupt_lf_subkeys(void **state)
153 {
154         struct test_ctx *test_ctx =
155                 talloc_get_type_abort(*state, struct test_ctx);
156         REGF_NK_REC *root, *subkey;
157
158         open_testfile(test_ctx, "regfio_corrupt_lf_subkeys.dat");
159
160         root = regfio_rootkey(test_ctx->rb);
161         assert_non_null(root);
162
163         root->subkey_index = 0;
164         while ((subkey = regfio_fetch_subkey(test_ctx->rb, root))) {
165         }
166 }
167
168 int main(void) {
169         const struct CMUnitTest tests[] = {
170                 cmocka_unit_test_setup_teardown(test_regfio_open_new_file,
171                                                 setup_context_tempfile,
172                                                 teardown_context),
173                 cmocka_unit_test_setup_teardown(test_regfio_corrupt_hbin,
174                                                 setup_context,
175                                                 teardown_context),
176                 cmocka_unit_test_setup_teardown(test_regfio_corrupt_lf_subkeys,
177                                                 setup_context,
178                                                 teardown_context),
179         };
180
181         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
182
183         return cmocka_run_group_tests(tests, NULL, NULL);
184 }