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.
5 // Keep this file in sync with the .proto files in this directory.
7 #include "sync/protocol/proto_value_conversions.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "sync/internal_api/public/base/model_type.h"
14 #include "sync/protocol/app_notification_specifics.pb.h"
15 #include "sync/protocol/app_setting_specifics.pb.h"
16 #include "sync/protocol/app_specifics.pb.h"
17 #include "sync/protocol/autofill_specifics.pb.h"
18 #include "sync/protocol/bookmark_specifics.pb.h"
19 #include "sync/protocol/device_info_specifics.pb.h"
20 #include "sync/protocol/encryption.pb.h"
21 #include "sync/protocol/experiments_specifics.pb.h"
22 #include "sync/protocol/extension_setting_specifics.pb.h"
23 #include "sync/protocol/extension_specifics.pb.h"
24 #include "sync/protocol/favicon_image_specifics.pb.h"
25 #include "sync/protocol/favicon_tracking_specifics.pb.h"
26 #include "sync/protocol/managed_user_setting_specifics.pb.h"
27 #include "sync/protocol/managed_user_shared_setting_specifics.pb.h"
28 #include "sync/protocol/managed_user_specifics.pb.h"
29 #include "sync/protocol/nigori_specifics.pb.h"
30 #include "sync/protocol/password_specifics.pb.h"
31 #include "sync/protocol/preference_specifics.pb.h"
32 #include "sync/protocol/priority_preference_specifics.pb.h"
33 #include "sync/protocol/search_engine_specifics.pb.h"
34 #include "sync/protocol/session_specifics.pb.h"
35 #include "sync/protocol/sync.pb.h"
36 #include "sync/protocol/theme_specifics.pb.h"
37 #include "sync/protocol/typed_url_specifics.pb.h"
38 #include "sync/protocol/wifi_credential_specifics.pb.h"
39 #include "testing/gtest/include/gtest/gtest.h"
44 class ProtoValueConversionsTest : public testing::Test {
47 void TestSpecificsToValue(
48 base::DictionaryValue* (*specifics_to_value)(const T&)) {
49 const T& specifics(T::default_instance());
50 scoped_ptr<base::DictionaryValue> value(specifics_to_value(specifics));
51 // We can't do much but make sure that this doesn't crash.
55 TEST_F(ProtoValueConversionsTest, ProtoChangeCheck) {
56 // If this number changes, that means we added or removed a data
57 // type. Don't forget to add a unit test for {New
58 // type}SpecificsToValue below.
59 EXPECT_EQ(33, MODEL_TYPE_COUNT);
61 // We'd also like to check if we changed any field in our messages.
62 // However, that's hard to do: sizeof could work, but it's
63 // platform-dependent. default_instance().ByteSize() won't change
64 // for most changes, since most of our fields are optional. So we
65 // just settle for comments in the proto files.
68 TEST_F(ProtoValueConversionsTest, EncryptedDataToValue) {
69 TestSpecificsToValue(EncryptedDataToValue);
72 TEST_F(ProtoValueConversionsTest, SessionHeaderToValue) {
73 TestSpecificsToValue(SessionHeaderToValue);
76 TEST_F(ProtoValueConversionsTest, SessionTabToValue) {
77 TestSpecificsToValue(SessionTabToValue);
80 TEST_F(ProtoValueConversionsTest, SessionWindowToValue) {
81 TestSpecificsToValue(SessionWindowToValue);
84 TEST_F(ProtoValueConversionsTest, TabNavigationToValue) {
85 TestSpecificsToValue(TabNavigationToValue);
88 TEST_F(ProtoValueConversionsTest, NavigationRedirectToValue) {
89 TestSpecificsToValue(NavigationRedirectToValue);
92 TEST_F(ProtoValueConversionsTest, PasswordSpecificsData) {
93 sync_pb::PasswordSpecificsData specifics;
94 specifics.set_password_value("secret");
95 scoped_ptr<base::DictionaryValue> value(
96 PasswordSpecificsDataToValue(specifics));
97 EXPECT_FALSE(value->empty());
98 std::string password_value;
99 EXPECT_TRUE(value->GetString("password_value", &password_value));
100 EXPECT_EQ("<redacted>", password_value);
103 TEST_F(ProtoValueConversionsTest, AppListSpecificsToValue) {
104 TestSpecificsToValue(AppListSpecificsToValue);
107 TEST_F(ProtoValueConversionsTest, AppNotificationToValue) {
108 TestSpecificsToValue(AppNotificationToValue);
111 TEST_F(ProtoValueConversionsTest, AppSettingSpecificsToValue) {
112 sync_pb::AppNotificationSettings specifics;
113 specifics.set_disabled(true);
114 specifics.set_oauth_client_id("some_id_value");
115 scoped_ptr<base::DictionaryValue> value(AppSettingsToValue(specifics));
116 EXPECT_FALSE(value->empty());
117 bool disabled_value = false;
118 std::string oauth_client_id_value;
119 EXPECT_TRUE(value->GetBoolean("disabled", &disabled_value));
120 EXPECT_EQ(true, disabled_value);
121 EXPECT_TRUE(value->GetString("oauth_client_id", &oauth_client_id_value));
122 EXPECT_EQ("some_id_value", oauth_client_id_value);
125 TEST_F(ProtoValueConversionsTest, AppSpecificsToValue) {
126 TestSpecificsToValue(AppSpecificsToValue);
129 TEST_F(ProtoValueConversionsTest, AutofillSpecificsToValue) {
130 TestSpecificsToValue(AutofillSpecificsToValue);
133 TEST_F(ProtoValueConversionsTest, AutofillProfileSpecificsToValue) {
134 TestSpecificsToValue(AutofillProfileSpecificsToValue);
137 TEST_F(ProtoValueConversionsTest, BookmarkSpecificsToValue) {
138 TestSpecificsToValue(BookmarkSpecificsToValue);
141 TEST_F(ProtoValueConversionsTest, BookmarkSpecificsData) {
142 const base::Time creation_time(base::Time::Now());
143 const std::string icon_url = "http://www.google.com/favicon.ico";
144 sync_pb::BookmarkSpecifics specifics;
145 specifics.set_creation_time_us(creation_time.ToInternalValue());
146 specifics.set_icon_url(icon_url);
147 sync_pb::MetaInfo* meta_1 = specifics.add_meta_info();
148 meta_1->set_key("key1");
149 meta_1->set_value("value1");
150 sync_pb::MetaInfo* meta_2 = specifics.add_meta_info();
151 meta_2->set_key("key2");
152 meta_2->set_value("value2");
154 scoped_ptr<base::DictionaryValue> value(BookmarkSpecificsToValue(specifics));
155 EXPECT_FALSE(value->empty());
156 std::string encoded_time;
157 EXPECT_TRUE(value->GetString("creation_time_us", &encoded_time));
158 EXPECT_EQ(base::Int64ToString(creation_time.ToInternalValue()), encoded_time);
159 std::string encoded_icon_url;
160 EXPECT_TRUE(value->GetString("icon_url", &encoded_icon_url));
161 EXPECT_EQ(icon_url, encoded_icon_url);
162 base::ListValue* meta_info_list;
163 ASSERT_TRUE(value->GetList("meta_info", &meta_info_list));
164 EXPECT_EQ(2u, meta_info_list->GetSize());
165 base::DictionaryValue* meta_info;
166 std::string meta_key;
167 std::string meta_value;
168 ASSERT_TRUE(meta_info_list->GetDictionary(0, &meta_info));
169 EXPECT_TRUE(meta_info->GetString("key", &meta_key));
170 EXPECT_TRUE(meta_info->GetString("value", &meta_value));
171 EXPECT_EQ("key1", meta_key);
172 EXPECT_EQ("value1", meta_value);
173 ASSERT_TRUE(meta_info_list->GetDictionary(1, &meta_info));
174 EXPECT_TRUE(meta_info->GetString("key", &meta_key));
175 EXPECT_TRUE(meta_info->GetString("value", &meta_value));
176 EXPECT_EQ("key2", meta_key);
177 EXPECT_EQ("value2", meta_value);
180 TEST_F(ProtoValueConversionsTest, PriorityPreferenceSpecificsToValue) {
181 TestSpecificsToValue(PriorityPreferenceSpecificsToValue);
184 TEST_F(ProtoValueConversionsTest, DeviceInfoSpecificsToValue) {
185 TestSpecificsToValue(DeviceInfoSpecificsToValue);
188 TEST_F(ProtoValueConversionsTest, ExperimentsSpecificsToValue) {
189 TestSpecificsToValue(ExperimentsSpecificsToValue);
192 TEST_F(ProtoValueConversionsTest, ExtensionSettingSpecificsToValue) {
193 TestSpecificsToValue(ExtensionSettingSpecificsToValue);
196 TEST_F(ProtoValueConversionsTest, ExtensionSpecificsToValue) {
197 TestSpecificsToValue(ExtensionSpecificsToValue);
200 TEST_F(ProtoValueConversionsTest, FaviconImageSpecificsToValue) {
201 TestSpecificsToValue(FaviconImageSpecificsToValue);
204 TEST_F(ProtoValueConversionsTest, FaviconTrackingSpecificsToValue) {
205 TestSpecificsToValue(FaviconTrackingSpecificsToValue);
208 TEST_F(ProtoValueConversionsTest, HistoryDeleteDirectiveSpecificsToValue) {
209 TestSpecificsToValue(HistoryDeleteDirectiveSpecificsToValue);
212 TEST_F(ProtoValueConversionsTest, ManagedUserSettingSpecificsToValue) {
213 TestSpecificsToValue(ManagedUserSettingSpecificsToValue);
216 TEST_F(ProtoValueConversionsTest, ManagedUserSpecificsToValue) {
217 TestSpecificsToValue(ManagedUserSpecificsToValue);
220 TEST_F(ProtoValueConversionsTest, ManagedUserSharedSettingSpecificsToValue) {
221 TestSpecificsToValue(ManagedUserSharedSettingSpecificsToValue);
224 TEST_F(ProtoValueConversionsTest, NigoriSpecificsToValue) {
225 TestSpecificsToValue(NigoriSpecificsToValue);
228 TEST_F(ProtoValueConversionsTest, PasswordSpecificsToValue) {
229 TestSpecificsToValue(PasswordSpecificsToValue);
232 TEST_F(ProtoValueConversionsTest, PreferenceSpecificsToValue) {
233 TestSpecificsToValue(PreferenceSpecificsToValue);
236 TEST_F(ProtoValueConversionsTest, SearchEngineSpecificsToValue) {
237 TestSpecificsToValue(SearchEngineSpecificsToValue);
240 TEST_F(ProtoValueConversionsTest, SessionSpecificsToValue) {
241 TestSpecificsToValue(SessionSpecificsToValue);
244 TEST_F(ProtoValueConversionsTest, SyncedNotificationAppInfoSpecificsToValue) {
245 TestSpecificsToValue(SyncedNotificationAppInfoSpecificsToValue);
248 TEST_F(ProtoValueConversionsTest, SyncedNotificationSpecificsToValue) {
249 TestSpecificsToValue(SyncedNotificationSpecificsToValue);
252 TEST_F(ProtoValueConversionsTest, ThemeSpecificsToValue) {
253 TestSpecificsToValue(ThemeSpecificsToValue);
256 TEST_F(ProtoValueConversionsTest, TypedUrlSpecificsToValue) {
257 TestSpecificsToValue(TypedUrlSpecificsToValue);
260 TEST_F(ProtoValueConversionsTest, DictionarySpecificsToValue) {
261 TestSpecificsToValue(DictionarySpecificsToValue);
264 TEST_F(ProtoValueConversionsTest, ArticleSpecificsToValue) {
265 TestSpecificsToValue(ArticleSpecificsToValue);
268 TEST_F(ProtoValueConversionsTest, WifiCredentialSpecificsToValue) {
269 TestSpecificsToValue(WifiCredentialSpecificsToValue);
272 // TODO(akalin): Figure out how to better test EntitySpecificsToValue.
274 TEST_F(ProtoValueConversionsTest, EntitySpecificsToValue) {
275 sync_pb::EntitySpecifics specifics;
276 // Touch the extensions to make sure it shows up in the generated
278 #define SET_FIELD(key) (void)specifics.mutable_##key()
282 SET_FIELD(app_notification);
283 SET_FIELD(app_setting);
286 SET_FIELD(autofill_profile);
288 SET_FIELD(device_info);
289 SET_FIELD(dictionary);
290 SET_FIELD(experiments);
291 SET_FIELD(extension);
292 SET_FIELD(extension_setting);
293 SET_FIELD(favicon_image);
294 SET_FIELD(favicon_tracking);
295 SET_FIELD(history_delete_directive);
296 SET_FIELD(managed_user_setting);
297 SET_FIELD(managed_user_shared_setting);
298 SET_FIELD(managed_user);
301 SET_FIELD(preference);
302 SET_FIELD(priority_preference);
303 SET_FIELD(search_engine);
305 SET_FIELD(synced_notification);
306 SET_FIELD(synced_notification_app_info);
308 SET_FIELD(typed_url);
309 SET_FIELD(wifi_credential);
313 scoped_ptr<base::DictionaryValue> value(EntitySpecificsToValue(specifics));
314 EXPECT_EQ(MODEL_TYPE_COUNT - FIRST_REAL_MODEL_TYPE -
315 (LAST_PROXY_TYPE - FIRST_PROXY_TYPE + 1),
316 static_cast<int>(value->size()));
320 // Returns whether the given value has specifics under the entries in the given
322 bool ValueHasSpecifics(const base::DictionaryValue& value,
323 const std::string& path) {
324 const base::ListValue* entities_list = NULL;
325 const base::DictionaryValue* entry_dictionary = NULL;
326 const base::DictionaryValue* specifics_dictionary = NULL;
328 if (!value.GetList(path, &entities_list))
331 if (!entities_list->GetDictionary(0, &entry_dictionary))
334 return entry_dictionary->GetDictionary("specifics",
335 &specifics_dictionary);
339 // Create a ClientToServerMessage with an EntitySpecifics. Converting it to
340 // a value should respect the |include_specifics| flag.
341 TEST_F(ProtoValueConversionsTest, ClientToServerMessageToValue) {
342 sync_pb::ClientToServerMessage message;
343 sync_pb::CommitMessage* commit_message = message.mutable_commit();
344 sync_pb::SyncEntity* entity = commit_message->add_entries();
345 entity->mutable_specifics();
347 scoped_ptr<base::DictionaryValue> value_with_specifics(
348 ClientToServerMessageToValue(message, true /* include_specifics */));
349 EXPECT_FALSE(value_with_specifics->empty());
350 EXPECT_TRUE(ValueHasSpecifics(*(value_with_specifics.get()),
353 scoped_ptr<base::DictionaryValue> value_without_specifics(
354 ClientToServerMessageToValue(message, false /* include_specifics */));
355 EXPECT_FALSE(value_without_specifics->empty());
356 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics.get()),
360 // Create a ClientToServerResponse with an EntitySpecifics. Converting it to
361 // a value should respect the |include_specifics| flag.
362 TEST_F(ProtoValueConversionsTest, ClientToServerResponseToValue) {
363 sync_pb::ClientToServerResponse message;
364 sync_pb::GetUpdatesResponse* response = message.mutable_get_updates();
365 sync_pb::SyncEntity* entity = response->add_entries();
366 entity->mutable_specifics();
368 scoped_ptr<base::DictionaryValue> value_with_specifics(
369 ClientToServerResponseToValue(message, true /* include_specifics */));
370 EXPECT_FALSE(value_with_specifics->empty());
371 EXPECT_TRUE(ValueHasSpecifics(*(value_with_specifics.get()),
372 "get_updates.entries"));
374 scoped_ptr<base::DictionaryValue> value_without_specifics(
375 ClientToServerResponseToValue(message, false /* include_specifics */));
376 EXPECT_FALSE(value_without_specifics->empty());
377 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics.get()),
378 "get_updates.entries"));
381 TEST_F(ProtoValueConversionsTest, AttachmentIdProtoToValue) {
382 TestSpecificsToValue(AttachmentIdProtoToValue);
386 } // namespace syncer