OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/gdata/gdata_db.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_db.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 8 #include "chrome/browser/chromeos/gdata/gdata.pb.h" |
8 #include "chrome/browser/chromeos/gdata/gdata_db_factory.h" | 9 #include "chrome/browser/chromeos/gdata/gdata_db_factory.h" |
9 #include "chrome/browser/chromeos/gdata/gdata_files.h" | 10 #include "chrome/browser/chromeos/gdata/gdata_files.h" |
| 11 #include "chrome/test/base/testing_profile.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "chrome/test/base/testing_profile.h" | |
12 | 13 |
13 namespace gdata { | 14 namespace gdata { |
14 namespace { | 15 namespace { |
15 | 16 |
16 class GDataDBTest : public testing::Test { | 17 class GDataDBTest : public testing::Test { |
17 public: | 18 public: |
18 GDataDBTest() { | 19 GDataDBTest() { |
19 } | 20 } |
20 | 21 |
21 virtual ~GDataDBTest() { | 22 virtual ~GDataDBTest() { |
22 } | 23 } |
23 | 24 |
24 protected: | 25 protected: |
25 // testing::Test implementation. | 26 // testing::Test implementation. |
26 virtual void SetUp() OVERRIDE; | 27 virtual void SetUp() OVERRIDE; |
27 | 28 |
28 // Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry | 29 // Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry |
29 // matching |source| does not exist. | 30 // matching |source| does not exist. |
30 void TestGetNotFound(const GDataEntry& source); | 31 void TestGetNotFound(const GDataEntry& source); |
31 | 32 |
32 // Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry | 33 // Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry |
33 // matching |source| exists. | 34 // matching |source| exists. |
34 void TestGetFound(const GDataEntry& source); | 35 void TestGetFound(const GDataEntry& source); |
35 | 36 |
| 37 // Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry |
| 38 // matching |source| is corrupt. |
| 39 void TestGetCorrupt(const GDataEntry& source); |
| 40 |
36 // Initialize the database with the following entries: | 41 // Initialize the database with the following entries: |
37 // drive/dir1 | 42 // drive/dir1 |
38 // drive/dir2 | 43 // drive/dir2 |
39 // drive/dir1/dir3 | 44 // drive/dir1/dir3 |
40 // drive/dir1/file4 | 45 // drive/dir1/file4 |
41 // drive/dir1/file5 | 46 // drive/dir1/file5 |
42 // drive/dir2/file6 | 47 // drive/dir2/file6 |
43 // drive/dir2/file7 | 48 // drive/dir2/file7 |
44 // drive/dir2/file8 | 49 // drive/dir2/file8 |
45 // drive/dir1/dir3/file9 | 50 // drive/dir1/dir3/file9 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 entry.reset(); | 96 entry.reset(); |
92 | 97 |
93 status = gdata_db_->GetByResourceId(source.resource_id(), &entry); | 98 status = gdata_db_->GetByResourceId(source.resource_id(), &entry); |
94 EXPECT_EQ(GDataDB::DB_OK, status); | 99 EXPECT_EQ(GDataDB::DB_OK, status); |
95 ASSERT_TRUE(entry.get()); | 100 ASSERT_TRUE(entry.get()); |
96 EXPECT_EQ(source.title(), entry->title()); | 101 EXPECT_EQ(source.title(), entry->title()); |
97 EXPECT_EQ(source.resource_id(), entry->resource_id()); | 102 EXPECT_EQ(source.resource_id(), entry->resource_id()); |
98 EXPECT_EQ(source.content_url(), entry->content_url()); | 103 EXPECT_EQ(source.content_url(), entry->content_url()); |
99 } | 104 } |
100 | 105 |
| 106 void GDataDBTest::TestGetCorrupt(const GDataEntry& source) { |
| 107 scoped_ptr<GDataEntry> entry; |
| 108 GDataDB::Status status = gdata_db_->GetByPath(source.GetFilePath(), &entry); |
| 109 EXPECT_EQ(GDataDB::DB_CORRUPTION, status); |
| 110 EXPECT_FALSE(entry.get()); |
| 111 |
| 112 status = gdata_db_->GetByResourceId(source.resource_id(), &entry); |
| 113 EXPECT_EQ(GDataDB::DB_CORRUPTION, status); |
| 114 EXPECT_FALSE(entry.get()); |
| 115 } |
| 116 |
101 void GDataDBTest::InitDB() { | 117 void GDataDBTest::InitDB() { |
102 int sequence_id = 1; | 118 int sequence_id = 1; |
103 GDataRootDirectory root; | 119 GDataRootDirectory root; |
104 GDataDirectory* dir1 = AddDirectory(&root, &root, sequence_id++); | 120 GDataDirectory* dir1 = AddDirectory(&root, &root, sequence_id++); |
105 GDataDirectory* dir2 = AddDirectory(&root, &root, sequence_id++); | 121 GDataDirectory* dir2 = AddDirectory(&root, &root, sequence_id++); |
106 GDataDirectory* dir3 = AddDirectory(dir1, &root, sequence_id++); | 122 GDataDirectory* dir3 = AddDirectory(dir1, &root, sequence_id++); |
107 | 123 |
108 AddFile(dir1, &root, sequence_id++); | 124 AddFile(dir1, &root, sequence_id++); |
109 AddFile(dir1, &root, sequence_id++); | 125 AddFile(dir1, &root, sequence_id++); |
110 | 126 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 GDataDirectory dir(&root, &root); | 195 GDataDirectory dir(&root, &root); |
180 dir.set_title("dir"); | 196 dir.set_title("dir"); |
181 dir.set_file_name("dir"); | 197 dir.set_file_name("dir"); |
182 dir.set_resource_id("dir_resource_id"); | 198 dir.set_resource_id("dir_resource_id"); |
183 dir.set_content_url(GURL("http://content/dir")); | 199 dir.set_content_url(GURL("http://content/dir")); |
184 dir.set_upload_url(GURL("http://upload/dir")); | 200 dir.set_upload_url(GURL("http://upload/dir")); |
185 | 201 |
186 TestGetNotFound(dir); | 202 TestGetNotFound(dir); |
187 | 203 |
188 GDataDB::Status status = gdata_db_->Put(dir); | 204 GDataDB::Status status = gdata_db_->Put(dir); |
189 EXPECT_EQ(GDataDB::DB_OK, status); | 205 ASSERT_EQ(GDataDB::DB_OK, status); |
190 | 206 |
191 TestGetFound(dir); | 207 TestGetFound(dir); |
192 | 208 |
193 scoped_ptr<GDataEntry> entry; | 209 scoped_ptr<GDataEntry> entry; |
194 gdata_db_->GetByPath(dir.GetFilePath(), &entry); | 210 status = gdata_db_->GetByPath(dir.GetFilePath(), &entry); |
| 211 ASSERT_EQ(GDataDB::DB_OK, status); |
195 EXPECT_EQ(dir.upload_url(), entry->AsGDataDirectory()->upload_url()); | 212 EXPECT_EQ(dir.upload_url(), entry->AsGDataDirectory()->upload_url()); |
196 EXPECT_TRUE(entry->AsGDataDirectory()->file_info().is_directory); | 213 EXPECT_TRUE(entry->AsGDataDirectory()->file_info().is_directory); |
197 | 214 |
198 status = gdata_db_->DeleteByPath(dir.GetFilePath()); | 215 status = gdata_db_->DeleteByPath(dir.GetFilePath()); |
199 EXPECT_EQ(GDataDB::DB_OK, status); | 216 ASSERT_EQ(GDataDB::DB_OK, status); |
200 | 217 |
201 TestGetNotFound(dir); | 218 TestGetNotFound(dir); |
202 | 219 |
203 GDataFile file(&dir, &root); | 220 GDataFile file(&dir, &root); |
204 file.set_title("file"); | 221 file.set_title("file"); |
205 file.set_file_name("file"); | 222 file.set_file_name("file"); |
206 file.set_resource_id("file_resource_id"); | 223 file.set_resource_id("file_resource_id"); |
207 file.set_content_url(GURL("http://content/dir/file")); | 224 file.set_content_url(GURL("http://content/dir/file")); |
208 file.set_file_md5("file_md5"); | 225 file.set_file_md5("file_md5"); |
209 | 226 |
210 TestGetNotFound(file); | 227 TestGetNotFound(file); |
211 | 228 |
212 status = gdata_db_->Put(file); | 229 status = gdata_db_->Put(file); |
213 EXPECT_EQ(GDataDB::DB_OK, status); | 230 ASSERT_EQ(GDataDB::DB_OK, status); |
214 | 231 |
215 TestGetFound(file); | 232 TestGetFound(file); |
216 | 233 |
217 gdata_db_->GetByPath(file.GetFilePath(), &entry); | 234 status = gdata_db_->GetByPath(file.GetFilePath(), &entry); |
| 235 ASSERT_EQ(GDataDB::DB_OK, status); |
218 EXPECT_EQ(file.file_md5(), entry->AsGDataFile()->file_md5()); | 236 EXPECT_EQ(file.file_md5(), entry->AsGDataFile()->file_md5()); |
219 EXPECT_FALSE(entry->AsGDataFile()->file_info().is_directory); | 237 EXPECT_FALSE(entry->AsGDataFile()->file_info().is_directory); |
220 | 238 |
221 status = gdata_db_->DeleteByPath(file.GetFilePath()); | 239 status = gdata_db_->DeleteByPath(file.GetFilePath()); |
222 EXPECT_EQ(GDataDB::DB_OK, status); | 240 ASSERT_EQ(GDataDB::DB_OK, status); |
223 | 241 |
224 TestGetNotFound(file); | 242 TestGetNotFound(file); |
225 } | 243 } |
226 | 244 |
227 TEST_F(GDataDBTest, IterTest) { | 245 TEST_F(GDataDBTest, IterTest) { |
228 InitDB(); | 246 InitDB(); |
229 | 247 |
230 const char* dir1_children[] = { | 248 const char* dir1_children[] = { |
231 "drive/dir1", | 249 "drive/dir1", |
232 "drive/dir1/dir3", | 250 "drive/dir1/dir3", |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 "drive/dir2", | 285 "drive/dir2", |
268 "drive/dir2/file6", | 286 "drive/dir2/file6", |
269 "drive/dir2/file7", | 287 "drive/dir2/file7", |
270 "drive/dir2/file8", | 288 "drive/dir2/file8", |
271 }; | 289 }; |
272 TestIter("", all_entries, arraysize(all_entries)); | 290 TestIter("", all_entries, arraysize(all_entries)); |
273 | 291 |
274 TestIter("dir4", NULL, 0); | 292 TestIter("dir4", NULL, 0); |
275 } | 293 } |
276 | 294 |
| 295 TEST_F(GDataDBTest, IncompatibleProtoTest) { |
| 296 GDataRootDirectory root; |
| 297 GDataFile file(&root, &root); |
| 298 file.set_title("file"); |
| 299 file.set_file_name("file"); |
| 300 file.set_resource_id("file_resource_id"); |
| 301 file.set_content_url(GURL("http://content/dir/file")); |
| 302 file.set_file_md5("file_md5"); |
| 303 |
| 304 // Add a file and check if it's found. |
| 305 GDataDB::Status status = gdata_db_->Put(file); |
| 306 ASSERT_EQ(GDataDB::DB_OK, status); |
| 307 TestGetFound(file); |
| 308 // Check if the iterator works too. |
| 309 const char* all_entries[] = { |
| 310 "drive/file", |
| 311 }; |
| 312 TestIter("", all_entries, arraysize(all_entries)); |
| 313 |
| 314 // Tweak the file proto to simulate an incompatible proto in the DB. |
| 315 GDataFileProto file_proto; |
| 316 file.ToProto(&file_proto); |
| 317 file_proto.clear_upload_url(); // This will make FromProto() fail. |
| 318 std::string serialized_proto; |
| 319 file_proto.SerializeToString(&serialized_proto); |
| 320 gdata_db_->PutRawForTesting("file_resource_id", serialized_proto); |
| 321 |
| 322 // Check if the corruption is detected. |
| 323 TestGetCorrupt(file); |
| 324 // We should no longer be able to find the file by iteration. |
| 325 TestIter("", NULL, 0); |
| 326 } |
| 327 |
277 } // namespace gdata | 328 } // namespace gdata |
OLD | NEW |