• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (strcmp(tc[i - 1].value, XATTR_TEST_VALUE))
125 			tst_resm(TFAIL, "Wrong value, expect \"%s\" got \"%s\"",
126 				 XATTR_TEST_VALUE, tc[i - 1].value);
127 		else
128 			tst_resm(TPASS, "Got the right value");
129 	}
130 
131 	cleanup();
132 	tst_exit();
133 }
134 
setup(void)135 static void setup(void)
136 {
137 	int fd;
138 	int i;
139 
140 	tst_require_root();
141 
142 	tst_tmpdir();
143 
144 	/* Create test file and setup initial xattr */
145 	snprintf(filename, BUFSIZ, "getxattr01testfile");
146 	fd = SAFE_CREAT(cleanup, filename, 0644);
147 	close(fd);
148 	if (setxattr(filename, XATTR_TEST_KEY, XATTR_TEST_VALUE,
149 		     strlen(XATTR_TEST_VALUE), XATTR_CREATE) == -1) {
150 		if (errno == ENOTSUP) {
151 			tst_brkm(TCONF, cleanup, "No xattr support in fs or "
152 				 "mount without user_xattr option");
153 		}
154 	}
155 
156 	/* Prepare test cases */
157 	for (i = 0; i <  ARRAY_SIZE(tc); i++) {
158 		tc[i].value = malloc(BUFFSIZE);
159 		if (tc[i].value == NULL) {
160 			tst_brkm(TBROK | TERRNO, cleanup,
161 				 "Cannot allocate memory");
162 		}
163 	}
164 
165 	TEST_PAUSE;
166 }
167 
cleanup(void)168 static void cleanup(void)
169 {
170 	tst_rmdir();
171 }
172 #else /* HAVE_SYS_XATTR_H */
main(int argc,char * argv[])173 int main(int argc, char *argv[])
174 {
175 	tst_brkm(TCONF, NULL, "<sys/xattr.h> does not exist.");
176 }
177 #endif
178