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 <cerrno> | 5 #include <cerrno> |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "content/browser/indexed_db/indexed_db_backing_store.h" | 11 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
12 #include "content/browser/indexed_db/leveldb/leveldb_database.h" | 12 #include "content/browser/indexed_db/leveldb/leveldb_database.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "third_party/leveldatabase/env_chromium.h" | 14 #include "third_party/leveldatabase/env_chromium.h" |
15 | 15 |
16 using base::StringPiece; | 16 using base::StringPiece; |
17 using content::IndexedDBBackingStore; | 17 using content::IndexedDBBackingStore; |
18 using content::LevelDBComparator; | 18 using content::LevelDBComparator; |
19 using content::LevelDBDatabase; | 19 using content::LevelDBDatabase; |
20 using content::LevelDBFactory; | 20 using content::LevelDBFactory; |
21 using content::LevelDBSnapshot; | 21 using content::LevelDBSnapshot; |
22 | 22 |
| 23 namespace base { |
| 24 class TaskRunner; |
| 25 } |
| 26 |
| 27 namespace content { |
| 28 class IndexedDBFactory; |
| 29 } |
| 30 |
| 31 namespace net { |
| 32 class URLRequestContext; |
| 33 } |
| 34 |
23 namespace { | 35 namespace { |
24 | 36 |
25 class BustedLevelDBDatabase : public LevelDBDatabase { | 37 class BustedLevelDBDatabase : public LevelDBDatabase { |
26 public: | 38 public: |
27 static scoped_ptr<LevelDBDatabase> Open( | 39 static scoped_ptr<LevelDBDatabase> Open( |
28 const base::FilePath& file_name, | 40 const base::FilePath& file_name, |
29 const LevelDBComparator* /*comparator*/) { | 41 const LevelDBComparator* /*comparator*/) { |
30 return scoped_ptr<LevelDBDatabase>(new BustedLevelDBDatabase); | 42 return scoped_ptr<LevelDBDatabase>(new BustedLevelDBDatabase); |
31 } | 43 } |
32 virtual bool Get(const base::StringPiece& key, | 44 virtual bool Get(const base::StringPiece& key, |
(...skipping 21 matching lines...) Expand all Loading... |
54 destroy_called_ = true; | 66 destroy_called_ = true; |
55 return false; | 67 return false; |
56 } | 68 } |
57 virtual ~MockLevelDBFactory() { EXPECT_TRUE(destroy_called_); } | 69 virtual ~MockLevelDBFactory() { EXPECT_TRUE(destroy_called_); } |
58 | 70 |
59 private: | 71 private: |
60 bool destroy_called_; | 72 bool destroy_called_; |
61 }; | 73 }; |
62 | 74 |
63 TEST(IndexedDBIOErrorTest, CleanUpTest) { | 75 TEST(IndexedDBIOErrorTest, CleanUpTest) { |
| 76 content::IndexedDBFactory* factory = NULL; |
64 const GURL origin("http://localhost:81"); | 77 const GURL origin("http://localhost:81"); |
65 base::ScopedTempDir temp_directory; | 78 base::ScopedTempDir temp_directory; |
66 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 79 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
67 const base::FilePath path = temp_directory.path(); | 80 const base::FilePath path = temp_directory.path(); |
| 81 net::URLRequestContext* request_context = NULL; |
68 MockLevelDBFactory mock_leveldb_factory; | 82 MockLevelDBFactory mock_leveldb_factory; |
69 blink::WebIDBDataLoss data_loss = | 83 blink::WebIDBDataLoss data_loss = |
70 blink::WebIDBDataLossNone; | 84 blink::WebIDBDataLossNone; |
71 std::string data_loss_message; | 85 std::string data_loss_message; |
72 bool disk_full = false; | 86 bool disk_full = false; |
| 87 base::TaskRunner* task_runner = NULL; |
| 88 bool clean_journal = false; |
73 scoped_refptr<IndexedDBBackingStore> backing_store = | 89 scoped_refptr<IndexedDBBackingStore> backing_store = |
74 IndexedDBBackingStore::Open(origin, | 90 IndexedDBBackingStore::Open(factory, |
| 91 origin, |
75 path, | 92 path, |
| 93 request_context, |
76 &data_loss, | 94 &data_loss, |
77 &data_loss_message, | 95 &data_loss_message, |
78 &disk_full, | 96 &disk_full, |
79 &mock_leveldb_factory); | 97 &mock_leveldb_factory, |
| 98 task_runner, |
| 99 clean_journal); |
80 } | 100 } |
81 | 101 |
82 // TODO(dgrogan): Remove expect_destroy if we end up not using it again. It is | 102 // TODO(dgrogan): Remove expect_destroy if we end up not using it again. It is |
83 // currently set to false in all 4 calls below. | 103 // currently set to false in all 4 calls below. |
84 template <class T> | 104 template <class T> |
85 class MockErrorLevelDBFactory : public LevelDBFactory { | 105 class MockErrorLevelDBFactory : public LevelDBFactory { |
86 public: | 106 public: |
87 MockErrorLevelDBFactory(T error, bool expect_destroy) | 107 MockErrorLevelDBFactory(T error, bool expect_destroy) |
88 : error_(error), | 108 : error_(error), |
89 expect_destroy_(expect_destroy), | 109 expect_destroy_(expect_destroy), |
(...skipping 15 matching lines...) Expand all Loading... |
105 EXPECT_EQ(expect_destroy_, destroy_called_); | 125 EXPECT_EQ(expect_destroy_, destroy_called_); |
106 } | 126 } |
107 | 127 |
108 private: | 128 private: |
109 T error_; | 129 T error_; |
110 bool expect_destroy_; | 130 bool expect_destroy_; |
111 bool destroy_called_; | 131 bool destroy_called_; |
112 }; | 132 }; |
113 | 133 |
114 TEST(IndexedDBNonRecoverableIOErrorTest, NuancedCleanupTest) { | 134 TEST(IndexedDBNonRecoverableIOErrorTest, NuancedCleanupTest) { |
| 135 content::IndexedDBFactory* factory = NULL; |
115 const GURL origin("http://localhost:81"); | 136 const GURL origin("http://localhost:81"); |
| 137 net::URLRequestContext* request_context = NULL; |
116 base::ScopedTempDir temp_directory; | 138 base::ScopedTempDir temp_directory; |
117 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 139 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
118 const base::FilePath path = temp_directory.path(); | 140 const base::FilePath path = temp_directory.path(); |
119 blink::WebIDBDataLoss data_loss = | 141 blink::WebIDBDataLoss data_loss = |
120 blink::WebIDBDataLossNone; | 142 blink::WebIDBDataLossNone; |
121 std::string data_loss_reason; | 143 std::string data_loss_reason; |
122 bool disk_full = false; | 144 bool disk_full = false; |
| 145 base::TaskRunner* task_runner = NULL; |
| 146 bool clean_journal = false; |
123 | 147 |
124 MockErrorLevelDBFactory<int> mock_leveldb_factory(ENOSPC, false); | 148 MockErrorLevelDBFactory<int> mock_leveldb_factory(ENOSPC, false); |
125 scoped_refptr<IndexedDBBackingStore> backing_store = | 149 scoped_refptr<IndexedDBBackingStore> backing_store = |
126 IndexedDBBackingStore::Open(origin, | 150 IndexedDBBackingStore::Open(factory, |
| 151 origin, |
127 path, | 152 path, |
| 153 request_context, |
128 &data_loss, | 154 &data_loss, |
129 &data_loss_reason, | 155 &data_loss_reason, |
130 &disk_full, | 156 &disk_full, |
131 &mock_leveldb_factory); | 157 &mock_leveldb_factory, |
| 158 task_runner, |
| 159 clean_journal); |
132 | 160 |
133 MockErrorLevelDBFactory<base::PlatformFileError> mock_leveldb_factory2( | 161 MockErrorLevelDBFactory<base::PlatformFileError> mock_leveldb_factory2( |
134 base::PLATFORM_FILE_ERROR_NO_MEMORY, false); | 162 base::PLATFORM_FILE_ERROR_NO_MEMORY, false); |
135 scoped_refptr<IndexedDBBackingStore> backing_store2 = | 163 scoped_refptr<IndexedDBBackingStore> backing_store2 = |
136 IndexedDBBackingStore::Open(origin, | 164 IndexedDBBackingStore::Open(factory, |
| 165 origin, |
137 path, | 166 path, |
| 167 request_context, |
138 &data_loss, | 168 &data_loss, |
139 &data_loss_reason, | 169 &data_loss_reason, |
140 &disk_full, | 170 &disk_full, |
141 &mock_leveldb_factory2); | 171 &mock_leveldb_factory2, |
| 172 task_runner, |
| 173 clean_journal); |
142 | 174 |
143 MockErrorLevelDBFactory<int> mock_leveldb_factory3(EIO, false); | 175 MockErrorLevelDBFactory<int> mock_leveldb_factory3(EIO, false); |
144 scoped_refptr<IndexedDBBackingStore> backing_store3 = | 176 scoped_refptr<IndexedDBBackingStore> backing_store3 = |
145 IndexedDBBackingStore::Open(origin, | 177 IndexedDBBackingStore::Open(factory, |
| 178 origin, |
146 path, | 179 path, |
| 180 request_context, |
147 &data_loss, | 181 &data_loss, |
148 &data_loss_reason, | 182 &data_loss_reason, |
149 &disk_full, | 183 &disk_full, |
150 &mock_leveldb_factory3); | 184 &mock_leveldb_factory3, |
| 185 task_runner, |
| 186 clean_journal); |
151 | 187 |
152 MockErrorLevelDBFactory<base::PlatformFileError> mock_leveldb_factory4( | 188 MockErrorLevelDBFactory<base::PlatformFileError> mock_leveldb_factory4( |
153 base::PLATFORM_FILE_ERROR_FAILED, false); | 189 base::PLATFORM_FILE_ERROR_FAILED, false); |
154 scoped_refptr<IndexedDBBackingStore> backing_store4 = | 190 scoped_refptr<IndexedDBBackingStore> backing_store4 = |
155 IndexedDBBackingStore::Open(origin, | 191 IndexedDBBackingStore::Open(factory, |
| 192 origin, |
156 path, | 193 path, |
| 194 request_context, |
157 &data_loss, | 195 &data_loss, |
158 &data_loss_reason, | 196 &data_loss_reason, |
159 &disk_full, | 197 &disk_full, |
160 &mock_leveldb_factory4); | 198 &mock_leveldb_factory4, |
| 199 task_runner, |
| 200 clean_journal); |
161 } | 201 } |
162 | 202 |
163 } // namespace | 203 } // namespace |
OLD | NEW |