Debian Codegraph @

src/test/test-label.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <string.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6
7 #include "errno-util.h"
8 #include "fd-util.h"
9 #include "fs-util.h"
10 #include "label-util.h"
11 #include "path-util.h"
12 #include "string-util.h"
13 #include "tests.h"
14
15 static struct stat buf;
16 static int check_path(int dir_fd, const char *path) {
17 assert(path);
18 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
19
20 if (isempty(path))
21 return -EINVAL;
22
23 /* assume length of pathname is not greater than 40 */
24 if (strlen(path) > 40)
25 return -ENAMETOOLONG;
26
27 /* assume a case where a specific label isn't allowed */
28 if (path_equal(path, "/restricted_directory"))
29 return -EACCES;
30 return 0;
31 }
32
33 static int pre_labelling_func(int dir_fd, const char *path, mode_t mode) {
34 int r;
35
36 assert(mode != MODE_INVALID);
37 r = check_path(dir_fd, path);
38 if (r < 0)
39 return log_error_errno(r, "Error in pathname =>: %m");
40
41 return 0;
42 }
43
44 static int post_labelling_func(int dir_fd, const char *path, bool created) {
45 int r;
46
47 /* assume label policies that restrict certain labels */
48 r = check_path(dir_fd, path);
49 if (r < 0)
50 return log_error_errno(r, "Error in pathname =>: %m");
51
52 /* Set file data to buf */
53 r = RET_NERRNO(fstatat(dir_fd, path, &buf, 0));
54 if (r < 0)
55 return log_error_errno(r, "Error in getting file status =>: %m");
56
57 return 0; /* on success */
58 }
59
60 static int get_dir_fd(const char *dir_path, mode_t mode) {
61 /* create a new directory and return its descriptor */
62 int dir_fd = -EBADF;
63
64 assert(dir_path);
65 dir_fd = RET_NERRNO(open_mkdir_at(AT_FDCWD, dir_path, O_CLOEXEC, mode));
66 if (dir_fd < 0)
67 return log_error_errno(dir_fd, "Error occurred while opening directory =>: %m");
68
69 return dir_fd;
70 }
71
72 static int labelling_op(int dir_fd, const char *text, const char *path, mode_t mode) {
73 /* Write some content into the file */
74 ssize_t count;
75 _cleanup_close_ int write_fd = -EBADF;
76 int r;
77
78 assert(text);
79 assert(mode != MODE_INVALID);
80 r = check_path(dir_fd, path);
81 if (r < 0)
82 return log_error_errno(r, "Error in pathname =>: %m");
83
84 /* Open the file within the directory for writing */
85 write_fd = RET_NERRNO(openat(dir_fd, path, O_CLOEXEC|O_WRONLY|O_TRUNC|O_CREAT, 0644));
86 if (write_fd < 0)
87 return log_error_errno(write_fd, "Error in opening directory for writing =>: %m");
88
89 /* Write data to the file */
90 count = RET_NERRNO(write(write_fd, text, strlen(text)));
91 if (count < 0)
92 return log_error_errno(count, "Error occurred while opening file for writing =>: %m");
93 return 0;
94 }
95
96 TEST(label_ops_set) {
97 static const LabelOps test_label_ops = {
98 .pre = NULL,
99 .post = NULL,
100 };
101
102 label_ops_reset();
103 assert_se(label_ops_set(&test_label_ops) == 0);
104 /* attempt to reset label_ops when already set */
105 assert_se(label_ops_set(&test_label_ops) == -EBUSY);
106 }
107
108 TEST(label_ops_pre) {
109 _cleanup_close_ int fd;
110 static const LabelOps test_label_ops = {
111 .pre = pre_labelling_func,
112 .post = NULL,
113 };
114
115 label_ops_reset();
116 label_ops_set(&test_label_ops);
117 fd = get_dir_fd("file1.txt", 0755);
118 assert_se(label_ops_pre(fd, "file1.txt", 0644) == 0);
119 assert_se(label_ops_pre(fd, "/restricted_directory", 0644) == -EACCES);
120 assert_se(label_ops_pre(fd, "", 0700) == -EINVAL);
121 assert_se(label_ops_pre(fd, "/tmp", 0700) == 0);
122 assert_se(label_ops_pre(fd, "wekrgoierhgoierhqgherhgwklegnlweehgorwfkryrit", 0644) == -ENAMETOOLONG);
123 }
124
125 TEST(label_ops_post) {
126 _cleanup_close_ int fd = -EBADF;
127 const char *text1, *text2;
128 static const LabelOps test_label_ops = {
129 .pre = NULL,
130 .post = post_labelling_func,
131 };
132
133 label_ops_reset();
134 label_ops_set(&test_label_ops);
135
136 /* Open directory */
137 fd = RET_NERRNO(get_dir_fd("label_test_dir", 0755));
138 text1 = "Add initial texts to file for testing label operations to file1\n";
139
140 assert_se(labelling_op(fd, text1, "file1.txt", 0644) == 0);
141 assert_se(label_ops_post(fd, "file1.txt", true) == 0);
142 assert_se(strlen(text1) == (size_t)buf.st_size);
143 text2 = "Add text2 data to file2\n";
144
145 assert_se(labelling_op(fd, text2, "file2.txt", 0644) == 0);
146 assert_se(label_ops_post(fd, "file2.txt", true) == 0);
147 assert_se(strlen(text2) == (size_t)buf.st_size);
148 assert_se(label_ops_post(fd, "file3.txt", true) == -ENOENT);
149 assert_se(label_ops_post(fd, "/abcd", true) == -ENOENT);
150 assert_se(label_ops_post(fd, "/restricted_directory", true) == -EACCES);
151 assert_se(label_ops_post(fd, "", true) == -EINVAL);
152 }
153
154 DEFINE_TEST_MAIN(LOG_INFO)