-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnomerge0001-Add-xattr-test.patch
249 lines (246 loc) · 7.81 KB
/
nomerge0001-Add-xattr-test.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
From 09ccc1c0a5b75e3352af29767f7529300738bcf2 Mon Sep 17 00:00:00 2001
From: Shengyu Zhang <lastavengers@outlook.com>
Date: Thu, 4 Aug 2016 09:13:08 +0800
Subject: [PATCH 1/2] Add xattr test
---
ext2fs/Makefile | 2 +-
ext2fs/xattr_test.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 217 insertions(+), 1 deletion(-)
create mode 100644 ext2fs/xattr_test.c
diff --git a/ext2fs/Makefile b/ext2fs/Makefile
index 0c2f4a2..5c2e88e 100644
--- a/ext2fs/Makefile
+++ b/ext2fs/Makefile
@@ -22,7 +22,7 @@ makemode := server
target = ext2fs
SRCS = balloc.c dir.c ext2fs.c getblk.c hyper.c ialloc.c \
inode.c pager.c pokel.c truncate.c storeinfo.c msg.c xinl.c \
- xattr.c
+ xattr.c xattr_test.c
OBJS = $(SRCS:.c=.o)
HURDLIBS = diskfs pager iohelp fshelp store ports ihash shouldbeinlibc
LDLIBS = -lpthread $(and $(HAVE_LIBBZ2),-lbz2) $(and $(HAVE_LIBZ),-lz)
diff --git a/ext2fs/xattr_test.c b/ext2fs/xattr_test.c
new file mode 100644
index 0000000..7fc0ca2
--- /dev/null
+++ b/ext2fs/xattr_test.c
@@ -0,0 +1,216 @@
+/* xattr test cases, remove me later.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Enable debug output */
+#define EXT2FS_DEBUG
+int ext2_debug_flag = 1;
+
+#include "ext2fs.h"
+#include "xattr.h"
+#include <sys/xattr.h>
+
+static void xattr_print_entry (struct ext2_xattr_entry *entry)
+{
+ ext2_debug ("entry:");
+ ext2_debug ("\t->e_name_len: %d", entry->e_name_len);
+ ext2_debug ("\t->e_name_index: %d", entry->e_name_index);
+ ext2_debug ("\t->e_value_offs: %d", entry->e_value_offs);
+ ext2_debug ("\t->e_value_block: %d", entry->e_value_block);
+ ext2_debug ("\t->e_value_size: %d", entry->e_value_size);
+ ext2_debug ("\t->e_hash: 0x%x", entry->e_hash);
+ ext2_debug ("\t->e_name: %.*s", entry->e_name_len, entry->e_name);
+}
+
+static void
+list_xattr_test (struct node *np, int exp_len, char *exp_buf,
+ error_t exp_err)
+{
+
+ char buf[256];
+ int len = sizeof (buf);
+
+ assert (ext2_list_xattr (np, buf, &len) == exp_err);
+
+ assert (len == exp_len);
+ assert (memcmp(buf, exp_buf, len) == 0);
+
+}
+
+static void
+get_xattr_test (struct node *np, char *exp_key, char *exp_val,
+ int exp_len, error_t exp_err)
+{
+ char buf[256];
+ int len = sizeof (buf);
+
+ memset (buf, 0, sizeof(len));
+ assert (ext2_get_xattr (np, exp_key, buf, &len) == exp_err);
+
+ assert (len == exp_len);
+
+ buf[len] = 0;
+ assert (strcmp (buf, exp_val) == 0);
+
+}
+
+static void
+set_xattr_test (struct node *np, char *exp_key,
+ char *exp_val, int exp_len,
+ int exp_flag, error_t exp_err)
+{
+ assert (ext2_set_xattr (np, exp_key, exp_val,
+ exp_len, exp_flag) == exp_err);
+
+}
+
+static void
+hash_xattr_test (struct node *np, unsigned int hash_arr[], int len)
+{
+
+ int i;
+ void *block;
+ block_t blkno;
+ struct ext2_inode *ei;
+ struct ext2_xattr_header *header;
+ struct ext2_xattr_entry *entry;
+
+ ei = dino_ref (np->cache_id);
+ blkno = ei->i_file_acl;
+
+ block = disk_cache_block_ref (blkno);
+
+ header = EXT2_XATTR_HEADER (block);
+ assert (header->h_hash == hash_arr[0]);
+
+ entry = EXT2_XATTR_ENTRY_FIRST (header);
+
+ for (i = 1; i < len; i++)
+ {
+ assert (entry->e_hash== hash_arr[i]);
+ entry = EXT2_XATTR_ENTRY_NEXT (entry);
+ }
+
+ assert (EXT2_XATTR_ENTRY_LAST (entry));
+
+ dino_deref (ei);
+ disk_cache_block_deref (block);
+
+}
+
+static void
+read_test (struct node *np)
+{
+/* Image for testing:
+ *
+ * dd if=/dev/zero of=$(IMG) bs=4M count=10
+ * mkfs.ext2 -b 4096 $(IMG)
+ * mkdir -p tmp
+ * sudo mount $(IMG) ./tmp
+ * sudo touch ./tmp/test || true
+ * sudo setfattr -n user.key_123 -v val_123 ./tmp/test || true
+ * sudo setfattr -n user.key_456 -v val_456 ./tmp/test || true
+ * sudo umount ./tmp
+ * rm -rf ./tmp
+ */
+
+ // Illegal parameter test start
+ int len = 0;
+ assert (ext2_list_xattr (np, NULL, NULL) == EINVAL);
+ assert (ext2_list_xattr (np, NULL, &len) == 0 );
+ assert (len == 26);
+
+ assert (ext2_get_xattr (np, NULL, NULL, &len) == EINVAL);
+ assert (ext2_get_xattr (np, "user.key_456", NULL, NULL) == EINVAL);
+ assert (ext2_get_xattr (np, "acl", NULL, &len) == EOPNOTSUPP);
+ assert (ext2_get_xattr (np, "user.key_456", NULL, &len) == 0);
+ assert (len == 7);
+
+ assert (ext2_set_xattr (np, NULL, NULL, 0, 0) == EINVAL);
+ assert (ext2_set_xattr (np, "user.key_012", "val_012", block_size + 10, 0) == ERANGE);
+
+ assert (ext2_set_xattr (np, "user.key_123", "val_012",
+ sizeof ("val_012") - 1, XATTR_CREATE) == EEXIST);
+ assert (ext2_set_xattr (np, "user.key_012", "val_012",
+ sizeof ("val_012") - 1, XATTR_REPLACE) == ENODATA);
+ assert (ext2_set_xattr (np, "user.key_012", NULL, 0, XATTR_CREATE) == EINVAL);
+ assert (ext2_set_xattr (np, "user.key_012", NULL, 0, XATTR_REPLACE) == EINVAL);
+ assert (ext2_set_xattr (np, "user.key_012", NULL, 0, 0) == ENODATA);
+
+ // Illegal parameter test end
+ list_xattr_test (np, 26, "user.key_123\0user.key_456\0", 0);
+ get_xattr_test (np, "user.key_123", "val_123", sizeof ("val_123") - 1, 0);
+ get_xattr_test (np, "user.key_456", "val_456", sizeof ("val_456") - 1, 0);
+
+ unsigned int hash_arr[] = {0x43cb502e, 0x6cfa2f34, 0x6cff3cd4};
+ hash_xattr_test (np, hash_arr, 3);
+
+}
+
+static void
+write_test (struct node *np)
+{
+/* Image for testing:
+ *
+ * dd if=/dev/zero of=$(IMG) bs=4M count=10
+ * mkfs.ext2 -b 4096 $(IMG)
+ * mkdir -p tmp
+ * sudo mount $(IMG) ./tmp
+ * sudo touch ./tmp/test || true
+ * sudo umount ./tmp
+ * rm -rf ./tmp
+ */
+
+ set_xattr_test (np, "user.key_123", "val_123", sizeof ("val_123") - 1, XATTR_CREATE, 0);
+ set_xattr_test (np, "user.key_456", "val_456", sizeof ("val_456") - 1, XATTR_CREATE, 0);
+ read_test (np);
+
+ set_xattr_test (np, "user.key_456", "val_2333333333", sizeof ("val_2333333333") - 1, XATTR_REPLACE, 0);
+ list_xattr_test (np, 26, "user.key_123\0user.key_456\0", 0);
+ get_xattr_test (np, "user.key_123", "val_123", sizeof ("val_123") - 1, 0);
+ get_xattr_test (np, "user.key_456", "val_2333333333", sizeof ("val_2333333333") - 1, 0);
+
+ unsigned int hash_arr[] = {0x43fd5628, 0x6cfa2f34, 0x6cc93ad2};
+ hash_xattr_test (np, hash_arr, 3);
+
+ set_xattr_test (np, "user.key_123", "abc", sizeof ("abc") - 1, XATTR_REPLACE, 0);
+ get_xattr_test (np, "user.key_123", "abc", sizeof ("abc") - 1, 0);
+ set_xattr_test (np, "user.key_456", "def", sizeof ("def") - 1, XATTR_REPLACE, 0);
+ get_xattr_test (np, "user.key_456", "def", sizeof ("def") - 1, 0);
+
+ set_xattr_test (np, "user.key_123", "val_123", sizeof ("val_123") - 1, XATTR_REPLACE, 0);
+ set_xattr_test (np, "user.key_456", "val_456", sizeof ("val_456") - 1, XATTR_REPLACE, 0);
+ read_test (np);
+
+ set_xattr_test (np, "user.key_123", NULL, 0, 0, 0);
+ list_xattr_test (np, 13, "user.key_456", 0);
+ set_xattr_test (np, "user.key_456", NULL, 0, 0, 0);
+ list_xattr_test (np, 0, "", 0);
+
+ set_xattr_test (np, "gnu.translotor", "/hurd/hello\0-c\0hello\0",
+ sizeof("/hurd/hello\0-c\0hello\0") - 1, XATTR_CREATE, 0);
+ list_xattr_test (np, sizeof("gnu.translotor"), "gnu.translotor", 0);
+ get_xattr_test (np, "gnu.translotor", "/hurd/hello\0-c\0hello\0",
+ sizeof ("/hurd/hello\0-c\0hello\0") - 1, 0);
+}
+
+error_t
+diskfs_xattr_test (struct node *np)
+{
+ write_test (np);
+
+ return 0;
+
+}
--
2.9.2