]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-xattr-util.c
Merge pull request #2947 from keszybz/test-nss
[thirdparty/systemd.git] / src / test / test-xattr-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
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 <errno.h>
21 #include <fcntl.h>
22 #include <sys/types.h>
23 #include <sys/xattr.h>
24 #include <unistd.h>
25
26 #include "alloc-util.h"
27 #include "fd-util.h"
28 #include "fs-util.h"
29 #include "macro.h"
30 #include "string-util.h"
31 #include "xattr-util.h"
32
33 static void test_fgetxattrat_fake(void) {
34 char t[] = "/var/tmp/xattrtestXXXXXX";
35 _cleanup_close_ int fd = -1;
36 const char *x;
37 char v[3] = {};
38 int r;
39
40 assert_se(mkdtemp(t));
41 x = strjoina(t, "/test");
42 assert_se(touch(x) >= 0);
43
44 r = setxattr(x, "user.foo", "bar", 3, 0);
45 if (r < 0 && errno == EOPNOTSUPP) /* no xattrs supported on /var/tmp... */
46 goto cleanup;
47 assert_se(r >= 0);
48
49 fd = open(t, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
50 assert_se(fd >= 0);
51
52 assert_se(fgetxattrat_fake(fd, "test", "user.foo", v, 3, 0) >= 0);
53 assert_se(memcmp(v, "bar", 3) == 0);
54
55 safe_close(fd);
56 fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
57 assert_se(fd >= 0);
58 assert_se(fgetxattrat_fake(fd, "usr", "user.idontexist", v, 3, 0) == -ENODATA);
59
60 cleanup:
61 assert_se(unlink(x) >= 0);
62 assert_se(rmdir(t) >= 0);
63 }
64
65 int main(void) {
66 test_fgetxattrat_fake();
67
68 return 0;
69 }