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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com>
*
* Author: Loic Dachary <loic@dachary.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library 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 Library Public License for more details.
*
*/
#include <stdio.h>
#include <signal.h>
#include "os/filestore/chain_xattr.h"
#include "include/Context.h"
#include "include/coredumpctl.h"
#include "common/errno.h"
#include "common/ceph_argparse.h"
#include "global/global_init.h"
#include <gtest/gtest.h>
#define LARGE_BLOCK_LEN CHAIN_XATTR_MAX_BLOCK_LEN + 1024
#define FILENAME "chain_xattr"
TEST(chain_xattr, get_and_set) {
const char* file = FILENAME;
::unlink(file);
int fd = ::open(file, O_CREAT|O_WRONLY|O_TRUNC, 0700);
const string user("user.");
{
const string name = user + string(CHAIN_XATTR_MAX_NAME_LEN - user.size(), '@');
const string x(LARGE_BLOCK_LEN, 'X');
{
char y[LARGE_BLOCK_LEN];
ASSERT_EQ(LARGE_BLOCK_LEN, chain_setxattr(file, name.c_str(), x.c_str(), LARGE_BLOCK_LEN));
ASSERT_EQ(LARGE_BLOCK_LEN, chain_getxattr(file, name.c_str(), 0, 0));
ASSERT_EQ(LARGE_BLOCK_LEN, chain_getxattr(file, name.c_str(), y, LARGE_BLOCK_LEN));
ASSERT_EQ(0, chain_removexattr(file, name.c_str()));
ASSERT_EQ(0, memcmp(x.c_str(), y, LARGE_BLOCK_LEN));
}
{
char y[LARGE_BLOCK_LEN];
ASSERT_EQ(LARGE_BLOCK_LEN, chain_fsetxattr(fd, name.c_str(), x.c_str(), LARGE_BLOCK_LEN));
ASSERT_EQ(LARGE_BLOCK_LEN, chain_fgetxattr(fd, name.c_str(), 0, 0));
ASSERT_EQ(LARGE_BLOCK_LEN, chain_fgetxattr(fd, name.c_str(), y, LARGE_BLOCK_LEN));
ASSERT_EQ(0, chain_fremovexattr(fd, name.c_str()));
ASSERT_EQ(0, memcmp(x.c_str(), y, LARGE_BLOCK_LEN));
}
}
//
// when chain_setxattr is used to store value that is
// CHAIN_XATTR_MAX_BLOCK_LEN * 2 + 10 bytes long it
//
// add user.foo => CHAIN_XATTR_MAX_BLOCK_LEN bytes
// add user.foo@1 => CHAIN_XATTR_MAX_BLOCK_LEN bytes
// add user.foo@2 => 10 bytes
//
// then ( no chain_removexattr in between ) when it is used to
// override with a value that is exactly CHAIN_XATTR_MAX_BLOCK_LEN
// bytes long it will
//
// replace user.foo => CHAIN_XATTR_MAX_BLOCK_LEN bytes
// remove user.foo@1 => CHAIN_XATTR_MAX_BLOCK_LEN bytes
// leak user.foo@2 => 10 bytes
//
// see http://marc.info/?l=ceph-devel&m=136027076615853&w=4 for the
// discussion
//
{
const string name = user + string(CHAIN_XATTR_MAX_NAME_LEN - user.size(), '@');
const string x(LARGE_BLOCK_LEN, 'X');
{
char y[CHAIN_XATTR_MAX_BLOCK_LEN];
ASSERT_EQ(LARGE_BLOCK_LEN, chain_setxattr(file, name.c_str(), x.c_str(), LARGE_BLOCK_LEN));
ASSERT_EQ(CHAIN_XATTR_MAX_BLOCK_LEN, chain_setxattr(file, name.c_str(), x.c_str(), CHAIN_XATTR_MAX_BLOCK_LEN));
ASSERT_EQ(CHAIN_XATTR_MAX_BLOCK_LEN, chain_getxattr(file, name.c_str(), 0, 0));
ASSERT_EQ(CHAIN_XATTR_MAX_BLOCK_LEN, chain_getxattr(file, name.c_str(), y, CHAIN_XATTR_MAX_BLOCK_LEN));
ASSERT_EQ(0, chain_removexattr(file, name.c_str()));
ASSERT_EQ(0, memcmp(x.c_str(), y, CHAIN_XATTR_MAX_BLOCK_LEN));
}
{
char y[CHAIN_XATTR_MAX_BLOCK_LEN];
ASSERT_EQ(LARGE_BLOCK_LEN, chain_fsetxattr(fd, name.c_str(), x.c_str(), LARGE_BLOCK_LEN));
ASSERT_EQ(CHAIN_XATTR_MAX_BLOCK_LEN, chain_fsetxattr(fd, name.c_str(), x.c_str(), CHAIN_XATTR_MAX_BLOCK_LEN));
ASSERT_EQ(CHAIN_XATTR_MAX_BLOCK_LEN, chain_fgetxattr(fd, name.c_str(), 0, 0));
ASSERT_EQ(CHAIN_XATTR_MAX_BLOCK_LEN, chain_fgetxattr(fd, name.c_str(), y, CHAIN_XATTR_MAX_BLOCK_LEN));
ASSERT_EQ(0, chain_fremovexattr(fd, name.c_str()));
ASSERT_EQ(0, memcmp(x.c_str(), y, CHAIN_XATTR_MAX_BLOCK_LEN));
}
}
{
int x = 0;
ASSERT_EQ(-ENOENT, chain_setxattr("UNLIKELY_TO_EXIST", "NAME", &x, sizeof(x)));
ASSERT_EQ(-ENOENT, chain_getxattr("UNLIKELY_TO_EXIST", "NAME", 0, 0));
ASSERT_EQ(-ENOENT, chain_getxattr("UNLIKELY_TO_EXIST", "NAME", &x, sizeof(x)));
ASSERT_EQ(-ENOENT, chain_removexattr("UNLIKELY_TO_EXIST", "NAME"));
int unlikely_to_be_a_valid_fd = 400;
ASSERT_EQ(-EBADF, chain_fsetxattr(unlikely_to_be_a_valid_fd, "NAME", &x, sizeof(x)));
ASSERT_EQ(-EBADF, chain_fgetxattr(unlikely_to_be_a_valid_fd, "NAME", 0, 0));
ASSERT_EQ(-EBADF, chain_fgetxattr(unlikely_to_be_a_valid_fd, "NAME", &x, sizeof(x)));
ASSERT_EQ(-EBADF, chain_fremovexattr(unlikely_to_be_a_valid_fd, "NAME"));
}
{
int x;
const string name = user + string(CHAIN_XATTR_MAX_NAME_LEN * 2, '@');
PrCtl unset_dumpable;
ASSERT_DEATH(chain_setxattr(file, name.c_str(), &x, sizeof(x)), "");
ASSERT_DEATH(chain_fsetxattr(fd, name.c_str(), &x, sizeof(x)), "");
}
{
const string name = user + string(CHAIN_XATTR_MAX_NAME_LEN - user.size(), '@');
const string x(LARGE_BLOCK_LEN, 'X');
{
char y[LARGE_BLOCK_LEN];
ASSERT_EQ(LARGE_BLOCK_LEN, chain_setxattr(file, name.c_str(), x.c_str(), LARGE_BLOCK_LEN));
ASSERT_EQ(-ERANGE, chain_getxattr(file, name.c_str(), y, LARGE_BLOCK_LEN - 1));
ASSERT_EQ(-ERANGE, chain_getxattr(file, name.c_str(), y, CHAIN_XATTR_MAX_BLOCK_LEN));
ASSERT_EQ(0, chain_removexattr(file, name.c_str()));
}
{
char y[LARGE_BLOCK_LEN];
ASSERT_EQ(LARGE_BLOCK_LEN, chain_fsetxattr(fd, name.c_str(), x.c_str(), LARGE_BLOCK_LEN));
ASSERT_EQ(-ERANGE, chain_fgetxattr(fd, name.c_str(), y, LARGE_BLOCK_LEN - 1));
ASSERT_EQ(-ERANGE, chain_fgetxattr(fd, name.c_str(), y, CHAIN_XATTR_MAX_BLOCK_LEN));
ASSERT_EQ(0, chain_fremovexattr(fd, name.c_str()));
}
}
::close(fd);
::unlink(file);
}
TEST(chain_xattr, chunk_aligned) {
const char* file = FILENAME;
::unlink(file);
int fd = ::open(file, O_CREAT|O_WRONLY|O_TRUNC, 0700);
const string user("user.");
// set N* chunk size
const string name = "user.foo";
const string name2 = "user.bar";
for (int len = CHAIN_XATTR_MAX_BLOCK_LEN - 10;
len < CHAIN_XATTR_MAX_BLOCK_LEN + 10;
++len) {
cout << len << std::endl;
const string x(len, 'x');
char buf[len*2];
ASSERT_EQ(len, chain_setxattr(file, name.c_str(), x.c_str(), len));
char attrbuf[4096];
int l = ceph_os_listxattr(file, attrbuf, sizeof(attrbuf));
for (char *p = attrbuf; p - attrbuf < l; p += strlen(p) + 1) {
cout << " attr " << p << std::endl;
}
ASSERT_EQ(len, chain_getxattr(file, name.c_str(), buf, len*2));
ASSERT_EQ(0, chain_removexattr(file, name.c_str()));
ASSERT_EQ(len, chain_fsetxattr(fd, name2.c_str(), x.c_str(), len));
l = ceph_os_flistxattr(fd, attrbuf, sizeof(attrbuf));
for (char *p = attrbuf; p - attrbuf < l; p += strlen(p) + 1) {
cout << " attr " << p << std::endl;
}
ASSERT_EQ(len, chain_fgetxattr(fd, name2.c_str(), buf, len*2));
ASSERT_EQ(0, chain_fremovexattr(fd, name2.c_str()));
}
for (int len = CHAIN_XATTR_SHORT_BLOCK_LEN - 10;
len < CHAIN_XATTR_SHORT_BLOCK_LEN + 10;
++len) {
cout << len << std::endl;
const string x(len, 'x');
char buf[len*2];
ASSERT_EQ(len, chain_setxattr(file, name.c_str(), x.c_str(), len));
char attrbuf[4096];
int l = ceph_os_listxattr(file, attrbuf, sizeof(attrbuf));
for (char *p = attrbuf; p - attrbuf < l; p += strlen(p) + 1) {
cout << " attr " << p << std::endl;
}
ASSERT_EQ(len, chain_getxattr(file, name.c_str(), buf, len*2));
}
{
// test tail path in chain_getxattr
const char *aname = "user.baz";
char buf[CHAIN_XATTR_SHORT_BLOCK_LEN*3];
memset(buf, 'x', sizeof(buf));
ASSERT_EQ((int)sizeof(buf), chain_setxattr(file, aname, buf, sizeof(buf)));
ASSERT_EQ(-ERANGE, chain_getxattr(file, aname, buf,
CHAIN_XATTR_SHORT_BLOCK_LEN*2));
}
{
// test tail path in chain_fgetxattr
const char *aname = "user.biz";
char buf[CHAIN_XATTR_SHORT_BLOCK_LEN*3];
memset(buf, 'x', sizeof(buf));
ASSERT_EQ((int)sizeof(buf), chain_fsetxattr(fd, aname, buf, sizeof(buf)));
ASSERT_EQ(-ERANGE, chain_fgetxattr(fd, aname, buf,
CHAIN_XATTR_SHORT_BLOCK_LEN*2));
}
::close(fd);
::unlink(file);
}
void get_vector_from_xattr(vector<string> &xattrs, char* xattr, int size) {
char *end = xattr + size;
while (xattr < end) {
if (*xattr == '\0' )
break;
xattrs.push_back(xattr);
xattr += strlen(xattr) + 1;
}
}
bool listxattr_cmp(char* xattr1, char* xattr2, int size) {
vector<string> xattrs1;
vector<string> xattrs2;
get_vector_from_xattr(xattrs1, xattr1, size);
get_vector_from_xattr(xattrs2, xattr2, size);
if (xattrs1.size() != xattrs2.size())
return false;
std::sort(xattrs1.begin(), xattrs1.end());
std::sort(xattrs2.begin(), xattrs2.end());
std::vector<string> diff;
std::set_difference(xattrs1.begin(), xattrs1.end(),
xattrs2.begin(), xattrs2.end(),
diff.begin());
return diff.empty();
}
TEST(chain_xattr, listxattr) {
const char* file = FILENAME;
::unlink(file);
int fd = ::open(file, O_CREAT|O_WRONLY|O_TRUNC, 0700);
const string user("user.");
const string name1 = user + string(CHAIN_XATTR_MAX_NAME_LEN - user.size(), '1');
const string name2 = user + string(CHAIN_XATTR_MAX_NAME_LEN - user.size(), '@');
const string x(LARGE_BLOCK_LEN, 'X');
const int y = 1234;
int orig_size = chain_listxattr(file, NULL, 0);
char *orig_buffer = NULL;
string orig_str;
if (orig_size) {
orig_buffer = (char*)malloc(orig_size);
chain_flistxattr(fd, orig_buffer, orig_size);
orig_str = string(orig_buffer);
orig_size = orig_str.size();
}
ASSERT_EQ(LARGE_BLOCK_LEN, chain_setxattr(file, name1.c_str(), x.c_str(), LARGE_BLOCK_LEN));
ASSERT_EQ((int)sizeof(y), chain_setxattr(file, name2.c_str(), &y, sizeof(y)));
int buffer_size = 0;
if (orig_size)
buffer_size += orig_size + sizeof(char);
buffer_size += name1.size() + sizeof(char) + name2.size() + sizeof(char);
int index = 0;
char* expected = (char*)malloc(buffer_size);
::memset(expected, '\0', buffer_size);
if (orig_size) {
::strcpy(expected, orig_str.c_str());
index = orig_size + 1;
}
::strcpy(expected + index, name1.c_str());
::strcpy(expected + index + name1.size() + 1, name2.c_str());
char* actual = (char*)malloc(buffer_size);
::memset(actual, '\0', buffer_size);
ASSERT_LT(buffer_size, chain_listxattr(file, NULL, 0)); // size evaluation is conservative
chain_listxattr(file, actual, buffer_size);
ASSERT_TRUE(listxattr_cmp(expected, actual, buffer_size));
::memset(actual, '\0', buffer_size);
chain_flistxattr(fd, actual, buffer_size);
ASSERT_TRUE(listxattr_cmp(expected, actual, buffer_size));
int unlikely_to_be_a_valid_fd = 400;
ASSERT_GT(0, chain_listxattr("UNLIKELY_TO_EXIST", actual, 0));
ASSERT_GT(0, chain_listxattr("UNLIKELY_TO_EXIST", actual, buffer_size));
ASSERT_GT(0, chain_flistxattr(unlikely_to_be_a_valid_fd, actual, 0));
ASSERT_GT(0, chain_flistxattr(unlikely_to_be_a_valid_fd, actual, buffer_size));
ASSERT_EQ(-ERANGE, chain_listxattr(file, actual, 1));
ASSERT_EQ(-ERANGE, chain_flistxattr(fd, actual, 1));
ASSERT_EQ(0, chain_removexattr(file, name1.c_str()));
ASSERT_EQ(0, chain_removexattr(file, name2.c_str()));
free(orig_buffer);
free(actual);
free(expected);
::unlink(file);
}
list<string> get_xattrs(int fd)
{
char _buf[1024];
char *buf = _buf;
int len = sys_flistxattr(fd, _buf, sizeof(_buf));
if (len < 0)
return list<string>();
list<string> ret;
while (len > 0) {
size_t next_len = strlen(buf);
ret.push_back(string(buf, buf + next_len));
ceph_assert(len >= (int)(next_len + 1));
buf += (next_len + 1);
len -= (next_len + 1);
}
return ret;
}
list<string> get_xattrs(string fn)
{
int fd = ::open(fn.c_str(), O_RDONLY);
if (fd < 0)
return list<string>();
auto ret = get_xattrs(fd);
::close(fd);
return ret;
}
TEST(chain_xattr, fskip_chain_cleanup_and_ensure_single_attr)
{
const char *name = "user.foo";
const char *file = FILENAME;
::unlink(file);
int fd = ::open(file, O_CREAT|O_RDWR|O_TRUNC, 0700);
ceph_assert(fd >= 0);
std::size_t existing_xattrs = get_xattrs(fd).size();
char buf[800];
memset(buf, 0x1F, sizeof(buf));
// set chunked without either
{
std::size_t r = chain_fsetxattr(fd, name, buf, sizeof(buf));
ASSERT_EQ(sizeof(buf), r);
ASSERT_GT(get_xattrs(fd).size(), existing_xattrs + 1UL);
}
// verify
{
char buf2[sizeof(buf)*2];
std::size_t r = chain_fgetxattr(fd, name, buf2, sizeof(buf2));
ASSERT_EQ(sizeof(buf), r);
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
}
// overwrite
{
std::size_t r = chain_fsetxattr<false, true>(fd, name, buf, sizeof(buf));
ASSERT_EQ(sizeof(buf), r);
ASSERT_EQ(existing_xattrs + 1UL, get_xattrs(fd).size());
}
// verify
{
char buf2[sizeof(buf)*2];
std::size_t r = chain_fgetxattr(fd, name, buf2, sizeof(buf2));
ASSERT_EQ(sizeof(buf), r);
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
}
::close(fd);
::unlink(file);
}
TEST(chain_xattr, skip_chain_cleanup_and_ensure_single_attr)
{
const char *name = "user.foo";
const char *file = FILENAME;
::unlink(file);
int fd = ::open(file, O_CREAT|O_RDWR|O_TRUNC, 0700);
ceph_assert(fd >= 0);
std::size_t existing_xattrs = get_xattrs(fd).size();
::close(fd);
char buf[3000];
memset(buf, 0x1F, sizeof(buf));
// set chunked without either
{
std::size_t r = chain_setxattr(file, name, buf, sizeof(buf));
ASSERT_EQ(sizeof(buf), r);
ASSERT_GT(get_xattrs(file).size(), existing_xattrs + 1UL);
}
// verify
{
char buf2[sizeof(buf)*2];
std::size_t r = chain_getxattr(file, name, buf2, sizeof(buf2));
ASSERT_EQ(sizeof(buf), r);
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
}
// overwrite
{
std::size_t r = chain_setxattr<false, true>(file, name, buf, sizeof(buf));
ASSERT_EQ(sizeof(buf), r);
ASSERT_EQ(existing_xattrs + 1UL, get_xattrs(file).size());
}
// verify
{
char buf2[sizeof(buf)*2];
std::size_t r = chain_getxattr(file, name, buf2, sizeof(buf2));
ASSERT_EQ(sizeof(buf), r);
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
}
::unlink(file);
}
int main(int argc, char **argv) {
vector<const char*> args;
argv_to_vec(argc, (const char **)argv, args);
auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
CODE_ENVIRONMENT_UTILITY,
CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
common_init_finish(g_ceph_context);
g_ceph_context->_conf.set_val("err_to_stderr", "false");
g_ceph_context->_conf.set_val("log_to_stderr", "false");
g_ceph_context->_conf.apply_changes(nullptr);
const char* file = FILENAME;
int x = 1234;
int y = 0;
int tmpfd = ::open(file, O_CREAT|O_WRONLY|O_TRUNC, 0700);
int ret = ::ceph_os_fsetxattr(tmpfd, "user.test", &x, sizeof(x));
if (ret >= 0)
ret = ::ceph_os_fgetxattr(tmpfd, "user.test", &y, sizeof(y));
::close(tmpfd);
::unlink(file);
if ((ret < 0) || (x != y)) {
cerr << "SKIP all tests because extended attributes don't appear to work in the file system in which the tests are run: " << cpp_strerror(ret) << std::endl;
} else {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
}
// Local Variables:
// compile-command: "cd ../.. ; make unittest_chain_xattr ; valgrind --tool=memcheck ./unittest_chain_xattr # --gtest_filter=chain_xattr.get_and_set"
// End:
|