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
3 // found in the LICENSE file.
5 #include "sync/engine/syncer_util.h"
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"
14 class GetUpdatePositionTest
: public ::testing::Test
{
16 GetUpdatePositionTest() {
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
= UniquePosition::RandomSuffix();
22 test_position
= UniquePosition::InitialPosition(pos_suffix
);
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");
37 void InitSuffixIngredients() {
38 update
.set_originator_cache_guid("CacheGUID");
39 update
.set_originator_client_item_id("OrigID");
42 void InitProtoPosition() {
43 test_position
.ToProto(update
.mutable_unique_position());
46 void InitInt64Position(int64 pos_value
) {
47 update
.set_position_in_parent(pos_value
);
50 sync_pb::SyncEntity update
;
51 UniquePosition test_position
;
54 // 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
56 // combine to create a globally unique value.
57 TEST_F(GetUpdatePositionTest
, SuffixFromUpdate
) {
58 InitSuffixIngredients();
60 // Expect suffix is valid and consistent.
61 std::string suffix1
= GetUniqueBookmarkTagFromUpdate(update
);
62 std::string suffix2
= GetUniqueBookmarkTagFromUpdate(update
);
64 EXPECT_EQ(suffix1
, suffix2
);
65 EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix1
));
68 // Receive an update without the ingredients used to make a consistent suffix.
70 // The server should never send us an update like this. If it does,
71 // that's a bug and it needs to be fixed. Still, we'd like to not
72 // crash and have fairly reasonable results in this scenario.
73 TEST_F(GetUpdatePositionTest
, SuffixFromRandom
) {
74 // Intentonally do not call InitSuffixIngredients()
76 // Expect suffix is valid but inconsistent.
77 std::string suffix1
= GetUniqueBookmarkTagFromUpdate(update
);
78 std::string suffix2
= GetUniqueBookmarkTagFromUpdate(update
);
80 EXPECT_NE(suffix1
, suffix2
);
81 EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix1
));
82 EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix2
));
85 TEST_F(GetUpdatePositionTest
, FromInt64
) {
86 InitSuffixIngredients();
87 InitInt64Position(10);
89 std::string suffix
= GetUniqueBookmarkTagFromUpdate(update
);
91 // Expect the result is valid.
92 UniquePosition pos
= GetUpdatePosition(update
, suffix
);
93 EXPECT_TRUE(pos
.IsValid());
95 // Expect the position had some effect on ordering.
96 EXPECT_TRUE(pos
.LessThan(
97 UniquePosition::FromInt64(11, UniquePosition::RandomSuffix())));
100 TEST_F(GetUpdatePositionTest
, FromProto
) {
101 InitSuffixIngredients();
102 InitInt64Position(10);
104 std::string suffix
= GetUniqueBookmarkTagFromUpdate(update
);
106 // The proto position is not set, so we should get one based on the int64.
107 // It should not match the proto we defined in the test harness.
108 UniquePosition int64_pos
= GetUpdatePosition(update
, suffix
);
109 EXPECT_FALSE(int64_pos
.Equals(test_position
));
111 // Move the test harness' position value into the update proto.
112 // Expect that it takes precedence over the int64-based position.
114 UniquePosition pos
= GetUpdatePosition(update
, suffix
);
115 EXPECT_TRUE(pos
.Equals(test_position
));
118 TEST_F(GetUpdatePositionTest
, FromNothing
) {
119 // Init none of the ingredients necessary to make a position.
120 // Verify we still generate a valid position locally.
122 std::string suffix
= GetUniqueBookmarkTagFromUpdate(update
);
123 UniquePosition pos
= GetUpdatePosition(update
, suffix
);
124 EXPECT_TRUE(pos
.IsValid());
127 } // namespace syncer