OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/chromeos/drive/drive_resource_metadata_storage.h" | 5 #include "chrome/browser/chromeos/drive/drive_resource_metadata_storage.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 } | 37 } |
38 | 38 |
39 // Overwrites |storage_|'s version. | 39 // Overwrites |storage_|'s version. |
40 void SetDBVersion(int version) { | 40 void SetDBVersion(int version) { |
41 scoped_ptr<DriveResourceMetadataHeader> header = storage_->GetHeader(); | 41 scoped_ptr<DriveResourceMetadataHeader> header = storage_->GetHeader(); |
42 ASSERT_TRUE(header); | 42 ASSERT_TRUE(header); |
43 header->set_version(version); | 43 header->set_version(version); |
44 storage_->PutHeader(*header); | 44 storage_->PutHeader(*header); |
45 } | 45 } |
46 | 46 |
| 47 bool CheckValidity() { |
| 48 return storage_->CheckValidity(); |
| 49 } |
| 50 |
47 base::ScopedTempDir temp_dir_; | 51 base::ScopedTempDir temp_dir_; |
48 scoped_ptr<DriveResourceMetadataStorageDB> storage_; | 52 scoped_ptr<DriveResourceMetadataStorageDB> storage_; |
49 }; | 53 }; |
50 | 54 |
51 TEST_F(DriveResourceMetadataStorageTest, LargestChangestamp) { | 55 TEST_F(DriveResourceMetadataStorageTest, LargestChangestamp) { |
52 const int64 kLargestChangestamp = 1234567890; | 56 const int64 kLargestChangestamp = 1234567890; |
53 storage_->SetLargestChangestamp(kLargestChangestamp); | 57 storage_->SetLargestChangestamp(kLargestChangestamp); |
54 EXPECT_EQ(kLargestChangestamp, storage_->GetLargestChangestamp()); | 58 EXPECT_EQ(kLargestChangestamp, storage_->GetLargestChangestamp()); |
55 } | 59 } |
56 | 60 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 } | 196 } |
193 } | 197 } |
194 | 198 |
195 TEST_F(DriveResourceMetadataStorageTest, OpenExistingDB) { | 199 TEST_F(DriveResourceMetadataStorageTest, OpenExistingDB) { |
196 const std::string parent_id1 = "abcdefg"; | 200 const std::string parent_id1 = "abcdefg"; |
197 const std::string child_name1 = "WXYZABC"; | 201 const std::string child_name1 = "WXYZABC"; |
198 const std::string child_id1 = "qwerty"; | 202 const std::string child_id1 = "qwerty"; |
199 | 203 |
200 DriveEntryProto entry1; | 204 DriveEntryProto entry1; |
201 entry1.set_resource_id(parent_id1); | 205 entry1.set_resource_id(parent_id1); |
| 206 DriveEntryProto entry2; |
| 207 entry2.set_resource_id(child_id1); |
| 208 entry2.set_parent_resource_id(parent_id1); |
| 209 entry2.set_base_name(child_name1); |
202 | 210 |
203 // Put some data. | 211 // Put some data. |
204 storage_->PutEntry(entry1); | 212 storage_->PutEntry(entry1); |
| 213 storage_->PutEntry(entry2); |
205 storage_->PutChild(parent_id1, child_name1, child_id1); | 214 storage_->PutChild(parent_id1, child_name1, child_id1); |
206 scoped_ptr<DriveEntryProto> result = storage_->GetEntry(parent_id1); | |
207 ASSERT_TRUE(result); | |
208 EXPECT_EQ(parent_id1, result->resource_id()); | |
209 EXPECT_EQ(child_id1, storage_->GetChild(parent_id1, child_name1)); | |
210 | 215 |
211 // Close DB and reopen. | 216 // Close DB and reopen. |
212 storage_.reset(new DriveResourceMetadataStorageDB(temp_dir_.path())); | 217 storage_.reset(new DriveResourceMetadataStorageDB(temp_dir_.path())); |
213 ASSERT_TRUE(storage_->Initialize()); | 218 ASSERT_TRUE(storage_->Initialize()); |
214 | 219 |
215 // Can read data. | 220 // Can read data. |
| 221 scoped_ptr<DriveEntryProto> result; |
216 result = storage_->GetEntry(parent_id1); | 222 result = storage_->GetEntry(parent_id1); |
217 ASSERT_TRUE(result); | 223 ASSERT_TRUE(result); |
218 EXPECT_EQ(parent_id1, result->resource_id()); | 224 EXPECT_EQ(parent_id1, result->resource_id()); |
| 225 |
| 226 result = storage_->GetEntry(child_id1); |
| 227 ASSERT_TRUE(result); |
| 228 EXPECT_EQ(child_id1, result->resource_id()); |
| 229 EXPECT_EQ(parent_id1, result->parent_resource_id()); |
| 230 EXPECT_EQ(child_name1, result->base_name()); |
| 231 |
219 EXPECT_EQ(child_id1, storage_->GetChild(parent_id1, child_name1)); | 232 EXPECT_EQ(child_id1, storage_->GetChild(parent_id1, child_name1)); |
220 } | 233 } |
221 | 234 |
222 TEST_F(DriveResourceMetadataStorageTest, IncompatibleDB) { | 235 TEST_F(DriveResourceMetadataStorageTest, IncompatibleDB) { |
223 const int64 kLargestChangestamp = 1234567890; | 236 const int64 kLargestChangestamp = 1234567890; |
224 const std::string key1 = "abcd"; | 237 const std::string key1 = "abcd"; |
225 | 238 |
226 DriveEntryProto entry; | 239 DriveEntryProto entry; |
227 entry.set_resource_id(key1); | 240 entry.set_resource_id(key1); |
228 | 241 |
(...skipping 15 matching lines...) Expand all Loading... |
244 TEST_F(DriveResourceMetadataStorageTest, WrongPath) { | 257 TEST_F(DriveResourceMetadataStorageTest, WrongPath) { |
245 // Create a file. | 258 // Create a file. |
246 base::FilePath path; | 259 base::FilePath path; |
247 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &path)); | 260 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &path)); |
248 | 261 |
249 storage_.reset(new DriveResourceMetadataStorageDB(path)); | 262 storage_.reset(new DriveResourceMetadataStorageDB(path)); |
250 // Cannot initialize DB beacause the path does not point a directory. | 263 // Cannot initialize DB beacause the path does not point a directory. |
251 ASSERT_FALSE(storage_->Initialize()); | 264 ASSERT_FALSE(storage_->Initialize()); |
252 } | 265 } |
253 | 266 |
| 267 TEST_F(DriveResourceMetadataStorageTest, CheckValidity) { |
| 268 const std::string key1 = "foo"; |
| 269 const std::string name1 = "hoge"; |
| 270 const std::string key2 = "bar"; |
| 271 const std::string name2 = "fuga"; |
| 272 const std::string key3 = "boo"; |
| 273 const std::string name3 = "piyo"; |
| 274 |
| 275 // Put entry with key1. |
| 276 DriveEntryProto entry; |
| 277 entry.set_resource_id(key1); |
| 278 entry.set_base_name(name1); |
| 279 storage_->PutEntry(entry); |
| 280 EXPECT_TRUE(CheckValidity()); |
| 281 |
| 282 // Put entry with key2 under key1. |
| 283 entry.set_resource_id(key2); |
| 284 entry.set_parent_resource_id(key1); |
| 285 entry.set_base_name(name2); |
| 286 storage_->PutEntry(entry); |
| 287 EXPECT_FALSE(CheckValidity()); // Missing parent-child relationship. |
| 288 |
| 289 // Add missing parent-child relationship between key1 and key2. |
| 290 storage_->PutChild(key1, name2, key2); |
| 291 EXPECT_TRUE(CheckValidity()); |
| 292 |
| 293 // Add parent-child relationship between key1 and key3. |
| 294 storage_->PutChild(key1, name3, key3); |
| 295 EXPECT_FALSE(CheckValidity()); // key3 is not stored in the storage. |
| 296 |
| 297 // Put entry with key3 under key1. |
| 298 entry.set_resource_id(key3); |
| 299 entry.set_parent_resource_id(key1); |
| 300 entry.set_base_name(name3); |
| 301 storage_->PutEntry(entry); |
| 302 EXPECT_TRUE(CheckValidity()); |
| 303 |
| 304 // Parent-child relationship with wrong name. |
| 305 storage_->PutChild(key1, name2, key3); |
| 306 EXPECT_FALSE(CheckValidity()); |
| 307 } |
| 308 |
254 } // namespace drive | 309 } // namespace drive |
OLD | NEW |