testsuite: Add cmocka unit test for smb_krb5_kt_open()
authorAndreas Schneider <asn@samba.org>
Thu, 15 Dec 2016 09:33:59 +0000 (10:33 +0100)
committerJeremy Allison <jra@samba.org>
Fri, 16 Dec 2016 04:43:10 +0000 (05:43 +0100)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
Autobuild-User(master): Jeremy Allison <jra@samba.org>
Autobuild-Date(master): Fri Dec 16 05:43:12 CET 2016 on sn-devel-144

selftest/tests.py
testsuite/unittests/test_krb5_samba.c [new file with mode: 0644]
testsuite/unittests/wscript [new file with mode: 0644]
wscript
wscript_build

index 04a8df2d95056e896c22eb2cea177582ca1d1404..eabe71401fcfa7dfc9f39c44e89f7c595d11eaed 100644 (file)
@@ -26,13 +26,20 @@ except KeyError:
     samba4bindir = bindir()
     config_h = os.path.join(samba4bindir, "default/include/config.h")
 
-# define here var to check what we support
+# check available features
+config_hash = dict()
 f = open(config_h, 'r')
 try:
-    have_man_pages_support = ("XSLTPROC_MANPAGES 1" in f.read())
+    lines = f.readlines()
+    config_hash = dict((x[0], ' '.join(x[1:]))
+            for x in map(lambda line: line.strip().split(' ')[1:],
+                         filter(lambda line: (line[0:7] == '#define') and (len(line.split(' ')) > 2), lines)))
 finally:
     f.close()
 
+have_man_pages_support = ("XSLTPROC_MANPAGES" in config_hash)
+with_cmocka = ("HAVE_CMOCKA" in config_hash)
+
 planpythontestsuite("none", "samba.tests.source")
 if have_man_pages_support:
     planpythontestsuite("none", "samba.tests.docs")
@@ -123,3 +130,7 @@ planpythontestsuite("none", "samba.tests.kcc.graph_utils")
 planpythontestsuite("none", "samba.tests.kcc.kcc_utils")
 planpythontestsuite("none", "samba.tests.kcc.ldif_import_export")
 plantestsuite("wafsamba.duplicate_symbols", "none", [os.path.join(srcdir(), "buildtools/wafsamba/test_duplicate_symbol.sh")])
+
+if with_cmocka:
+    plantestsuite("samba.unittests.krb5samba", "none",
+                  [os.path.join(bindir(), "default/testsuite/unittests/test_krb5samba")])
diff --git a/testsuite/unittests/test_krb5_samba.c b/testsuite/unittests/test_krb5_samba.c
new file mode 100644 (file)
index 0000000..8b7e843
--- /dev/null
@@ -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/wscript b/testsuite/unittests/wscript
new file mode 100644 (file)
index 0000000..ea4af07
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+
+import os
+
+def configure(conf):
+    pkg_name = 'cmocka'
+    pkg_minversion = '1.0'
+
+    conf.CHECK_BUNDLED_SYSTEM_PKG(pkg_name, minversion=pkg_minversion)
+
+def build(bld):
+    if bld.CONFIG_SET('HAVE_CMOCKA'):
+        bld.SAMBA_BINARY('test_krb5samba',
+                         source='test_krb5_samba.c',
+                         deps='krb5samba cmocka')
diff --git a/wscript b/wscript
index 9b0ee3912dd54e83a66a76031cbd294327a35f68..9168db122ae66f0287ec34bde289ad5bb5c7ffff 100644 (file)
--- a/wscript
+++ b/wscript
@@ -188,6 +188,7 @@ def configure(conf):
     if conf.env.with_ctdb:
         conf.RECURSE('ctdb')
     conf.RECURSE('lib/socket')
+    conf.RECURSE('testsuite/unittests')
 
     conf.SAMBA_CHECK_UNDEFINED_SYMBOL_FLAGS()
 
index 93b1832cf906ba592158c4148f669a7cbaacfc32..0c3a2aee864bbb132c9bc81fcfaabac152b8117c 100644 (file)
@@ -122,6 +122,7 @@ bld.RECURSE('libcli/samsync')
 bld.RECURSE('libcli/registry')
 bld.RECURSE('source4/lib/policy')
 bld.RECURSE('libcli/named_pipe_auth')
+bld.RECURSE('testsuite/unittests')
 
 if bld.CONFIG_GET('KRB5_VENDOR') in (None, 'heimdal'):
     if bld.CONFIG_GET("HEIMDAL_KRB5_CONFIG") and bld.CONFIG_GET("USING_SYSTEM_KRB5"):