Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(659)

Side by Side Diff: chrome/browser/sync/syncable/syncable_unittest.cc

Issue 429003: Final part of PathString cleanup. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/sync/syncable/syncable.h" 5 #include "chrome/browser/sync/syncable/syncable.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #include <sys/types.h> 9 #include <sys/types.h>
10 10
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 53
54 namespace syncable { 54 namespace syncable {
55 55
56 namespace { 56 namespace {
57 // A lot of these tests were written expecting to be able to read and write 57 // A lot of these tests were written expecting to be able to read and write
58 // object data on entries. However, the design has changed. 58 // object data on entries. However, the design has changed.
59 void PutDataAsExtendedAttribute(WriteTransaction* wtrans, 59 void PutDataAsExtendedAttribute(WriteTransaction* wtrans,
60 MutableEntry* e, 60 MutableEntry* e,
61 const char* bytes, 61 const char* bytes,
62 size_t bytes_length) { 62 size_t bytes_length) {
63 ExtendedAttributeKey key(e->Get(META_HANDLE), PSTR("DATA")); 63 ExtendedAttributeKey key(e->Get(META_HANDLE), "DATA");
64 MutableExtendedAttribute attr(wtrans, CREATE, key); 64 MutableExtendedAttribute attr(wtrans, CREATE, key);
65 Blob bytes_blob(bytes, bytes + bytes_length); 65 Blob bytes_blob(bytes, bytes + bytes_length);
66 attr.mutable_value()->swap(bytes_blob); 66 attr.mutable_value()->swap(bytes_blob);
67 } 67 }
68 68
69 void ExpectDataFromExtendedAttributeEquals(BaseTransaction* trans, 69 void ExpectDataFromExtendedAttributeEquals(BaseTransaction* trans,
70 Entry* e, 70 Entry* e,
71 const char* bytes, 71 const char* bytes,
72 size_t bytes_length) { 72 size_t bytes_length) {
73 ASSERT_TRUE(e->good()); 73 ASSERT_TRUE(e->good());
74 Blob expected_value(bytes, bytes + bytes_length); 74 Blob expected_value(bytes, bytes + bytes_length);
75 ExtendedAttributeKey key(e->Get(META_HANDLE), PSTR("DATA")); 75 ExtendedAttributeKey key(e->Get(META_HANDLE), "DATA");
76 ExtendedAttribute attr(trans, GET_BY_HANDLE, key); 76 ExtendedAttribute attr(trans, GET_BY_HANDLE, key);
77 EXPECT_FALSE(attr.is_deleted()); 77 EXPECT_FALSE(attr.is_deleted());
78 EXPECT_EQ(expected_value, attr.value()); 78 EXPECT_EQ(expected_value, attr.value());
79 } 79 }
80 } // namespace 80 } // namespace
81 81
82 TEST(Syncable, General) { 82 TEST(Syncable, General) {
83 remove("SimpleTest.sqlite3"); 83 remove("SimpleTest.sqlite3");
84 Directory dir; 84 Directory dir;
85 FilePath test_db(FILE_PATH_LITERAL("SimpleTest.sqlite3")); 85 FilePath test_db(FILE_PATH_LITERAL("SimpleTest.sqlite3"));
86 dir.Open(test_db, PSTR("SimpleTest")); 86 dir.Open(test_db, "SimpleTest");
87 87
88 int64 written_metahandle; 88 int64 written_metahandle;
89 const Id id = TestIdFactory::FromNumber(99); 89 const Id id = TestIdFactory::FromNumber(99);
90 PathString name = PSTR("Jeff"); 90 string name = "Jeff";
91 // Test simple read operations on an empty DB. 91 // Test simple read operations on an empty DB.
92 { 92 {
93 ReadTransaction rtrans(&dir, __FILE__, __LINE__); 93 ReadTransaction rtrans(&dir, __FILE__, __LINE__);
94 Entry e(&rtrans, GET_BY_ID, id); 94 Entry e(&rtrans, GET_BY_ID, id);
95 ASSERT_FALSE(e.good()); // Hasn't been written yet. 95 ASSERT_FALSE(e.good()); // Hasn't been written yet.
96 96
97 Directory::ChildHandles child_handles; 97 Directory::ChildHandles child_handles;
98 dir.GetChildHandles(&rtrans, rtrans.root_id(), &child_handles); 98 dir.GetChildHandles(&rtrans, rtrans.root_id(), &child_handles);
99 EXPECT_TRUE(child_handles.empty()); 99 EXPECT_TRUE(child_handles.empty());
100 } 100 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 remove("SimpleTest.sqlite3"); 162 remove("SimpleTest.sqlite3");
163 } 163 }
164 164
165 namespace { 165 namespace {
166 166
167 // A Directory whose backing store always fails SaveChanges by returning false. 167 // A Directory whose backing store always fails SaveChanges by returning false.
168 class TestUnsaveableDirectory : public Directory { 168 class TestUnsaveableDirectory : public Directory {
169 public: 169 public:
170 class UnsaveableBackingStore : public DirectoryBackingStore { 170 class UnsaveableBackingStore : public DirectoryBackingStore {
171 public: 171 public:
172 UnsaveableBackingStore(const PathString& dir_name, 172 UnsaveableBackingStore(const string& dir_name,
173 const FilePath& backing_filepath) 173 const FilePath& backing_filepath)
174 : DirectoryBackingStore(dir_name, backing_filepath) { } 174 : DirectoryBackingStore(dir_name, backing_filepath) { }
175 virtual bool SaveChanges(const Directory::SaveChangesSnapshot& snapshot) { 175 virtual bool SaveChanges(const Directory::SaveChangesSnapshot& snapshot) {
176 return false; 176 return false;
177 } 177 }
178 }; 178 };
179 virtual DirectoryBackingStore* CreateBackingStore( 179 virtual DirectoryBackingStore* CreateBackingStore(
180 const PathString& dir_name, const FilePath& backing_filepath) { 180 const string& dir_name, const FilePath& backing_filepath) {
181 return new UnsaveableBackingStore(dir_name, backing_filepath); 181 return new UnsaveableBackingStore(dir_name, backing_filepath);
182 } 182 }
183 }; 183 };
184 184
185 // Test suite for syncable::Directory. 185 // Test suite for syncable::Directory.
186 class SyncableDirectoryTest : public testing::Test { 186 class SyncableDirectoryTest : public testing::Test {
187 protected: 187 protected:
188 static const FilePath::CharType kFilePath[]; 188 static const FilePath::CharType kFilePath[];
189 static const PathString kName; 189 static const string kName;
190 static const Id kId; 190 static const Id kId;
191 191
192 // SetUp() is called before each test case is run. 192 // SetUp() is called before each test case is run.
193 // The sqlite3 DB is deleted before each test is run. 193 // The sqlite3 DB is deleted before each test is run.
194 virtual void SetUp() { 194 virtual void SetUp() {
195 file_path_ = FilePath(kFilePath); 195 file_path_ = FilePath(kFilePath);
196 file_util::Delete(file_path_, true); 196 file_util::Delete(file_path_, true);
197 dir_.reset(new Directory()); 197 dir_.reset(new Directory());
198 ASSERT_TRUE(dir_.get()); 198 ASSERT_TRUE(dir_.get());
199 ASSERT_TRUE(OPENED == dir_->Open(file_path_, kName)); 199 ASSERT_TRUE(OPENED == dir_->Open(file_path_, kName));
200 ASSERT_TRUE(dir_->good()); 200 ASSERT_TRUE(dir_->good());
201 } 201 }
202 202
203 virtual void TearDown() { 203 virtual void TearDown() {
204 // This also closes file handles. 204 // This also closes file handles.
205 dir_->SaveChanges(); 205 dir_->SaveChanges();
206 dir_.reset(); 206 dir_.reset();
207 file_util::Delete(file_path_, true); 207 file_util::Delete(file_path_, true);
208 } 208 }
209 209
210 scoped_ptr<Directory> dir_; 210 scoped_ptr<Directory> dir_;
211 FilePath file_path_; 211 FilePath file_path_;
212 212
213 // Creates an empty entry and sets the ID field to the default kId. 213 // Creates an empty entry and sets the ID field to the default kId.
214 void CreateEntry(const PathString &entryname) { 214 void CreateEntry(const string& entryname) {
215 CreateEntry(entryname, kId); 215 CreateEntry(entryname, kId);
216 } 216 }
217 217
218 // Creates an empty entry and sets the ID field to id. 218 // Creates an empty entry and sets the ID field to id.
219 void CreateEntry(const PathString &entryname, const int id) { 219 void CreateEntry(const string& entryname, const int id) {
220 CreateEntry(entryname, TestIdFactory::FromNumber(id)); 220 CreateEntry(entryname, TestIdFactory::FromNumber(id));
221 } 221 }
222 void CreateEntry(const PathString &entryname, Id id) { 222 void CreateEntry(const string& entryname, Id id) {
223 WriteTransaction wtrans(dir_.get(), UNITTEST, __FILE__, __LINE__); 223 WriteTransaction wtrans(dir_.get(), UNITTEST, __FILE__, __LINE__);
224 MutableEntry me(&wtrans, CREATE, wtrans.root_id(), entryname); 224 MutableEntry me(&wtrans, CREATE, wtrans.root_id(), entryname);
225 ASSERT_TRUE(me.good()); 225 ASSERT_TRUE(me.good());
226 me.Put(ID, id); 226 me.Put(ID, id);
227 me.Put(IS_UNSYNCED, true); 227 me.Put(IS_UNSYNCED, true);
228 } 228 }
229 229
230 void ValidateEntry(BaseTransaction* trans, int64 id, bool check_name, 230 void ValidateEntry(BaseTransaction* trans, int64 id, bool check_name,
231 PathString name, int64 base_version, int64 server_version, bool is_del); 231 string name, int64 base_version, int64 server_version, bool is_del);
232 void CreateAndCheck(WriteTransaction* trans, int64 parent_id, int64 id, 232 void CreateAndCheck(WriteTransaction* trans, int64 parent_id, int64 id,
233 PathString name, PathString server_name, int64 version, 233 string name, string server_name, int64 version,
234 bool set_server_fields, bool is_dir, bool add_to_lru, int64 *meta_handle); 234 bool set_server_fields, bool is_dir, bool add_to_lru, int64 *meta_handle);
235 }; 235 };
236 236
237 const FilePath::CharType SyncableDirectoryTest::kFilePath[] = 237 const FilePath::CharType SyncableDirectoryTest::kFilePath[] =
238 FILE_PATH_LITERAL("Test.sqlite3"); 238 FILE_PATH_LITERAL("Test.sqlite3");
239 const PathString SyncableDirectoryTest::kName(PSTR("Foo")); 239 const string SyncableDirectoryTest::kName("Foo");
240 const Id SyncableDirectoryTest::kId(TestIdFactory::FromNumber(-99)); 240 const Id SyncableDirectoryTest::kId(TestIdFactory::FromNumber(-99));
241 241
242 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) { 242 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) {
243 ReadTransaction rtrans(dir_.get(), __FILE__, __LINE__); 243 ReadTransaction rtrans(dir_.get(), __FILE__, __LINE__);
244 Entry e(&rtrans, GET_BY_ID, kId); 244 Entry e(&rtrans, GET_BY_ID, kId);
245 ASSERT_FALSE(e.good()); 245 ASSERT_FALSE(e.good());
246 } 246 }
247 247
248 TEST_F(SyncableDirectoryTest, TestBasicLookupValidID) { 248 TEST_F(SyncableDirectoryTest, TestBasicLookupValidID) {
249 CreateEntry(PSTR("rtc")); 249 CreateEntry("rtc");
250 ReadTransaction rtrans(dir_.get(), __FILE__, __LINE__); 250 ReadTransaction rtrans(dir_.get(), __FILE__, __LINE__);
251 Entry e(&rtrans, GET_BY_ID, kId); 251 Entry e(&rtrans, GET_BY_ID, kId);
252 ASSERT_TRUE(e.good()); 252 ASSERT_TRUE(e.good());
253 } 253 }
254 254
255 TEST_F(SyncableDirectoryTest, TestDelete) { 255 TEST_F(SyncableDirectoryTest, TestDelete) {
256 PathString name = PSTR("peanut butter jelly time"); 256 string name = "peanut butter jelly time";
257 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 257 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
258 MutableEntry e1(&trans, CREATE, trans.root_id(), name); 258 MutableEntry e1(&trans, CREATE, trans.root_id(), name);
259 ASSERT_TRUE(e1.good()); 259 ASSERT_TRUE(e1.good());
260 ASSERT_TRUE(e1.Put(IS_DEL, true)); 260 ASSERT_TRUE(e1.Put(IS_DEL, true));
261 MutableEntry e2(&trans, CREATE, trans.root_id(), name); 261 MutableEntry e2(&trans, CREATE, trans.root_id(), name);
262 ASSERT_TRUE(e2.good()); 262 ASSERT_TRUE(e2.good());
263 ASSERT_TRUE(e2.Put(IS_DEL, true)); 263 ASSERT_TRUE(e2.Put(IS_DEL, true));
264 MutableEntry e3(&trans, CREATE, trans.root_id(), name); 264 MutableEntry e3(&trans, CREATE, trans.root_id(), name);
265 ASSERT_TRUE(e3.good()); 265 ASSERT_TRUE(e3.good());
266 ASSERT_TRUE(e3.Put(IS_DEL, true)); 266 ASSERT_TRUE(e3.Put(IS_DEL, true));
267 267
268 ASSERT_TRUE(e1.Put(IS_DEL, false)); 268 ASSERT_TRUE(e1.Put(IS_DEL, false));
269 ASSERT_TRUE(e2.Put(IS_DEL, false)); 269 ASSERT_TRUE(e2.Put(IS_DEL, false));
270 ASSERT_TRUE(e3.Put(IS_DEL, false)); 270 ASSERT_TRUE(e3.Put(IS_DEL, false));
271 271
272 ASSERT_TRUE(e1.Put(IS_DEL, true)); 272 ASSERT_TRUE(e1.Put(IS_DEL, true));
273 ASSERT_TRUE(e2.Put(IS_DEL, true)); 273 ASSERT_TRUE(e2.Put(IS_DEL, true));
274 ASSERT_TRUE(e3.Put(IS_DEL, true)); 274 ASSERT_TRUE(e3.Put(IS_DEL, true));
275 } 275 }
276 276
277 TEST_F(SyncableDirectoryTest, TestGetUnsynced) { 277 TEST_F(SyncableDirectoryTest, TestGetUnsynced) {
278 Directory::UnsyncedMetaHandles handles; 278 Directory::UnsyncedMetaHandles handles;
279 int64 handle1, handle2; 279 int64 handle1, handle2;
280 { 280 {
281 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 281 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
282 282
283 dir_->GetUnsyncedMetaHandles(&trans, &handles); 283 dir_->GetUnsyncedMetaHandles(&trans, &handles);
284 ASSERT_TRUE(0 == handles.size()); 284 ASSERT_TRUE(0 == handles.size());
285 285
286 MutableEntry e1(&trans, CREATE, trans.root_id(), PSTR("abba")); 286 MutableEntry e1(&trans, CREATE, trans.root_id(), "abba");
287 ASSERT_TRUE(e1.good()); 287 ASSERT_TRUE(e1.good());
288 handle1 = e1.Get(META_HANDLE); 288 handle1 = e1.Get(META_HANDLE);
289 e1.Put(BASE_VERSION, 1); 289 e1.Put(BASE_VERSION, 1);
290 e1.Put(IS_DIR, true); 290 e1.Put(IS_DIR, true);
291 e1.Put(ID, TestIdFactory::FromNumber(101)); 291 e1.Put(ID, TestIdFactory::FromNumber(101));
292 292
293 MutableEntry e2(&trans, CREATE, e1.Get(ID), PSTR("bread")); 293 MutableEntry e2(&trans, CREATE, e1.Get(ID), "bread");
294 ASSERT_TRUE(e2.good()); 294 ASSERT_TRUE(e2.good());
295 handle2 = e2.Get(META_HANDLE); 295 handle2 = e2.Get(META_HANDLE);
296 e2.Put(BASE_VERSION, 1); 296 e2.Put(BASE_VERSION, 1);
297 e2.Put(ID, TestIdFactory::FromNumber(102)); 297 e2.Put(ID, TestIdFactory::FromNumber(102));
298 } 298 }
299 dir_->SaveChanges(); 299 dir_->SaveChanges();
300 { 300 {
301 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 301 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
302 302
303 dir_->GetUnsyncedMetaHandles(&trans, &handles); 303 dir_->GetUnsyncedMetaHandles(&trans, &handles);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 347
348 TEST_F(SyncableDirectoryTest, TestGetUnappliedUpdates) { 348 TEST_F(SyncableDirectoryTest, TestGetUnappliedUpdates) {
349 Directory::UnappliedUpdateMetaHandles handles; 349 Directory::UnappliedUpdateMetaHandles handles;
350 int64 handle1, handle2; 350 int64 handle1, handle2;
351 { 351 {
352 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 352 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
353 353
354 dir_->GetUnappliedUpdateMetaHandles(&trans, &handles); 354 dir_->GetUnappliedUpdateMetaHandles(&trans, &handles);
355 ASSERT_TRUE(0 == handles.size()); 355 ASSERT_TRUE(0 == handles.size());
356 356
357 MutableEntry e1(&trans, CREATE, trans.root_id(), PSTR("abba")); 357 MutableEntry e1(&trans, CREATE, trans.root_id(), "abba");
358 ASSERT_TRUE(e1.good()); 358 ASSERT_TRUE(e1.good());
359 handle1 = e1.Get(META_HANDLE); 359 handle1 = e1.Get(META_HANDLE);
360 e1.Put(IS_UNAPPLIED_UPDATE, false); 360 e1.Put(IS_UNAPPLIED_UPDATE, false);
361 e1.Put(BASE_VERSION, 1); 361 e1.Put(BASE_VERSION, 1);
362 e1.Put(ID, TestIdFactory::FromNumber(101)); 362 e1.Put(ID, TestIdFactory::FromNumber(101));
363 e1.Put(IS_DIR, true); 363 e1.Put(IS_DIR, true);
364 364
365 MutableEntry e2(&trans, CREATE, e1.Get(ID), PSTR("bread")); 365 MutableEntry e2(&trans, CREATE, e1.Get(ID), "bread");
366 ASSERT_TRUE(e2.good()); 366 ASSERT_TRUE(e2.good());
367 handle2 = e2.Get(META_HANDLE); 367 handle2 = e2.Get(META_HANDLE);
368 e2.Put(IS_UNAPPLIED_UPDATE, false); 368 e2.Put(IS_UNAPPLIED_UPDATE, false);
369 e2.Put(BASE_VERSION, 1); 369 e2.Put(BASE_VERSION, 1);
370 e2.Put(ID, TestIdFactory::FromNumber(102)); 370 e2.Put(ID, TestIdFactory::FromNumber(102));
371 } 371 }
372 dir_->SaveChanges(); 372 dir_->SaveChanges();
373 { 373 {
374 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 374 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
375 375
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 } 416 }
417 } 417 }
418 418
419 419
420 TEST_F(SyncableDirectoryTest, DeleteBug_531383) { 420 TEST_F(SyncableDirectoryTest, DeleteBug_531383) {
421 // Try to evoke a check failure... 421 // Try to evoke a check failure...
422 TestIdFactory id_factory; 422 TestIdFactory id_factory;
423 int64 grandchild_handle, twin_handle; 423 int64 grandchild_handle, twin_handle;
424 { 424 {
425 WriteTransaction wtrans(dir_.get(), UNITTEST, __FILE__, __LINE__); 425 WriteTransaction wtrans(dir_.get(), UNITTEST, __FILE__, __LINE__);
426 MutableEntry parent(&wtrans, CREATE, id_factory.root(), PSTR("Bob")); 426 MutableEntry parent(&wtrans, CREATE, id_factory.root(), "Bob");
427 ASSERT_TRUE(parent.good()); 427 ASSERT_TRUE(parent.good());
428 parent.Put(IS_DIR, true); 428 parent.Put(IS_DIR, true);
429 parent.Put(ID, id_factory.NewServerId()); 429 parent.Put(ID, id_factory.NewServerId());
430 parent.Put(BASE_VERSION, 1); 430 parent.Put(BASE_VERSION, 1);
431 MutableEntry child(&wtrans, CREATE, parent.Get(ID), PSTR("Bob")); 431 MutableEntry child(&wtrans, CREATE, parent.Get(ID), "Bob");
432 ASSERT_TRUE(child.good()); 432 ASSERT_TRUE(child.good());
433 child.Put(IS_DIR, true); 433 child.Put(IS_DIR, true);
434 child.Put(ID, id_factory.NewServerId()); 434 child.Put(ID, id_factory.NewServerId());
435 child.Put(BASE_VERSION, 1); 435 child.Put(BASE_VERSION, 1);
436 MutableEntry grandchild(&wtrans, CREATE, child.Get(ID), PSTR("Bob")); 436 MutableEntry grandchild(&wtrans, CREATE, child.Get(ID), "Bob");
437 ASSERT_TRUE(grandchild.good()); 437 ASSERT_TRUE(grandchild.good());
438 grandchild.Put(ID, id_factory.NewServerId()); 438 grandchild.Put(ID, id_factory.NewServerId());
439 grandchild.Put(BASE_VERSION, 1); 439 grandchild.Put(BASE_VERSION, 1);
440 ASSERT_TRUE(grandchild.Put(IS_DEL, true)); 440 ASSERT_TRUE(grandchild.Put(IS_DEL, true));
441 MutableEntry twin(&wtrans, CREATE, child.Get(ID), PSTR("Bob")); 441 MutableEntry twin(&wtrans, CREATE, child.Get(ID), "Bob");
442 ASSERT_TRUE(twin.good()); 442 ASSERT_TRUE(twin.good());
443 ASSERT_TRUE(twin.Put(IS_DEL, true)); 443 ASSERT_TRUE(twin.Put(IS_DEL, true));
444 ASSERT_TRUE(grandchild.Put(IS_DEL, false)); 444 ASSERT_TRUE(grandchild.Put(IS_DEL, false));
445 445
446 grandchild_handle = grandchild.Get(META_HANDLE); 446 grandchild_handle = grandchild.Get(META_HANDLE);
447 twin_handle = twin.Get(META_HANDLE); 447 twin_handle = twin.Get(META_HANDLE);
448 } 448 }
449 dir_->SaveChanges(); 449 dir_->SaveChanges();
450 { 450 {
451 WriteTransaction wtrans(dir_.get(), UNITTEST, __FILE__, __LINE__); 451 WriteTransaction wtrans(dir_.get(), UNITTEST, __FILE__, __LINE__);
452 MutableEntry grandchild(&wtrans, GET_BY_HANDLE, grandchild_handle); 452 MutableEntry grandchild(&wtrans, GET_BY_HANDLE, grandchild_handle);
453 grandchild.Put(IS_DEL, true); // Used to CHECK fail here. 453 grandchild.Put(IS_DEL, true); // Used to CHECK fail here.
454 } 454 }
455 } 455 }
456 456
457 static inline bool IsLegalNewParent(const Entry& a, const Entry& b) { 457 static inline bool IsLegalNewParent(const Entry& a, const Entry& b) {
458 return IsLegalNewParent(a.trans(), a.Get(ID), b.Get(ID)); 458 return IsLegalNewParent(a.trans(), a.Get(ID), b.Get(ID));
459 } 459 }
460 460
461 TEST_F(SyncableDirectoryTest, TestIsLegalNewParent) { 461 TEST_F(SyncableDirectoryTest, TestIsLegalNewParent) {
462 TestIdFactory id_factory; 462 TestIdFactory id_factory;
463 WriteTransaction wtrans(dir_.get(), UNITTEST, __FILE__, __LINE__); 463 WriteTransaction wtrans(dir_.get(), UNITTEST, __FILE__, __LINE__);
464 Entry root(&wtrans, GET_BY_ID, id_factory.root()); 464 Entry root(&wtrans, GET_BY_ID, id_factory.root());
465 ASSERT_TRUE(root.good()); 465 ASSERT_TRUE(root.good());
466 MutableEntry parent(&wtrans, CREATE, root.Get(ID), PSTR("Bob")); 466 MutableEntry parent(&wtrans, CREATE, root.Get(ID), "Bob");
467 ASSERT_TRUE(parent.good()); 467 ASSERT_TRUE(parent.good());
468 parent.Put(IS_DIR, true); 468 parent.Put(IS_DIR, true);
469 parent.Put(ID, id_factory.NewServerId()); 469 parent.Put(ID, id_factory.NewServerId());
470 parent.Put(BASE_VERSION, 1); 470 parent.Put(BASE_VERSION, 1);
471 MutableEntry child(&wtrans, CREATE, parent.Get(ID), PSTR("Bob")); 471 MutableEntry child(&wtrans, CREATE, parent.Get(ID), "Bob");
472 ASSERT_TRUE(child.good()); 472 ASSERT_TRUE(child.good());
473 child.Put(IS_DIR, true); 473 child.Put(IS_DIR, true);
474 child.Put(ID, id_factory.NewServerId()); 474 child.Put(ID, id_factory.NewServerId());
475 child.Put(BASE_VERSION, 1); 475 child.Put(BASE_VERSION, 1);
476 MutableEntry grandchild(&wtrans, CREATE, child.Get(ID), PSTR("Bob")); 476 MutableEntry grandchild(&wtrans, CREATE, child.Get(ID), "Bob");
477 ASSERT_TRUE(grandchild.good()); 477 ASSERT_TRUE(grandchild.good());
478 grandchild.Put(ID, id_factory.NewServerId()); 478 grandchild.Put(ID, id_factory.NewServerId());
479 grandchild.Put(BASE_VERSION, 1); 479 grandchild.Put(BASE_VERSION, 1);
480 480
481 MutableEntry parent2(&wtrans, CREATE, root.Get(ID), PSTR("Pete")); 481 MutableEntry parent2(&wtrans, CREATE, root.Get(ID), "Pete");
482 ASSERT_TRUE(parent2.good()); 482 ASSERT_TRUE(parent2.good());
483 parent2.Put(IS_DIR, true); 483 parent2.Put(IS_DIR, true);
484 parent2.Put(ID, id_factory.NewServerId()); 484 parent2.Put(ID, id_factory.NewServerId());
485 parent2.Put(BASE_VERSION, 1); 485 parent2.Put(BASE_VERSION, 1);
486 MutableEntry child2(&wtrans, CREATE, parent2.Get(ID), PSTR("Pete")); 486 MutableEntry child2(&wtrans, CREATE, parent2.Get(ID), "Pete");
487 ASSERT_TRUE(child2.good()); 487 ASSERT_TRUE(child2.good());
488 child2.Put(IS_DIR, true); 488 child2.Put(IS_DIR, true);
489 child2.Put(ID, id_factory.NewServerId()); 489 child2.Put(ID, id_factory.NewServerId());
490 child2.Put(BASE_VERSION, 1); 490 child2.Put(BASE_VERSION, 1);
491 MutableEntry grandchild2(&wtrans, CREATE, child2.Get(ID), PSTR("Pete")); 491 MutableEntry grandchild2(&wtrans, CREATE, child2.Get(ID), "Pete");
492 ASSERT_TRUE(grandchild2.good()); 492 ASSERT_TRUE(grandchild2.good());
493 grandchild2.Put(ID, id_factory.NewServerId()); 493 grandchild2.Put(ID, id_factory.NewServerId());
494 grandchild2.Put(BASE_VERSION, 1); 494 grandchild2.Put(BASE_VERSION, 1);
495 // resulting tree 495 // resulting tree
496 // root 496 // root
497 // / | 497 // / |
498 // parent parent2 498 // parent parent2
499 // | | 499 // | |
500 // child child2 500 // child child2
501 // | | 501 // | |
502 // grandchild grandchild2 502 // grandchild grandchild2
503 ASSERT_TRUE(IsLegalNewParent(child, root)); 503 ASSERT_TRUE(IsLegalNewParent(child, root));
504 ASSERT_TRUE(IsLegalNewParent(child, parent)); 504 ASSERT_TRUE(IsLegalNewParent(child, parent));
505 ASSERT_FALSE(IsLegalNewParent(child, child)); 505 ASSERT_FALSE(IsLegalNewParent(child, child));
506 ASSERT_FALSE(IsLegalNewParent(child, grandchild)); 506 ASSERT_FALSE(IsLegalNewParent(child, grandchild));
507 ASSERT_TRUE(IsLegalNewParent(child, parent2)); 507 ASSERT_TRUE(IsLegalNewParent(child, parent2));
508 ASSERT_TRUE(IsLegalNewParent(child, grandchild2)); 508 ASSERT_TRUE(IsLegalNewParent(child, grandchild2));
509 ASSERT_FALSE(IsLegalNewParent(parent, grandchild)); 509 ASSERT_FALSE(IsLegalNewParent(parent, grandchild));
510 ASSERT_FALSE(IsLegalNewParent(root, grandchild)); 510 ASSERT_FALSE(IsLegalNewParent(root, grandchild));
511 ASSERT_FALSE(IsLegalNewParent(parent, grandchild)); 511 ASSERT_FALSE(IsLegalNewParent(parent, grandchild));
512 } 512 }
513 513
514 TEST_F(SyncableDirectoryTest, TestEntryIsInFolder) { 514 TEST_F(SyncableDirectoryTest, TestEntryIsInFolder) {
515 // Create a subdir and an entry. 515 // Create a subdir and an entry.
516 int64 entry_handle; 516 int64 entry_handle;
517 syncable::Id folder_id; 517 syncable::Id folder_id;
518 syncable::Id entry_id; 518 syncable::Id entry_id;
519 PathString entry_name = PSTR("entry"); 519 string entry_name = "entry";
520 520
521 { 521 {
522 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 522 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
523 MutableEntry folder(&trans, CREATE, trans.root_id(), PSTR("folder")); 523 MutableEntry folder(&trans, CREATE, trans.root_id(), "folder");
524 ASSERT_TRUE(folder.good()); 524 ASSERT_TRUE(folder.good());
525 EXPECT_TRUE(folder.Put(IS_DIR, true)); 525 EXPECT_TRUE(folder.Put(IS_DIR, true));
526 EXPECT_TRUE(folder.Put(IS_UNSYNCED, true)); 526 EXPECT_TRUE(folder.Put(IS_UNSYNCED, true));
527 folder_id = folder.Get(ID); 527 folder_id = folder.Get(ID);
528 528
529 MutableEntry entry(&trans, CREATE, folder.Get(ID), entry_name); 529 MutableEntry entry(&trans, CREATE, folder.Get(ID), entry_name);
530 ASSERT_TRUE(entry.good()); 530 ASSERT_TRUE(entry.good());
531 entry_handle = entry.Get(META_HANDLE); 531 entry_handle = entry.Get(META_HANDLE);
532 entry.Put(IS_UNSYNCED, true); 532 entry.Put(IS_UNSYNCED, true);
533 entry_id = entry.Get(ID); 533 entry_id = entry.Get(ID);
534 } 534 }
535 535
536 // Make sure we can find the entry in the folder. 536 // Make sure we can find the entry in the folder.
537 { 537 {
538 ReadTransaction trans(dir_.get(), __FILE__, __LINE__); 538 ReadTransaction trans(dir_.get(), __FILE__, __LINE__);
539 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), entry_name)); 539 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), entry_name));
540 EXPECT_EQ(1, CountEntriesWithName(&trans, folder_id, entry_name)); 540 EXPECT_EQ(1, CountEntriesWithName(&trans, folder_id, entry_name));
541 541
542 Entry entry(&trans, GET_BY_ID, entry_id); 542 Entry entry(&trans, GET_BY_ID, entry_id);
543 ASSERT_TRUE(entry.good()); 543 ASSERT_TRUE(entry.good());
544 EXPECT_EQ(entry_handle, entry.Get(META_HANDLE)); 544 EXPECT_EQ(entry_handle, entry.Get(META_HANDLE));
545 EXPECT_TRUE(entry.Get(NON_UNIQUE_NAME) == entry_name); 545 EXPECT_TRUE(entry.Get(NON_UNIQUE_NAME) == entry_name);
546 EXPECT_TRUE(entry.Get(PARENT_ID) == folder_id); 546 EXPECT_TRUE(entry.Get(PARENT_ID) == folder_id);
547 } 547 }
548 } 548 }
549 549
550 TEST_F(SyncableDirectoryTest, TestParentIdIndexUpdate) { 550 TEST_F(SyncableDirectoryTest, TestParentIdIndexUpdate) {
551 PathString child_name = PSTR("child"); 551 string child_name = "child";
552 552
553 WriteTransaction wt(dir_.get(), UNITTEST, __FILE__, __LINE__); 553 WriteTransaction wt(dir_.get(), UNITTEST, __FILE__, __LINE__);
554 MutableEntry parent_folder(&wt, CREATE, wt.root_id(), PSTR("folder1")); 554 MutableEntry parent_folder(&wt, CREATE, wt.root_id(), "folder1");
555 parent_folder.Put(IS_UNSYNCED, true); 555 parent_folder.Put(IS_UNSYNCED, true);
556 EXPECT_TRUE(parent_folder.Put(IS_DIR, true)); 556 EXPECT_TRUE(parent_folder.Put(IS_DIR, true));
557 557
558 MutableEntry parent_folder2(&wt, CREATE, wt.root_id(), PSTR("folder2")); 558 MutableEntry parent_folder2(&wt, CREATE, wt.root_id(), "folder2");
559 parent_folder2.Put(IS_UNSYNCED, true); 559 parent_folder2.Put(IS_UNSYNCED, true);
560 EXPECT_TRUE(parent_folder2.Put(IS_DIR, true)); 560 EXPECT_TRUE(parent_folder2.Put(IS_DIR, true));
561 561
562 MutableEntry child(&wt, CREATE, parent_folder.Get(ID), child_name); 562 MutableEntry child(&wt, CREATE, parent_folder.Get(ID), child_name);
563 EXPECT_TRUE(child.Put(IS_DIR, true)); 563 EXPECT_TRUE(child.Put(IS_DIR, true));
564 child.Put(IS_UNSYNCED, true); 564 child.Put(IS_UNSYNCED, true);
565 565
566 ASSERT_TRUE(child.good()); 566 ASSERT_TRUE(child.good());
567 567
568 EXPECT_EQ(0, CountEntriesWithName(&wt, wt.root_id(), child_name)); 568 EXPECT_EQ(0, CountEntriesWithName(&wt, wt.root_id(), child_name));
569 EXPECT_EQ(parent_folder.Get(ID), child.Get(PARENT_ID)); 569 EXPECT_EQ(parent_folder.Get(ID), child.Get(PARENT_ID));
570 EXPECT_EQ(1, CountEntriesWithName(&wt, parent_folder.Get(ID), child_name)); 570 EXPECT_EQ(1, CountEntriesWithName(&wt, parent_folder.Get(ID), child_name));
571 EXPECT_EQ(0, CountEntriesWithName(&wt, parent_folder2.Get(ID), child_name)); 571 EXPECT_EQ(0, CountEntriesWithName(&wt, parent_folder2.Get(ID), child_name));
572 child.Put(PARENT_ID, parent_folder2.Get(ID)); 572 child.Put(PARENT_ID, parent_folder2.Get(ID));
573 EXPECT_EQ(parent_folder2.Get(ID), child.Get(PARENT_ID)); 573 EXPECT_EQ(parent_folder2.Get(ID), child.Get(PARENT_ID));
574 EXPECT_EQ(0, CountEntriesWithName(&wt, parent_folder.Get(ID), child_name)); 574 EXPECT_EQ(0, CountEntriesWithName(&wt, parent_folder.Get(ID), child_name));
575 EXPECT_EQ(1, CountEntriesWithName(&wt, parent_folder2.Get(ID), child_name)); 575 EXPECT_EQ(1, CountEntriesWithName(&wt, parent_folder2.Get(ID), child_name));
576 576
577 } 577 }
578 578
579 TEST_F(SyncableDirectoryTest, TestNoReindexDeletedItems) { 579 TEST_F(SyncableDirectoryTest, TestNoReindexDeletedItems) {
580 PathString folder_name = PSTR("folder"); 580 string folder_name = "folder";
581 PathString new_name = PSTR("new_name"); 581 string new_name = "new_name";
582 582
583 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 583 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
584 MutableEntry folder(&trans, CREATE, trans.root_id(), folder_name); 584 MutableEntry folder(&trans, CREATE, trans.root_id(), folder_name);
585 ASSERT_TRUE(folder.good()); 585 ASSERT_TRUE(folder.good());
586 ASSERT_TRUE(folder.Put(IS_DIR, true)); 586 ASSERT_TRUE(folder.Put(IS_DIR, true));
587 ASSERT_TRUE(folder.Put(IS_DEL, true)); 587 ASSERT_TRUE(folder.Put(IS_DEL, true));
588 588
589 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), folder_name)); 589 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), folder_name));
590 590
591 MutableEntry deleted(&trans, GET_BY_ID, folder.Get(ID)); 591 MutableEntry deleted(&trans, GET_BY_ID, folder.Get(ID));
592 ASSERT_TRUE(deleted.good()); 592 ASSERT_TRUE(deleted.good());
593 ASSERT_TRUE(deleted.Put(PARENT_ID, trans.root_id())); 593 ASSERT_TRUE(deleted.Put(PARENT_ID, trans.root_id()));
594 ASSERT_TRUE(deleted.Put(NON_UNIQUE_NAME, new_name)); 594 ASSERT_TRUE(deleted.Put(NON_UNIQUE_NAME, new_name));
595 595
596 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), folder_name)); 596 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), folder_name));
597 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), new_name)); 597 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), new_name));
598 } 598 }
599 599
600 TEST_F(SyncableDirectoryTest, TestCaseChangeRename) { 600 TEST_F(SyncableDirectoryTest, TestCaseChangeRename) {
601 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 601 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
602 MutableEntry folder(&trans, CREATE, trans.root_id(), PSTR("CaseChange")); 602 MutableEntry folder(&trans, CREATE, trans.root_id(), "CaseChange");
603 ASSERT_TRUE(folder.good()); 603 ASSERT_TRUE(folder.good());
604 EXPECT_TRUE(folder.Put(PARENT_ID, trans.root_id())); 604 EXPECT_TRUE(folder.Put(PARENT_ID, trans.root_id()));
605 EXPECT_TRUE(folder.Put(NON_UNIQUE_NAME, PSTR("CASECHANGE"))); 605 EXPECT_TRUE(folder.Put(NON_UNIQUE_NAME, "CASECHANGE"));
606 EXPECT_TRUE(folder.Put(IS_DEL, true)); 606 EXPECT_TRUE(folder.Put(IS_DEL, true));
607 } 607 }
608 608
609 TEST_F(SyncableDirectoryTest, TestShareInfo) { 609 TEST_F(SyncableDirectoryTest, TestShareInfo) {
610 dir_->set_last_sync_timestamp(100); 610 dir_->set_last_sync_timestamp(100);
611 dir_->set_store_birthday("Jan 31st"); 611 dir_->set_store_birthday("Jan 31st");
612 { 612 {
613 ReadTransaction trans(dir_.get(), __FILE__, __LINE__); 613 ReadTransaction trans(dir_.get(), __FILE__, __LINE__);
614 EXPECT_EQ(100, dir_->last_sync_timestamp()); 614 EXPECT_EQ(100, dir_->last_sync_timestamp());
615 EXPECT_EQ("Jan 31st", dir_->store_birthday()); 615 EXPECT_EQ("Jan 31st", dir_->store_birthday());
616 } 616 }
617 dir_->set_last_sync_timestamp(200); 617 dir_->set_last_sync_timestamp(200);
618 dir_->set_store_birthday("April 10th"); 618 dir_->set_store_birthday("April 10th");
619 dir_->SaveChanges(); 619 dir_->SaveChanges();
620 { 620 {
621 ReadTransaction trans(dir_.get(), __FILE__, __LINE__); 621 ReadTransaction trans(dir_.get(), __FILE__, __LINE__);
622 EXPECT_EQ(200, dir_->last_sync_timestamp()); 622 EXPECT_EQ(200, dir_->last_sync_timestamp());
623 EXPECT_EQ("April 10th", dir_->store_birthday()); 623 EXPECT_EQ("April 10th", dir_->store_birthday());
624 } 624 }
625 } 625 }
626 626
627 TEST_F(SyncableDirectoryTest, TestSimpleFieldsPreservedDuringSaveChanges) { 627 TEST_F(SyncableDirectoryTest, TestSimpleFieldsPreservedDuringSaveChanges) {
628 Id update_id = TestIdFactory::FromNumber(1); 628 Id update_id = TestIdFactory::FromNumber(1);
629 Id create_id; 629 Id create_id;
630 EntryKernel create_pre_save, update_pre_save; 630 EntryKernel create_pre_save, update_pre_save;
631 EntryKernel create_post_save, update_post_save; 631 EntryKernel create_post_save, update_post_save;
632 PathString create_name = PSTR("Create"); 632 string create_name = "Create";
633 633
634 { 634 {
635 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 635 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
636 MutableEntry create(&trans, CREATE, trans.root_id(), create_name); 636 MutableEntry create(&trans, CREATE, trans.root_id(), create_name);
637 MutableEntry update(&trans, CREATE_NEW_UPDATE_ITEM, update_id); 637 MutableEntry update(&trans, CREATE_NEW_UPDATE_ITEM, update_id);
638 create.Put(IS_UNSYNCED, true); 638 create.Put(IS_UNSYNCED, true);
639 update.Put(IS_UNAPPLIED_UPDATE, true); 639 update.Put(IS_UNAPPLIED_UPDATE, true);
640 create_pre_save = create.GetKernelCopy(); 640 create_pre_save = create.GetKernelCopy();
641 update_pre_save = update.GetKernelCopy(); 641 update_pre_save = update.GetKernelCopy();
642 create_id = create.Get(ID); 642 create_id = create.Get(ID);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
684 update_post_save.ref((StringField)i)) 684 update_post_save.ref((StringField)i))
685 << "String field #" << i << " changed during save/load"; 685 << "String field #" << i << " changed during save/load";
686 } 686 }
687 } 687 }
688 688
689 TEST_F(SyncableDirectoryTest, TestSaveChangesFailure) { 689 TEST_F(SyncableDirectoryTest, TestSaveChangesFailure) {
690 int64 handle1 = 0; 690 int64 handle1 = 0;
691 { 691 {
692 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 692 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
693 693
694 MutableEntry e1(&trans, CREATE, trans.root_id(), PSTR("aguilera")); 694 MutableEntry e1(&trans, CREATE, trans.root_id(), "aguilera");
695 ASSERT_TRUE(e1.good()); 695 ASSERT_TRUE(e1.good());
696 handle1 = e1.Get(META_HANDLE); 696 handle1 = e1.Get(META_HANDLE);
697 e1.Put(BASE_VERSION, 1); 697 e1.Put(BASE_VERSION, 1);
698 e1.Put(IS_DIR, true); 698 e1.Put(IS_DIR, true);
699 e1.Put(ID, TestIdFactory::FromNumber(101)); 699 e1.Put(ID, TestIdFactory::FromNumber(101));
700 } 700 }
701 ASSERT_TRUE(dir_->SaveChanges()); 701 ASSERT_TRUE(dir_->SaveChanges());
702 702
703 dir_.reset(new TestUnsaveableDirectory()); 703 dir_.reset(new TestUnsaveableDirectory());
704 ASSERT_TRUE(dir_.get()); 704 ASSERT_TRUE(dir_.get());
705 ASSERT_TRUE(OPENED == dir_->Open(FilePath(kFilePath), kName)); 705 ASSERT_TRUE(OPENED == dir_->Open(FilePath(kFilePath), kName));
706 ASSERT_TRUE(dir_->good()); 706 ASSERT_TRUE(dir_->good());
707 int64 handle2 = 0; 707 int64 handle2 = 0;
708 { 708 {
709 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 709 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
710 710
711 MutableEntry aguilera(&trans, GET_BY_HANDLE, handle1); 711 MutableEntry aguilera(&trans, GET_BY_HANDLE, handle1);
712 ASSERT_TRUE(aguilera.good()); 712 ASSERT_TRUE(aguilera.good());
713 aguilera.Put(NON_UNIQUE_NAME, PSTR("christina")); 713 aguilera.Put(NON_UNIQUE_NAME, "christina");
714 ASSERT_TRUE(aguilera.GetKernelCopy().dirty[NON_UNIQUE_NAME]); 714 ASSERT_TRUE(aguilera.GetKernelCopy().dirty[NON_UNIQUE_NAME]);
715 715
716 MutableEntry kids_on_block(&trans, CREATE, trans.root_id(), PSTR("kids")); 716 MutableEntry kids_on_block(&trans, CREATE, trans.root_id(), "kids");
717 ASSERT_TRUE(kids_on_block.good()); 717 ASSERT_TRUE(kids_on_block.good());
718 handle2 = kids_on_block.Get(META_HANDLE); 718 handle2 = kids_on_block.Get(META_HANDLE);
719 kids_on_block.Put(BASE_VERSION, 1); 719 kids_on_block.Put(BASE_VERSION, 1);
720 kids_on_block.Put(IS_DIR, true); 720 kids_on_block.Put(IS_DIR, true);
721 kids_on_block.Put(ID, TestIdFactory::FromNumber(102)); 721 kids_on_block.Put(ID, TestIdFactory::FromNumber(102));
722 EXPECT_TRUE(kids_on_block.Get(IS_NEW)); 722 EXPECT_TRUE(kids_on_block.Get(IS_NEW));
723 } 723 }
724 724
725 // We are using an unsaveable directory, so this can't succeed. However, 725 // We are using an unsaveable directory, so this can't succeed. However,
726 // the HandleSaveChangesFailure code path should have been triggered. 726 // the HandleSaveChangesFailure code path should have been triggered.
727 ASSERT_FALSE(dir_->SaveChanges()); 727 ASSERT_FALSE(dir_->SaveChanges());
728 728
729 // Make sure things were rolled back and the world is as it was before call. 729 // Make sure things were rolled back and the world is as it was before call.
730 { 730 {
731 ReadTransaction trans(dir_.get(), __FILE__, __LINE__); 731 ReadTransaction trans(dir_.get(), __FILE__, __LINE__);
732 Entry e1(&trans, GET_BY_HANDLE, handle1); 732 Entry e1(&trans, GET_BY_HANDLE, handle1);
733 ASSERT_TRUE(e1.good()); 733 ASSERT_TRUE(e1.good());
734 const EntryKernel& aguilera = e1.GetKernelCopy(); 734 const EntryKernel& aguilera = e1.GetKernelCopy();
735 Entry kids_on_block(&trans, GET_BY_HANDLE, handle2); 735 Entry kids_on_block(&trans, GET_BY_HANDLE, handle2);
736 ASSERT_TRUE(kids_on_block.good()); 736 ASSERT_TRUE(kids_on_block.good());
737 737
738 EXPECT_TRUE(aguilera.dirty[NON_UNIQUE_NAME]); 738 EXPECT_TRUE(aguilera.dirty[NON_UNIQUE_NAME]);
739 EXPECT_TRUE(kids_on_block.Get(IS_NEW)); 739 EXPECT_TRUE(kids_on_block.Get(IS_NEW));
740 } 740 }
741 } 741 }
742 742
743 743
744 void SyncableDirectoryTest::ValidateEntry(BaseTransaction* trans, int64 id, 744 void SyncableDirectoryTest::ValidateEntry(BaseTransaction* trans, int64 id,
745 bool check_name, PathString name, int64 base_version, int64 server_version, 745 bool check_name, string name, int64 base_version, int64 server_version,
746 bool is_del) { 746 bool is_del) {
747 Entry e(trans, GET_BY_ID, TestIdFactory::FromNumber(id)); 747 Entry e(trans, GET_BY_ID, TestIdFactory::FromNumber(id));
748 ASSERT_TRUE(e.good()); 748 ASSERT_TRUE(e.good());
749 if (check_name) { 749 if (check_name) {
750 ASSERT_TRUE(name == e.Get(NON_UNIQUE_NAME)); 750 ASSERT_TRUE(name == e.Get(NON_UNIQUE_NAME));
751 } 751 }
752 ASSERT_TRUE(base_version == e.Get(BASE_VERSION)); 752 ASSERT_TRUE(base_version == e.Get(BASE_VERSION));
753 ASSERT_TRUE(server_version == e.Get(SERVER_VERSION)); 753 ASSERT_TRUE(server_version == e.Get(SERVER_VERSION));
754 ASSERT_TRUE(is_del == e.Get(IS_DEL)); 754 ASSERT_TRUE(is_del == e.Get(IS_DEL));
755 } 755 }
756 756
757 TEST(SyncableDirectoryManager, TestFileRelease) { 757 TEST(SyncableDirectoryManager, TestFileRelease) {
758 DirectoryManager dm(FilePath(FILE_PATH_LITERAL("."))); 758 DirectoryManager dm(FilePath(FILE_PATH_LITERAL(".")));
759 ASSERT_TRUE(dm.Open(PSTR("ScopeTest"))); 759 ASSERT_TRUE(dm.Open("ScopeTest"));
760 { 760 {
761 ScopedDirLookup(&dm, PSTR("ScopeTest")); 761 ScopedDirLookup(&dm, "ScopeTest");
762 } 762 }
763 dm.Close(PSTR("ScopeTest")); 763 dm.Close("ScopeTest");
764 ASSERT_TRUE(file_util::Delete(dm.GetSyncDataDatabasePath(), true)); 764 ASSERT_TRUE(file_util::Delete(dm.GetSyncDataDatabasePath(), true));
765 } 765 }
766 766
767 class ThreadOpenTestDelegate : public PlatformThread::Delegate { 767 class ThreadOpenTestDelegate : public PlatformThread::Delegate {
768 public: 768 public:
769 explicit ThreadOpenTestDelegate(DirectoryManager* dm) 769 explicit ThreadOpenTestDelegate(DirectoryManager* dm)
770 : directory_manager_(dm) {} 770 : directory_manager_(dm) {}
771 DirectoryManager* const directory_manager_; 771 DirectoryManager* const directory_manager_;
772 772
773 private: 773 private:
774 // PlatformThread::Delegate methods: 774 // PlatformThread::Delegate methods:
775 virtual void ThreadMain() { 775 virtual void ThreadMain() {
776 CHECK(directory_manager_->Open(PSTR("Open"))); 776 CHECK(directory_manager_->Open("Open"));
777 } 777 }
778 778
779 DISALLOW_COPY_AND_ASSIGN(ThreadOpenTestDelegate); 779 DISALLOW_COPY_AND_ASSIGN(ThreadOpenTestDelegate);
780 }; 780 };
781 781
782 TEST(SyncableDirectoryManager, ThreadOpenTest) { 782 TEST(SyncableDirectoryManager, ThreadOpenTest) {
783 DirectoryManager dm(FilePath(FILE_PATH_LITERAL("."))); 783 DirectoryManager dm(FilePath(FILE_PATH_LITERAL(".")));
784 PlatformThreadHandle thread_handle; 784 PlatformThreadHandle thread_handle;
785 ThreadOpenTestDelegate test_delegate(&dm); 785 ThreadOpenTestDelegate test_delegate(&dm);
786 ASSERT_TRUE(PlatformThread::Create(0, &test_delegate, &thread_handle)); 786 ASSERT_TRUE(PlatformThread::Create(0, &test_delegate, &thread_handle));
787 PlatformThread::Join(thread_handle); 787 PlatformThread::Join(thread_handle);
788 { 788 {
789 ScopedDirLookup dir(&dm, PSTR("Open")); 789 ScopedDirLookup dir(&dm, "Open");
790 ASSERT_TRUE(dir.good()); 790 ASSERT_TRUE(dir.good());
791 } 791 }
792 dm.Close(PSTR("Open")); 792 dm.Close("Open");
793 ScopedDirLookup dir(&dm, PSTR("Open")); 793 ScopedDirLookup dir(&dm, "Open");
794 ASSERT_FALSE(dir.good()); 794 ASSERT_FALSE(dir.good());
795 } 795 }
796 796
797 struct Step { 797 struct Step {
798 Step() : condvar(&mutex), number(0) {} 798 Step() : condvar(&mutex), number(0) {}
799 799
800 Lock mutex; 800 Lock mutex;
801 ConditionVariable condvar; 801 ConditionVariable condvar;
802 int number; 802 int number;
803 int64 metahandle; 803 int64 metahandle;
804 }; 804 };
805 805
806 class ThreadBugDelegate : public PlatformThread::Delegate { 806 class ThreadBugDelegate : public PlatformThread::Delegate {
807 public: 807 public:
808 // a role is 0 or 1, meaning this thread does the odd or event steps. 808 // a role is 0 or 1, meaning this thread does the odd or event steps.
809 ThreadBugDelegate(int role, Step* step, DirectoryManager* dirman) 809 ThreadBugDelegate(int role, Step* step, DirectoryManager* dirman)
810 : role_(role), step_(step), directory_manager_(dirman) {} 810 : role_(role), step_(step), directory_manager_(dirman) {}
811 811
812 protected: 812 protected:
813 const int role_; 813 const int role_;
814 Step* const step_; 814 Step* const step_;
815 DirectoryManager* const directory_manager_; 815 DirectoryManager* const directory_manager_;
816 816
817 // PlatformThread::Delegate methods: 817 // PlatformThread::Delegate methods:
818 virtual void ThreadMain() { 818 virtual void ThreadMain() {
819 const PathString dirname = PSTR("ThreadBug1"); 819 const string dirname = "ThreadBug1";
820 AutoLock scoped_lock(step_->mutex); 820 AutoLock scoped_lock(step_->mutex);
821 821
822 while (step_->number < 3) { 822 while (step_->number < 3) {
823 while (step_->number % 2 != role_) { 823 while (step_->number % 2 != role_) {
824 step_->condvar.Wait(); 824 step_->condvar.Wait();
825 } 825 }
826 switch (step_->number) { 826 switch (step_->number) {
827 case 0: 827 case 0:
828 directory_manager_->Open(dirname); 828 directory_manager_->Open(dirname);
829 break; 829 break;
830 case 1: 830 case 1:
831 { 831 {
832 directory_manager_->Close(dirname); 832 directory_manager_->Close(dirname);
833 directory_manager_->Open(dirname); 833 directory_manager_->Open(dirname);
834 ScopedDirLookup dir(directory_manager_, dirname); 834 ScopedDirLookup dir(directory_manager_, dirname);
835 CHECK(dir.good()); 835 CHECK(dir.good());
836 WriteTransaction trans(dir, UNITTEST, __FILE__, __LINE__); 836 WriteTransaction trans(dir, UNITTEST, __FILE__, __LINE__);
837 MutableEntry me(&trans, CREATE, trans.root_id(), PSTR("Jeff")); 837 MutableEntry me(&trans, CREATE, trans.root_id(), "Jeff");
838 step_->metahandle = me.Get(META_HANDLE); 838 step_->metahandle = me.Get(META_HANDLE);
839 me.Put(IS_UNSYNCED, true); 839 me.Put(IS_UNSYNCED, true);
840 } 840 }
841 break; 841 break;
842 case 2: 842 case 2:
843 { 843 {
844 ScopedDirLookup dir(directory_manager_, dirname); 844 ScopedDirLookup dir(directory_manager_, dirname);
845 CHECK(dir.good()); 845 CHECK(dir.good());
846 ReadTransaction trans(dir, __FILE__, __LINE__); 846 ReadTransaction trans(dir, __FILE__, __LINE__);
847 Entry e(&trans, GET_BY_HANDLE, step_->metahandle); 847 Entry e(&trans, GET_BY_HANDLE, step_->metahandle);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 // directory would be closed and re-opened, and then an old 880 // directory would be closed and re-opened, and then an old
881 // Directory::Kernel with stale information would get saved to the db. 881 // Directory::Kernel with stale information would get saved to the db.
882 class DirectoryKernelStalenessBugDelegate : public ThreadBugDelegate { 882 class DirectoryKernelStalenessBugDelegate : public ThreadBugDelegate {
883 public: 883 public:
884 DirectoryKernelStalenessBugDelegate(int role, Step* step, 884 DirectoryKernelStalenessBugDelegate(int role, Step* step,
885 DirectoryManager* dirman) 885 DirectoryManager* dirman)
886 : ThreadBugDelegate(role, step, dirman) {} 886 : ThreadBugDelegate(role, step, dirman) {}
887 887
888 virtual void ThreadMain() { 888 virtual void ThreadMain() {
889 const char test_bytes[] = "test data"; 889 const char test_bytes[] = "test data";
890 const PathString dirname = PSTR("DirectoryKernelStalenessBug"); 890 const string dirname = "DirectoryKernelStalenessBug";
891 AutoLock scoped_lock(step_->mutex); 891 AutoLock scoped_lock(step_->mutex);
892 const Id jeff_id = TestIdFactory::FromNumber(100); 892 const Id jeff_id = TestIdFactory::FromNumber(100);
893 893
894 while (step_->number < 4) { 894 while (step_->number < 4) {
895 while (step_->number % 2 != role_) { 895 while (step_->number % 2 != role_) {
896 step_->condvar.Wait(); 896 step_->condvar.Wait();
897 } 897 }
898 switch (step_->number) { 898 switch (step_->number) {
899 case 0: 899 case 0:
900 { 900 {
901 // Clean up remnants of earlier test runs. 901 // Clean up remnants of earlier test runs.
902 file_util::Delete(directory_manager_->GetSyncDataDatabasePath(), 902 file_util::Delete(directory_manager_->GetSyncDataDatabasePath(),
903 true); 903 true);
904 // Test. 904 // Test.
905 directory_manager_->Open(dirname); 905 directory_manager_->Open(dirname);
906 ScopedDirLookup dir(directory_manager_, dirname); 906 ScopedDirLookup dir(directory_manager_, dirname);
907 CHECK(dir.good()); 907 CHECK(dir.good());
908 WriteTransaction trans(dir, UNITTEST, __FILE__, __LINE__); 908 WriteTransaction trans(dir, UNITTEST, __FILE__, __LINE__);
909 MutableEntry me(&trans, CREATE, trans.root_id(), PSTR("Jeff")); 909 MutableEntry me(&trans, CREATE, trans.root_id(), "Jeff");
910 me.Put(BASE_VERSION, 1); 910 me.Put(BASE_VERSION, 1);
911 me.Put(ID, jeff_id); 911 me.Put(ID, jeff_id);
912 PutDataAsExtendedAttribute(&trans, &me, test_bytes, 912 PutDataAsExtendedAttribute(&trans, &me, test_bytes,
913 sizeof(test_bytes)); 913 sizeof(test_bytes));
914 } 914 }
915 { 915 {
916 ScopedDirLookup dir(directory_manager_, dirname); 916 ScopedDirLookup dir(directory_manager_, dirname);
917 CHECK(dir.good()); 917 CHECK(dir.good());
918 dir->SaveChanges(); 918 dir->SaveChanges();
919 } 919 }
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
965 965
966 ASSERT_TRUE(PlatformThread::Create(0, &thread_delegate_1, &thread_handle_1)); 966 ASSERT_TRUE(PlatformThread::Create(0, &thread_delegate_1, &thread_handle_1));
967 ASSERT_TRUE(PlatformThread::Create(0, &thread_delegate_2, &thread_handle_2)); 967 ASSERT_TRUE(PlatformThread::Create(0, &thread_delegate_2, &thread_handle_2));
968 968
969 PlatformThread::Join(thread_handle_1); 969 PlatformThread::Join(thread_handle_1);
970 PlatformThread::Join(thread_handle_2); 970 PlatformThread::Join(thread_handle_2);
971 } 971 }
972 972
973 class StressTransactionsDelegate : public PlatformThread::Delegate { 973 class StressTransactionsDelegate : public PlatformThread::Delegate {
974 public: 974 public:
975 StressTransactionsDelegate(DirectoryManager* dm, PathString dirname, 975 StressTransactionsDelegate(DirectoryManager* dm, string dirname,
976 int thread_number) 976 int thread_number)
977 : directory_manager_(dm), dirname_(dirname), 977 : directory_manager_(dm), dirname_(dirname),
978 thread_number_(thread_number) {} 978 thread_number_(thread_number) {}
979 979
980 private: 980 private:
981 DirectoryManager* const directory_manager_; 981 DirectoryManager* const directory_manager_;
982 PathString dirname_; 982 string dirname_;
983 const int thread_number_; 983 const int thread_number_;
984 984
985 // PlatformThread::Delegate methods: 985 // PlatformThread::Delegate methods:
986 virtual void ThreadMain() { 986 virtual void ThreadMain() {
987 ScopedDirLookup dir(directory_manager_, dirname_); 987 ScopedDirLookup dir(directory_manager_, dirname_);
988 CHECK(dir.good()); 988 CHECK(dir.good());
989 int entry_count = 0; 989 int entry_count = 0;
990 PathString path_name; 990 string path_name;
991 991
992 for (int i = 0; i < 20; ++i) { 992 for (int i = 0; i < 20; ++i) {
993 const int rand_action = rand() % 10; 993 const int rand_action = rand() % 10;
994 if (rand_action < 4 && !path_name.empty()) { 994 if (rand_action < 4 && !path_name.empty()) {
995 ReadTransaction trans(dir, __FILE__, __LINE__); 995 ReadTransaction trans(dir, __FILE__, __LINE__);
996 CHECK(1 == CountEntriesWithName(&trans, trans.root_id(), path_name)); 996 CHECK(1 == CountEntriesWithName(&trans, trans.root_id(), path_name));
997 PlatformThread::Sleep(rand() % 10); 997 PlatformThread::Sleep(rand() % 10);
998 } else { 998 } else {
999 string unique_name = StringPrintf("%d.%d", thread_number_, 999 string unique_name = StringPrintf("%d.%d", thread_number_,
1000 entry_count++); 1000 entry_count++);
1001 path_name.assign(unique_name.begin(), unique_name.end()); 1001 path_name.assign(unique_name.begin(), unique_name.end());
1002 WriteTransaction trans(dir, UNITTEST, __FILE__, __LINE__); 1002 WriteTransaction trans(dir, UNITTEST, __FILE__, __LINE__);
1003 MutableEntry e(&trans, CREATE, trans.root_id(), path_name); 1003 MutableEntry e(&trans, CREATE, trans.root_id(), path_name);
1004 CHECK(e.good()); 1004 CHECK(e.good());
1005 PlatformThread::Sleep(rand() % 20); 1005 PlatformThread::Sleep(rand() % 20);
1006 e.Put(IS_UNSYNCED, true); 1006 e.Put(IS_UNSYNCED, true);
1007 if (e.Put(ID, TestIdFactory::FromNumber(rand())) && 1007 if (e.Put(ID, TestIdFactory::FromNumber(rand())) &&
1008 e.Get(ID).ServerKnows() && !e.Get(ID).IsRoot()) 1008 e.Get(ID).ServerKnows() && !e.Get(ID).IsRoot())
1009 e.Put(BASE_VERSION, 1); 1009 e.Put(BASE_VERSION, 1);
1010 } 1010 }
1011 } 1011 }
1012 } 1012 }
1013 1013
1014 DISALLOW_COPY_AND_ASSIGN(StressTransactionsDelegate); 1014 DISALLOW_COPY_AND_ASSIGN(StressTransactionsDelegate);
1015 }; 1015 };
1016 1016
1017 TEST(SyncableDirectory, StressTransactions) { 1017 TEST(SyncableDirectory, StressTransactions) {
1018 DirectoryManager dirman(FilePath(FILE_PATH_LITERAL("."))); 1018 DirectoryManager dirman(FilePath(FILE_PATH_LITERAL(".")));
1019 PathString dirname = PSTR("stress"); 1019 string dirname = "stress";
1020 file_util::Delete(dirman.GetSyncDataDatabasePath(), true); 1020 file_util::Delete(dirman.GetSyncDataDatabasePath(), true);
1021 dirman.Open(dirname); 1021 dirman.Open(dirname);
1022 1022
1023 const int kThreadCount = 7; 1023 const int kThreadCount = 7;
1024 PlatformThreadHandle threads[kThreadCount]; 1024 PlatformThreadHandle threads[kThreadCount];
1025 scoped_ptr<StressTransactionsDelegate> thread_delegates[kThreadCount]; 1025 scoped_ptr<StressTransactionsDelegate> thread_delegates[kThreadCount];
1026 1026
1027 for (int i = 0; i < kThreadCount; ++i) { 1027 for (int i = 0; i < kThreadCount; ++i) {
1028 thread_delegates[i].reset( 1028 thread_delegates[i].reset(
1029 new StressTransactionsDelegate(&dirman, dirname, i)); 1029 new StressTransactionsDelegate(&dirman, dirname, i));
(...skipping 21 matching lines...) Expand all
1051 { 'a', 'a', 0 }, 1051 { 'a', 'a', 0 },
1052 { 'A', 'B', -1 }, 1052 { 'A', 'B', -1 },
1053 { 'A', 'b', -1 }, 1053 { 'A', 'b', -1 },
1054 { 'a', 'B', -1 }, 1054 { 'a', 'B', -1 },
1055 { 'a', 'b', -1 }, 1055 { 'a', 'b', -1 },
1056 { 'B', 'A', 1 }, 1056 { 'B', 'A', 1 },
1057 { 'B', 'a', 1 }, 1057 { 'B', 'a', 1 },
1058 { 'b', 'A', 1 }, 1058 { 'b', 'A', 1 },
1059 { 'b', 'a', 1 } }; 1059 { 'b', 'a', 1 } };
1060 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 1060 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
1061 PathString a(1, tests[i].a); 1061 string a(1, tests[i].a);
1062 PathString b(1, tests[i].b); 1062 string b(1, tests[i].b);
1063 const int result = ComparePathNames(a, b); 1063 const int result = ComparePathNames(a, b);
1064 if (result != tests[i].expected_result) { 1064 if (result != tests[i].expected_result) {
1065 ADD_FAILURE() << "ComparePathNames(" << tests[i].a << ", " << tests[i].b 1065 ADD_FAILURE() << "ComparePathNames(" << tests[i].a << ", " << tests[i].b
1066 << ") returned " << result << "; expected " 1066 << ") returned " << result << "; expected "
1067 << tests[i].expected_result; 1067 << tests[i].expected_result;
1068 } 1068 }
1069 } 1069 }
1070 } 1070 }
1071 1071
1072 void FakeSync(MutableEntry* e, const char* fake_id) { 1072 void FakeSync(MutableEntry* e, const char* fake_id) {
1073 e->Put(IS_UNSYNCED, false); 1073 e->Put(IS_UNSYNCED, false);
1074 e->Put(BASE_VERSION, 2); 1074 e->Put(BASE_VERSION, 2);
1075 e->Put(ID, Id::CreateFromServerId(fake_id)); 1075 e->Put(ID, Id::CreateFromServerId(fake_id));
1076 } 1076 }
1077 1077
1078 TEST_F(SyncableDirectoryTest, Bug1509232) { 1078 TEST_F(SyncableDirectoryTest, Bug1509232) {
1079 const PathString a = PSTR("alpha"); 1079 const string a = "alpha";
1080 const Id entry_id = dir_.get()->NextId(); 1080 const Id entry_id = dir_.get()->NextId();
1081 CreateEntry(a, entry_id); 1081 CreateEntry(a, entry_id);
1082 { 1082 {
1083 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); 1083 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__);
1084 MutableEntry e(&trans, GET_BY_ID, entry_id); 1084 MutableEntry e(&trans, GET_BY_ID, entry_id);
1085 ASSERT_TRUE(e.good()); 1085 ASSERT_TRUE(e.good());
1086 ExtendedAttributeKey key(e.Get(META_HANDLE), PSTR("resourcefork")); 1086 ExtendedAttributeKey key(e.Get(META_HANDLE), "resourcefork");
1087 MutableExtendedAttribute ext(&trans, CREATE, key); 1087 MutableExtendedAttribute ext(&trans, CREATE, key);
1088 ASSERT_TRUE(ext.good()); 1088 ASSERT_TRUE(ext.good());
1089 const char value[] = "stuff"; 1089 const char value[] = "stuff";
1090 Blob value_blob(value, value + arraysize(value)); 1090 Blob value_blob(value, value + arraysize(value));
1091 ext.mutable_value()->swap(value_blob); 1091 ext.mutable_value()->swap(value_blob);
1092 ext.delete_attribute(); 1092 ext.delete_attribute();
1093 } 1093 }
1094 // This call to SaveChanges used to CHECK fail. 1094 // This call to SaveChanges used to CHECK fail.
1095 dir_.get()->SaveChanges(); 1095 dir_.get()->SaveChanges();
1096 } 1096 }
1097 1097
1098 } // namespace 1098 } // namespace
1099 } // namespace syncable 1099 } // namespace syncable
OLDNEW
« no previous file with comments | « chrome/browser/sync/syncable/syncable.cc ('k') | chrome/browser/sync/util/character_set_converters.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698