]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
regfio: Add trivial unit test
authorMichael Hanselmann <public@hansmi.ch>
Mon, 18 Mar 2019 23:47:52 +0000 (00:47 +0100)
committerKarolin Seeger <kseeger@samba.org>
Tue, 2 Apr 2019 09:10:55 +0000 (09:10 +0000)
An upcoming commit will resolve two cases of insufficient handling of
mangled registry hive files and will include unit tests.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=13840

Signed-off-by: Michael Hanselmann <public@hansmi.ch>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
(cherry picked from commit 9b2cb845b23cd1c91ab3b5ea8ad791b18b3ab733)

selftest/tests.py
source3/registry/tests/test_regfio.c [new file with mode: 0644]
source3/wscript_build

index 89e5ff43507ed4c42732538cf8a361b0a0e9cf1b..7dbc0a9871faf922f7be6431f5a4c7576400c02e 100644 (file)
@@ -208,3 +208,5 @@ plantestsuite("samba.unittests.ms_fnmatch", "none",
               [os.path.join(bindir(), "default/lib/util/test_ms_fnmatch")])
 plantestsuite("samba.unittests.ntlm_check", "none",
               [os.path.join(bindir(), "default/libcli/auth/test_ntlm_check")])
+plantestsuite("samba.unittests.test_registry_regfio", "none",
+              [os.path.join(bindir(), "default/source3/test_registry_regfio")])
diff --git a/source3/registry/tests/test_regfio.c b/source3/registry/tests/test_regfio.c
new file mode 100644 (file)
index 0000000..ba557a3
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * Unix SMB/CIFS implementation.
+ *
+ * Copyright (C) 2019      Michael Hanselmann <public@hansmi.ch>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "includes.h"
+#include "lib/replace/replace.h"
+#include "lib/util/samba_util.h"
+#include "registry/regfio.h"
+
+struct test_ctx {
+       char *tmp_regfile;
+       int tmp_regfile_fd;
+       REGF_FILE *rb;
+};
+
+static int setup_context(void **state)
+{
+       struct test_ctx *test_ctx;
+
+       test_ctx = talloc_zero(NULL, struct test_ctx);
+       assert_non_null(test_ctx);
+
+       test_ctx->tmp_regfile_fd  = -1;
+
+       *state = test_ctx;
+
+       return 0;
+}
+
+static int setup_context_tempfile(void **state)
+{
+       struct test_ctx *test_ctx;
+       int ret;
+
+       ret = setup_context(state);
+
+       if (ret == 0) {
+               test_ctx = talloc_get_type_abort(*state, struct test_ctx);
+
+               test_ctx->tmp_regfile = talloc_strdup(test_ctx, "/tmp/regfio.XXXXXX");
+               assert_non_null(test_ctx->tmp_regfile);
+
+               test_ctx->tmp_regfile_fd = mkstemp(test_ctx->tmp_regfile);
+               assert_return_code(test_ctx->tmp_regfile_fd, errno);
+       }
+
+       return ret;
+}
+
+static int teardown_context(void **state)
+{
+       struct test_ctx *test_ctx =
+               talloc_get_type_abort(*state, struct test_ctx);
+
+       if (test_ctx->rb) {
+               regfio_close(test_ctx->rb);
+       }
+
+       if (test_ctx->tmp_regfile) {
+               unlink(test_ctx->tmp_regfile);
+       }
+
+       if (test_ctx->tmp_regfile_fd != -1) {
+               close(test_ctx->tmp_regfile_fd);
+       }
+
+       talloc_free(test_ctx);
+
+       return 0;
+}
+
+static void test_regfio_open_new_file(void **state)
+{
+       struct test_ctx *test_ctx =
+               talloc_get_type_abort(*state, struct test_ctx);
+       REGF_NK_REC *root;
+       struct regval_ctr *values;
+       struct regsubkey_ctr *subkeys;
+       WERROR werr;
+
+       test_ctx->rb = regfio_open(test_ctx->tmp_regfile,
+                                  O_RDWR | O_CREAT | O_TRUNC, 0600);
+       assert_non_null(test_ctx->rb);
+
+       root = regfio_rootkey(test_ctx->rb);
+       assert_null(root);
+
+       werr = regsubkey_ctr_init(NULL, &subkeys);
+       assert_true(W_ERROR_IS_OK(werr));
+
+       werr = regval_ctr_init(subkeys, &values);
+       assert_true(W_ERROR_IS_OK(werr));
+
+       // Write root key
+       regfio_write_key(test_ctx->rb, "", values, subkeys, NULL, NULL);
+
+       root = regfio_rootkey(test_ctx->rb);
+       assert_non_null(root);
+       assert_memory_equal(root->header, "nk", sizeof(root->header));
+       assert_int_equal(root->key_type, NK_TYPE_ROOTKEY);
+}
+
+int main(void) {
+       const struct CMUnitTest tests[] = {
+               cmocka_unit_test_setup_teardown(test_regfio_open_new_file,
+                                               setup_context_tempfile,
+                                               teardown_context),
+       };
+
+       cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
+
+       return cmocka_run_group_tests(tests, NULL, NULL);
+}
index f0d85969692aae7c13e9569a07f8e53bd8ff905a..f67ce59fe52d00630200e91d6832c9bd2266e87c 100644 (file)
@@ -204,6 +204,12 @@ bld.SAMBA3_SUBSYSTEM('REGFIO',
                     source='registry/regfio.c',
                     deps='samba-util REG_PARSE_PRS')
 
+bld.SAMBA_BINARY('test_registry_regfio',
+                 source='registry/tests/test_regfio.c',
+                 deps='cmocka samba3-util smbconf REGFIO',
+                 local_include=False,
+                 install=False)
+
 bld.SAMBA3_SUBSYSTEM('REG_API_REGF',
                     source='registry/reg_api_regf.c',
                     deps='samba-util')