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/>.
27 #include <sys/types.h>
32 #include "lib/replace/replace.h"
33 #include "lib/util/samba_util.h"
34 #include "registry/regfio.h"
42 static int setup_context(void **state)
44 struct test_ctx *test_ctx;
46 test_ctx = talloc_zero(NULL, struct test_ctx);
47 assert_non_null(test_ctx);
49 test_ctx->tmp_regfile_fd = -1;
56 static int setup_context_tempfile(void **state)
58 struct test_ctx *test_ctx;
61 ret = setup_context(state);
64 test_ctx = talloc_get_type_abort(*state, struct test_ctx);
66 test_ctx->tmp_regfile = talloc_strdup(test_ctx, "/tmp/regfio.XXXXXX");
67 assert_non_null(test_ctx->tmp_regfile);
69 test_ctx->tmp_regfile_fd = mkstemp(test_ctx->tmp_regfile);
70 assert_return_code(test_ctx->tmp_regfile_fd, errno);
76 static int teardown_context(void **state)
78 struct test_ctx *test_ctx =
79 talloc_get_type_abort(*state, struct test_ctx);
82 regfio_close(test_ctx->rb);
85 if (test_ctx->tmp_regfile) {
86 unlink(test_ctx->tmp_regfile);
89 if (test_ctx->tmp_regfile_fd != -1) {
90 close(test_ctx->tmp_regfile_fd);
93 talloc_free(test_ctx);
98 static void open_testfile(struct test_ctx *test_ctx, const char *filename)
102 path = talloc_asprintf(test_ctx, "%s/testdata/samba3/%s", SRCDIR, filename);
103 assert_non_null(path);
105 test_ctx->rb = regfio_open(path, O_RDONLY, 0600);
106 assert_non_null(test_ctx->rb);
109 static void test_regfio_open_new_file(void **state)
111 struct test_ctx *test_ctx =
112 talloc_get_type_abort(*state, struct test_ctx);
114 struct regval_ctr *values;
115 struct regsubkey_ctr *subkeys;
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);
122 root = regfio_rootkey(test_ctx->rb);
125 werr = regsubkey_ctr_init(NULL, &subkeys);
126 assert_true(W_ERROR_IS_OK(werr));
128 werr = regval_ctr_init(subkeys, &values);
129 assert_true(W_ERROR_IS_OK(werr));
132 regfio_write_key(test_ctx->rb, "", values, subkeys, NULL, NULL);
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);
140 static void test_regfio_corrupt_hbin(void **state)
142 struct test_ctx *test_ctx =
143 talloc_get_type_abort(*state, struct test_ctx);
146 open_testfile(test_ctx, "regfio_corrupt_hbin1.dat");
148 root = regfio_rootkey(test_ctx->rb);
152 static void test_regfio_corrupt_lf_subkeys(void **state)
154 struct test_ctx *test_ctx =
155 talloc_get_type_abort(*state, struct test_ctx);
156 REGF_NK_REC *root, *subkey;
158 open_testfile(test_ctx, "regfio_corrupt_lf_subkeys.dat");
160 root = regfio_rootkey(test_ctx->rb);
161 assert_non_null(root);
163 root->subkey_index = 0;
164 while ((subkey = regfio_fetch_subkey(test_ctx->rb, root))) {
169 const struct CMUnitTest tests[] = {
170 cmocka_unit_test_setup_teardown(test_regfio_open_new_file,
171 setup_context_tempfile,
173 cmocka_unit_test_setup_teardown(test_regfio_corrupt_hbin,
176 cmocka_unit_test_setup_teardown(test_regfio_corrupt_lf_subkeys,
181 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
183 return cmocka_run_group_tests(tests, NULL, NULL);