lib:util: Add test for path_expand_tilde()
authorAndreas Schneider <asn@samba.org>
Mon, 11 May 2020 10:50:11 +0000 (12:50 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Fri, 15 May 2020 13:18:38 +0000 (13:18 +0000)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14370

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
lib/util/tests/test_util_paths.c [new file with mode: 0644]
lib/util/wscript_build
selftest/tests.py

diff --git a/lib/util/tests/test_util_paths.c b/lib/util/tests/test_util_paths.c
new file mode 100644 (file)
index 0000000..b89abf0
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * Unix SMB/CIFS implementation.
+ *
+ * Copyright (C) 2020      Andreas Schneider <asn@samba.org>
+ *
+ * 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 <stdint.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <talloc.h>
+
+#include "lib/replace/replace.h"
+#include "lib/util/util_paths.c"
+
+static int setup(void **state)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(NULL);
+
+       assert_non_null(mem_ctx);
+       *state = mem_ctx;
+
+       return 0;
+}
+
+static int teardown(void **state)
+{
+       TALLOC_CTX *mem_ctx = *state;
+       TALLOC_FREE(mem_ctx);
+
+    return 0;
+}
+
+static void test_get_user_home_dir(void **state)
+{
+       TALLOC_CTX *mem_ctx = *state;
+       struct passwd *pwd = getpwuid(getuid());
+       char *user;
+
+       user = get_user_home_dir(mem_ctx);
+       assert_non_null(user);
+       assert_string_equal(user, pwd->pw_dir);
+
+       TALLOC_FREE(user);
+}
+
+static void test_path_expand_tilde(void **state)
+{
+       TALLOC_CTX *mem_ctx = *state;
+       char h[256] = {0};
+       char *d = NULL;
+       const char *user = NULL;
+       char *home = NULL;
+
+       user = getenv("USER");
+       if (user == NULL){
+               user = getenv("LOGNAME");
+       }
+
+       /* In certain CIs there no such variables */
+       if (user == NULL) {
+               struct passwd *pw = getpwuid(getuid());
+               if (pw){
+                       user = pw->pw_name;
+               }
+       }
+
+       home = getenv("HOME");
+       assert_non_null(home);
+       snprintf(h, sizeof(h), "%s/.cache", home);
+
+       d = path_expand_tilde(mem_ctx, "~/.cache");
+       assert_non_null(d);
+       assert_string_equal(d, h);
+       TALLOC_FREE(d);
+
+       snprintf(h, sizeof(h), "%s/.cache/X~", home);
+       d = path_expand_tilde(mem_ctx, "~/.cache/X~");
+       assert_string_equal(d, h);
+       TALLOC_FREE(d);
+
+       d = path_expand_tilde(mem_ctx, "/guru/meditation");
+       assert_non_null(d);
+       assert_string_equal(d, "/guru/meditation");
+       TALLOC_FREE(d);
+
+       snprintf(h, sizeof(h), "~%s/.cache", user);
+       d = path_expand_tilde(mem_ctx, h);
+       assert_non_null(d);
+
+       snprintf(h, sizeof(h), "%s/.cache", home);
+       assert_string_equal(d, h);
+       TALLOC_FREE(d);
+}
+
+int main(int argc, char *argv[])
+{
+       int rc;
+       const struct CMUnitTest tests[] = {
+               cmocka_unit_test(test_get_user_home_dir),
+               cmocka_unit_test(test_path_expand_tilde),
+       };
+
+       if (argc == 2) {
+               cmocka_set_test_filter(argv[1]);
+       }
+       cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
+
+       rc = cmocka_run_group_tests(tests, setup, teardown);
+
+       return rc;
+}
index fc760ff81aa6ccdc54e75c0ebdf93b59d65d1b01..807c62636fd996897a4f12a4e0bfee69b6cf46cc 100644 (file)
@@ -299,3 +299,9 @@ else:
                      deps='cmocka replace samba-util',
                      local_include=False,
                      for_selftest=True)
+
+    bld.SAMBA_BINARY('test_util_paths',
+                     source='tests/test_util_paths.c',
+                     deps='cmocka replace talloc samba-util',
+                     local_include=False,
+                     for_selftest=True)
index e5fc25f7cec6c385f794e1bf7e4096b0758cc2ca..73932cd28beb6ac1cd9d26815dbc4e9e443caf95 100644 (file)
@@ -393,6 +393,8 @@ plantestsuite("samba.unittests.bytearray", "none",
               [os.path.join(bindir(), "default/lib/util/test_bytearray")])
 plantestsuite("samba.unittests.byteorder_verify", "none",
               [os.path.join(bindir(), "default/lib/util/test_byteorder_verify")])
+plantestsuite("samba.unittests.util_paths", "none",
+              [os.path.join(bindir(), "default/lib/util/test_util_paths")])
 plantestsuite("samba.unittests.ntlm_check", "none",
               [os.path.join(bindir(), "default/libcli/auth/test_ntlm_check")])
 plantestsuite("samba.unittests.gnutls", "none",