]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-acl-util.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / test / test-acl-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2015 Zbigniew Jędrzejewski-Szmek
4 ***/
5
6 #include <fcntl.h>
7 #include <stdlib.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10
11 #include "acl-util.h"
12 #include "fd-util.h"
13 #include "fileio.h"
14 #include "string-util.h"
15 #include "user-util.h"
16
17 static void test_add_acls_for_user(void) {
18 char fn[] = "/tmp/test-empty.XXXXXX";
19 _cleanup_close_ int fd = -1;
20 char *cmd;
21 uid_t uid;
22 int r;
23
24 fd = mkostemp_safe(fn);
25 assert_se(fd >= 0);
26
27 /* Use the mode that user journal files use */
28 assert_se(fchmod(fd, 0640) == 0);
29
30 cmd = strjoina("ls -l ", fn);
31 assert_se(system(cmd) == 0);
32
33 cmd = strjoina("getfacl -p ", fn);
34 assert_se(system(cmd) == 0);
35
36 if (getuid() == 0) {
37 const char *nobody = NOBODY_USER_NAME;
38 r = get_user_creds(&nobody, &uid, NULL, NULL, NULL);
39 if (r < 0)
40 uid = 0;
41 } else
42 uid = getuid();
43
44 r = add_acls_for_user(fd, uid);
45 assert_se(r >= 0);
46
47 cmd = strjoina("ls -l ", fn);
48 assert_se(system(cmd) == 0);
49
50 cmd = strjoina("getfacl -p ", fn);
51 assert_se(system(cmd) == 0);
52
53 /* set the acls again */
54
55 r = add_acls_for_user(fd, uid);
56 assert_se(r >= 0);
57
58 cmd = strjoina("ls -l ", fn);
59 assert_se(system(cmd) == 0);
60
61 cmd = strjoina("getfacl -p ", fn);
62 assert_se(system(cmd) == 0);
63
64 unlink(fn);
65 }
66
67 int main(int argc, char **argv) {
68 test_add_acls_for_user();
69
70 return 0;
71 }