summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Slebodnik <lslebodn@redhat.com>2014-05-28 12:54:52 +0200
committerJakub Hrozek <jhrozek@redhat.com>2014-06-03 12:57:22 +0200
commit9cd08bf4b843529c14f71655d62687589301f198 (patch)
treee871b730a5e3a1e357b5170e1a6150adfa913310
parent00fb34246a4d1d2cb846a6e2a40d8155ee5a36a1 (diff)
downloadsssd-9cd08bf4b843529c14f71655d62687589301f198.tar.gz
sssd-9cd08bf4b843529c14f71655d62687589301f198.tar.xz
sssd-9cd08bf4b843529c14f71655d62687589301f198.zip
TEST: Add test for expand homedir
Reviewed-by: Pavel Reichl <preichl@redhat.com>
-rw-r--r--src/tests/cmocka/test_utils.c190
1 files changed, 190 insertions, 0 deletions
diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c
index 2d9094307..1bb095bdb 100644
--- a/src/tests/cmocka/test_utils.c
+++ b/src/tests/cmocka/test_utils.c
@@ -23,12 +23,26 @@
#include <popt.h>
#include "tests/cmocka/common_mock.h"
+#include "util/sss_nss.h"
#define DOM_COUNT 10
#define DOMNAME_TMPL "name_%zu.dom"
#define FLATNAME_TMPL "name_%zu"
#define SID_TMPL "S-1-5-21-1-2-%zu"
+#define MACRO_EXPAND(tok) #tok
+#define STR(tok) MACRO_EXPAND(tok)
+
+#define USERNAME "sssduser"
+#define UID 1234
+#define DOMAIN "sssddomain"
+#define ORIGINAL_HOME "/home/user"
+#define FLATNAME "flatname"
+#define HOMEDIR_SUBSTR "/mnt/home"
+
+#define DUMMY "dummy"
+#define DUMMY2 "dummy2"
+
struct dom_list_test_ctx {
size_t dom_count;
struct sss_domain_info *dom_list;
@@ -212,6 +226,178 @@ void test_sss_filter_sanitize_for_dom(void **state)
talloc_free(lc_sanitized);
}
+void check_expanded_value(TALLOC_CTX *tmp_ctx,
+ struct sss_nss_homedir_ctx *homedir_ctx,
+ const char *template, const char *exp_val)
+{
+ char *homedir;
+
+ homedir = expand_homedir_template(tmp_ctx, template, homedir_ctx);
+ if (exp_val != NULL) {
+ assert_string_equal(homedir, exp_val);
+ } else {
+ assert_null(homedir);
+ }
+
+ talloc_free(homedir);
+}
+
+void setup_homedir_ctx(void **state)
+{
+ struct sss_nss_homedir_ctx *homedir_ctx;
+
+ assert_true(leak_check_setup());
+
+ homedir_ctx= talloc_zero(global_talloc_context,
+ struct sss_nss_homedir_ctx);
+ assert_non_null(homedir_ctx);
+
+ homedir_ctx->username = USERNAME;
+ homedir_ctx->uid = UID;
+ homedir_ctx->original = ORIGINAL_HOME;
+ homedir_ctx->domain = DOMAIN;
+ homedir_ctx->flatname = FLATNAME;
+ homedir_ctx->config_homedir_substr = HOMEDIR_SUBSTR;
+
+ check_leaks_push(homedir_ctx);
+ *state = homedir_ctx;
+}
+
+void teardown_homedir_ctx(void **state)
+{
+ struct sss_nss_homedir_ctx *homedir_ctx = talloc_get_type(*state,
+ struct sss_nss_homedir_ctx);
+ if (homedir_ctx == NULL) {
+ DEBUG(SSSDBG_CRIT_FAILURE, "Type mismatch\n");
+ return;
+ }
+
+ assert_true(check_leaks_pop(homedir_ctx) == true);
+ talloc_free(homedir_ctx);
+ assert_true(leak_check_teardown());
+}
+
+void test_expand_homedir_template_NULL(void **state)
+{
+ TALLOC_CTX *tmp_ctx;
+ char *homedir;
+ struct sss_nss_homedir_ctx *homedir_ctx;
+
+ /* following format strings requires data in homedir_ctx */
+ const char *format_strings[] = { "%u", "%U", "%d", "%f", "%F", "%H",
+ NULL };
+ int i;
+
+ tmp_ctx = talloc_new(NULL);
+ assert_non_null(tmp_ctx);
+
+ homedir_ctx = talloc_zero(tmp_ctx, struct sss_nss_homedir_ctx);
+ assert_non_null(homedir_ctx);
+
+ homedir = expand_homedir_template(tmp_ctx, NULL, NULL);
+ assert_null(homedir);
+
+ homedir = expand_homedir_template(tmp_ctx, "template", NULL);
+ assert_null(homedir);
+
+ /* missing data in homedir_ctx */
+ check_expanded_value(tmp_ctx, homedir_ctx, "%%", "%");
+ check_expanded_value(tmp_ctx, homedir_ctx, "%o", "");
+
+ for (i = 0; format_strings[i] != NULL; ++i) {
+ check_expanded_value(tmp_ctx, homedir_ctx, format_strings[i], NULL);
+ }
+
+ /* flatname requires domain and username */
+ homedir_ctx->username = DUMMY;
+ check_expanded_value(tmp_ctx, homedir_ctx, "%f", NULL);
+
+ homedir_ctx->username = NULL;
+ homedir_ctx->domain = DUMMY;
+ check_expanded_value(tmp_ctx, homedir_ctx, "%f", NULL);
+
+ /* test unknown format string */
+ check_expanded_value(tmp_ctx, homedir_ctx, "%x", NULL);
+
+ /* test malformed format string */
+ check_expanded_value(tmp_ctx, homedir_ctx, "%", NULL);
+
+ talloc_free(tmp_ctx);
+}
+
+void test_expand_homedir_template(void **state)
+{
+ struct sss_nss_homedir_ctx *homedir_ctx = talloc_get_type(*state,
+ struct sss_nss_homedir_ctx);
+ TALLOC_CTX *tmp_ctx;
+
+ tmp_ctx = talloc_new(NULL);
+ assert_non_null(tmp_ctx);
+
+ /* string without template */
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY, DUMMY);
+
+ check_expanded_value(tmp_ctx, homedir_ctx, "%u", USERNAME);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%u", DUMMY USERNAME);
+ check_expanded_value(tmp_ctx, homedir_ctx, "%u"DUMMY, USERNAME DUMMY);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%u"DUMMY2,
+ DUMMY USERNAME DUMMY2);
+
+ check_expanded_value(tmp_ctx, homedir_ctx, "%U", STR(UID));
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%U", DUMMY STR(UID));
+ check_expanded_value(tmp_ctx, homedir_ctx, "%U"DUMMY, STR(UID) DUMMY);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%U"DUMMY2,
+ DUMMY STR(UID) DUMMY2);
+
+ check_expanded_value(tmp_ctx, homedir_ctx, "%d", DOMAIN);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%d", DUMMY DOMAIN);
+ check_expanded_value(tmp_ctx, homedir_ctx, "%d"DUMMY, DOMAIN DUMMY);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%d"DUMMY2,
+ DUMMY DOMAIN DUMMY2);
+
+ check_expanded_value(tmp_ctx, homedir_ctx, "%f", USERNAME"@"DOMAIN);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%f",
+ DUMMY USERNAME"@"DOMAIN);
+ check_expanded_value(tmp_ctx, homedir_ctx, "%f"DUMMY,
+ USERNAME"@"DOMAIN DUMMY);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%f"DUMMY2,
+ DUMMY USERNAME"@"DOMAIN DUMMY2);
+
+ check_expanded_value(tmp_ctx, homedir_ctx, "%o", ORIGINAL_HOME);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%o", DUMMY ORIGINAL_HOME);
+ check_expanded_value(tmp_ctx, homedir_ctx, "%o"DUMMY, ORIGINAL_HOME DUMMY);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%o"DUMMY2,
+ DUMMY ORIGINAL_HOME DUMMY2);
+
+ check_expanded_value(tmp_ctx, homedir_ctx, "%F", FLATNAME);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%F", DUMMY FLATNAME);
+ check_expanded_value(tmp_ctx, homedir_ctx, "%F"DUMMY, FLATNAME DUMMY);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%F"DUMMY2,
+ DUMMY FLATNAME DUMMY2);
+
+ check_expanded_value(tmp_ctx, homedir_ctx, "%H", HOMEDIR_SUBSTR);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%H",
+ DUMMY HOMEDIR_SUBSTR);
+ check_expanded_value(tmp_ctx, homedir_ctx, "%H"DUMMY,
+ HOMEDIR_SUBSTR DUMMY);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%H"DUMMY2,
+ DUMMY HOMEDIR_SUBSTR DUMMY2);
+
+ check_expanded_value(tmp_ctx, homedir_ctx, "%%", "%");
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%%", DUMMY"%");
+ check_expanded_value(tmp_ctx, homedir_ctx, "%%"DUMMY, "%"DUMMY);
+ check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%%"DUMMY2,
+ DUMMY"%"DUMMY2);
+
+ /* test all format strings */
+ check_expanded_value(tmp_ctx, homedir_ctx,
+ DUMMY"/%u/%U/%d/%f/%o/%F/%%/%H/"DUMMY2,
+ DUMMY"/"USERNAME"/" STR(UID) "/"DOMAIN"/"
+ USERNAME"@"DOMAIN"/"ORIGINAL_HOME"/"FLATNAME"/%/"
+ HOMEDIR_SUBSTR"/"DUMMY2);
+ talloc_free(tmp_ctx);
+}
+
int main(int argc, const char *argv[])
{
poptContext pc;
@@ -232,6 +418,10 @@ int main(int argc, const char *argv[])
unit_test_setup_teardown(test_sss_filter_sanitize_for_dom,
setup_dom_list, teardown_dom_list),
+
+ unit_test(test_expand_homedir_template_NULL),
+ unit_test_setup_teardown(test_expand_homedir_template,
+ setup_homedir_ctx, teardown_homedir_ctx),
};
/* Set debug level to invalid value so we can deside if -d 0 was used. */