1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync/protocol/proto_value_conversions.h"
#include <memory>
#include <string>
#include <utility>
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/unique_position.h"
#include "components/sync/protocol/app_setting_specifics.pb.h"
#include "components/sync/protocol/app_specifics.pb.h"
#include "components/sync/protocol/autofill_specifics.pb.h"
#include "components/sync/protocol/bookmark_specifics.pb.h"
#include "components/sync/protocol/collaboration_group_specifics.pb.h"
#include "components/sync/protocol/contact_info_specifics.pb.h"
#include "components/sync/protocol/cookie_specifics.pb.h"
#include "components/sync/protocol/data_type_progress_marker.pb.h"
#include "components/sync/protocol/device_info_specifics.pb.h"
#include "components/sync/protocol/encryption.pb.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/protocol/extension_setting_specifics.pb.h"
#include "components/sync/protocol/extension_specifics.pb.h"
#include "components/sync/protocol/managed_user_setting_specifics.pb.h"
#include "components/sync/protocol/nigori_specifics.pb.h"
#include "components/sync/protocol/os_preference_specifics.pb.h"
#include "components/sync/protocol/os_priority_preference_specifics.pb.h"
#include "components/sync/protocol/password_specifics.pb.h"
#include "components/sync/protocol/preference_specifics.pb.h"
#include "components/sync/protocol/priority_preference_specifics.pb.h"
#include "components/sync/protocol/product_comparison_specifics.pb.h"
#include "components/sync/protocol/search_engine_specifics.pb.h"
#include "components/sync/protocol/session_specifics.pb.h"
#include "components/sync/protocol/sharing_message_specifics.pb.h"
#include "components/sync/protocol/sync.pb.h"
#include "components/sync/protocol/sync_entity.pb.h"
#include "components/sync/protocol/theme_specifics.pb.h"
#include "components/sync/protocol/typed_url_specifics.pb.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
namespace {
using testing::Not;
// Keep this file in sync with the .proto files in this directory.
#define DEFINE_SPECIFICS_TO_VALUE_TEST(Key) \
TEST(ProtoValueConversionsTest, Proto_##Key##_SpecificsToValue) { \
sync_pb::EntitySpecifics specifics; \
specifics.mutable_##Key(); \
base::Value value = EntitySpecificsToValue(specifics); \
ASSERT_TRUE(value.is_dict()); \
EXPECT_EQ(1u, value.GetDict().size()); \
}
// We'd also like to check if we changed any field in our messages. However,
// that's hard to do: sizeof could work, but it's platform-dependent.
// default_instance().ByteSizeLong() won't change for most changes, since most
// of our fields are optional. So we just settle for comments in the proto
// files.
DEFINE_SPECIFICS_TO_VALUE_TEST(encrypted)
static_assert(55 == syncer::GetNumDataTypes(),
"When adding a new field, add a DEFINE_SPECIFICS_TO_VALUE_TEST "
"for your field below, and optionally a test for the specific "
"conversions.");
DEFINE_SPECIFICS_TO_VALUE_TEST(app)
DEFINE_SPECIFICS_TO_VALUE_TEST(app_list)
DEFINE_SPECIFICS_TO_VALUE_TEST(app_setting)
DEFINE_SPECIFICS_TO_VALUE_TEST(arc_package)
DEFINE_SPECIFICS_TO_VALUE_TEST(autofill)
DEFINE_SPECIFICS_TO_VALUE_TEST(autofill_offer)
DEFINE_SPECIFICS_TO_VALUE_TEST(autofill_profile)
DEFINE_SPECIFICS_TO_VALUE_TEST(autofill_wallet)
DEFINE_SPECIFICS_TO_VALUE_TEST(autofill_wallet_credential)
DEFINE_SPECIFICS_TO_VALUE_TEST(autofill_wallet_usage)
DEFINE_SPECIFICS_TO_VALUE_TEST(bookmark)
DEFINE_SPECIFICS_TO_VALUE_TEST(collaboration_group)
DEFINE_SPECIFICS_TO_VALUE_TEST(contact_info)
DEFINE_SPECIFICS_TO_VALUE_TEST(cookie)
DEFINE_SPECIFICS_TO_VALUE_TEST(device_info)
DEFINE_SPECIFICS_TO_VALUE_TEST(dictionary)
DEFINE_SPECIFICS_TO_VALUE_TEST(extension)
DEFINE_SPECIFICS_TO_VALUE_TEST(extension_setting)
DEFINE_SPECIFICS_TO_VALUE_TEST(history)
DEFINE_SPECIFICS_TO_VALUE_TEST(history_delete_directive)
DEFINE_SPECIFICS_TO_VALUE_TEST(incoming_password_sharing_invitation)
DEFINE_SPECIFICS_TO_VALUE_TEST(managed_user_setting)
DEFINE_SPECIFICS_TO_VALUE_TEST(nigori)
DEFINE_SPECIFICS_TO_VALUE_TEST(os_preference)
DEFINE_SPECIFICS_TO_VALUE_TEST(os_priority_preference)
DEFINE_SPECIFICS_TO_VALUE_TEST(outgoing_password_sharing_invitation)
DEFINE_SPECIFICS_TO_VALUE_TEST(password)
DEFINE_SPECIFICS_TO_VALUE_TEST(plus_address)
DEFINE_SPECIFICS_TO_VALUE_TEST(plus_address_setting)
DEFINE_SPECIFICS_TO_VALUE_TEST(power_bookmark)
DEFINE_SPECIFICS_TO_VALUE_TEST(preference)
DEFINE_SPECIFICS_TO_VALUE_TEST(printer)
DEFINE_SPECIFICS_TO_VALUE_TEST(printers_authorization_server)
DEFINE_SPECIFICS_TO_VALUE_TEST(priority_preference)
DEFINE_SPECIFICS_TO_VALUE_TEST(product_comparison)
DEFINE_SPECIFICS_TO_VALUE_TEST(reading_list)
DEFINE_SPECIFICS_TO_VALUE_TEST(saved_tab_group)
DEFINE_SPECIFICS_TO_VALUE_TEST(search_engine)
DEFINE_SPECIFICS_TO_VALUE_TEST(security_event)
DEFINE_SPECIFICS_TO_VALUE_TEST(send_tab_to_self)
DEFINE_SPECIFICS_TO_VALUE_TEST(session)
DEFINE_SPECIFICS_TO_VALUE_TEST(shared_tab_group_data)
DEFINE_SPECIFICS_TO_VALUE_TEST(sharing_message)
DEFINE_SPECIFICS_TO_VALUE_TEST(theme)
DEFINE_SPECIFICS_TO_VALUE_TEST(typed_url)
DEFINE_SPECIFICS_TO_VALUE_TEST(user_consent)
DEFINE_SPECIFICS_TO_VALUE_TEST(user_event)
DEFINE_SPECIFICS_TO_VALUE_TEST(wallet_metadata)
DEFINE_SPECIFICS_TO_VALUE_TEST(web_apk)
DEFINE_SPECIFICS_TO_VALUE_TEST(web_app)
DEFINE_SPECIFICS_TO_VALUE_TEST(webauthn_credential)
DEFINE_SPECIFICS_TO_VALUE_TEST(wifi_configuration)
DEFINE_SPECIFICS_TO_VALUE_TEST(workspace_desk)
DEFINE_SPECIFICS_TO_VALUE_TEST(autofill_valuable)
DEFINE_SPECIFICS_TO_VALUE_TEST(shared_tab_group_account_data)
TEST(ProtoValueConversionsTest, AutofillWalletSpecificsToValue) {
sync_pb::AutofillWalletSpecifics specifics;
specifics.mutable_masked_card()->set_name_on_card("Igloo");
specifics.mutable_address()->set_recipient_name("John");
specifics.mutable_customer_data()->set_id("123456");
specifics.mutable_cloud_token_data()->set_masked_card_id("1111");
specifics.set_type(sync_pb::AutofillWalletSpecifics::UNKNOWN);
base::Value::Dict value =
AutofillWalletSpecificsToValue(specifics).TakeDict();
EXPECT_FALSE(value.contains("masked_card"));
EXPECT_FALSE(value.contains("address"));
EXPECT_FALSE(value.contains("customer_data"));
EXPECT_FALSE(value.contains("cloud_token_data"));
specifics.set_type(sync_pb::AutofillWalletSpecifics::MASKED_CREDIT_CARD);
value = AutofillWalletSpecificsToValue(specifics).TakeDict();
EXPECT_TRUE(value.contains("masked_card"));
EXPECT_FALSE(value.contains("address"));
EXPECT_FALSE(value.contains("customer_data"));
EXPECT_FALSE(value.contains("cloud_token_data"));
specifics.set_type(sync_pb::AutofillWalletSpecifics::POSTAL_ADDRESS);
value = AutofillWalletSpecificsToValue(specifics).TakeDict();
EXPECT_FALSE(value.contains("masked_card"));
EXPECT_TRUE(value.contains("address"));
EXPECT_FALSE(value.contains("customer_data"));
EXPECT_FALSE(value.contains("cloud_token_data"));
specifics.set_type(sync_pb::AutofillWalletSpecifics::CUSTOMER_DATA);
value = AutofillWalletSpecificsToValue(specifics).TakeDict();
EXPECT_FALSE(value.contains("masked_card"));
EXPECT_FALSE(value.contains("address"));
EXPECT_TRUE(value.contains("customer_data"));
EXPECT_FALSE(value.contains("cloud_token_data"));
specifics.set_type(
sync_pb::AutofillWalletSpecifics::CREDIT_CARD_CLOUD_TOKEN_DATA);
value = AutofillWalletSpecificsToValue(specifics).TakeDict();
EXPECT_FALSE(value.contains("masked_card"));
EXPECT_FALSE(value.contains("address"));
EXPECT_FALSE(value.contains("customer_data"));
EXPECT_TRUE(value.contains("cloud_token_data"));
}
TEST(ProtoValueConversionsTest, BookmarkSpecificsData) {
const base::Time creation_time(base::Time::Now());
const std::string icon_url = "http://www.google.com/favicon.ico";
sync_pb::BookmarkSpecifics specifics;
specifics.set_creation_time_us(creation_time.ToInternalValue());
specifics.set_icon_url(icon_url);
sync_pb::MetaInfo* meta_1 = specifics.add_meta_info();
meta_1->set_key("key1");
meta_1->set_value("value1");
sync_pb::MetaInfo* meta_2 = specifics.add_meta_info();
meta_2->set_key("key2");
meta_2->set_value("value2");
base::Value::Dict value = BookmarkSpecificsToValue(specifics).TakeDict();
EXPECT_FALSE(value.empty());
const std::string* encoded_time = value.FindString("creation_time_us");
EXPECT_TRUE(encoded_time);
EXPECT_EQ(base::NumberToString(creation_time.ToInternalValue()),
*encoded_time);
const std::string* encoded_icon_url = value.FindString("icon_url");
EXPECT_TRUE(encoded_icon_url);
EXPECT_EQ(icon_url, *encoded_icon_url);
const base::Value::List* meta_info_list = value.FindList("meta_info");
EXPECT_EQ(2u, meta_info_list->size());
std::string meta_key;
std::string meta_value;
const auto& meta_info_value = (*meta_info_list)[0].GetDict();
ASSERT_TRUE((*meta_info_list)[0].is_dict());
EXPECT_STREQ("key1", meta_info_value.FindString("key")->c_str());
EXPECT_STREQ("value1", meta_info_value.FindString("value")->c_str());
const auto& meta_info_value_1 = (*meta_info_list)[1].GetDict();
ASSERT_TRUE((*meta_info_list)[1].is_dict());
EXPECT_STREQ("key2", meta_info_value_1.FindString("key")->c_str());
EXPECT_STREQ("value2", meta_info_value_1.FindString("value")->c_str());
}
TEST(ProtoValueConversionsTest, UniquePositionToValue) {
sync_pb::SyncEntity entity;
entity.mutable_unique_position()->set_custom_compressed_v1("test");
base::Value::Dict value =
SyncEntityToValue(entity, {.include_specifics = false}).TakeDict();
const std::string* unique_position = value.FindString("unique_position");
EXPECT_TRUE(unique_position);
std::string expected_unique_position =
UniquePosition::FromProto(entity.unique_position()).ToDebugString();
EXPECT_EQ(expected_unique_position, *unique_position);
}
TEST(ProtoValueConversionsTest, SyncEntityToValueIncludeSpecifics) {
sync_pb::SyncEntity entity;
entity.mutable_specifics();
base::Value::Dict value =
SyncEntityToValue(entity, {.include_specifics = true}).TakeDict();
EXPECT_TRUE(value.FindDict("specifics"));
value = SyncEntityToValue(entity, {.include_specifics = false}).TakeDict();
EXPECT_FALSE(value.FindDict("specifics"));
}
namespace {
// Returns whether the given value has specifics under the entries in the given
// path.
bool ValueHasSpecifics(const base::Value::Dict& value,
const std::string& path) {
const base::Value::List* entities_list = value.FindListByDottedPath(path);
if (!entities_list) {
return false;
}
const base::Value& entry_dictionary_value = (*entities_list)[0];
if (!entry_dictionary_value.is_dict()) {
return false;
}
const base::Value::Dict& entry_dictionary = entry_dictionary_value.GetDict();
return entry_dictionary.FindDict("specifics") != nullptr;
}
MATCHER(ValueHasNonEmptyGetUpdateTriggers, "") {
const base::Value::Dict& value_dict = arg;
const base::Value::List* entities_list =
value_dict.FindListByDottedPath("get_updates.from_progress_marker");
if (!entities_list) {
*result_listener << "no from_progress_marker list";
return false;
}
const base::Value& entry_dictionary_value = entities_list->front();
if (!entry_dictionary_value.is_dict()) {
*result_listener << "from_progress_marker does not contain a dictionary";
return false;
}
const base::Value::Dict& entry_dictionary = entry_dictionary_value.GetDict();
const base::Value::Dict* get_update_triggers_dictionary =
entry_dictionary.FindDict("get_update_triggers");
if (!get_update_triggers_dictionary) {
*result_listener << "no get_update_triggers dictionary";
return false;
}
return !get_update_triggers_dictionary->empty();
}
} // namespace
// Create a ClientToServerMessage with an EntitySpecifics. Converting it to
// a value should respect the `include_specifics` flag.
TEST(ProtoValueConversionsTest, ClientToServerMessageToValue) {
sync_pb::ClientToServerMessage message;
sync_pb::CommitMessage* commit_message = message.mutable_commit();
sync_pb::SyncEntity* entity = commit_message->add_entries();
entity->mutable_specifics();
base::Value::Dict value_with_specifics =
ClientToServerMessageToValue(message, {.include_specifics = true})
.TakeDict();
EXPECT_FALSE(value_with_specifics.empty());
EXPECT_TRUE(ValueHasSpecifics(value_with_specifics, "commit.entries"));
base::Value::Dict value_without_specifics =
ClientToServerMessageToValue(message, {.include_specifics = false})
.TakeDict();
EXPECT_FALSE(value_without_specifics.empty());
EXPECT_FALSE(ValueHasSpecifics(value_without_specifics, "commit.entries"));
}
TEST(ProtoValueConversionsTest, ClientToServerMessageToValueGUTriggers) {
sync_pb::ClientToServerMessage message;
sync_pb::GetUpdateTriggers* get_update_triggers =
message.mutable_get_updates()
->add_from_progress_marker()
->mutable_get_update_triggers();
get_update_triggers->set_client_dropped_hints(false);
get_update_triggers->set_server_dropped_hints(false);
get_update_triggers->set_datatype_refresh_nudges(0);
get_update_triggers->set_local_modification_nudges(0);
get_update_triggers->set_initial_sync_in_progress(false);
get_update_triggers->set_sync_for_resolve_conflict_in_progress(false);
base::Value::Dict value_with_full_gu_triggers =
ClientToServerMessageToValue(message,
{.include_full_get_update_triggers = true})
.TakeDict();
EXPECT_FALSE(value_with_full_gu_triggers.empty());
EXPECT_THAT(value_with_full_gu_triggers, ValueHasNonEmptyGetUpdateTriggers());
base::Value::Dict value_without_full_gu_triggers =
ClientToServerMessageToValue(message,
{.include_full_get_update_triggers = false})
.TakeDict();
EXPECT_FALSE(value_without_full_gu_triggers.empty());
EXPECT_THAT(value_without_full_gu_triggers,
Not(ValueHasNonEmptyGetUpdateTriggers()));
}
// Create a ClientToServerResponse with an EntitySpecifics. Converting it to
// a value should respect the `include_specifics` flag.
TEST(ProtoValueConversionsTest, ClientToServerResponseToValue) {
sync_pb::ClientToServerResponse message;
sync_pb::GetUpdatesResponse* response = message.mutable_get_updates();
sync_pb::SyncEntity* entity = response->add_entries();
entity->mutable_specifics();
base::Value::Dict value_with_specifics =
ClientToServerResponseToValue(message, {.include_specifics = true})
.TakeDict();
EXPECT_FALSE(value_with_specifics.empty());
EXPECT_TRUE(ValueHasSpecifics(value_with_specifics, "get_updates.entries"));
base::Value::Dict value_without_specifics =
ClientToServerResponseToValue(message, {.include_specifics = false})
.TakeDict();
EXPECT_FALSE(value_without_specifics.empty());
EXPECT_FALSE(
ValueHasSpecifics(value_without_specifics, "get_updates.entries"));
}
TEST(ProtoValueConversionsTest, CompareSpecificsData) {
sync_pb::ProductComparisonSpecifics specifics;
specifics.set_uuid("my_uuid");
specifics.set_creation_time_unix_epoch_millis(1708532099);
specifics.set_update_time_unix_epoch_millis(1708642103);
specifics.set_name("my_name");
specifics.add_data();
specifics.mutable_data(0)->set_url("https://www.foo.com");
specifics.add_data();
specifics.mutable_data(1)->set_url("https://www.bar.com");
base::Value::Dict value =
ProductComparisonSpecificsToValue(specifics).TakeDict();
EXPECT_FALSE(value.empty());
EXPECT_TRUE(value.FindString("uuid"));
EXPECT_STREQ("my_uuid", value.FindString("uuid")->c_str());
EXPECT_TRUE(value.FindString("creation_time_unix_epoch_millis"));
EXPECT_STREQ("1708532099",
value.FindString("creation_time_unix_epoch_millis")->c_str());
EXPECT_TRUE(value.FindString("update_time_unix_epoch_millis"));
EXPECT_STREQ("1708642103",
value.FindString("update_time_unix_epoch_millis")->c_str());
EXPECT_TRUE(value.FindString("name"));
EXPECT_STREQ("my_name", value.FindString("name")->c_str());
const base::Value::List* data_list = value.FindList("data");
EXPECT_TRUE(data_list);
EXPECT_EQ(2u, data_list->size());
EXPECT_TRUE((*data_list)[0].GetDict().FindString("url"));
EXPECT_STREQ("https://www.foo.com",
(*data_list)[0].GetDict().FindString("url")->c_str());
EXPECT_TRUE((*data_list)[1].GetDict().FindString("url"));
EXPECT_STREQ("https://www.bar.com",
(*data_list)[1].GetDict().FindString("url")->c_str());
}
} // namespace
} // namespace syncer
|