1 /*
2  * Copyright (C) 2011  Red Hat, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it
13  * is free of the rightful claim of any third person regarding
14  * infringement or the like.  Any license provided herein, whether
15  * implied or otherwise, applies only to this software file.  Patent
16  * licenses, if any, provided herein do not apply to combinations of
17  * this program with other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301, USA.
23  */
24 
25 /*
26  * Basic tests for getxattr(2) and make sure getxattr(2) handles error
27  * conditions correctly.
28  *
29  * There are 4 test cases:
30  * 1. Get an non-existing attribute,
31  *    getxattr(2) should return -1 and set errno to ENODATA
32  * 2. Buffer size is smaller than attribute value size,
33  *    getxattr(2) should return -1 and set errno to ERANGE
34  * 3. Get attribute, getxattr(2) should succeed
35  * 4. Verify the attribute got by getxattr(2) is same as the value we set
36  */
37 
38 #include "config.h"
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #ifdef HAVE_SYS_XATTR_H
48 # include <sys/xattr.h>
49 #endif
50 #include "test.h"
51 #include "safe_macros.h"
52 
53 char *TCID = "getxattr01";
54 
55 #ifdef HAVE_SYS_XATTR_H
56 #define XATTR_TEST_KEY "user.testkey"
57 #define XATTR_TEST_VALUE "this is a test value"
58 #define XATTR_TEST_VALUE_SIZE 20
59 #define BUFFSIZE 64
60 
61 static void setup(void);
62 static void cleanup(void);
63 
64 char filename[BUFSIZ];
65 
66 struct test_case {
67 	char *fname;
68 	char *key;
69 	char *value;
70 	size_t size;
71 	int exp_err;
72 };
73 struct test_case tc[] = {
74 	{			/* case 00, get non-existing attribute */
75 	 .fname = filename,
76 	 .key = "user.nosuchkey",
77 	 .value = NULL,
78 	 .size = BUFFSIZE - 1,
79 	 .exp_err = ENODATA,
80 	 },
81 	{			/* case 01, small value buffer */
82 	 .fname = filename,
83 	 .key = XATTR_TEST_KEY,
84 	 .value = NULL,
85 	 .size = 1,
86 	 .exp_err = ERANGE,
87 	 },
88 	{			/* case 02, get existing attribute */
89 	 .fname = filename,
90 	 .key = XATTR_TEST_KEY,
91 	 .value = NULL,
92 	 .size = BUFFSIZE - 1,
93 	 .exp_err = 0,
94 	 },
95 };
96 
97 int TST_TOTAL = sizeof(tc) / sizeof(tc[0]) + 1;
98 
main(int argc,char * argv[])99 int main(int argc, char *argv[])
100 {
101 	int lc;
102 	int i;
103 
104 	tst_parse_opts(argc, argv, NULL, NULL);
105 
106 	setup();
107 
108 	for (lc = 0; TEST_LOOPING(lc); lc++) {
109 		tst_count = 0;
110 
111 		for (i = 0; i < ARRAY_SIZE(tc); i++) {
112 			TEST(getxattr(tc[i].fname, tc[i].key, tc[i].value,
113 				      tc[i].size));
114 
115 			if (TEST_ERRNO == tc[i].exp_err) {
116 				tst_resm(TPASS | TTERRNO, "expected behavior");
117 			} else {
118 				tst_resm(TFAIL | TTERRNO, "unexpected behavior"
119 					 "- expected errno %d - Got",
120 					 tc[i].exp_err);
121 			}
122 		}
123 
124 		if (TEST_RETURN != XATTR_TEST_VALUE_SIZE) {
125 			tst_resm(TFAIL,
126 				 "getxattr() returned wrong size %ld expected %d",
127 				 TEST_RETURN, XATTR_TEST_VALUE_SIZE);
128 			continue;
129 		}
130 
131 		if (memcmp(tc[i - 1].value, XATTR_TEST_VALUE, XATTR_TEST_VALUE_SIZE))
132 			tst_resm(TFAIL, "Wrong value, expect \"%s\" got \"%s\"",
133 				 XATTR_TEST_VALUE, tc[i - 1].value);
134 		else
135 			tst_resm(TPASS, "Got the right value");
136 	}
137 
138 	cleanup();
139 	tst_exit();
140 }
141 
setup(void)142 static void setup(void)
143 {
144 	int fd;
145 	int i;
146 
147 	tst_require_root();
148 
149 	tst_tmpdir();
150 
151 	/* Create test file and setup initial xattr */
152 	snprintf(filename, BUFSIZ, "getxattr01testfile");
153 	fd = SAFE_CREAT(cleanup, filename, 0644);
154 	close(fd);
155 	if (setxattr(filename, XATTR_TEST_KEY, XATTR_TEST_VALUE,
156 		     strlen(XATTR_TEST_VALUE), XATTR_CREATE) == -1) {
157 		if (errno == ENOTSUP) {
158 			tst_brkm(TCONF, cleanup, "No xattr support in fs or "
159 				 "mount without user_xattr option");
160 		}
161 	}
162 
163 	/* Prepare test cases */
164 	for (i = 0; i <  ARRAY_SIZE(tc); i++) {
165 		tc[i].value = malloc(BUFFSIZE);
166 		if (tc[i].value == NULL) {
167 			tst_brkm(TBROK | TERRNO, cleanup,
168 				 "Cannot allocate memory");
169 		}
170 	}
171 
172 	TEST_PAUSE;
173 }
174 
cleanup(void)175 static void cleanup(void)
176 {
177 	tst_rmdir();
178 }
179 #else /* HAVE_SYS_XATTR_H */
main(int argc,char * argv[])180 int main(int argc, char *argv[])
181 {
182 	tst_brkm(TCONF, NULL, "<sys/xattr.h> does not exist.");
183 }
184 #endif
185