1 /*
2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "tests.h"
29
30 #ifdef HAVE_SYS_XATTR_H
31
32 # include <stdio.h>
33 # include <sys/xattr.h>
34
35 # ifndef XATTR_SIZE_MAX
36 # define XATTR_SIZE_MAX 65536
37 # endif
38
39 #define DEFAULT_STRLEN 32
40
41 int
main(void)42 main(void)
43 {
44 static const char name[] = "strace.test";
45 static const char c_value[] = "foo\0bar";
46 static const char q_value[] = "foo\\0bar";
47
48 const char *const z_value = tail_memdup(c_value, sizeof(c_value));
49 char *const efault = tail_alloc(1) + 1;
50 const char *const value = tail_memdup(c_value, sizeof(c_value) - 1);
51 char *const big = tail_alloc(XATTR_SIZE_MAX + 1);
52 long rc;
53 const char *errstr;
54
55 rc = fsetxattr(-1, 0, 0, 0, XATTR_CREATE);
56 printf("fsetxattr(-1, NULL, NULL, 0, XATTR_CREATE) = %s\n",
57 sprintrc(rc));
58
59 rc = fsetxattr(-1, 0, z_value, 0, XATTR_CREATE);
60 printf("fsetxattr(-1, NULL, \"\", 0, XATTR_CREATE) = %s\n",
61 sprintrc(rc));
62
63 rc = fsetxattr(-1, name, big, XATTR_SIZE_MAX + 1, XATTR_CREATE);
64 printf("fsetxattr(-1, \"%s\", %p, %u, XATTR_CREATE) = %s\n",
65 name, big, XATTR_SIZE_MAX + 1, sprintrc(rc));
66
67 rc = fsetxattr(-1, name, value, sizeof(c_value), XATTR_CREATE);
68 printf("fsetxattr(-1, \"%s\", %p, %u, XATTR_CREATE) = %s\n",
69 name, value, (unsigned) sizeof(c_value), sprintrc(rc));
70
71 rc = fsetxattr(-1, name, z_value, sizeof(c_value), XATTR_REPLACE);
72 printf("fsetxattr(-1, \"%s\", \"%s\", %u, XATTR_REPLACE) = %s\n",
73 name, q_value, (unsigned) sizeof(c_value), sprintrc(rc));
74
75 rc = fsetxattr(-1, name, value, sizeof(c_value) - 1, XATTR_CREATE|XATTR_REPLACE);
76 printf("fsetxattr(-1, \"%s\", \"%s\", %u, XATTR_CREATE|XATTR_REPLACE)"
77 " = %s\n",
78 name, q_value, (unsigned) sizeof(c_value) - 1, sprintrc(rc));
79
80 rc = setxattr(".", name, z_value, sizeof(c_value), XATTR_CREATE);
81 printf("setxattr(\".\", \"%s\", \"%s\", %u, XATTR_CREATE) = %s\n",
82 name, q_value, (unsigned) sizeof(c_value), sprintrc(rc));
83
84 rc = lsetxattr(".", name, value, sizeof(c_value) - 1, XATTR_CREATE);
85 printf("lsetxattr(\".\", \"%s\", \"%s\", %u, XATTR_CREATE) = %s\n",
86 name, q_value, (unsigned) sizeof(c_value) - 1, sprintrc(rc));
87
88 rc = fgetxattr(-1, name, efault, 4);
89 printf("fgetxattr(-1, \"%s\", %p, 4) = %s\n",
90 name, efault, sprintrc(rc));
91
92 rc = getxattr(".", name, big, XATTR_SIZE_MAX + 1);
93 printf("getxattr(\".\", \"%s\", %p, %u) = %s\n",
94 name, big, XATTR_SIZE_MAX + 1, sprintrc(rc));
95
96 rc = lgetxattr(".", name, big + 1, XATTR_SIZE_MAX);
97 printf("lgetxattr(\".\", \"%s\", %p, %u) = %s\n",
98 name, big + 1, XATTR_SIZE_MAX, sprintrc(rc));
99
100 rc = flistxattr(-1, efault, 4);
101 printf("flistxattr(-1, %p, 4) = %s\n", efault, sprintrc(rc));
102
103 rc = llistxattr("", efault + 1, 4);
104 printf("llistxattr(\"\", %p, 4) = %s\n", efault + 1, sprintrc(rc));
105
106 rc = listxattr(".", big, 0);
107 printf("listxattr(\".\", %p, 0) = %s\n", big, sprintrc(rc));
108
109 rc = listxattr(".", big, XATTR_SIZE_MAX + 1);
110 errstr = sprintrc(rc);
111 printf("listxattr(\".\", ");
112 if (rc < 0)
113 printf("%p", big);
114 else {
115 const int ellipsis = rc > DEFAULT_STRLEN;
116
117 putchar('"');
118 print_quoted_memory(big, ellipsis ? DEFAULT_STRLEN : rc);
119 putchar('"');
120 if (ellipsis)
121 fputs("...", stdout);
122 }
123 printf(", %u) = %s\n", XATTR_SIZE_MAX + 1, errstr);
124
125 rc = fremovexattr(-1, name);
126 printf("fremovexattr(-1, \"%s\") = %s\n", name, sprintrc(rc));
127
128 rc = removexattr(".", name);
129 printf("removexattr(\".\", \"%s\") = %s\n", name, sprintrc(rc));
130
131 rc = lremovexattr(".", name);
132 printf("lremovexattr(\".\", \"%s\") = %s\n", name, sprintrc(rc));
133
134 puts("+++ exited with 0 +++");
135 return 0;
136 }
137
138 #else
139
140 SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")
141
142 #endif
143