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 "rocksdb/perf_context.h"
9 #if !defined(ROCKSDB_LITE)
10 #include "test_util/sync_point.h"
11 #endif
12 #include <iostream>
13 #include <string>
14
15 namespace ROCKSDB_NAMESPACE {
16
17 class DBEncryptionTest : public DBTestBase {
18 public:
DBEncryptionTest()19 DBEncryptionTest() : DBTestBase("/db_encryption_test") {}
20 };
21
22 #ifndef ROCKSDB_LITE
23
TEST_F(DBEncryptionTest,CheckEncrypted)24 TEST_F(DBEncryptionTest, CheckEncrypted) {
25 ASSERT_OK(Put("foo567", "v1.fetdq"));
26 ASSERT_OK(Put("bar123", "v2.dfgkjdfghsd"));
27 Close();
28
29 // Open all files and look for the values we've put in there.
30 // They should not be found if encrypted, otherwise
31 // they should be found.
32 std::vector<std::string> fileNames;
33 auto status = env_->GetChildren(dbname_, &fileNames);
34 ASSERT_OK(status);
35
36 auto defaultEnv = Env::Default();
37 int hits = 0;
38 for (auto it = fileNames.begin() ; it != fileNames.end(); ++it) {
39 if ((*it == "..") || (*it == ".")) {
40 continue;
41 }
42 auto filePath = dbname_ + "/" + *it;
43 std::unique_ptr<SequentialFile> seqFile;
44 auto envOptions = EnvOptions(CurrentOptions());
45 status = defaultEnv->NewSequentialFile(filePath, &seqFile, envOptions);
46 ASSERT_OK(status);
47
48 uint64_t fileSize;
49 status = defaultEnv->GetFileSize(filePath, &fileSize);
50 ASSERT_OK(status);
51
52 std::string scratch;
53 scratch.reserve(fileSize);
54 Slice data;
55 status = seqFile->Read(fileSize, &data, (char*)scratch.data());
56 ASSERT_OK(status);
57
58 if (data.ToString().find("foo567") != std::string::npos) {
59 hits++;
60 //std::cout << "Hit in " << filePath << "\n";
61 }
62 if (data.ToString().find("v1.fetdq") != std::string::npos) {
63 hits++;
64 //std::cout << "Hit in " << filePath << "\n";
65 }
66 if (data.ToString().find("bar123") != std::string::npos) {
67 hits++;
68 //std::cout << "Hit in " << filePath << "\n";
69 }
70 if (data.ToString().find("v2.dfgkjdfghsd") != std::string::npos) {
71 hits++;
72 //std::cout << "Hit in " << filePath << "\n";
73 }
74 if (data.ToString().find("dfgk") != std::string::npos) {
75 hits++;
76 //std::cout << "Hit in " << filePath << "\n";
77 }
78 }
79 if (encrypted_env_) {
80 ASSERT_EQ(hits, 0);
81 } else {
82 ASSERT_GE(hits, 4);
83 }
84 }
85
TEST_F(DBEncryptionTest,ReadEmptyFile)86 TEST_F(DBEncryptionTest, ReadEmptyFile) {
87 auto defaultEnv = Env::Default();
88
89 // create empty file for reading it back in later
90 auto envOptions = EnvOptions(CurrentOptions());
91 auto filePath = dbname_ + "/empty.empty";
92
93 Status status;
94 {
95 std::unique_ptr<WritableFile> writableFile;
96 status = defaultEnv->NewWritableFile(filePath, &writableFile, envOptions);
97 ASSERT_OK(status);
98 }
99
100 std::unique_ptr<SequentialFile> seqFile;
101 status = defaultEnv->NewSequentialFile(filePath, &seqFile, envOptions);
102 ASSERT_OK(status);
103
104 std::string scratch;
105 Slice data;
106 // reading back 16 bytes from the empty file shouldn't trigger an assertion.
107 // it should just work and return an empty string
108 status = seqFile->Read(16, &data, (char*)scratch.data());
109 ASSERT_OK(status);
110
111 ASSERT_TRUE(data.empty());
112 }
113
114 #endif // ROCKSDB_LITE
115
116 } // namespace ROCKSDB_NAMESPACE
117
main(int argc,char ** argv)118 int main(int argc, char** argv) {
119 ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
120 ::testing::InitGoogleTest(&argc, argv);
121 return RUN_ALL_TESTS();
122 }
123