db_encryption_test.cc 3.5 KB

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