OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/sync/engine/syncer_proto_util.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/compiler_specific.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/time.h" | |
13 #include "chrome/browser/sync/engine/syncproto.h" | |
14 #include "chrome/browser/sync/sessions/session_state.h" | |
15 #include "chrome/browser/sync/sessions/sync_session_context.h" | |
16 #include "chrome/browser/sync/syncable/blob.h" | |
17 #include "chrome/browser/sync/syncable/model_type_test_util.h" | |
18 #include "chrome/browser/sync/syncable/syncable.h" | |
19 #include "chrome/browser/sync/test/engine/mock_connection_manager.h" | |
20 #include "chrome/browser/sync/test/engine/test_directory_setter_upper.h" | |
21 #include "sync/protocol/bookmark_specifics.pb.h" | |
22 #include "sync/protocol/password_specifics.pb.h" | |
23 #include "sync/protocol/sync.pb.h" | |
24 #include "sync/protocol/sync_enums.pb.h" | |
25 | |
26 #include "testing/gtest/include/gtest/gtest.h" | |
27 | |
28 using syncable::Blob; | |
29 using ::testing::_; | |
30 | |
31 namespace browser_sync { | |
32 using sessions::SyncSessionContext; | |
33 | |
34 class MockSyncSessionContext : public SyncSessionContext { | |
35 public: | |
36 MockSyncSessionContext() {} | |
37 ~MockSyncSessionContext() {} | |
38 MOCK_METHOD2(SetUnthrottleTime, void(syncable::ModelTypeSet, | |
39 const base::TimeTicks&)); | |
40 }; | |
41 | |
42 class MockDelegate : public sessions::SyncSession::Delegate { | |
43 public: | |
44 MockDelegate() {} | |
45 ~MockDelegate() {} | |
46 | |
47 MOCK_METHOD0(IsSyncingCurrentlySilenced, bool()); | |
48 MOCK_METHOD1(OnReceivedShortPollIntervalUpdate, void(const base::TimeDelta&)); | |
49 MOCK_METHOD1(OnReceivedLongPollIntervalUpdate ,void(const base::TimeDelta&)); | |
50 MOCK_METHOD1(OnReceivedSessionsCommitDelay, void(const base::TimeDelta&)); | |
51 MOCK_METHOD1(OnSyncProtocolError, void(const sessions::SyncSessionSnapshot&)); | |
52 MOCK_METHOD0(OnShouldStopSyncingPermanently, void()); | |
53 MOCK_METHOD1(OnSilencedUntil, void(const base::TimeTicks&)); | |
54 }; | |
55 | |
56 TEST(SyncerProtoUtil, TestBlobToProtocolBufferBytesUtilityFunctions) { | |
57 unsigned char test_data1[] = {1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 4, 2, 9}; | |
58 unsigned char test_data2[] = {1, 99, 3, 4, 5, 6, 7, 8, 0, 1, 4, 2, 9}; | |
59 unsigned char test_data3[] = {99, 2, 3, 4, 5, 6, 7, 8}; | |
60 | |
61 syncable::Blob test_blob1, test_blob2, test_blob3; | |
62 for (size_t i = 0; i < arraysize(test_data1); ++i) | |
63 test_blob1.push_back(test_data1[i]); | |
64 for (size_t i = 0; i < arraysize(test_data2); ++i) | |
65 test_blob2.push_back(test_data2[i]); | |
66 for (size_t i = 0; i < arraysize(test_data3); ++i) | |
67 test_blob3.push_back(test_data3[i]); | |
68 | |
69 std::string test_message1(reinterpret_cast<char*>(test_data1), | |
70 arraysize(test_data1)); | |
71 std::string test_message2(reinterpret_cast<char*>(test_data2), | |
72 arraysize(test_data2)); | |
73 std::string test_message3(reinterpret_cast<char*>(test_data3), | |
74 arraysize(test_data3)); | |
75 | |
76 EXPECT_TRUE(SyncerProtoUtil::ProtoBytesEqualsBlob(test_message1, | |
77 test_blob1)); | |
78 EXPECT_FALSE(SyncerProtoUtil::ProtoBytesEqualsBlob(test_message1, | |
79 test_blob2)); | |
80 EXPECT_FALSE(SyncerProtoUtil::ProtoBytesEqualsBlob(test_message1, | |
81 test_blob3)); | |
82 EXPECT_FALSE(SyncerProtoUtil::ProtoBytesEqualsBlob(test_message2, | |
83 test_blob1)); | |
84 EXPECT_TRUE(SyncerProtoUtil::ProtoBytesEqualsBlob(test_message2, | |
85 test_blob2)); | |
86 EXPECT_FALSE(SyncerProtoUtil::ProtoBytesEqualsBlob(test_message2, | |
87 test_blob3)); | |
88 EXPECT_FALSE(SyncerProtoUtil::ProtoBytesEqualsBlob(test_message3, | |
89 test_blob1)); | |
90 EXPECT_FALSE(SyncerProtoUtil::ProtoBytesEqualsBlob(test_message3, | |
91 test_blob2)); | |
92 EXPECT_TRUE(SyncerProtoUtil::ProtoBytesEqualsBlob(test_message3, | |
93 test_blob3)); | |
94 | |
95 Blob blob1_copy; | |
96 EXPECT_FALSE(SyncerProtoUtil::ProtoBytesEqualsBlob(test_message1, | |
97 blob1_copy)); | |
98 SyncerProtoUtil::CopyProtoBytesIntoBlob(test_message1, &blob1_copy); | |
99 EXPECT_TRUE(SyncerProtoUtil::ProtoBytesEqualsBlob(test_message1, | |
100 blob1_copy)); | |
101 | |
102 std::string message2_copy; | |
103 EXPECT_FALSE(SyncerProtoUtil::ProtoBytesEqualsBlob(message2_copy, | |
104 test_blob2)); | |
105 SyncerProtoUtil::CopyBlobIntoProtoBytes(test_blob2, &message2_copy); | |
106 EXPECT_TRUE(SyncerProtoUtil::ProtoBytesEqualsBlob(message2_copy, | |
107 test_blob2)); | |
108 } | |
109 | |
110 // Tests NameFromSyncEntity and NameFromCommitEntryResponse when only the name | |
111 // field is provided. | |
112 TEST(SyncerProtoUtil, NameExtractionOneName) { | |
113 SyncEntity one_name_entity; | |
114 CommitResponse_EntryResponse one_name_response; | |
115 | |
116 const std::string one_name_string("Eggheadednesses"); | |
117 one_name_entity.set_name(one_name_string); | |
118 one_name_response.set_name(one_name_string); | |
119 | |
120 const std::string name_a = | |
121 SyncerProtoUtil::NameFromSyncEntity(one_name_entity); | |
122 EXPECT_EQ(one_name_string, name_a); | |
123 } | |
124 | |
125 TEST(SyncerProtoUtil, NameExtractionOneUniqueName) { | |
126 SyncEntity one_name_entity; | |
127 CommitResponse_EntryResponse one_name_response; | |
128 | |
129 const std::string one_name_string("Eggheadednesses"); | |
130 | |
131 one_name_entity.set_non_unique_name(one_name_string); | |
132 one_name_response.set_non_unique_name(one_name_string); | |
133 | |
134 const std::string name_a = | |
135 SyncerProtoUtil::NameFromSyncEntity(one_name_entity); | |
136 EXPECT_EQ(one_name_string, name_a); | |
137 } | |
138 | |
139 // Tests NameFromSyncEntity and NameFromCommitEntryResponse when both the name | |
140 // field and the non_unique_name fields are provided. | |
141 // Should prioritize non_unique_name. | |
142 TEST(SyncerProtoUtil, NameExtractionTwoNames) { | |
143 SyncEntity two_name_entity; | |
144 CommitResponse_EntryResponse two_name_response; | |
145 | |
146 const std::string neuro("Neuroanatomists"); | |
147 const std::string oxyphen("Oxyphenbutazone"); | |
148 | |
149 two_name_entity.set_name(oxyphen); | |
150 two_name_entity.set_non_unique_name(neuro); | |
151 | |
152 two_name_response.set_name(oxyphen); | |
153 two_name_response.set_non_unique_name(neuro); | |
154 | |
155 const std::string name_a = | |
156 SyncerProtoUtil::NameFromSyncEntity(two_name_entity); | |
157 EXPECT_EQ(neuro, name_a); | |
158 } | |
159 | |
160 class SyncerProtoUtilTest : public testing::Test { | |
161 public: | |
162 virtual void SetUp() { | |
163 dir_maker_.SetUp(); | |
164 } | |
165 | |
166 virtual void TearDown() { | |
167 dir_maker_.TearDown(); | |
168 } | |
169 | |
170 syncable::Directory* directory() { | |
171 return dir_maker_.directory(); | |
172 } | |
173 | |
174 protected: | |
175 MessageLoop message_loop_; | |
176 TestDirectorySetterUpper dir_maker_; | |
177 }; | |
178 | |
179 TEST_F(SyncerProtoUtilTest, VerifyResponseBirthday) { | |
180 // Both sides empty | |
181 EXPECT_TRUE(directory()->store_birthday().empty()); | |
182 ClientToServerResponse response; | |
183 EXPECT_FALSE(SyncerProtoUtil::VerifyResponseBirthday(directory(), &response)); | |
184 | |
185 // Remote set, local empty | |
186 response.set_store_birthday("flan"); | |
187 EXPECT_TRUE(SyncerProtoUtil::VerifyResponseBirthday(directory(), &response)); | |
188 EXPECT_EQ(directory()->store_birthday(), "flan"); | |
189 | |
190 // Remote empty, local set. | |
191 response.clear_store_birthday(); | |
192 EXPECT_TRUE(SyncerProtoUtil::VerifyResponseBirthday(directory(), &response)); | |
193 EXPECT_EQ(directory()->store_birthday(), "flan"); | |
194 | |
195 // Doesn't match | |
196 response.set_store_birthday("meat"); | |
197 EXPECT_FALSE(SyncerProtoUtil::VerifyResponseBirthday(directory(), &response)); | |
198 | |
199 response.set_error_code(sync_pb::SyncEnums::CLEAR_PENDING); | |
200 EXPECT_FALSE(SyncerProtoUtil::VerifyResponseBirthday(directory(), &response)); | |
201 } | |
202 | |
203 TEST_F(SyncerProtoUtilTest, AddRequestBirthday) { | |
204 EXPECT_TRUE(directory()->store_birthday().empty()); | |
205 ClientToServerMessage msg; | |
206 SyncerProtoUtil::AddRequestBirthday(directory(), &msg); | |
207 EXPECT_FALSE(msg.has_store_birthday()); | |
208 | |
209 directory()->set_store_birthday("meat"); | |
210 SyncerProtoUtil::AddRequestBirthday(directory(), &msg); | |
211 EXPECT_EQ(msg.store_birthday(), "meat"); | |
212 } | |
213 | |
214 class DummyConnectionManager : public browser_sync::ServerConnectionManager { | |
215 public: | |
216 DummyConnectionManager() | |
217 : ServerConnectionManager("unused", 0, false, "version"), | |
218 send_error_(false), | |
219 access_denied_(false) {} | |
220 | |
221 virtual ~DummyConnectionManager() {} | |
222 virtual bool PostBufferWithCachedAuth( | |
223 PostBufferParams* params, | |
224 ScopedServerStatusWatcher* watcher) OVERRIDE { | |
225 if (send_error_) { | |
226 return false; | |
227 } | |
228 | |
229 ClientToServerResponse response; | |
230 if (access_denied_) { | |
231 response.set_error_code(sync_pb::SyncEnums::ACCESS_DENIED); | |
232 } | |
233 response.SerializeToString(¶ms->buffer_out); | |
234 | |
235 return true; | |
236 } | |
237 | |
238 void set_send_error(bool send) { | |
239 send_error_ = send; | |
240 } | |
241 | |
242 void set_access_denied(bool denied) { | |
243 access_denied_ = denied; | |
244 } | |
245 | |
246 private: | |
247 bool send_error_; | |
248 bool access_denied_; | |
249 }; | |
250 | |
251 TEST_F(SyncerProtoUtilTest, PostAndProcessHeaders) { | |
252 DummyConnectionManager dcm; | |
253 ClientToServerMessage msg; | |
254 msg.set_share("required"); | |
255 msg.set_message_contents(ClientToServerMessage::GET_UPDATES); | |
256 ClientToServerResponse response; | |
257 | |
258 dcm.set_send_error(true); | |
259 EXPECT_FALSE(SyncerProtoUtil::PostAndProcessHeaders(&dcm, NULL, | |
260 msg, &response)); | |
261 | |
262 dcm.set_send_error(false); | |
263 EXPECT_TRUE(SyncerProtoUtil::PostAndProcessHeaders(&dcm, NULL, | |
264 msg, &response)); | |
265 | |
266 dcm.set_access_denied(true); | |
267 EXPECT_FALSE(SyncerProtoUtil::PostAndProcessHeaders(&dcm, NULL, | |
268 msg, &response)); | |
269 } | |
270 | |
271 TEST_F(SyncerProtoUtilTest, HandleThrottlingWithDatatypes) { | |
272 MockSyncSessionContext context; | |
273 SyncProtocolError error; | |
274 error.error_type = browser_sync::THROTTLED; | |
275 syncable::ModelTypeSet types; | |
276 types.Put(syncable::BOOKMARKS); | |
277 types.Put(syncable::PASSWORDS); | |
278 error.error_data_types = types; | |
279 | |
280 base::TimeTicks ticks = base::TimeTicks::Now(); | |
281 | |
282 EXPECT_CALL(context, SetUnthrottleTime(HasModelTypes(types), ticks)); | |
283 | |
284 SyncerProtoUtil::HandleThrottleError(error, ticks, &context, NULL); | |
285 } | |
286 | |
287 TEST_F(SyncerProtoUtilTest, HandleThrottlingNoDatatypes) { | |
288 MockDelegate delegate; | |
289 SyncProtocolError error; | |
290 error.error_type = browser_sync::THROTTLED; | |
291 | |
292 base::TimeTicks ticks = base::TimeTicks::Now(); | |
293 | |
294 EXPECT_CALL(delegate, OnSilencedUntil(ticks)); | |
295 | |
296 SyncerProtoUtil::HandleThrottleError(error, ticks, NULL, &delegate); | |
297 } | |
298 } // namespace browser_sync | |
OLD | NEW |