OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/test/values_test_util.h" | 7 #include "base/test/values_test_util.h" |
8 #include "chrome/browser/extensions/test_extension_environment.h" | 8 #include "chrome/browser/extensions/test_extension_environment.h" |
9 #include "chrome/common/extensions/permissions/chrome_permission_message_provide
r.h" | 9 #include "chrome/common/extensions/permissions/chrome_permission_message_provide
r.h" |
10 #include "extensions/common/extension.h" | 10 #include "extensions/common/extension.h" |
| 11 #include "extensions/common/features/simple_feature.h" |
11 #include "extensions/common/permissions/permission_message_test_util.h" | 12 #include "extensions/common/permissions/permission_message_test_util.h" |
12 #include "extensions/common/permissions/permissions_data.h" | 13 #include "extensions/common/permissions/permissions_data.h" |
13 #include "extensions/common/switches.h" | 14 #include "extensions/common/switches.h" |
14 #include "testing/gmock/include/gmock/gmock-matchers.h" | 15 #include "testing/gmock/include/gmock/gmock-matchers.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace extensions { | 18 namespace extensions { |
18 | 19 |
| 20 const char kWhitelistedExtensionID[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 21 |
19 // Tests that ChromePermissionMessageProvider produces the expected messages for | 22 // Tests that ChromePermissionMessageProvider produces the expected messages for |
20 // various combinations of app/extension permissions. | 23 // various combinations of app/extension permissions. |
21 class PermissionMessageCombinationsUnittest : public testing::Test { | 24 class PermissionMessageCombinationsUnittest : public testing::Test { |
22 public: | 25 public: |
23 PermissionMessageCombinationsUnittest() | 26 PermissionMessageCombinationsUnittest() |
24 : message_provider_(new ChromePermissionMessageProvider()) {} | 27 : message_provider_(new ChromePermissionMessageProvider()), |
| 28 whitelisted_extension_id_(kWhitelistedExtensionID) {} |
25 ~PermissionMessageCombinationsUnittest() override {} | 29 ~PermissionMessageCombinationsUnittest() override {} |
26 | 30 |
27 // Overridden from testing::Test: | 31 // Overridden from testing::Test: |
28 void SetUp() override { | 32 void SetUp() override { |
29 testing::Test::SetUp(); | 33 testing::Test::SetUp(); |
30 // Force creation of ExtensionPrefs before adding extensions. | 34 // Force creation of ExtensionPrefs before adding extensions. |
31 env_.GetExtensionPrefs(); | 35 env_.GetExtensionPrefs(); |
32 } | 36 } |
33 | 37 |
34 protected: | 38 protected: |
35 // Create and install an app or extension with the given manifest JSON string. | 39 // Create and install an app or extension with the given manifest JSON string. |
36 // Single-quotes in the string will be replaced with double quotes. | 40 // Single-quotes in the string will be replaced with double quotes. |
37 void CreateAndInstall(const std::string& json_manifest) { | 41 void CreateAndInstall(const std::string& json_manifest) { |
38 std::string json_manifest_with_double_quotes = json_manifest; | 42 std::string json_manifest_with_double_quotes = json_manifest; |
39 std::replace(json_manifest_with_double_quotes.begin(), | 43 std::replace(json_manifest_with_double_quotes.begin(), |
40 json_manifest_with_double_quotes.end(), '\'', '"'); | 44 json_manifest_with_double_quotes.end(), '\'', '"'); |
41 app_ = env_.MakeExtension( | 45 app_ = env_.MakeExtension( |
42 *base::test::ParseJson(json_manifest_with_double_quotes)); | 46 *base::test::ParseJson(json_manifest_with_double_quotes), |
43 // Add the app to any whitelists so we can test all permissions. | 47 kWhitelistedExtensionID); |
44 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
45 switches::kWhitelistedExtensionID, app_->id()); | |
46 } | 48 } |
47 | 49 |
48 // Checks whether the currently installed app or extension produces the given | 50 // Checks whether the currently installed app or extension produces the given |
49 // permission messages. Call this after installing an app with the expected | 51 // permission messages. Call this after installing an app with the expected |
50 // permission messages. The messages are tested for existence in any order. | 52 // permission messages. The messages are tested for existence in any order. |
51 testing::AssertionResult CheckManifestProducesPermissions() { | 53 testing::AssertionResult CheckManifestProducesPermissions() { |
52 return VerifyNoPermissionMessages(app_->permissions_data()); | 54 return VerifyNoPermissionMessages(app_->permissions_data()); |
53 } | 55 } |
54 testing::AssertionResult CheckManifestProducesPermissions( | 56 testing::AssertionResult CheckManifestProducesPermissions( |
55 const std::string& expected_message_1) { | 57 const std::string& expected_message_1) { |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 expected_submessages.push_back(expected_submessages_5); | 192 expected_submessages.push_back(expected_submessages_5); |
191 return VerifyPermissionMessagesWithSubmessages(app_->permissions_data(), | 193 return VerifyPermissionMessagesWithSubmessages(app_->permissions_data(), |
192 expected_messages, | 194 expected_messages, |
193 expected_submessages, false); | 195 expected_submessages, false); |
194 } | 196 } |
195 | 197 |
196 private: | 198 private: |
197 extensions::TestExtensionEnvironment env_; | 199 extensions::TestExtensionEnvironment env_; |
198 scoped_ptr<ChromePermissionMessageProvider> message_provider_; | 200 scoped_ptr<ChromePermissionMessageProvider> message_provider_; |
199 scoped_refptr<const Extension> app_; | 201 scoped_refptr<const Extension> app_; |
| 202 // Whitelist a known extension id so we can test all permissions. This ID |
| 203 // will be used for each test app. |
| 204 extensions::SimpleFeature::ScopedWhitelistForTest whitelisted_extension_id_; |
200 | 205 |
201 DISALLOW_COPY_AND_ASSIGN(PermissionMessageCombinationsUnittest); | 206 DISALLOW_COPY_AND_ASSIGN(PermissionMessageCombinationsUnittest); |
202 }; | 207 }; |
203 | 208 |
204 // Test that the USB, Bluetooth and Serial permissions do not coalesce on their | 209 // Test that the USB, Bluetooth and Serial permissions do not coalesce on their |
205 // own, but do coalesce when more than 1 is present. | 210 // own, but do coalesce when more than 1 is present. |
206 TEST_F(PermissionMessageCombinationsUnittest, USBSerialBluetoothCoalescing) { | 211 TEST_F(PermissionMessageCombinationsUnittest, USBSerialBluetoothCoalescing) { |
207 // Test that the USB permission does not coalesce on its own. | 212 // Test that the USB permission does not coalesce on its own. |
208 CreateAndInstall( | 213 CreateAndInstall( |
209 "{" | 214 "{" |
(...skipping 953 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1163 // TODO(sashab): Add a test that ensures that all permissions that can generate | 1168 // TODO(sashab): Add a test that ensures that all permissions that can generate |
1164 // a coalesced message can also generate a message on their own (i.e. ensure | 1169 // a coalesced message can also generate a message on their own (i.e. ensure |
1165 // that no permissions only modify other permissions). | 1170 // that no permissions only modify other permissions). |
1166 | 1171 |
1167 // TODO(sashab): Add a test for every permission message combination that can | 1172 // TODO(sashab): Add a test for every permission message combination that can |
1168 // generate a message. | 1173 // generate a message. |
1169 | 1174 |
1170 // TODO(aboxhall): Add tests for the automation API permission messages. | 1175 // TODO(aboxhall): Add tests for the automation API permission messages. |
1171 | 1176 |
1172 } // namespace extensions | 1177 } // namespace extensions |
OLD | NEW |