| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
| 15 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" | 15 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" |
| 16 #include "content/browser/indexed_db/leveldb/leveldb_database.h" | 16 #include "content/browser/indexed_db/leveldb/leveldb_database.h" |
| 17 #include "content/browser/indexed_db/leveldb/leveldb_env.h" | 17 #include "content/browser/indexed_db/leveldb/leveldb_env.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "third_party/leveldatabase/env_chromium.h" | 19 #include "third_party/leveldatabase/env_chromium.h" |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 static const size_t kDefaultMaxOpenIteratorsPerDatabase = 50; |
| 24 | 25 |
| 25 class SimpleComparator : public LevelDBComparator { | 26 class SimpleComparator : public LevelDBComparator { |
| 26 public: | 27 public: |
| 27 int Compare(const base::StringPiece& a, | 28 int Compare(const base::StringPiece& a, |
| 28 const base::StringPiece& b) const override { | 29 const base::StringPiece& b) const override { |
| 29 size_t len = std::min(a.size(), b.size()); | 30 size_t len = std::min(a.size(), b.size()); |
| 30 return memcmp(a.begin(), b.begin(), len); | 31 return memcmp(a.begin(), b.begin(), len); |
| 31 } | 32 } |
| 32 const char* Name() const override { return "temp_comparator"; } | 33 const char* Name() const override { return "temp_comparator"; } |
| 33 }; | 34 }; |
| 34 | 35 |
| 35 } // namespace | 36 } // namespace |
| 36 | 37 |
| 37 TEST(LevelDBDatabaseTest, CorruptionTest) { | 38 TEST(LevelDBDatabaseTest, CorruptionTest) { |
| 38 base::ScopedTempDir temp_directory; | 39 base::ScopedTempDir temp_directory; |
| 39 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 40 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 40 | 41 |
| 41 const std::string key("key"); | 42 const std::string key("key"); |
| 42 const std::string value("value"); | 43 const std::string value("value"); |
| 43 std::string put_value; | 44 std::string put_value; |
| 44 std::string got_value; | 45 std::string got_value; |
| 45 SimpleComparator comparator; | 46 SimpleComparator comparator; |
| 46 | 47 |
| 47 std::unique_ptr<LevelDBDatabase> leveldb; | 48 std::unique_ptr<LevelDBDatabase> leveldb; |
| 48 LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, &leveldb); | 49 LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, &leveldb, |
| 50 kDefaultMaxOpenIteratorsPerDatabase); |
| 49 EXPECT_TRUE(leveldb); | 51 EXPECT_TRUE(leveldb); |
| 50 put_value = value; | 52 put_value = value; |
| 51 leveldb::Status status = leveldb->Put(key, &put_value); | 53 leveldb::Status status = leveldb->Put(key, &put_value); |
| 52 EXPECT_TRUE(status.ok()); | 54 EXPECT_TRUE(status.ok()); |
| 53 leveldb.reset(); | 55 leveldb.reset(); |
| 54 EXPECT_FALSE(leveldb); | 56 EXPECT_FALSE(leveldb); |
| 55 | 57 |
| 56 LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, &leveldb); | 58 LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, &leveldb, |
| 59 kDefaultMaxOpenIteratorsPerDatabase); |
| 57 EXPECT_TRUE(leveldb); | 60 EXPECT_TRUE(leveldb); |
| 58 bool found = false; | 61 bool found = false; |
| 59 status = leveldb->Get(key, &got_value, &found); | 62 status = leveldb->Get(key, &got_value, &found); |
| 60 EXPECT_TRUE(status.ok()); | 63 EXPECT_TRUE(status.ok()); |
| 61 EXPECT_TRUE(found); | 64 EXPECT_TRUE(found); |
| 62 EXPECT_EQ(value, got_value); | 65 EXPECT_EQ(value, got_value); |
| 63 leveldb.reset(); | 66 leveldb.reset(); |
| 64 EXPECT_FALSE(leveldb); | 67 EXPECT_FALSE(leveldb); |
| 65 | 68 |
| 66 base::FilePath file_path = temp_directory.GetPath().AppendASCII("CURRENT"); | 69 base::FilePath file_path = temp_directory.GetPath().AppendASCII("CURRENT"); |
| 67 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_WRITE); | 70 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_WRITE); |
| 68 file.SetLength(0); | 71 file.SetLength(0); |
| 69 file.Close(); | 72 file.Close(); |
| 70 | 73 |
| 71 status = | 74 status = LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, |
| 72 LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, &leveldb); | 75 &leveldb, kDefaultMaxOpenIteratorsPerDatabase); |
| 73 EXPECT_FALSE(leveldb); | 76 EXPECT_FALSE(leveldb); |
| 74 EXPECT_FALSE(status.ok()); | 77 EXPECT_FALSE(status.ok()); |
| 75 EXPECT_TRUE(status.IsCorruption()); | 78 EXPECT_TRUE(status.IsCorruption()); |
| 76 | 79 |
| 77 status = LevelDBDatabase::Destroy(temp_directory.GetPath()); | 80 status = LevelDBDatabase::Destroy(temp_directory.GetPath()); |
| 78 EXPECT_TRUE(status.ok()); | 81 EXPECT_TRUE(status.ok()); |
| 79 | 82 |
| 80 status = | 83 status = LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, |
| 81 LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, &leveldb); | 84 &leveldb, kDefaultMaxOpenIteratorsPerDatabase); |
| 82 EXPECT_TRUE(status.ok()); | 85 EXPECT_TRUE(status.ok()); |
| 83 EXPECT_TRUE(leveldb); | 86 EXPECT_TRUE(leveldb); |
| 84 status = leveldb->Get(key, &got_value, &found); | 87 status = leveldb->Get(key, &got_value, &found); |
| 85 EXPECT_TRUE(status.ok()); | 88 EXPECT_TRUE(status.ok()); |
| 86 EXPECT_FALSE(found); | 89 EXPECT_FALSE(found); |
| 87 } | 90 } |
| 88 | 91 |
| 89 TEST(LevelDB, Locking) { | 92 TEST(LevelDB, Locking) { |
| 90 base::ScopedTempDir temp_directory; | 93 base::ScopedTempDir temp_directory; |
| 91 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 94 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 104 | 107 |
| 105 leveldb::FileLock* lock2; | 108 leveldb::FileLock* lock2; |
| 106 status = env->LockFile(file.AsUTF8Unsafe(), &lock2); | 109 status = env->LockFile(file.AsUTF8Unsafe(), &lock2); |
| 107 EXPECT_FALSE(status.ok()); | 110 EXPECT_FALSE(status.ok()); |
| 108 | 111 |
| 109 status = env->UnlockFile(lock); | 112 status = env->UnlockFile(lock); |
| 110 EXPECT_TRUE(status.ok()); | 113 EXPECT_TRUE(status.ok()); |
| 111 } | 114 } |
| 112 | 115 |
| 113 } // namespace content | 116 } // namespace content |
| OLD | NEW |