OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "sync/engine/syncer_util.h" | 5 #include "sync/engine/syncer_util.h" |
6 | 6 |
7 #include "base/rand_util.h" | 7 #include "base/rand_util.h" |
8 #include "sync/internal_api/public/base/unique_position.h" | 8 #include "sync/internal_api/public/base/unique_position.h" |
| 9 #include "sync/internal_api/public/test/test_entry_factory.h" |
9 #include "sync/protocol/sync.pb.h" | 10 #include "sync/protocol/sync.pb.h" |
| 11 #include "sync/syncable/mutable_entry.h" |
| 12 #include "sync/syncable/syncable_write_transaction.h" |
| 13 #include "sync/test/engine/test_directory_setter_upper.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
11 | 15 |
12 namespace syncer { | 16 namespace syncer { |
13 | 17 |
14 class GetUpdatePositionTest : public ::testing::Test { | 18 class GetUpdatePositionTest : public ::testing::Test { |
15 public: | 19 public: |
| 20 virtual void SetUp() { |
| 21 dir_maker_.SetUp(); |
| 22 entry_factory_.reset(new TestEntryFactory(directory())); |
| 23 } |
| 24 |
| 25 virtual void TearDown() { |
| 26 dir_maker_.TearDown(); |
| 27 } |
| 28 |
| 29 syncable::Directory* directory() { |
| 30 return dir_maker_.directory(); |
| 31 } |
| 32 |
| 33 TestEntryFactory* entry_factory() { |
| 34 return entry_factory_.get(); |
| 35 } |
| 36 |
16 GetUpdatePositionTest() { | 37 GetUpdatePositionTest() { |
17 InitUpdate(); | 38 InitUpdate(); |
18 | 39 |
19 // Init test_position to some valid position value, but don't assign | 40 // Init test_position to some valid position value, but don't assign |
20 // it to the update just yet. | 41 // it to the update just yet. |
21 std::string pos_suffix = UniquePosition::RandomSuffix(); | 42 std::string pos_suffix = UniquePosition::RandomSuffix(); |
22 test_position = UniquePosition::InitialPosition(pos_suffix); | 43 test_position = UniquePosition::InitialPosition(pos_suffix); |
23 } | 44 } |
24 | 45 |
25 void InitUpdate() { | 46 void InitUpdate() { |
(...skipping 16 matching lines...) Expand all Loading... |
42 void InitProtoPosition() { | 63 void InitProtoPosition() { |
43 test_position.ToProto(update.mutable_unique_position()); | 64 test_position.ToProto(update.mutable_unique_position()); |
44 } | 65 } |
45 | 66 |
46 void InitInt64Position(int64 pos_value) { | 67 void InitInt64Position(int64 pos_value) { |
47 update.set_position_in_parent(pos_value); | 68 update.set_position_in_parent(pos_value); |
48 } | 69 } |
49 | 70 |
50 sync_pb::SyncEntity update; | 71 sync_pb::SyncEntity update; |
51 UniquePosition test_position; | 72 UniquePosition test_position; |
| 73 base::MessageLoop message_loop_; |
| 74 TestDirectorySetterUpper dir_maker_; |
| 75 scoped_ptr<TestEntryFactory> entry_factory_; |
52 }; | 76 }; |
53 | 77 |
54 // Generate a suffix from originator client GUID and client-assigned ID. These | 78 // Generate a suffix from originator client GUID and client-assigned ID. These |
55 // values should always be present in updates sent down to the client, and | 79 // values should always be present in updates sent down to the client, and |
56 // combine to create a globally unique value. | 80 // combine to create a globally unique value. |
57 TEST_F(GetUpdatePositionTest, SuffixFromUpdate) { | 81 TEST_F(GetUpdatePositionTest, SuffixFromUpdate) { |
58 InitSuffixIngredients(); | 82 InitSuffixIngredients(); |
59 | 83 |
60 // Expect suffix is valid and consistent. | 84 // Expect suffix is valid and consistent. |
61 std::string suffix1 = GetUniqueBookmarkTagFromUpdate(update); | 85 std::string suffix1 = GetUniqueBookmarkTagFromUpdate(update); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 141 |
118 TEST_F(GetUpdatePositionTest, FromNothing) { | 142 TEST_F(GetUpdatePositionTest, FromNothing) { |
119 // Init none of the ingredients necessary to make a position. | 143 // Init none of the ingredients necessary to make a position. |
120 // Verify we still generate a valid position locally. | 144 // Verify we still generate a valid position locally. |
121 | 145 |
122 std::string suffix = GetUniqueBookmarkTagFromUpdate(update); | 146 std::string suffix = GetUniqueBookmarkTagFromUpdate(update); |
123 UniquePosition pos = GetUpdatePosition(update, suffix); | 147 UniquePosition pos = GetUpdatePosition(update, suffix); |
124 EXPECT_TRUE(pos.IsValid()); | 148 EXPECT_TRUE(pos.IsValid()); |
125 } | 149 } |
126 | 150 |
| 151 namespace { |
| 152 |
| 153 sync_pb::EntitySpecifics DefaultBookmarkSpecifics() { |
| 154 sync_pb::EntitySpecifics result; |
| 155 AddDefaultFieldValue(BOOKMARKS, &result); |
| 156 return result; |
| 157 } |
| 158 |
| 159 } // namespace |
| 160 |
| 161 // Checks that whole cycle of unique_position updating from |
| 162 // server works fine and does not browser crash. |
| 163 TEST_F(GetUpdatePositionTest, UpdateServerFieldsFromUpdateTest) { |
| 164 InitSuffixIngredients(); // Initialize update with valid data. |
| 165 |
| 166 std::string root_server_id = syncable::GetNullId().GetServerId(); |
| 167 int64 handle = entry_factory()->CreateUnappliedNewBookmarkItemWithParent( |
| 168 "I", DefaultBookmarkSpecifics(), root_server_id); |
| 169 |
| 170 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory()); |
| 171 syncable::MutableEntry target(&trans, syncable::GET_BY_HANDLE, handle); |
| 172 |
| 173 // Before update, target has invalid bookmark tag and unique position. |
| 174 EXPECT_FALSE(UniquePosition::IsValidSuffix(target.GetUniqueBookmarkTag())); |
| 175 EXPECT_FALSE(target.GetServerUniquePosition().IsValid()); |
| 176 UpdateServerFieldsFromUpdate(&target, update, "name"); |
| 177 |
| 178 // After update, target has valid bookmark tag and unique position. |
| 179 EXPECT_TRUE(UniquePosition::IsValidSuffix(target.GetUniqueBookmarkTag())); |
| 180 EXPECT_TRUE(target.GetServerUniquePosition().IsValid()); |
| 181 } |
| 182 |
| 183 // Checks that whole cycle of unique_position updating does not |
| 184 // browser crash even data from server is invalid. |
| 185 // It looks like server bug, but browser should not crash and work further. |
| 186 TEST_F(GetUpdatePositionTest, UpdateServerFieldsFromInvalidUpdateTest) { |
| 187 // Do not initialize data in update, update is invalid. |
| 188 |
| 189 std::string root_server_id = syncable::GetNullId().GetServerId(); |
| 190 int64 handle = entry_factory()->CreateUnappliedNewBookmarkItemWithParent( |
| 191 "I", DefaultBookmarkSpecifics(), root_server_id); |
| 192 |
| 193 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory()); |
| 194 syncable::MutableEntry target(&trans, syncable::GET_BY_HANDLE, handle); |
| 195 |
| 196 // Before update, target has invalid bookmark tag and unique position. |
| 197 EXPECT_FALSE(UniquePosition::IsValidSuffix(target.GetUniqueBookmarkTag())); |
| 198 EXPECT_FALSE(target.GetServerUniquePosition().IsValid()); |
| 199 UpdateServerFieldsFromUpdate(&target, update, "name"); |
| 200 |
| 201 // After update, target has valid bookmark tag and unique position. |
| 202 EXPECT_TRUE(UniquePosition::IsValidSuffix(target.GetUniqueBookmarkTag())); |
| 203 EXPECT_TRUE(target.GetServerUniquePosition().IsValid()); |
| 204 } |
| 205 |
127 } // namespace syncer | 206 } // namespace syncer |
OLD | NEW |