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/>.
28 #include <sys/types.h>
33 #include "lib/replace/replace.h"
34 #include "system/filesys.h"
35 #include "lib/util/samba_util.h"
36 #include "registry/regfio.h"
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;
58 static int setup_context_tempfile(void **state
)
60 struct test_ctx
*test_ctx
;
63 ret
= setup_context(state
);
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
);
78 static int teardown_context(void **state
)
80 struct test_ctx
*test_ctx
=
81 talloc_get_type_abort(*state
, struct test_ctx
);
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
);
100 static void open_testfile(struct test_ctx
*test_ctx
, const char *filename
)
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
);
116 struct regval_ctr
*values
;
117 struct regsubkey_ctr
*subkeys
;
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
);
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
));
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
);
148 open_testfile(test_ctx
, "regfio_corrupt_hbin1.dat");
150 root
= regfio_rootkey(test_ctx
->rb
);
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
))) {
171 const struct CMUnitTest tests
[] = {
172 cmocka_unit_test_setup_teardown(test_regfio_open_new_file
,
173 setup_context_tempfile
,
175 cmocka_unit_test_setup_teardown(test_regfio_corrupt_hbin
,
178 cmocka_unit_test_setup_teardown(test_regfio_corrupt_lf_subkeys
,
183 cmocka_set_message_output(CM_OUTPUT_SUBUNIT
);
185 return cmocka_run_group_tests(tests
, NULL
, NULL
);