Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / generic_change_processor_unittest.cc
1 // Copyright 2013 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/glue/generic_change_processor.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/stringprintf.h"
11 #include "components/sync_driver/data_type_error_handler_mock.h"
12 #include "sync/api/fake_syncable_service.h"
13 #include "sync/api/sync_change.h"
14 #include "sync/api/sync_merge_result.h"
15 #include "sync/internal_api/public/base/model_type.h"
16 #include "sync/internal_api/public/read_node.h"
17 #include "sync/internal_api/public/read_transaction.h"
18 #include "sync/internal_api/public/test/test_user_share.h"
19 #include "sync/internal_api/public/user_share.h"
20 #include "sync/internal_api/public/write_node.h"
21 #include "sync/internal_api/public/write_transaction.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace browser_sync {
25
26 namespace {
27
28 class SyncGenericChangeProcessorTest : public testing::Test {
29  public:
30   // It doesn't matter which type we use.  Just pick one.
31   static const syncer::ModelType kType = syncer::PREFERENCES;
32
33   SyncGenericChangeProcessorTest() :
34       sync_merge_result_(kType),
35       merge_result_ptr_factory_(&sync_merge_result_),
36       syncable_service_ptr_factory_(&fake_syncable_service_) {
37   }
38
39   virtual void SetUp() OVERRIDE {
40     test_user_share_.SetUp();
41     syncer::ModelTypeSet types = syncer::ProtocolTypes();
42     for (syncer::ModelTypeSet::Iterator iter = types.First(); iter.Good();
43          iter.Inc()) {
44       syncer::TestUserShare::CreateRoot(iter.Get(),
45                                         test_user_share_.user_share());
46     }
47     test_user_share_.encryption_handler()->Init();
48     change_processor_.reset(
49         new GenericChangeProcessor(
50             &data_type_error_handler_,
51             syncable_service_ptr_factory_.GetWeakPtr(),
52             merge_result_ptr_factory_.GetWeakPtr(),
53             test_user_share_.user_share()));
54   }
55
56   virtual void TearDown() OVERRIDE {
57     test_user_share_.TearDown();
58   }
59
60   void BuildChildNodes(int n) {
61     syncer::WriteTransaction trans(FROM_HERE, user_share());
62     syncer::ReadNode root(&trans);
63     ASSERT_EQ(syncer::BaseNode::INIT_OK,
64               root.InitByTagLookup(syncer::ModelTypeToRootTag(kType)));
65     for (int i = 0; i < n; ++i) {
66       syncer::WriteNode node(&trans);
67       node.InitUniqueByCreation(kType, root, base::StringPrintf("node%05d", i));
68     }
69   }
70
71   GenericChangeProcessor* change_processor() {
72     return change_processor_.get();
73   }
74
75   syncer::UserShare* user_share() {
76     return test_user_share_.user_share();
77   }
78
79  private:
80   base::MessageLoopForUI loop_;
81
82   syncer::SyncMergeResult sync_merge_result_;
83   base::WeakPtrFactory<syncer::SyncMergeResult> merge_result_ptr_factory_;
84
85   syncer::FakeSyncableService fake_syncable_service_;
86   base::WeakPtrFactory<syncer::FakeSyncableService>
87       syncable_service_ptr_factory_;
88
89   DataTypeErrorHandlerMock data_type_error_handler_;
90   syncer::TestUserShare test_user_share_;
91
92   scoped_ptr<GenericChangeProcessor> change_processor_;
93 };
94
95 // Similar to above, but focused on the method that implements sync/api
96 // interfaces and is hence exposed to datatypes directly.
97 TEST_F(SyncGenericChangeProcessorTest, StressGetAllSyncData) {
98   const int kNumChildNodes = 1000;
99   const int kRepeatCount = 1;
100
101   ASSERT_NO_FATAL_FAILURE(BuildChildNodes(kNumChildNodes));
102
103   for (int i = 0; i < kRepeatCount; ++i) {
104     syncer::SyncDataList sync_data =
105         change_processor()->GetAllSyncData(kType);
106
107     // Start with a simple test.  We can add more in-depth testing later.
108     EXPECT_EQ(static_cast<size_t>(kNumChildNodes), sync_data.size());
109   }
110 }
111
112 TEST_F(SyncGenericChangeProcessorTest, SetGetPasswords) {
113   const int kNumPasswords = 10;
114   sync_pb::PasswordSpecificsData password_data;
115   password_data.set_username_value("user");
116
117   sync_pb::EntitySpecifics password_holder;
118
119   syncer::SyncChangeList change_list;
120   for (int i = 0; i < kNumPasswords; ++i) {
121     password_data.set_password_value(
122         base::StringPrintf("password%i", i));
123     password_holder.mutable_password()->mutable_client_only_encrypted_data()->
124         CopyFrom(password_data);
125     change_list.push_back(
126         syncer::SyncChange(FROM_HERE,
127                            syncer::SyncChange::ACTION_ADD,
128                            syncer::SyncData::CreateLocalData(
129                                base::StringPrintf("tag%i", i),
130                                base::StringPrintf("title%i", i),
131                                password_holder)));
132   }
133
134   ASSERT_FALSE(
135       change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet());
136
137   syncer::SyncDataList password_list(
138       change_processor()->GetAllSyncData(syncer::PASSWORDS));
139
140   ASSERT_EQ(password_list.size(), change_list.size());
141   for (int i = 0; i < kNumPasswords; ++i) {
142     // Verify the password is returned properly.
143     ASSERT_TRUE(password_list[i].GetSpecifics().has_password());
144     ASSERT_TRUE(password_list[i].GetSpecifics().password().
145                     has_client_only_encrypted_data());
146     ASSERT_FALSE(password_list[i].GetSpecifics().password().has_encrypted());
147     const sync_pb::PasswordSpecificsData& sync_password =
148         password_list[i].GetSpecifics().password().client_only_encrypted_data();
149     const sync_pb::PasswordSpecificsData& change_password =
150         change_list[i].sync_data().GetSpecifics().password().
151             client_only_encrypted_data();
152     ASSERT_EQ(sync_password.password_value(), change_password.password_value());
153     ASSERT_EQ(sync_password.username_value(), change_password.username_value());
154
155     // Verify the raw sync data was stored securely.
156     syncer::ReadTransaction read_transaction(FROM_HERE, user_share());
157     syncer::ReadNode node(&read_transaction);
158     ASSERT_EQ(node.InitByClientTagLookup(syncer::PASSWORDS,
159                                          base::StringPrintf("tag%i", i)),
160               syncer::BaseNode::INIT_OK);
161     ASSERT_EQ(node.GetTitle(), "encrypted");
162     const sync_pb::EntitySpecifics& raw_specifics = node.GetEntitySpecifics();
163     ASSERT_TRUE(raw_specifics.has_password());
164     ASSERT_TRUE(raw_specifics.password().has_encrypted());
165     ASSERT_FALSE(raw_specifics.password().has_client_only_encrypted_data());
166   }
167 }
168
169 TEST_F(SyncGenericChangeProcessorTest, UpdatePasswords) {
170   const int kNumPasswords = 10;
171   sync_pb::PasswordSpecificsData password_data;
172   password_data.set_username_value("user");
173
174   sync_pb::EntitySpecifics password_holder;
175
176   syncer::SyncChangeList change_list;
177   syncer::SyncChangeList change_list2;
178   for (int i = 0; i < kNumPasswords; ++i) {
179     password_data.set_password_value(
180         base::StringPrintf("password%i", i));
181     password_holder.mutable_password()->mutable_client_only_encrypted_data()->
182         CopyFrom(password_data);
183     change_list.push_back(
184         syncer::SyncChange(FROM_HERE,
185                            syncer::SyncChange::ACTION_ADD,
186                            syncer::SyncData::CreateLocalData(
187                                base::StringPrintf("tag%i", i),
188                                base::StringPrintf("title%i", i),
189                                password_holder)));
190     password_data.set_password_value(
191         base::StringPrintf("password_m%i", i));
192     password_holder.mutable_password()->mutable_client_only_encrypted_data()->
193         CopyFrom(password_data);
194     change_list2.push_back(
195         syncer::SyncChange(FROM_HERE,
196                            syncer::SyncChange::ACTION_UPDATE,
197                            syncer::SyncData::CreateLocalData(
198                                base::StringPrintf("tag%i", i),
199                                base::StringPrintf("title_m%i", i),
200                                password_holder)));
201   }
202
203   ASSERT_FALSE(
204       change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet());
205   ASSERT_FALSE(
206       change_processor()->ProcessSyncChanges(FROM_HERE, change_list2).IsSet());
207
208   syncer::SyncDataList password_list(
209       change_processor()->GetAllSyncData(syncer::PASSWORDS));
210
211   ASSERT_EQ(password_list.size(), change_list2.size());
212   for (int i = 0; i < kNumPasswords; ++i) {
213     // Verify the password is returned properly.
214     ASSERT_TRUE(password_list[i].GetSpecifics().has_password());
215     ASSERT_TRUE(password_list[i].GetSpecifics().password().
216                     has_client_only_encrypted_data());
217     ASSERT_FALSE(password_list[i].GetSpecifics().password().has_encrypted());
218     const sync_pb::PasswordSpecificsData& sync_password =
219         password_list[i].GetSpecifics().password().client_only_encrypted_data();
220     const sync_pb::PasswordSpecificsData& change_password =
221         change_list2[i].sync_data().GetSpecifics().password().
222             client_only_encrypted_data();
223     ASSERT_EQ(sync_password.password_value(), change_password.password_value());
224     ASSERT_EQ(sync_password.username_value(), change_password.username_value());
225
226     // Verify the raw sync data was stored securely.
227     syncer::ReadTransaction read_transaction(FROM_HERE, user_share());
228     syncer::ReadNode node(&read_transaction);
229     ASSERT_EQ(node.InitByClientTagLookup(syncer::PASSWORDS,
230                                          base::StringPrintf("tag%i", i)),
231               syncer::BaseNode::INIT_OK);
232     ASSERT_EQ(node.GetTitle(), "encrypted");
233     const sync_pb::EntitySpecifics& raw_specifics = node.GetEntitySpecifics();
234     ASSERT_TRUE(raw_specifics.has_password());
235     ASSERT_TRUE(raw_specifics.password().has_encrypted());
236     ASSERT_FALSE(raw_specifics.password().has_client_only_encrypted_data());
237   }
238 }
239
240 }  // namespace
241
242 }  // namespace browser_sync
243