blob: e72436ca67af052c385c82cbdae7c057dc0b469b [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
Dmitry V. Levinea7b9db2016-05-10 18:18:32 +000047 const char *const z_value = tail_memdup(c_value, sizeof(c_value));
48 char *const efault = tail_alloc(1) + 1;
49 const char *const value = tail_memdup(c_value, sizeof(c_value) - 1);
50 char *const big = tail_alloc(XATTR_SIZE_MAX + 1);
Dmitry V. Levinea7b9db2016-05-10 18:18:32 +000051
52 assert(fsetxattr(-1, 0, 0, 0, XATTR_CREATE) == -1);
53 printf("fsetxattr(-1, NULL, NULL, 0, XATTR_CREATE) = -1 %s (%m)\n",
54 errno2name());
55
56 assert(fsetxattr(-1, 0, z_value, 0, XATTR_CREATE) == -1);
57 printf("fsetxattr(-1, NULL, \"\", 0, XATTR_CREATE) = -1 %s (%m)\n",
58 errno2name());
59
60 assert(fsetxattr(-1, name, big, XATTR_SIZE_MAX + 1, XATTR_CREATE) == -1);
61 printf("fsetxattr(-1, \"%s\", %p, %u, XATTR_CREATE)"
62 " = -1 %s (%m)\n",
63 name, big, XATTR_SIZE_MAX + 1, errno2name());
64
65 assert(fsetxattr(-1, name, value, sizeof(c_value), XATTR_CREATE) == -1);
66 printf("fsetxattr(-1, \"%s\", %p, %u, XATTR_CREATE)"
67 " = -1 %s (%m)\n",
68 name, value, (unsigned) sizeof(c_value), errno2name());
69
70 assert(fsetxattr(-1, name, z_value, sizeof(c_value), XATTR_REPLACE) == -1);
71 printf("fsetxattr(-1, \"%s\", \"%s\", %u, XATTR_REPLACE)"
72 " = -1 %s (%m)\n",
73 name, q_value, (unsigned) sizeof(c_value), errno2name());
74
75 assert(fsetxattr(-1, name, value, sizeof(c_value) - 1, XATTR_CREATE|XATTR_REPLACE) == -1);
76 printf("fsetxattr(-1, \"%s\", \"%s\", %u, XATTR_CREATE|XATTR_REPLACE)"
77 " = -1 %s (%m)\n",
78 name, q_value, (unsigned) sizeof(c_value) - 1, errno2name());
79
80 assert(setxattr(".", name, z_value, sizeof(c_value), XATTR_CREATE) == -1);
81 printf("setxattr(\".\", \"%s\", \"%s\", %u, XATTR_CREATE)"
82 " = -1 %s (%m)\n",
83 name, q_value, (unsigned) sizeof(c_value), errno2name());
84
85 assert(lsetxattr(".", name, value, sizeof(c_value) - 1, XATTR_CREATE) == -1);
86 printf("lsetxattr(\".\", \"%s\", \"%s\", %u, XATTR_CREATE)"
87 " = -1 %s (%m)\n",
88 name, q_value, (unsigned) sizeof(c_value) - 1, errno2name());
89
90 assert(fgetxattr(-1, name, efault, 4) == -1);
91 printf("fgetxattr(-1, \"%s\", %p, 4) = -1 %s (%m)\n",
92 name, efault, errno2name());
93
94 assert(getxattr(".", name, big, XATTR_SIZE_MAX + 1) == -1);
95 printf("getxattr(\".\", \"%s\", %p, %u) = -1 %s (%m)\n",
96 name, big, XATTR_SIZE_MAX + 1, errno2name());
97
98 assert(lgetxattr(".", name, big + 1, XATTR_SIZE_MAX) == -1);
99 printf("lgetxattr(\".\", \"%s\", %p, %u) = -1 %s (%m)\n",
100 name, big + 1, XATTR_SIZE_MAX, errno2name());
101
102 assert(flistxattr(-1, efault, 4) == -1);
103 printf("flistxattr(-1, %p, 4) = -1 %s (%m)\n",
104 efault, errno2name());
105
106 assert(llistxattr("", efault + 1, 4) == -1);
107 printf("llistxattr(\"\", %p, 4) = -1 %s (%m)\n",
108 efault + 1, errno2name());
109
110 ssize_t rc = listxattr(".", big, 0);
111 if (rc < 0)
112 printf("listxattr(\".\", %p, 0) = -1 %s (%m)\n",
113 big, errno2name());
114 else
115 printf("listxattr(\".\", %p, 0) = %ld\n",
116 big, (long) rc);
117
118 rc = listxattr(".", big, XATTR_SIZE_MAX + 1);
119 if (rc < 0)
120 printf("listxattr(\".\", %p, %u) = -1 %s (%m)\n",
121 big, XATTR_SIZE_MAX + 1, errno2name());
122 else {
123 printf("listxattr(\".\", \"");
124 print_quoted_memory(big, rc);
125 printf("\", %u) = %ld\n", XATTR_SIZE_MAX + 1, (long) rc);
126 }
127
128 assert(fremovexattr(-1, name) == -1);
129 printf("fremovexattr(-1, \"%s\") = -1 %s (%m)\n",
130 name, errno2name());
131
132 assert(removexattr(".", name) == -1);
133 printf("removexattr(\".\", \"%s\") = -1 %s (%m)\n",
134 name, errno2name());
135
136 assert(lremovexattr(".", name) == -1);
137 printf("lremovexattr(\".\", \"%s\") = -1 %s (%m)\n",
138 name, errno2name());
139
140 puts("+++ exited with 0 +++");
Dmitry V. Levinb60b2a52015-07-15 00:09:08 +0000141 return 0;
142}
143
144#else
145
Dmitry V. Levinc2c840d2016-01-05 23:18:25 +0000146SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")
Dmitry V. Levinb60b2a52015-07-15 00:09:08 +0000147
148#endif