ctdb-scripts: Move database handling to its own event script
[samba4-gss.git] / source3 / registry / tests / test_regfio.c
blobe835e65e0dff912e9bdb2d02324de6f7026b399d
1 /*
2 * Unix SMB/CIFS implementation.
4 * Copyright (C) 2019 Michael Hanselmann <public@hansmi.ch>
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.
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.
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/>.
20 #include <stdarg.h>
21 #include <stddef.h>
22 #include <setjmp.h>
23 #include <stdint.h>
24 #include <cmocka.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
32 #include "includes.h"
33 #include "lib/replace/replace.h"
34 #include "system/filesys.h"
35 #include "lib/util/samba_util.h"
36 #include "registry/regfio.h"
38 struct test_ctx {
39 char *tmp_regfile;
40 int tmp_regfile_fd;
41 REGF_FILE *rb;
44 static int setup_context(void **state)
46 struct test_ctx *test_ctx;
48 test_ctx = talloc_zero(NULL, struct test_ctx);
49 assert_non_null(test_ctx);
51 test_ctx->tmp_regfile_fd = -1;
53 *state = test_ctx;
55 return 0;
58 static int setup_context_tempfile(void **state)
60 struct test_ctx *test_ctx;
61 int ret;
63 ret = setup_context(state);
65 if (ret == 0) {
66 test_ctx = talloc_get_type_abort(*state, struct test_ctx);
68 test_ctx->tmp_regfile = talloc_strdup(test_ctx, "/tmp/regfio.XXXXXX");
69 assert_non_null(test_ctx->tmp_regfile);
71 test_ctx->tmp_regfile_fd = mkstemp(test_ctx->tmp_regfile);
72 assert_return_code(test_ctx->tmp_regfile_fd, errno);
75 return ret;
78 static int teardown_context(void **state)
80 struct test_ctx *test_ctx =
81 talloc_get_type_abort(*state, struct test_ctx);
83 if (test_ctx->rb) {
84 regfio_close(test_ctx->rb);
87 if (test_ctx->tmp_regfile) {
88 unlink(test_ctx->tmp_regfile);
91 if (test_ctx->tmp_regfile_fd != -1) {
92 close(test_ctx->tmp_regfile_fd);
95 talloc_free(test_ctx);
97 return 0;
100 static void open_testfile(struct test_ctx *test_ctx, const char *filename)
102 char *path;
104 path = talloc_asprintf(test_ctx, "%s/testdata/samba3/%s", SRCDIR, filename);
105 assert_non_null(path);
107 test_ctx->rb = regfio_open(path, O_RDONLY, 0600);
108 assert_non_null(test_ctx->rb);
111 static void test_regfio_open_new_file(void **state)
113 struct test_ctx *test_ctx =
114 talloc_get_type_abort(*state, struct test_ctx);
115 REGF_NK_REC *root;
116 struct regval_ctr *values;
117 struct regsubkey_ctr *subkeys;
118 WERROR werr;
120 test_ctx->rb = regfio_open(test_ctx->tmp_regfile,
121 O_RDWR | O_CREAT | O_TRUNC, 0600);
122 assert_non_null(test_ctx->rb);
124 root = regfio_rootkey(test_ctx->rb);
125 assert_null(root);
127 werr = regsubkey_ctr_init(NULL, &subkeys);
128 assert_true(W_ERROR_IS_OK(werr));
130 werr = regval_ctr_init(subkeys, &values);
131 assert_true(W_ERROR_IS_OK(werr));
133 /* Write root key */
134 regfio_write_key(test_ctx->rb, "", values, subkeys, NULL, NULL);
136 root = regfio_rootkey(test_ctx->rb);
137 assert_non_null(root);
138 assert_memory_equal(root->header, "nk", sizeof(root->header));
139 assert_int_equal(root->key_type, NK_TYPE_ROOTKEY);
142 static void test_regfio_corrupt_hbin(void **state)
144 struct test_ctx *test_ctx =
145 talloc_get_type_abort(*state, struct test_ctx);
146 REGF_NK_REC *root;
148 open_testfile(test_ctx, "regfio_corrupt_hbin1.dat");
150 root = regfio_rootkey(test_ctx->rb);
151 assert_null(root);
154 static void test_regfio_corrupt_lf_subkeys(void **state)
156 struct test_ctx *test_ctx =
157 talloc_get_type_abort(*state, struct test_ctx);
158 REGF_NK_REC *root, *subkey;
160 open_testfile(test_ctx, "regfio_corrupt_lf_subkeys.dat");
162 root = regfio_rootkey(test_ctx->rb);
163 assert_non_null(root);
165 root->subkey_index = 0;
166 while ((subkey = regfio_fetch_subkey(test_ctx->rb, root))) {
170 int main(void) {
171 const struct CMUnitTest tests[] = {
172 cmocka_unit_test_setup_teardown(test_regfio_open_new_file,
173 setup_context_tempfile,
174 teardown_context),
175 cmocka_unit_test_setup_teardown(test_regfio_corrupt_hbin,
176 setup_context,
177 teardown_context),
178 cmocka_unit_test_setup_teardown(test_regfio_corrupt_lf_subkeys,
179 setup_context,
180 teardown_context),
183 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
185 return cmocka_run_group_tests(tests, NULL, NULL);