[email protected] | 4557d22 | 2012-03-04 23:33:36 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 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/extensions/extension_sync_data.h" |
| 6 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 10 | #include "base/version.h" |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame^] | 11 | #include "components/sync/api/string_ordinal.h" |
| 12 | #include "components/sync/protocol/app_specifics.pb.h" |
| 13 | #include "components/sync/protocol/extension_specifics.pb.h" |
| 14 | #include "components/sync/protocol/sync.pb.h" |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 15 | #include "extensions/common/extension.h" |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 17 | #include "url/gurl.h" |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 18 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 19 | namespace extensions { |
| 20 | |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 21 | namespace { |
| 22 | |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 23 | const char kValidId[] = "abcdefghijklmnopabcdefghijklmnop"; |
[email protected] | 095ccbe4 | 2013-09-26 00:06:42 | [diff] [blame] | 24 | const char kVersion[] = "1.0.0.1"; |
| 25 | const char kValidUpdateUrl[] = |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 26 | "https://clients2.google.com/service/update2/crx"; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 27 | const int kValidDisableReasons = Extension::DISABLE_USER_ACTION; |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 28 | const char kName[] = "MyExtension"; |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 29 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 30 | // Serializes a protobuf structure (entity specifics) into an ExtensionSyncData |
| 31 | // and back again, and confirms that the input is the same as the output. |
| 32 | void ProtobufToSyncDataEqual(const sync_pb::EntitySpecifics& entity) { |
| 33 | syncer::SyncData sync_data = |
| 34 | syncer::SyncData::CreateLocalData("sync_tag", "non_unique_title", entity); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 35 | std::unique_ptr<ExtensionSyncData> extension_sync_data = |
yoz | 87044453 | 2015-03-12 18:42:53 | [diff] [blame] | 36 | ExtensionSyncData::CreateFromSyncData(sync_data); |
| 37 | ASSERT_TRUE(extension_sync_data.get()); |
| 38 | syncer::SyncData output_sync_data = extension_sync_data->GetSyncData(); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 39 | const sync_pb::ExtensionSpecifics& output = |
| 40 | output_sync_data.GetSpecifics().extension(); |
| 41 | const sync_pb::ExtensionSpecifics& input = entity.extension(); |
| 42 | |
| 43 | // Check for field-by-field quality. It'd be nice if we could use |
| 44 | // AssertionResults here (instead of EXPECT_EQ) so that we could get valid |
| 45 | // line numbers, but it's not worth the ugliness of the verbose comparison. |
| 46 | EXPECT_EQ(input.id(), output.id()); |
| 47 | EXPECT_EQ(input.name(), output.name()); |
| 48 | EXPECT_EQ(input.version(), output.version()); |
| 49 | EXPECT_EQ(input.update_url(), output.update_url()); |
| 50 | EXPECT_EQ(input.enabled(), output.enabled()); |
| 51 | EXPECT_EQ(input.incognito_enabled(), output.incognito_enabled()); |
| 52 | EXPECT_EQ(input.remote_install(), output.remote_install()); |
| 53 | EXPECT_EQ(input.installed_by_custodian(), output.installed_by_custodian()); |
| 54 | EXPECT_EQ(input.has_all_urls_enabled(), output.has_all_urls_enabled()); |
| 55 | if (input.has_all_urls_enabled()) |
| 56 | EXPECT_EQ(input.all_urls_enabled(), output.all_urls_enabled()); |
| 57 | } |
| 58 | |
| 59 | // Serializes an ExtensionSyncData into a protobuf structure and back again, and |
| 60 | // confirms that the input is the same as the output. |
| 61 | void SyncDataToProtobufEqual(const ExtensionSyncData& input) { |
| 62 | syncer::SyncData sync_data = input.GetSyncData(); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 63 | std::unique_ptr<ExtensionSyncData> output = |
yoz | 87044453 | 2015-03-12 18:42:53 | [diff] [blame] | 64 | ExtensionSyncData::CreateFromSyncData(sync_data); |
| 65 | ASSERT_TRUE(output.get()); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 66 | |
yoz | 87044453 | 2015-03-12 18:42:53 | [diff] [blame] | 67 | EXPECT_EQ(input.id(), output->id()); |
| 68 | EXPECT_EQ(input.uninstalled(), output->uninstalled()); |
| 69 | EXPECT_EQ(input.enabled(), output->enabled()); |
| 70 | EXPECT_EQ(input.incognito_enabled(), output->incognito_enabled()); |
| 71 | EXPECT_EQ(input.remote_install(), output->remote_install()); |
| 72 | EXPECT_EQ(input.installed_by_custodian(), output->installed_by_custodian()); |
| 73 | EXPECT_EQ(input.all_urls_enabled(), output->all_urls_enabled()); |
robpercival | dcd8b10 | 2016-01-25 19:39:00 | [diff] [blame] | 74 | EXPECT_EQ(input.version(), output->version()); |
yoz | 87044453 | 2015-03-12 18:42:53 | [diff] [blame] | 75 | EXPECT_EQ(input.update_url(), output->update_url()); |
| 76 | EXPECT_EQ(input.name(), output->name()); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | } // namespace |
| 80 | |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 81 | class ExtensionSyncDataTest : public testing::Test { |
| 82 | }; |
| 83 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 84 | // Tests the conversion process from a protobuf to an ExtensionSyncData and vice |
| 85 | // versa. |
| 86 | TEST_F(ExtensionSyncDataTest, ExtensionSyncDataForExtension) { |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 87 | sync_pb::EntitySpecifics entity; |
[email protected] | 4557d22 | 2012-03-04 23:33:36 | [diff] [blame] | 88 | sync_pb::ExtensionSpecifics* extension_specifics = entity.mutable_extension(); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 89 | extension_specifics->set_id(kValidId); |
[email protected] | 095ccbe4 | 2013-09-26 00:06:42 | [diff] [blame] | 90 | extension_specifics->set_update_url(kValidUpdateUrl); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 91 | extension_specifics->set_enabled(false); |
| 92 | extension_specifics->set_incognito_enabled(true); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 93 | extension_specifics->set_remote_install(false); |
| 94 | extension_specifics->set_installed_by_custodian(false); |
| 95 | extension_specifics->set_all_urls_enabled(true); |
[email protected] | 095ccbe4 | 2013-09-26 00:06:42 | [diff] [blame] | 96 | extension_specifics->set_version(kVersion); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 97 | extension_specifics->set_name(kName); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 98 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 99 | // Check the serialize-deserialize process for proto to ExtensionSyncData. |
| 100 | ProtobufToSyncDataEqual(entity); |
| 101 | |
| 102 | // Explicitly test that conversion to an ExtensionSyncData gets the correct |
| 103 | // result (otherwise we just know that conversion to/from a proto gives us |
| 104 | // the same result, but don't know that it's right). |
| 105 | ExtensionSyncData extension_sync_data; |
| 106 | extension_sync_data.PopulateFromExtensionSpecifics(*extension_specifics); |
| 107 | EXPECT_EQ(kValidId, extension_sync_data.id()); |
| 108 | EXPECT_EQ(GURL(kValidUpdateUrl), extension_sync_data.update_url()); |
vivek.vg | 3b60d1cd | 2015-02-17 16:54:15 | [diff] [blame] | 109 | EXPECT_FALSE(extension_sync_data.enabled()); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 110 | EXPECT_EQ(true, extension_sync_data.incognito_enabled()); |
vivek.vg | 3b60d1cd | 2015-02-17 16:54:15 | [diff] [blame] | 111 | EXPECT_FALSE(extension_sync_data.remote_install()); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 112 | EXPECT_EQ(ExtensionSyncData::BOOLEAN_TRUE, |
| 113 | extension_sync_data.all_urls_enabled()); |
robpercival | dcd8b10 | 2016-01-25 19:39:00 | [diff] [blame] | 114 | EXPECT_EQ(Version(kVersion), extension_sync_data.version()); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 115 | EXPECT_EQ(std::string(kName), extension_sync_data.name()); |
| 116 | |
| 117 | // Check the serialize-deserialize process for ExtensionSyncData to proto. |
| 118 | SyncDataToProtobufEqual(extension_sync_data); |
| 119 | |
| 120 | // The most important thing to test is the "all urls" bit, since it is a |
| 121 | // tri-state boolean (and thus has more logic). Also flip another bit for a |
| 122 | // sanity check. |
| 123 | extension_specifics->set_all_urls_enabled(false); |
| 124 | extension_specifics->set_incognito_enabled(false); |
| 125 | ProtobufToSyncDataEqual(entity); |
| 126 | |
| 127 | extension_sync_data.PopulateFromExtensionSpecifics(*extension_specifics); |
| 128 | EXPECT_EQ(ExtensionSyncData::BOOLEAN_FALSE, |
| 129 | extension_sync_data.all_urls_enabled()); |
vivek.vg | 3b60d1cd | 2015-02-17 16:54:15 | [diff] [blame] | 130 | EXPECT_FALSE(extension_sync_data.incognito_enabled()); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 131 | |
| 132 | SyncDataToProtobufEqual(extension_sync_data); |
| 133 | |
| 134 | extension_specifics->clear_all_urls_enabled(); |
| 135 | ProtobufToSyncDataEqual(entity); |
| 136 | |
| 137 | extension_sync_data.PopulateFromExtensionSpecifics(*extension_specifics); |
| 138 | EXPECT_FALSE(extension_specifics->has_all_urls_enabled()); |
| 139 | EXPECT_EQ(ExtensionSyncData::BOOLEAN_UNSET, |
| 140 | extension_sync_data.all_urls_enabled()); |
| 141 | |
| 142 | SyncDataToProtobufEqual(extension_sync_data); |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 143 | } |
| 144 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 145 | class AppSyncDataTest : public testing::Test { |
| 146 | public: |
| 147 | AppSyncDataTest() {} |
| 148 | ~AppSyncDataTest() override {} |
| 149 | |
| 150 | void SetRequiredExtensionValues( |
| 151 | sync_pb::ExtensionSpecifics* extension_specifics) { |
| 152 | extension_specifics->set_id(kValidId); |
| 153 | extension_specifics->set_update_url(kValidUpdateUrl); |
| 154 | extension_specifics->set_version(kVersion); |
| 155 | extension_specifics->set_enabled(false); |
| 156 | extension_specifics->set_disable_reasons(kValidDisableReasons); |
| 157 | extension_specifics->set_incognito_enabled(true); |
| 158 | extension_specifics->set_remote_install(false); |
| 159 | extension_specifics->set_all_urls_enabled(true); |
| 160 | extension_specifics->set_installed_by_custodian(false); |
| 161 | extension_specifics->set_name(kName); |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | TEST_F(AppSyncDataTest, SyncDataToExtensionSyncDataForApp) { |
| 166 | sync_pb::EntitySpecifics entity; |
| 167 | sync_pb::AppSpecifics* app_specifics = entity.mutable_app(); |
| 168 | app_specifics->set_app_launch_ordinal( |
| 169 | syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue()); |
| 170 | app_specifics->set_page_ordinal( |
| 171 | syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue()); |
| 172 | |
| 173 | SetRequiredExtensionValues(app_specifics->mutable_extension()); |
| 174 | |
| 175 | syncer::SyncData sync_data = |
| 176 | syncer::SyncData::CreateLocalData("sync_tag", "non_unique_title", entity); |
| 177 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 178 | std::unique_ptr<ExtensionSyncData> app_sync_data = |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 179 | ExtensionSyncData::CreateFromSyncData(sync_data); |
| 180 | ASSERT_TRUE(app_sync_data.get()); |
| 181 | EXPECT_EQ(app_specifics->app_launch_ordinal(), |
| 182 | app_sync_data->app_launch_ordinal().ToInternalValue()); |
| 183 | EXPECT_EQ(app_specifics->page_ordinal(), |
| 184 | app_sync_data->page_ordinal().ToInternalValue()); |
| 185 | } |
| 186 | |
| 187 | TEST_F(AppSyncDataTest, ExtensionSyncDataToSyncDataForApp) { |
| 188 | sync_pb::EntitySpecifics entity; |
| 189 | sync_pb::AppSpecifics* input_specifics = entity.mutable_app(); |
| 190 | input_specifics->set_app_launch_ordinal( |
| 191 | syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue()); |
| 192 | input_specifics->set_page_ordinal( |
| 193 | syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue()); |
| 194 | |
| 195 | SetRequiredExtensionValues(input_specifics->mutable_extension()); |
| 196 | |
| 197 | syncer::SyncData sync_data = |
| 198 | syncer::SyncData::CreateLocalData("sync_tag", "non_unique_title", entity); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 199 | std::unique_ptr<ExtensionSyncData> app_sync_data = |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 200 | ExtensionSyncData::CreateFromSyncData(sync_data); |
| 201 | ASSERT_TRUE(app_sync_data.get()); |
| 202 | |
| 203 | syncer::SyncData output_sync_data = app_sync_data->GetSyncData(); |
| 204 | EXPECT_TRUE(sync_data.GetSpecifics().has_app()); |
| 205 | const sync_pb::AppSpecifics& output_specifics = |
| 206 | output_sync_data.GetSpecifics().app(); |
| 207 | EXPECT_EQ(input_specifics->SerializeAsString(), |
| 208 | output_specifics.SerializeAsString()); |
| 209 | } |
| 210 | |
| 211 | // Ensures that invalid StringOrdinals don't break ExtensionSyncData. |
| 212 | TEST_F(AppSyncDataTest, ExtensionSyncDataInvalidOrdinal) { |
| 213 | sync_pb::EntitySpecifics entity; |
| 214 | sync_pb::AppSpecifics* app_specifics = entity.mutable_app(); |
| 215 | // Set the ordinals as invalid. |
| 216 | app_specifics->set_app_launch_ordinal(""); |
| 217 | app_specifics->set_page_ordinal(""); |
| 218 | |
| 219 | SetRequiredExtensionValues(app_specifics->mutable_extension()); |
| 220 | |
| 221 | syncer::SyncData sync_data = |
| 222 | syncer::SyncData::CreateLocalData("sync_tag", "non_unique_title", entity); |
| 223 | |
| 224 | // There should be no issue loading the sync data. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 225 | std::unique_ptr<ExtensionSyncData> app_sync_data = |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 226 | ExtensionSyncData::CreateFromSyncData(sync_data); |
| 227 | ASSERT_TRUE(app_sync_data.get()); |
| 228 | app_sync_data->GetSyncData(); |
| 229 | } |
| 230 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 231 | } // namespace extensions |