]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/db/db_encryption_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).
6 #include "db/db_test_util.h"
7 #include "port/stack_trace.h"
8 #include "rocksdb/perf_context.h"
9 #if !defined(ROCKSDB_LITE)
10 #include "test_util/sync_point.h"
15 namespace ROCKSDB_NAMESPACE
{
17 class DBEncryptionTest
: public DBTestBase
{
20 : DBTestBase("db_encryption_test", /*env_do_fsync=*/true) {}
22 if (encrypted_env_
!= nullptr) {
23 return (static_cast<EnvWrapper
*>(encrypted_env_
))->target();
32 TEST_F(DBEncryptionTest
, CheckEncrypted
) {
33 ASSERT_OK(Put("foo567", "v1.fetdq"));
34 ASSERT_OK(Put("bar123", "v2.dfgkjdfghsd"));
37 // Open all files and look for the values we've put in there.
38 // They should not be found if encrypted, otherwise
39 // they should be found.
40 std::vector
<std::string
> fileNames
;
41 auto status
= env_
->GetChildren(dbname_
, &fileNames
);
44 Env
* target
= GetTargetEnv();
46 for (auto it
= fileNames
.begin(); it
!= fileNames
.end(); ++it
) {
50 auto filePath
= dbname_
+ "/" + *it
;
51 std::unique_ptr
<SequentialFile
> seqFile
;
52 auto envOptions
= EnvOptions(CurrentOptions());
53 status
= target
->NewSequentialFile(filePath
, &seqFile
, envOptions
);
57 status
= target
->GetFileSize(filePath
, &fileSize
);
61 scratch
.reserve(fileSize
);
63 status
= seqFile
->Read(fileSize
, &data
, (char*)scratch
.data());
66 if (data
.ToString().find("foo567") != std::string::npos
) {
68 // std::cout << "Hit in " << filePath << "\n";
70 if (data
.ToString().find("v1.fetdq") != std::string::npos
) {
72 // std::cout << "Hit in " << filePath << "\n";
74 if (data
.ToString().find("bar123") != std::string::npos
) {
76 // std::cout << "Hit in " << filePath << "\n";
78 if (data
.ToString().find("v2.dfgkjdfghsd") != std::string::npos
) {
80 // std::cout << "Hit in " << filePath << "\n";
82 if (data
.ToString().find("dfgk") != std::string::npos
) {
84 // std::cout << "Hit in " << filePath << "\n";
94 TEST_F(DBEncryptionTest
, ReadEmptyFile
) {
95 auto defaultEnv
= GetTargetEnv();
97 // create empty file for reading it back in later
98 auto envOptions
= EnvOptions(CurrentOptions());
99 auto filePath
= dbname_
+ "/empty.empty";
103 std::unique_ptr
<WritableFile
> writableFile
;
104 status
= defaultEnv
->NewWritableFile(filePath
, &writableFile
, envOptions
);
108 std::unique_ptr
<SequentialFile
> seqFile
;
109 status
= defaultEnv
->NewSequentialFile(filePath
, &seqFile
, envOptions
);
114 // reading back 16 bytes from the empty file shouldn't trigger an assertion.
115 // it should just work and return an empty string
116 status
= seqFile
->Read(16, &data
, (char*)scratch
.data());
119 ASSERT_TRUE(data
.empty());
122 #endif // ROCKSDB_LITE
124 } // namespace ROCKSDB_NAMESPACE
126 int main(int argc
, char** argv
) {
127 ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
128 ::testing::InitGoogleTest(&argc
, argv
);
129 return RUN_ALL_TESTS();