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