1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com>
8 * Author: Loic Dachary <loic@dachary.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Library Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library Public License for more details.
24 #include "os/filestore/LFNIndex.h"
25 #include "os/filestore/chain_xattr.h"
26 #include "common/ceph_argparse.h"
27 #include "global/global_init.h"
28 #include <gtest/gtest.h>
30 class TestWrapLFNIndex
: public LFNIndex
{
32 TestWrapLFNIndex(CephContext
* cct
,
34 const char *base_path
,
35 uint32_t index_version
)
36 : LFNIndex(cct
, collection
, base_path
, index_version
) {}
38 uint32_t collection_version() override
{
42 int cleanup() override
{ return 0; }
48 ) override
{ return 0; }
50 void test_generate_and_parse(const ghobject_t
&hoid
, const std::string
&mangled_expected
) {
51 const std::string mangled_name
= lfn_generate_object_name(hoid
);
52 EXPECT_EQ(mangled_expected
, mangled_name
);
53 ghobject_t hoid_parsed
;
54 EXPECT_EQ(0, lfn_parse_object_name(mangled_name
, &hoid_parsed
));
55 EXPECT_EQ(hoid
, hoid_parsed
);
59 int _init() override
{ return 0; }
62 const vector
<string
> &path
,
63 const ghobject_t
&hoid
,
64 const string
&mangled_name
65 ) override
{ return 0; }
68 const vector
<string
> &path
,
69 const ghobject_t
&hoid
,
70 const string
&mangled_name
71 ) override
{ return 0; }
74 const ghobject_t
&hoid
,
78 ) override
{ return 0; }
80 int _collection_list_partial(
81 const ghobject_t
&start
,
82 const ghobject_t
&end
,
84 vector
<ghobject_t
> *ls
,
86 ) override
{ return 0; }
87 int _pre_hash_collection(
89 uint64_t expected_num_objs
90 ) override
{ return 0; }
94 class TestHASH_INDEX_TAG
: public TestWrapLFNIndex
, public ::testing::Test
{
97 : TestWrapLFNIndex(g_ceph_context
, coll_t(), "PATH_1",
98 CollectionIndex::HASH_INDEX_TAG
) {
102 TEST_F(TestHASH_INDEX_TAG
, generate_and_parse_name
) {
103 const vector
<string
> path
;
104 const std::string key
;
105 uint64_t hash
= 0xABABABAB;
108 test_generate_and_parse(ghobject_t(hobject_t(object_t(".A/B_\\C.D"), key
, CEPH_NOSNAP
, hash
, pool
, "")),
109 "\\.A\\sB_\\\\C.D_head_ABABABAB");
110 test_generate_and_parse(ghobject_t(hobject_t(object_t("DIR_A"), key
, CEPH_NOSNAP
, hash
, pool
, "")),
111 "\\dA_head_ABABABAB");
114 class TestHASH_INDEX_TAG_2
: public TestWrapLFNIndex
, public ::testing::Test
{
116 TestHASH_INDEX_TAG_2()
117 : TestWrapLFNIndex(g_ceph_context
,
118 coll_t(), "PATH_1", CollectionIndex::HASH_INDEX_TAG_2
) {
122 TEST_F(TestHASH_INDEX_TAG_2
, generate_and_parse_name
) {
123 const vector
<string
> path
;
124 const std::string
key("KEY");
125 uint64_t hash
= 0xABABABAB;
129 std::string
name(".XA/B_\\C.D");
131 ghobject_t
hoid(hobject_t(object_t(name
), key
, CEPH_NOSNAP
, hash
, pool
, ""));
133 test_generate_and_parse(hoid
, "\\.\\nA\\sB\\u\\\\C.D_KEY_head_ABABABAB");
135 test_generate_and_parse(ghobject_t(hobject_t(object_t("DIR_A"), key
, CEPH_NOSNAP
, hash
, pool
, "")),
136 "\\dA_KEY_head_ABABABAB");
139 class TestHOBJECT_WITH_POOL
: public TestWrapLFNIndex
, public ::testing::Test
{
141 TestHOBJECT_WITH_POOL()
142 : TestWrapLFNIndex(g_ceph_context
, coll_t(),
143 "PATH_1", CollectionIndex::HOBJECT_WITH_POOL
) {
147 TEST_F(TestHOBJECT_WITH_POOL
, generate_and_parse_name
) {
148 const vector
<string
> path
;
149 const std::string
key("KEY");
150 uint64_t hash
= 0xABABABAB;
151 uint64_t pool
= 0xCDCDCDCD;
152 int64_t gen
= 0xefefefefef;
153 shard_id_t
shard_id(0xb);
156 std::string
name(".XA/B_\\C.D");
158 ghobject_t
hoid(hobject_t(object_t(name
), key
, CEPH_NOSNAP
, hash
, pool
, ""));
159 hoid
.hobj
.nspace
= "NSPACE";
161 test_generate_and_parse(hoid
, "\\.\\nA\\sB\\u\\\\C.D_KEY_head_ABABABAB_NSPACE_cdcdcdcd");
164 ghobject_t
hoid(hobject_t(object_t("DIR_A"), key
, CEPH_NOSNAP
, hash
, pool
, ""));
165 hoid
.hobj
.nspace
= "NSPACE";
167 test_generate_and_parse(hoid
, "\\dA_KEY_head_ABABABAB_NSPACE_cdcdcdcd");
170 std::string
name(".XA/B_\\C.D");
172 ghobject_t
hoid(hobject_t(object_t(name
), key
, CEPH_NOSNAP
, hash
, pool
, ""), gen
, shard_id
);
173 hoid
.hobj
.nspace
= "NSPACE";
175 test_generate_and_parse(hoid
, "\\.\\nA\\sB\\u\\\\C.D_KEY_head_ABABABAB_NSPACE_cdcdcdcd_efefefefef_b");
178 ghobject_t
hoid(hobject_t(object_t("DIR_A"), key
, CEPH_NOSNAP
, hash
, pool
, ""), gen
, shard_id
);
179 hoid
.hobj
.nspace
= "NSPACE";
181 test_generate_and_parse(hoid
, "\\dA_KEY_head_ABABABAB_NSPACE_cdcdcdcd_efefefefef_b");
185 class TestLFNIndex
: public TestWrapLFNIndex
, public ::testing::Test
{
188 : TestWrapLFNIndex(g_ceph_context
, coll_t(), "PATH_1",
189 CollectionIndex::HOBJECT_WITH_POOL
) {
192 void SetUp() override
{
193 ::chmod("PATH_1", 0700);
194 ASSERT_EQ(0, ::system("rm -fr PATH_1"));
195 ASSERT_EQ(0, ::mkdir("PATH_1", 0700));
198 void TearDown() override
{
199 ASSERT_EQ(0, ::system("rm -fr PATH_1"));
203 TEST_F(TestLFNIndex
, remove_object
) {
204 const vector
<string
> path
;
207 // small object name removal
210 std::string mangled_name
;
212 ghobject_t
hoid(hobject_t(sobject_t("ABC", CEPH_NOSNAP
)));
214 EXPECT_EQ(0, ::chmod("PATH_1", 0000));
216 EXPECT_EQ(-EACCES
, remove_object(path
, hoid
));
218 EXPECT_EQ(0, ::chmod("PATH_1", 0700));
219 EXPECT_EQ(-ENOENT
, remove_object(path
, hoid
));
220 EXPECT_EQ(0, get_mangled_name(path
, hoid
, &mangled_name
, &exists
));
221 const std::string
pathname("PATH_1/" + mangled_name
);
222 EXPECT_EQ(0, ::close(::creat(pathname
.c_str(), 0600)));
223 EXPECT_EQ(0, remove_object(path
, hoid
));
224 EXPECT_EQ(-1, ::access(pathname
.c_str(), 0));
225 EXPECT_EQ(ENOENT
, errno
);
228 // long object name removal of a single file
231 std::string mangled_name
;
233 const std::string
object_name(1024, 'A');
234 ghobject_t
hoid(hobject_t(sobject_t(object_name
, CEPH_NOSNAP
)));
236 EXPECT_EQ(0, get_mangled_name(path
, hoid
, &mangled_name
, &exists
));
237 EXPECT_EQ(0, exists
);
238 EXPECT_NE(std::string::npos
, mangled_name
.find("0_long"));
239 std::string
pathname("PATH_1/" + mangled_name
);
240 EXPECT_EQ(0, ::close(::creat(pathname
.c_str(), 0600)));
241 EXPECT_EQ(0, created(hoid
, pathname
.c_str()));
243 EXPECT_EQ(0, remove_object(path
, hoid
));
244 EXPECT_EQ(-1, ::access(pathname
.c_str(), 0));
245 EXPECT_EQ(ENOENT
, errno
);
249 // long object name removal of the last file
252 std::string mangled_name
;
254 const std::string
object_name(1024, 'A');
255 ghobject_t
hoid(hobject_t(sobject_t(object_name
, CEPH_NOSNAP
)));
258 // PATH_1/AAA..._0_long => does not match long object name
260 EXPECT_EQ(0, get_mangled_name(path
, hoid
, &mangled_name
, &exists
));
261 EXPECT_EQ(0, exists
);
262 EXPECT_NE(std::string::npos
, mangled_name
.find("0_long"));
263 std::string
pathname("PATH_1/" + mangled_name
);
264 EXPECT_EQ(0, ::close(::creat(pathname
.c_str(), 0600)));
265 EXPECT_EQ(0, created(hoid
, pathname
.c_str()));
266 string LFN_ATTR
= "user.cephos.lfn";
267 if (index_version
!= HASH_INDEX_TAG
) {
269 snprintf(buf
, sizeof(buf
), "%d", index_version
);
270 LFN_ATTR
+= string(buf
);
272 const std::string object_name_1
= object_name
+ "SUFFIX";
273 EXPECT_EQ(object_name_1
.size(), (unsigned)chain_setxattr(pathname
.c_str(), LFN_ATTR
.c_str(), object_name_1
.c_str(), object_name_1
.size()));
276 // PATH_1/AAA..._1_long => matches long object name
278 std::string mangled_name_1
;
280 EXPECT_EQ(0, get_mangled_name(path
, hoid
, &mangled_name_1
, &exists
));
281 EXPECT_NE(std::string::npos
, mangled_name_1
.find("1_long"));
282 EXPECT_EQ(0, exists
);
283 std::string
pathname_1("PATH_1/" + mangled_name_1
);
284 EXPECT_EQ(0, ::close(::creat(pathname_1
.c_str(), 0600)));
285 EXPECT_EQ(0, created(hoid
, pathname_1
.c_str()));
288 // remove_object skips PATH_1/AAA..._0_long and removes PATH_1/AAA..._1_long
290 EXPECT_EQ(0, remove_object(path
, hoid
));
291 EXPECT_EQ(0, ::access(pathname
.c_str(), 0));
292 EXPECT_EQ(-1, ::access(pathname_1
.c_str(), 0));
293 EXPECT_EQ(ENOENT
, errno
);
294 EXPECT_EQ(0, ::unlink(pathname
.c_str()));
298 // long object name removal of a file in the middle of the list
301 std::string mangled_name
;
303 const std::string
object_name(1024, 'A');
304 ghobject_t
hoid(hobject_t(sobject_t(object_name
, CEPH_NOSNAP
)));
307 // PATH_1/AAA..._0_long => matches long object name
309 EXPECT_EQ(0, get_mangled_name(path
, hoid
, &mangled_name
, &exists
));
310 EXPECT_EQ(0, exists
);
311 EXPECT_NE(std::string::npos
, mangled_name
.find("0_long"));
312 std::string
pathname("PATH_1/" + mangled_name
);
313 EXPECT_EQ(0, ::close(::creat(pathname
.c_str(), 0600)));
314 EXPECT_EQ(0, created(hoid
, pathname
.c_str()));
316 // PATH_1/AAA..._1_long => matches long object name
318 std::string mangled_name_1
= mangled_name
;
319 mangled_name_1
.replace(mangled_name_1
.find("0_long"), 6, "1_long");
320 const std::string
pathname_1("PATH_1/" + mangled_name_1
);
321 const std::string
cmd("cp -a " + pathname
+ " " + pathname_1
);
322 EXPECT_EQ(0, ::system(cmd
.c_str()));
323 const string ATTR
= "user.MARK";
324 EXPECT_EQ((unsigned)1, (unsigned)chain_setxattr(pathname_1
.c_str(), ATTR
.c_str(), "Y", 1));
327 // remove_object replaces the file to be removed with the last from the
328 // collision list. In this case it replaces
329 // PATH_1/AAA..._0_long
331 // PATH_1/AAA..._1_long
333 EXPECT_EQ(0, remove_object(path
, hoid
));
334 EXPECT_EQ(0, ::access(pathname
.c_str(), 0));
335 char buffer
[1] = { 0, };
336 EXPECT_EQ((unsigned)1, (unsigned)chain_getxattr(pathname
.c_str(), ATTR
.c_str(), buffer
, 1));
337 EXPECT_EQ('Y', buffer
[0]);
338 EXPECT_EQ(-1, ::access(pathname_1
.c_str(), 0));
339 EXPECT_EQ(ENOENT
, errno
);
343 TEST_F(TestLFNIndex
, get_mangled_name
) {
344 const vector
<string
> path
;
350 std::string mangled_name
;
352 ghobject_t
hoid(hobject_t(sobject_t("ABC", CEPH_NOSNAP
)));
354 EXPECT_EQ(0, get_mangled_name(path
, hoid
, &mangled_name
, &exists
));
355 EXPECT_NE(std::string::npos
, mangled_name
.find("ABC__head"));
356 EXPECT_EQ(std::string::npos
, mangled_name
.find("0_long"));
357 EXPECT_EQ(0, exists
);
358 const std::string
pathname("PATH_1/" + mangled_name
);
359 EXPECT_EQ(0, ::close(::creat(pathname
.c_str(), 0600)));
360 EXPECT_EQ(0, get_mangled_name(path
, hoid
, &mangled_name
, &exists
));
361 EXPECT_NE(std::string::npos
, mangled_name
.find("ABC__head"));
362 EXPECT_EQ(1, exists
);
363 EXPECT_EQ(0, ::unlink(pathname
.c_str()));
369 std::string mangled_name
;
371 const std::string
object_name(1024, 'A');
372 ghobject_t
hoid(hobject_t(sobject_t(object_name
, CEPH_NOSNAP
)));
375 // long version of the mangled name and no matching
378 mangled_name
.clear();
380 EXPECT_EQ(0, get_mangled_name(path
, hoid
, &mangled_name
, &exists
));
381 EXPECT_NE(std::string::npos
, mangled_name
.find("0_long"));
382 EXPECT_EQ(0, exists
);
384 const std::string
pathname("PATH_1/" + mangled_name
);
387 // if a file by the same name exists but does not have the
388 // expected extended attribute, it is silently removed
390 mangled_name
.clear();
392 EXPECT_EQ(0, ::close(::creat(pathname
.c_str(), 0600)));
393 EXPECT_EQ(0, get_mangled_name(path
, hoid
, &mangled_name
, &exists
));
394 EXPECT_NE(std::string::npos
, mangled_name
.find("0_long"));
395 EXPECT_EQ(0, exists
);
396 EXPECT_EQ(-1, ::access(pathname
.c_str(), 0));
397 EXPECT_EQ(ENOENT
, errno
);
400 // if a file by the same name exists but does not have the
401 // expected extended attribute, and cannot be removed,
404 mangled_name
.clear();
406 EXPECT_EQ(0, ::close(::creat(pathname
.c_str(), 0600)));
407 EXPECT_EQ(0, ::chmod("PATH_1", 0500));
409 EXPECT_EQ(-EACCES
, get_mangled_name(path
, hoid
, &mangled_name
, &exists
));
411 EXPECT_EQ("", mangled_name
);
412 EXPECT_EQ(666, exists
);
413 EXPECT_EQ(0, ::chmod("PATH_1", 0700));
414 EXPECT_EQ(0, ::unlink(pathname
.c_str()));
417 // long version of the mangled name and a file
418 // exists by that name and contains the long object name
420 mangled_name
.clear();
422 EXPECT_EQ(0, ::close(::creat(pathname
.c_str(), 0600)));
423 EXPECT_EQ(0, created(hoid
, pathname
.c_str()));
424 EXPECT_EQ(0, get_mangled_name(path
, hoid
, &mangled_name
, &exists
));
425 EXPECT_NE(std::string::npos
, mangled_name
.find("0_long"));
426 EXPECT_EQ(1, exists
);
427 EXPECT_EQ(0, ::access(pathname
.c_str(), 0));
430 // long version of the mangled name and a file exists by that name
431 // and contains a long object name with the same prefix but they
432 // are not identical and it so happens that their SHA1 is
433 // identical : a collision number is used to differentiate them
435 string LFN_ATTR
= "user.cephos.lfn";
436 if (index_version
!= HASH_INDEX_TAG
) {
438 snprintf(buf
, sizeof(buf
), "%d", index_version
);
439 LFN_ATTR
+= string(buf
);
441 const std::string object_name_same_prefix
= object_name
+ "SUFFIX";
442 EXPECT_EQ(object_name_same_prefix
.size(), (unsigned)chain_setxattr(pathname
.c_str(), LFN_ATTR
.c_str(), object_name_same_prefix
.c_str(), object_name_same_prefix
.size()));
443 std::string mangled_name_same_prefix
;
445 EXPECT_EQ(0, get_mangled_name(path
, hoid
, &mangled_name_same_prefix
, &exists
));
446 EXPECT_NE(std::string::npos
, mangled_name_same_prefix
.find("1_long"));
447 EXPECT_EQ(0, exists
);
449 EXPECT_EQ(0, ::unlink(pathname
.c_str()));
453 int main(int argc
, char **argv
) {
454 int fd
= ::creat("detect", 0600);
456 cerr
<< "failed to create file detect" << std::endl
;
459 int ret
= chain_fsetxattr(fd
, "user.test", "A", 1);
463 cerr
<< "SKIP LFNIndex because unable to test for xattr" << std::endl
;
465 vector
<const char*> args
;
466 argv_to_vec(argc
, (const char **)argv
, args
);
468 auto cct
= global_init(NULL
, args
, CEPH_ENTITY_TYPE_CLIENT
,
469 CODE_ENVIRONMENT_UTILITY
, 0);
470 common_init_finish(g_ceph_context
);
472 ::testing::InitGoogleTest(&argc
, argv
);
473 return RUN_ALL_TESTS();
479 * compile-command: "cd ../.. ;
480 * make unittest_lfnindex &&
481 * valgrind --tool=memcheck ./unittest_lfnindex \
482 * # --gtest_filter=TestLFNIndex.* --log-to-stderr=true --debug-filestore=20"