]> git.proxmox.com Git - systemd.git/blob - src/test/test-acl-util.c
Merge tag 'upstream/229'
[systemd.git] / src / test / test-acl-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2015 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include "acl-util.h"
26 #include "fd-util.h"
27 #include "fileio.h"
28 #include "string-util.h"
29 #include "user-util.h"
30
31 static void test_add_acls_for_user(void) {
32 char fn[] = "/tmp/test-empty.XXXXXX";
33 _cleanup_close_ int fd = -1;
34 char *cmd;
35 uid_t uid;
36 int r;
37
38 fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
39 assert_se(fd >= 0);
40
41 /* Use the mode that user journal files use */
42 assert_se(fchmod(fd, 0640) == 0);
43
44 cmd = strjoina("ls -l ", fn);
45 assert_se(system(cmd) == 0);
46
47 cmd = strjoina("getfacl -p ", fn);
48 assert_se(system(cmd) == 0);
49
50 if (getuid() == 0) {
51 const char *nobody = "nobody";
52 r = get_user_creds(&nobody, &uid, NULL, NULL, NULL);
53 if (r < 0)
54 uid = 0;
55 } else
56 uid = getuid();
57
58 r = add_acls_for_user(fd, uid);
59 assert_se(r >= 0);
60
61 cmd = strjoina("ls -l ", fn);
62 assert_se(system(cmd) == 0);
63
64 cmd = strjoina("getfacl -p ", fn);
65 assert_se(system(cmd) == 0);
66
67 /* set the acls again */
68
69 r = add_acls_for_user(fd, uid);
70 assert_se(r >= 0);
71
72 cmd = strjoina("ls -l ", fn);
73 assert_se(system(cmd) == 0);
74
75 cmd = strjoina("getfacl -p ", fn);
76 assert_se(system(cmd) == 0);
77
78 unlink(fn);
79 }
80
81 int main(int argc, char **argv) {
82 test_add_acls_for_user();
83
84 return 0;
85 }