summaryrefslogtreecommitdiff
path: root/lib/util/tests/test_util_paths.c
blob: 4dfe11c1445546e16fb888e6357b259205bfee5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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 "lib/replace/replace.h"
#include <talloc.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;
}