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