]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/db/blob/db_blob_corruption_test.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / db / blob / db_blob_corruption_test.cc
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5
6 #include "db/db_test_util.h"
7 #include "port/stack_trace.h"
8 #include "test_util/sync_point.h"
9
10 namespace ROCKSDB_NAMESPACE {
11
12 class DBBlobCorruptionTest : public DBTestBase {
13 protected:
14 DBBlobCorruptionTest()
15 : DBTestBase("db_blob_corruption_test", /* env_do_fsync */ false) {}
16
17 void Corrupt(FileType filetype, int offset, int bytes_to_corrupt) {
18 // Pick file to corrupt
19 std::vector<std::string> filenames;
20 ASSERT_OK(env_->GetChildren(dbname_, &filenames));
21 uint64_t number;
22 FileType type;
23 std::string fname;
24 uint64_t picked_number = kInvalidBlobFileNumber;
25 for (size_t i = 0; i < filenames.size(); i++) {
26 if (ParseFileName(filenames[i], &number, &type) && type == filetype &&
27 number > picked_number) { // Pick latest file
28 fname = dbname_ + "/" + filenames[i];
29 picked_number = number;
30 }
31 }
32 ASSERT_TRUE(!fname.empty()) << filetype;
33 ASSERT_OK(test::CorruptFile(env_, fname, offset, bytes_to_corrupt));
34 }
35 };
36
37 #ifndef ROCKSDB_LITE
38 TEST_F(DBBlobCorruptionTest, VerifyWholeBlobFileChecksum) {
39 Options options = GetDefaultOptions();
40 options.enable_blob_files = true;
41 options.min_blob_size = 0;
42 options.create_if_missing = true;
43 options.file_checksum_gen_factory =
44 ROCKSDB_NAMESPACE::GetFileChecksumGenCrc32cFactory();
45 Reopen(options);
46
47 ASSERT_OK(Put(Slice("key_1"), Slice("blob_value_1")));
48 ASSERT_OK(Flush());
49 ASSERT_OK(Put(Slice("key_2"), Slice("blob_value_2")));
50 ASSERT_OK(Flush());
51 ASSERT_OK(db_->VerifyFileChecksums(ReadOptions()));
52 Close();
53
54 Corrupt(kBlobFile, 0, 2);
55
56 ASSERT_OK(TryReopen(options));
57
58 int count{0};
59 SyncPoint::GetInstance()->SetCallBack(
60 "DBImpl::VerifyFullFileChecksum:mismatch", [&](void* arg) {
61 const Status* s = static_cast<Status*>(arg);
62 ASSERT_NE(s, nullptr);
63 ++count;
64 ASSERT_NOK(*s);
65 });
66 SyncPoint::GetInstance()->EnableProcessing();
67
68 ASSERT_TRUE(db_->VerifyFileChecksums(ReadOptions()).IsCorruption());
69 ASSERT_EQ(1, count);
70
71 ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
72 ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
73 }
74 #endif // !ROCKSDB_LITE
75 } // namespace ROCKSDB_NAMESPACE
76
77 int main(int argc, char** argv) {
78 ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
79 ::testing::InitGoogleTest(&argc, argv);
80 RegisterCustomObjects(argc, argv);
81 return RUN_ALL_TESTS();
82 }