]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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) | |
f67539c2 | 10 | #include "test_util/sync_point.h" |
11fdf7f2 TL |
11 | #endif |
12 | #include <iostream> | |
13 | #include <string> | |
14 | ||
f67539c2 | 15 | namespace ROCKSDB_NAMESPACE { |
11fdf7f2 TL |
16 | |
17 | class DBEncryptionTest : public DBTestBase { | |
18 | public: | |
20effc67 TL |
19 | DBEncryptionTest() |
20 | : DBTestBase("/db_encryption_test", /*env_do_fsync=*/true) {} | |
21 | Env* GetTargetEnv() { | |
22 | if (encrypted_env_ != nullptr) { | |
23 | return (static_cast<EnvWrapper*>(encrypted_env_))->target(); | |
24 | } else { | |
25 | return env_; | |
26 | } | |
27 | } | |
11fdf7f2 TL |
28 | }; |
29 | ||
30 | #ifndef ROCKSDB_LITE | |
31 | ||
32 | TEST_F(DBEncryptionTest, CheckEncrypted) { | |
33 | ASSERT_OK(Put("foo567", "v1.fetdq")); | |
34 | ASSERT_OK(Put("bar123", "v2.dfgkjdfghsd")); | |
35 | Close(); | |
36 | ||
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); | |
42 | ASSERT_OK(status); | |
43 | ||
20effc67 | 44 | Env* target = GetTargetEnv(); |
11fdf7f2 TL |
45 | int hits = 0; |
46 | for (auto it = fileNames.begin() ; it != fileNames.end(); ++it) { | |
20effc67 | 47 | if ((*it == "..") || (*it == ".") || (*it == "LOCK")) { |
11fdf7f2 TL |
48 | continue; |
49 | } | |
50 | auto filePath = dbname_ + "/" + *it; | |
494da23a | 51 | std::unique_ptr<SequentialFile> seqFile; |
11fdf7f2 | 52 | auto envOptions = EnvOptions(CurrentOptions()); |
20effc67 | 53 | status = target->NewSequentialFile(filePath, &seqFile, envOptions); |
11fdf7f2 TL |
54 | ASSERT_OK(status); |
55 | ||
56 | uint64_t fileSize; | |
20effc67 | 57 | status = target->GetFileSize(filePath, &fileSize); |
11fdf7f2 TL |
58 | ASSERT_OK(status); |
59 | ||
60 | std::string scratch; | |
61 | scratch.reserve(fileSize); | |
62 | Slice data; | |
63 | status = seqFile->Read(fileSize, &data, (char*)scratch.data()); | |
64 | ASSERT_OK(status); | |
65 | ||
66 | if (data.ToString().find("foo567") != std::string::npos) { | |
67 | hits++; | |
68 | //std::cout << "Hit in " << filePath << "\n"; | |
69 | } | |
70 | if (data.ToString().find("v1.fetdq") != std::string::npos) { | |
71 | hits++; | |
72 | //std::cout << "Hit in " << filePath << "\n"; | |
73 | } | |
74 | if (data.ToString().find("bar123") != std::string::npos) { | |
75 | hits++; | |
76 | //std::cout << "Hit in " << filePath << "\n"; | |
77 | } | |
78 | if (data.ToString().find("v2.dfgkjdfghsd") != std::string::npos) { | |
79 | hits++; | |
80 | //std::cout << "Hit in " << filePath << "\n"; | |
81 | } | |
82 | if (data.ToString().find("dfgk") != std::string::npos) { | |
83 | hits++; | |
84 | //std::cout << "Hit in " << filePath << "\n"; | |
85 | } | |
86 | } | |
87 | if (encrypted_env_) { | |
88 | ASSERT_EQ(hits, 0); | |
89 | } else { | |
90 | ASSERT_GE(hits, 4); | |
91 | } | |
92 | } | |
93 | ||
f67539c2 | 94 | TEST_F(DBEncryptionTest, ReadEmptyFile) { |
20effc67 | 95 | auto defaultEnv = GetTargetEnv(); |
f67539c2 TL |
96 | |
97 | // create empty file for reading it back in later | |
98 | auto envOptions = EnvOptions(CurrentOptions()); | |
99 | auto filePath = dbname_ + "/empty.empty"; | |
100 | ||
101 | Status status; | |
102 | { | |
103 | std::unique_ptr<WritableFile> writableFile; | |
104 | status = defaultEnv->NewWritableFile(filePath, &writableFile, envOptions); | |
105 | ASSERT_OK(status); | |
106 | } | |
107 | ||
108 | std::unique_ptr<SequentialFile> seqFile; | |
109 | status = defaultEnv->NewSequentialFile(filePath, &seqFile, envOptions); | |
110 | ASSERT_OK(status); | |
111 | ||
112 | std::string scratch; | |
113 | Slice data; | |
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()); | |
117 | ASSERT_OK(status); | |
118 | ||
119 | ASSERT_TRUE(data.empty()); | |
120 | } | |
121 | ||
11fdf7f2 TL |
122 | #endif // ROCKSDB_LITE |
123 | ||
f67539c2 | 124 | } // namespace ROCKSDB_NAMESPACE |
11fdf7f2 TL |
125 | |
126 | int main(int argc, char** argv) { | |
f67539c2 | 127 | ROCKSDB_NAMESPACE::port::InstallStackTraceHandler(); |
11fdf7f2 TL |
128 | ::testing::InitGoogleTest(&argc, argv); |
129 | return RUN_ALL_TESTS(); | |
130 | } |