forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mount-support-test.c
141 lines (127 loc) · 4.54 KB
/
mount-support-test.c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (C) 2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* 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 "mount-support.h"
#include "mount-support.c"
#include "mount-support-nvidia.h"
#include "mount-support-nvidia.c"
#include <glib.h>
#include <glib/gstdio.h>
static void replace_slashes_with_NUL(char *path, size_t len)
{
for (size_t i = 0; i < len; i++) {
if (path[i] == '/')
path[i] = '\0';
}
}
static void test_get_nextpath__typical(void)
{
char path[] = "/some/path";
size_t offset = 0;
size_t fulllen = strlen(path);
// Prepare path for usage with get_nextpath() by replacing
// all path separators with the NUL byte.
replace_slashes_with_NUL(path, fulllen);
// Run get_nextpath a few times to see what happens.
char *result;
result = get_nextpath(path, &offset, fulllen);
g_assert_cmpstr(result, ==, "some");
result = get_nextpath(path, &offset, fulllen);
g_assert_cmpstr(result, ==, "path");
result = get_nextpath(path, &offset, fulllen);
g_assert_cmpstr(result, ==, NULL);
}
static void test_get_nextpath__weird(void)
{
char path[] = "..///path";
size_t offset = 0;
size_t fulllen = strlen(path);
// Prepare path for usage with get_nextpath() by replacing
// all path separators with the NUL byte.
replace_slashes_with_NUL(path, fulllen);
// Run get_nextpath a few times to see what happens.
char *result;
result = get_nextpath(path, &offset, fulllen);
g_assert_cmpstr(result, ==, "path");
result = get_nextpath(path, &offset, fulllen);
g_assert_cmpstr(result, ==, NULL);
}
static void test_is_subdir(void)
{
// Sensible exaples are sensible
g_assert_true(is_subdir("/dir/subdir", "/dir/"));
g_assert_true(is_subdir("/dir/subdir", "/dir"));
g_assert_true(is_subdir("/dir/", "/dir"));
g_assert_true(is_subdir("/dir", "/dir"));
// Also without leading slash
g_assert_true(is_subdir("dir/subdir", "dir/"));
g_assert_true(is_subdir("dir/subdir", "dir"));
g_assert_true(is_subdir("dir/", "dir"));
g_assert_true(is_subdir("dir", "dir"));
// Some more ideas
g_assert_true(is_subdir("//", "/"));
g_assert_true(is_subdir("/", "/"));
g_assert_true(is_subdir("", ""));
// but this is not true
g_assert_false(is_subdir("/", "/dir"));
g_assert_false(is_subdir("/rid", "/dir"));
g_assert_false(is_subdir("/different/dir", "/dir"));
g_assert_false(is_subdir("/", ""));
}
static void test_must_mkdir_and_open_with_perms(void)
{
// make a directory with some contents and check we can
// must_mkdir_and_open_with_perms() to get control of it
GError *error = NULL;
GStatBuf st;
gchar *test_dir = g_dir_make_tmp("test-mkdir-XXXXXX", &error);
g_assert_no_error(error);
g_assert_nonnull(test_dir);
g_assert_cmpint(chmod(test_dir, 0755), ==, 0);
g_assert_true(g_file_test
(test_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR));
g_assert_cmpint(g_stat(test_dir, &st), ==, 0);
g_assert_true(st.st_uid == getuid());
g_assert_true(st.st_gid == getgid());
g_assert_true(st.st_mode == (S_IFDIR | 0755));
gchar *test_subdir = g_build_filename(test_dir, "foo", NULL);
g_assert_cmpint(g_mkdir_with_parents(test_dir, 0755), ==, 0);
g_file_test(test_subdir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
// take over dir
int fd =
must_mkdir_and_open_with_perms(test_dir, getuid(), getgid(), 0700);
// check can unlink dir itself with no contents successfully and it
// still exists
g_assert_cmpint(fd, >=, 0);
g_assert_false(g_file_test
(test_subdir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR));
g_assert_true(g_file_test
(test_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR));
g_assert_cmpint(g_stat(test_dir, &st), ==, 0);
g_assert_true(st.st_uid == getuid());
g_assert_true(st.st_gid == getgid());
g_assert_true(st.st_mode == (S_IFDIR | 0700));
close(fd);
}
static void __attribute__((constructor)) init(void)
{
g_test_add_func("/mount/get_nextpath/typical",
test_get_nextpath__typical);
g_test_add_func("/mount/get_nextpath/weird", test_get_nextpath__weird);
g_test_add_func("/mount/is_subdir", test_is_subdir);
g_test_add_func("/mount/must_mkdir_and_open_with_perms",
test_must_mkdir_and_open_with_perms);
}