blob: c7a3342370794871cc480e688ec47a05a1634f7f [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
Dmitry V. Levinc2c840d2016-01-05 23:18:25 +00002 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00003 * 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
Dmitry V. Levin0c8853c2016-01-02 13:28:43 +000028#include "tests.h"
Dmitry V. Levinc2c840d2016-01-05 23:18:25 +000029
Dmitry V. Levinb60b2a52015-07-15 00:09:08 +000030#ifdef HAVE_SYS_XATTR_H
Dmitry V. Levinc2c840d2016-01-05 23:18:25 +000031
Dmitry V. Levinea7b9db2016-05-10 18:18:32 +000032# include <assert.h>
33# include <stdio.h>
Dmitry V. Levinb60b2a52015-07-15 00:09:08 +000034# include <sys/xattr.h>
35
Dmitry V. Levinea7b9db2016-05-10 18:18:32 +000036# ifndef XATTR_SIZE_MAX
37# define XATTR_SIZE_MAX 65536
38# endif
39
Dmitry V. Levinb60b2a52015-07-15 00:09:08 +000040int
41main(void)
42{
Dmitry V. Levinea7b9db2016-05-10 18:18:32 +000043 static const char name[] = "strace.test";
44 static const char c_value[] = "foo\0bar";
45 static const char q_value[] = "foo\\0bar";
46
47 tail_alloc(1);
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 tail_alloc(1);
53
54 assert(fsetxattr(-1, 0, 0, 0, XATTR_CREATE) == -1);
55 printf("fsetxattr(-1, NULL, NULL, 0, XATTR_CREATE) = -1 %s (%m)\n",
56 errno2name());
57
58 assert(fsetxattr(-1, 0, z_value, 0, XATTR_CREATE) == -1);
59 printf("fsetxattr(-1, NULL, \"\", 0, XATTR_CREATE) = -1 %s (%m)\n",
60 errno2name());
61
62 assert(fsetxattr(-1, name, big, XATTR_SIZE_MAX + 1, XATTR_CREATE) == -1);
63 printf("fsetxattr(-1, \"%s\", %p, %u, XATTR_CREATE)"
64 " = -1 %s (%m)\n",
65 name, big, XATTR_SIZE_MAX + 1, errno2name());
66
67 assert(fsetxattr(-1, name, value, sizeof(c_value), XATTR_CREATE) == -1);
68 printf("fsetxattr(-1, \"%s\", %p, %u, XATTR_CREATE)"
69 " = -1 %s (%m)\n",
70 name, value, (unsigned) sizeof(c_value), errno2name());
71
72 assert(fsetxattr(-1, name, z_value, sizeof(c_value), XATTR_REPLACE) == -1);
73 printf("fsetxattr(-1, \"%s\", \"%s\", %u, XATTR_REPLACE)"
74 " = -1 %s (%m)\n",
75 name, q_value, (unsigned) sizeof(c_value), errno2name());
76
77 assert(fsetxattr(-1, name, value, sizeof(c_value) - 1, XATTR_CREATE|XATTR_REPLACE) == -1);
78 printf("fsetxattr(-1, \"%s\", \"%s\", %u, XATTR_CREATE|XATTR_REPLACE)"
79 " = -1 %s (%m)\n",
80 name, q_value, (unsigned) sizeof(c_value) - 1, errno2name());
81
82 assert(setxattr(".", name, z_value, sizeof(c_value), XATTR_CREATE) == -1);
83 printf("setxattr(\".\", \"%s\", \"%s\", %u, XATTR_CREATE)"
84 " = -1 %s (%m)\n",
85 name, q_value, (unsigned) sizeof(c_value), errno2name());
86
87 assert(lsetxattr(".", name, value, sizeof(c_value) - 1, XATTR_CREATE) == -1);
88 printf("lsetxattr(\".\", \"%s\", \"%s\", %u, XATTR_CREATE)"
89 " = -1 %s (%m)\n",
90 name, q_value, (unsigned) sizeof(c_value) - 1, errno2name());
91
92 assert(fgetxattr(-1, name, efault, 4) == -1);
93 printf("fgetxattr(-1, \"%s\", %p, 4) = -1 %s (%m)\n",
94 name, efault, errno2name());
95
96 assert(getxattr(".", name, big, XATTR_SIZE_MAX + 1) == -1);
97 printf("getxattr(\".\", \"%s\", %p, %u) = -1 %s (%m)\n",
98 name, big, XATTR_SIZE_MAX + 1, errno2name());
99
100 assert(lgetxattr(".", name, big + 1, XATTR_SIZE_MAX) == -1);
101 printf("lgetxattr(\".\", \"%s\", %p, %u) = -1 %s (%m)\n",
102 name, big + 1, XATTR_SIZE_MAX, errno2name());
103
104 assert(flistxattr(-1, efault, 4) == -1);
105 printf("flistxattr(-1, %p, 4) = -1 %s (%m)\n",
106 efault, errno2name());
107
108 assert(llistxattr("", efault + 1, 4) == -1);
109 printf("llistxattr(\"\", %p, 4) = -1 %s (%m)\n",
110 efault + 1, errno2name());
111
112 ssize_t rc = listxattr(".", big, 0);
113 if (rc < 0)
114 printf("listxattr(\".\", %p, 0) = -1 %s (%m)\n",
115 big, errno2name());
116 else
117 printf("listxattr(\".\", %p, 0) = %ld\n",
118 big, (long) rc);
119
120 rc = listxattr(".", big, XATTR_SIZE_MAX + 1);
121 if (rc < 0)
122 printf("listxattr(\".\", %p, %u) = -1 %s (%m)\n",
123 big, XATTR_SIZE_MAX + 1, errno2name());
124 else {
125 printf("listxattr(\".\", \"");
126 print_quoted_memory(big, rc);
127 printf("\", %u) = %ld\n", XATTR_SIZE_MAX + 1, (long) rc);
128 }
129
130 assert(fremovexattr(-1, name) == -1);
131 printf("fremovexattr(-1, \"%s\") = -1 %s (%m)\n",
132 name, errno2name());
133
134 assert(removexattr(".", name) == -1);
135 printf("removexattr(\".\", \"%s\") = -1 %s (%m)\n",
136 name, errno2name());
137
138 assert(lremovexattr(".", name) == -1);
139 printf("lremovexattr(\".\", \"%s\") = -1 %s (%m)\n",
140 name, errno2name());
141
142 puts("+++ exited with 0 +++");
Dmitry V. Levinb60b2a52015-07-15 00:09:08 +0000143 return 0;
144}
145
146#else
147
Dmitry V. Levinc2c840d2016-01-05 23:18:25 +0000148SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")
Dmitry V. Levinb60b2a52015-07-15 00:09:08 +0000149
150#endif