s4:torture: Initialize struct cldap_netlogon
[Samba.git] / lib / util / tests / test_util_paths.c
blob4dfe11c1445546e16fb888e6357b259205bfee5f
1 /*
2 * Unix SMB/CIFS implementation.
4 * Copyright (C) 2020 Andreas Schneider <asn@samba.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdarg.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <setjmp.h>
24 #include <cmocka.h>
26 #include "lib/replace/replace.h"
27 #include <talloc.h>
29 #include "lib/util/util_paths.c"
31 static int setup(void **state)
33 TALLOC_CTX *mem_ctx = talloc_new(NULL);
35 assert_non_null(mem_ctx);
36 *state = mem_ctx;
38 return 0;
41 static int teardown(void **state)
43 TALLOC_CTX *mem_ctx = *state;
44 TALLOC_FREE(mem_ctx);
46 return 0;
49 static void test_get_user_home_dir(void **state)
51 TALLOC_CTX *mem_ctx = *state;
52 struct passwd *pwd = getpwuid(getuid());
53 char *user;
55 user = get_user_home_dir(mem_ctx);
56 assert_non_null(user);
57 assert_string_equal(user, pwd->pw_dir);
59 TALLOC_FREE(user);
62 static void test_path_expand_tilde(void **state)
64 TALLOC_CTX *mem_ctx = *state;
65 char h[256] = {0};
66 char *d = NULL;
67 const char *user = NULL;
68 char *home = NULL;
70 user = getenv("USER");
71 if (user == NULL){
72 user = getenv("LOGNAME");
75 /* In certain CIs there no such variables */
76 if (user == NULL) {
77 struct passwd *pw = getpwuid(getuid());
78 if (pw){
79 user = pw->pw_name;
83 home = getenv("HOME");
84 assert_non_null(home);
85 snprintf(h, sizeof(h), "%s/.cache", home);
87 d = path_expand_tilde(mem_ctx, "~/.cache");
88 assert_non_null(d);
89 assert_string_equal(d, h);
90 TALLOC_FREE(d);
92 snprintf(h, sizeof(h), "%s/.cache/X~", home);
93 d = path_expand_tilde(mem_ctx, "~/.cache/X~");
94 assert_string_equal(d, h);
95 TALLOC_FREE(d);
97 d = path_expand_tilde(mem_ctx, "/guru/meditation");
98 assert_non_null(d);
99 assert_string_equal(d, "/guru/meditation");
100 TALLOC_FREE(d);
102 snprintf(h, sizeof(h), "~%s/.cache", user);
103 d = path_expand_tilde(mem_ctx, h);
104 assert_non_null(d);
106 snprintf(h, sizeof(h), "%s/.cache", home);
107 assert_string_equal(d, h);
108 TALLOC_FREE(d);
111 int main(int argc, char *argv[])
113 int rc;
114 const struct CMUnitTest tests[] = {
115 cmocka_unit_test(test_get_user_home_dir),
116 cmocka_unit_test(test_path_expand_tilde),
119 if (argc == 2) {
120 cmocka_set_test_filter(argv[1]);
122 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
124 rc = cmocka_run_group_tests(tests, setup, teardown);
126 return rc;