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

Side by Side Diff: sync/engine/syncer_util_unittest.cc

Issue 278153002: sync: Improve handling of bad UniquePositions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Testing receipt of invalid position updates Created 6 years, 7 months 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
(Empty)
1 // Copyright 2012 The Chromium Authors. All rights reserved.
maniscalco 2014/05/12 19:21:50 nit: old copyright notice
rlarocque 2014/05/12 20:26:13 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "sync/engine/syncer_util.h"
6
7 #include "base/rand_util.h"
8 #include "sync/internal_api/public/base/unique_position.h"
9 #include "sync/protocol/sync.pb.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace syncer {
13
14 class GetUpdatePositionTest : public ::testing::Test {
15 public:
16 GetUpdatePositionTest() {
17 InitUpdate();
18
19 // Init test_position to some valid position value, but don't assign
20 // it to the update just yet.
21 std::string pos_suffix = RandSuffix();
22 test_position = UniquePosition::InitialPosition(pos_suffix);
23 }
24
25 void InitUpdate() {
26 update.set_id_string("I");
27 update.set_parent_id_string("P");
28 update.set_version(10);
29 update.set_mtime(100);
30 update.set_ctime(100);
31 update.set_deleted(false);
32 update.mutable_specifics()->mutable_bookmark()->set_title("Chrome");
33 update.mutable_specifics()->mutable_bookmark()->
34 set_url("https://www.chrome.com");
35 }
36
37 void InitSuffixIngredients() {
38 update.set_originator_cache_guid("CacheGUID");
39 update.set_originator_client_item_id("OrigID");
40 }
41
42 void InitProtoPosition() {
43 test_position.ToProto(update.mutable_unique_position());
44 }
45
46 void InitInt64Position(int64 pos_value) {
47 update.set_position_in_parent(pos_value);
48 }
49
50 static std::string RandSuffix() {
51 return base::RandBytesAsString(UniquePosition::kSuffixLength);
52 }
53
54 sync_pb::SyncEntity update;
55 UniquePosition test_position;
56 };
57
58 // Generate a suffix from originator client GUID an original client-assigned
maniscalco 2014/05/12 19:21:50 s/GUID an original/GUID and an original/ ?
rlarocque 2014/05/12 20:26:13 Should have been s/an/and/. I've rewritten it sli
59 // ID. These values should always be present in updates sent down to the
maniscalco 2014/05/12 19:21:50 s/down to/down from/ ?
rlarocque 2014/05/12 20:26:13 s/server/client.
60 // server, and combine to create a globally unique value.
61 TEST_F(GetUpdatePositionTest, SuffixFromUpdate) {
62 InitSuffixIngredients();
63
64 // Expect suffix is valid and consistent.
65 std::string suffix1 = GetUniqueBookmarkTagFromUpdate(update);
66 std::string suffix2 = GetUniqueBookmarkTagFromUpdate(update);
67
68 EXPECT_EQ(suffix1, suffix2);
69 EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix1));
70 }
71
72 // Receive an update without the ingredients used to make a consistent suffix.
73 //
74 // The server should never send us an update like this. If it does,
75 // that's a bug and it needs to be fixed. Still, we'd like to not
76 // crash and have fairly reasonable results in this scenario.
77 TEST_F(GetUpdatePositionTest, SuffixFromRandom) {
78 // Intentonally do not call InitSuffixIngredients()
79
80 // Expect suffix is valid but inconsistent.
81 std::string suffix1 = GetUniqueBookmarkTagFromUpdate(update);
82 std::string suffix2 = GetUniqueBookmarkTagFromUpdate(update);
83
84 EXPECT_NE(suffix1, suffix2);
85 EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix1));
86 EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix2));
87 }
88
89 TEST_F(GetUpdatePositionTest, FromInt64) {
90 InitSuffixIngredients();
91 InitInt64Position(10);
92
93 std::string suffix = GetUniqueBookmarkTagFromUpdate(update);
94
95 // Expect the result is valid.
96 UniquePosition pos = GetUpdatePosition(update, suffix);
97 EXPECT_TRUE(pos.IsValid());
98
99 // Expect the position had some effect on ordering.
100 EXPECT_TRUE(pos.LessThan(UniquePosition::FromInt64(11, RandSuffix())));
101 }
102
103 TEST_F(GetUpdatePositionTest, FromProto) {
104 InitSuffixIngredients();
105 InitInt64Position(10);
106
107 std::string suffix = GetUniqueBookmarkTagFromUpdate(update);
108
109 // The proto position is not set, so we should get one based on the int64.
110 // It should not match the proto we defined in the test harness.
111 UniquePosition int64_pos = GetUpdatePosition(update, suffix);
112 EXPECT_FALSE(int64_pos.Equals(test_position));
113
114 // Move the test harness' position value into the update proto.
115 // Expect that it takes precedence over the int64-based position.
116 InitProtoPosition();
117 UniquePosition pos = GetUpdatePosition(update, suffix);
118 EXPECT_TRUE(pos.Equals(test_position));
119 }
120
121 TEST_F(GetUpdatePositionTest, FromNothing) {
122 // Init none of the ingredients necessary to make a position.
123 // Verify we still generate a valid position locally.
124
125 std::string suffix = GetUniqueBookmarkTagFromUpdate(update);
126 UniquePosition pos = GetUpdatePosition(update, suffix);
127 EXPECT_TRUE(pos.IsValid());
128 }
129
130 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698