summaryrefslogtreecommitdiffstats
path: root/testsuite/unittests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:20:00 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:20:00 +0000
commit8daa83a594a2e98f39d764422bfbdbc62c9efd44 (patch)
tree4099e8021376c7d8c05bdf8503093d80e9c7bad0 /testsuite/unittests
parentInitial commit. (diff)
downloadsamba-8daa83a594a2e98f39d764422bfbdbc62c9efd44.tar.xz
samba-8daa83a594a2e98f39d764422bfbdbc62c9efd44.zip
Adding upstream version 2:4.20.0+dfsg.upstream/2%4.20.0+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testsuite/unittests')
-rw-r--r--testsuite/unittests/rpc_test_dummy_module.c21
-rw-r--r--testsuite/unittests/test_background_send.c82
-rw-r--r--testsuite/unittests/test_krb5_samba.c145
-rw-r--r--testsuite/unittests/test_lib_util_modules.c66
-rw-r--r--testsuite/unittests/wscript36
5 files changed, 350 insertions, 0 deletions
diff --git a/testsuite/unittests/rpc_test_dummy_module.c b/testsuite/unittests/rpc_test_dummy_module.c
new file mode 100644
index 0000000..2b4e6e2
--- /dev/null
+++ b/testsuite/unittests/rpc_test_dummy_module.c
@@ -0,0 +1,21 @@
+#include "replace.h"
+#include <sys/types.h>
+#include <signal.h>
+#include <unistd.h>
+#include "libcli/util/ntstatus.h"
+
+NTSTATUS samba_init_module(void);
+NTSTATUS samba_init_module(void)
+{
+ int rc;
+
+ fprintf(stderr, "Test dummy executed!\n");
+
+ rc = setenv("UNITTEST_DUMMY_MODULE_LOADED", "TRUE", 1);
+ if (rc < 0) {
+ kill(getpid(), SIGILL);
+ exit(-1);
+ }
+
+ return NT_STATUS_OK;
+}
diff --git a/testsuite/unittests/test_background_send.c b/testsuite/unittests/test_background_send.c
new file mode 100644
index 0000000..ea65428
--- /dev/null
+++ b/testsuite/unittests/test_background_send.c
@@ -0,0 +1,82 @@
+/*
+ * Unix SMB/CIFS implementation.
+ *
+ * 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 "replace.h"
+#include <assert.h>
+#include "source3/lib/background.h"
+#include "source3/include/messages.h"
+#include "lib/util/talloc_stack.h"
+#include "source3/param/loadparm.h"
+#include "dynconfig/dynconfig.h"
+
+static int bg_trigger(void *private_data)
+{
+ return 1;
+}
+
+static void test_background_send(void)
+{
+ TALLOC_CTX *frame = talloc_stackframe();
+ struct tevent_context *ev = NULL;
+ struct messaging_context *msg_ctx = NULL;
+ struct tevent_req *req = NULL;
+ uint32_t ping_msg = MSG_PING;
+
+ ev = tevent_context_init(frame);
+ assert(ev != NULL);
+
+ msg_ctx = messaging_init(frame, ev);
+ assert(msg_ctx != NULL);
+
+ req = background_job_send(
+ frame, ev, msg_ctx, &ping_msg, 1, 0, bg_trigger, NULL);
+ assert(req != NULL);
+
+ /*
+ * Here's the core of this test: TALLOC_FREE msg_ctx before
+ * req. This happens if you use background_job_send() smbd and
+ * don't manually TALLOC_FREE req before exit_server()
+ */
+ TALLOC_FREE(msg_ctx);
+ TALLOC_FREE(req);
+
+ TALLOC_FREE(frame);
+}
+
+int main(int argc, const char *argv[])
+{
+ const char testname[] = "test_background_send";
+ bool ok;
+
+ if (argc != 2) {
+ fprintf(stderr, "usage: %s <configfile>\n", argv[0]);
+ return 1;
+ }
+
+ printf("test: %s\n", testname);
+
+ ok = lp_load_initial_only(argv[1]);
+ if (!ok) {
+ fprintf(stderr, "lp_load_initial_only(%s) failed\n", argv[1]);
+ return 1;
+ }
+
+ test_background_send(); /* crashes on failure */
+
+ printf("success: %s\n", testname);
+ return 0;
+}
diff --git a/testsuite/unittests/test_krb5_samba.c b/testsuite/unittests/test_krb5_samba.c
new file mode 100644
index 0000000..8b7e843
--- /dev/null
+++ b/testsuite/unittests/test_krb5_samba.c
@@ -0,0 +1,145 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <krb5.h>
+
+#include "includes.h"
+#include "lib/krb5_wrap/krb5_samba.h"
+
+
+static int setup_krb5_context(void **state)
+{
+ krb5_context context = NULL;
+ krb5_error_code code;
+
+ code = krb5_init_context(&context);
+ assert_return_code(code, code);
+
+ *state = context;
+
+ return 0;
+}
+
+static int teardown_krb5_context(void **state)
+{
+ krb5_context context = *state;
+
+ if (context != NULL) {
+ krb5_free_context(context);
+ }
+ return 0;
+}
+
+static void test_smb_krb5_kt_open(void **state)
+{
+ krb5_context context = *state;
+ krb5_keytab keytab = NULL;
+ krb5_error_code code;
+ char keytab_template[] = "/tmp/keytab.XXXXXX";
+ int fd;
+
+ fd = mkstemp(keytab_template);
+ assert_return_code(fd, errno);
+ unlink(keytab_template);
+
+ code = smb_krb5_kt_open(context,
+ keytab_template,
+ false,
+ &keytab);
+ assert_int_equal(code, 0);
+
+ krb5_kt_close(context, keytab);
+ close(fd);
+}
+
+static void test_smb_krb5_kt_open_file(void **state)
+{
+ krb5_context context = *state;
+ krb5_keytab keytab = NULL;
+ krb5_error_code code;
+ char keytab_template[] = "/tmp/keytab.XXXXXX";
+ char keytab_file[6 + strlen(keytab_template)];
+ int fd;
+
+ fd = mkstemp(keytab_template);
+ assert_return_code(fd, errno);
+ unlink(keytab_template);
+
+ snprintf(keytab_file, sizeof(keytab_file), "FILE:%s", keytab_template);
+
+ code = smb_krb5_kt_open(context,
+ keytab_file,
+ false,
+ &keytab);
+ assert_int_equal(code, 0);
+
+ krb5_kt_close(context, keytab);
+ close(fd);
+}
+
+static void test_smb_krb5_kt_open_fail(void **state)
+{
+ krb5_context context = *state;
+ krb5_keytab keytab = NULL;
+ krb5_error_code code;
+
+ code = smb_krb5_kt_open(context,
+ NULL,
+ false,
+ &keytab);
+ assert_int_equal(code, KRB5_KT_BADNAME);
+ code = smb_krb5_kt_open(context,
+ "wurst",
+ false,
+ &keytab);
+ assert_int_equal(code, KRB5_KT_BADNAME);
+
+ code = smb_krb5_kt_open(context,
+ "FILE:wurst",
+ false,
+ &keytab);
+ assert_int_equal(code, KRB5_KT_BADNAME);
+
+ code = smb_krb5_kt_open(context,
+ "WRFILE:wurst",
+ false,
+ &keytab);
+ assert_int_equal(code, KRB5_KT_BADNAME);
+}
+
+static void test_smb_krb5_kt_open_relative_memory(void **state)
+{
+ krb5_context context = *state;
+ krb5_keytab keytab = NULL;
+ krb5_error_code code;
+
+ code = smb_krb5_kt_open_relative(context,
+ NULL,
+ true,
+ &keytab);
+ assert_int_equal(code, 0);
+
+ krb5_kt_close(context, keytab);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open,
+ setup_krb5_context,
+ teardown_krb5_context),
+ cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_file,
+ setup_krb5_context,
+ teardown_krb5_context),
+ cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_fail,
+ setup_krb5_context,
+ teardown_krb5_context),
+ cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_relative_memory,
+ setup_krb5_context,
+ teardown_krb5_context),
+ };
+
+ cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/testsuite/unittests/test_lib_util_modules.c b/testsuite/unittests/test_lib_util_modules.c
new file mode 100644
index 0000000..647fc1f
--- /dev/null
+++ b/testsuite/unittests/test_lib_util_modules.c
@@ -0,0 +1,66 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <talloc.h>
+
+#include "include/config.h"
+#include "libcli/util/ntstatus.h"
+#include "lib/util/samba_modules.h"
+
+static int teardown(void **state)
+{
+ unsetenv("UNITTEST_DUMMY_MODULE_LOADED");
+
+ return 0;
+}
+
+static void test_samba_module_probe_dummy(void **state)
+{
+ const char *module_env;
+ NTSTATUS status;
+
+ status = smb_probe_module("rpc", "test_dummy_module");
+ assert_true(NT_STATUS_IS_OK(status));
+
+ module_env = getenv("UNITTEST_DUMMY_MODULE_LOADED");
+ assert_non_null(module_env);
+ assert_string_equal(module_env, "TRUE");
+}
+
+static void test_samba_module_probe_slash(void **state)
+{
+ char dummy_module_path[4096] = {0};
+ const char *module_env;
+ NTSTATUS status;
+
+ snprintf(dummy_module_path,
+ sizeof(dummy_module_path),
+ "%s/bin/modules/rpc/test_dummy_module.so",
+ SRCDIR);
+
+ status = smb_probe_module("rpc", dummy_module_path);
+ assert_true(NT_STATUS_IS_ERR(status));
+
+ module_env = getenv("UNITTEST_DUMMY_MODULE_LOADED");
+ assert_null(module_env);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test_teardown(test_samba_module_probe_dummy,
+ teardown),
+ cmocka_unit_test_teardown(test_samba_module_probe_slash,
+ teardown),
+ };
+
+ cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/testsuite/unittests/wscript b/testsuite/unittests/wscript
new file mode 100644
index 0000000..10ab024
--- /dev/null
+++ b/testsuite/unittests/wscript
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+import os
+
+def configure(conf):
+ return
+
+def build(bld):
+ bld.SAMBA_BINARY('test_krb5samba',
+ source='test_krb5_samba.c',
+ deps='krb5samba cmocka',
+ for_selftest=True)
+
+ bld.SAMBA_BINARY('test_lib_util_modules',
+ source='test_lib_util_modules.c',
+ deps='''
+ samba-modules
+ cmocka
+ ''',
+ for_selftest=True)
+
+ bld.SAMBA_BINARY('test_background_send',
+ source='test_background_send.c',
+ deps='''
+ samba3core
+ ''',
+ for_selftest=True)
+
+ bld.SAMBA_MODULE('rpc_test_dummy_module',
+ source='rpc_test_dummy_module.c',
+ deps='ndr smbd_base RPC_SERVICE RPC_SOCK_HELPER',
+ subsystem='rpc',
+ allow_undefined_symbols=True,
+ init_function='',
+ internal_module=False,
+ install=False)