Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / manifest_handlers / content_capabilities_manifest_unittest.cc
blobe9f78c3a546961fe820388ae903ebfb637aad26d
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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "extensions/common/manifest_constants.h"
7 #include "extensions/common/manifest_handlers/content_capabilities_handler.h"
8 #include "extensions/common/manifest_test.h"
9 #include "extensions/common/permissions/api_permission.h"
10 #include "extensions/common/permissions/extensions_api_permissions.h"
11 #include "extensions/common/permissions/permissions_info.h"
12 #include "extensions/common/switches.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "url/gurl.h"
16 namespace extensions {
18 using extensions::APIPermission;
20 class ContentCapabilitiesManifestTest : public ManifestTest {
21 std::string GetTestExtensionID() const override {
22 return std::string("apdfllckaahabafndbhieahigkjlhalf");
26 TEST_F(ContentCapabilitiesManifestTest, RejectDomainWildcards) {
27 scoped_refptr<Extension> extension(LoadAndExpectWarning(
28 "content_capabilities_domain_wildcard.json",
29 manifest_errors::kInvalidContentCapabilitiesMatchOrigin));
30 const ContentCapabilitiesInfo& info = ContentCapabilitiesInfo::Get(
31 extension.get());
32 // Make sure the wildcard is not included in the pattern set.
33 EXPECT_FALSE(info.url_patterns.MatchesURL(GURL("https://bar.example.com/")));
34 EXPECT_TRUE(info.url_patterns.MatchesURL(GURL("https://foo.example.com/")));
37 TEST_F(ContentCapabilitiesManifestTest, RejectedAllHosts) {
38 scoped_refptr<Extension> extension(LoadAndExpectWarning(
39 "content_capabilities_all_hosts.json",
40 manifest_errors::kInvalidContentCapabilitiesMatchOrigin));
41 const ContentCapabilitiesInfo& info = ContentCapabilitiesInfo::Get(
42 extension.get());
43 // Make sure the wildcard is not included in the pattern set.
44 EXPECT_FALSE(info.url_patterns.MatchesURL(GURL("https://nonspecific.com/")));
45 EXPECT_TRUE(info.url_patterns.MatchesURL(GURL("https://example.com/")));
48 TEST_F(ContentCapabilitiesManifestTest, InvalidPermission) {
49 scoped_refptr<Extension> extension(LoadAndExpectWarning(
50 "content_capabilities_invalid_permission.json",
51 manifest_errors::kInvalidContentCapabilitiesPermission));
52 const ContentCapabilitiesInfo& info = ContentCapabilitiesInfo::Get(
53 extension.get());
54 // Make sure the invalid permission is not included in the permission set.
55 EXPECT_EQ(3u, info.permissions.size());
56 EXPECT_EQ(1u, info.permissions.count(APIPermission::kClipboardRead));
57 EXPECT_EQ(1u, info.permissions.count(APIPermission::kClipboardWrite));
58 EXPECT_EQ(1u, info.permissions.count(APIPermission::kUnlimitedStorage));
59 EXPECT_EQ(0u, info.permissions.count(APIPermission::kUsb));
62 TEST_F(ContentCapabilitiesManifestTest, InvalidValue) {
63 LoadAndExpectError("content_capabilities_invalid_value.json",
64 "expected dictionary, got list");
67 TEST_F(ContentCapabilitiesManifestTest, RejectNonHttpsUrlPatterns) {
68 LoadAndExpectError("content_capabilities_non_https_matches.json",
69 manifest_errors::kInvalidContentCapabilitiesMatch);
72 TEST_F(ContentCapabilitiesManifestTest, Valid) {
73 scoped_refptr<Extension> extension(
74 LoadAndExpectSuccess("content_capabilities_valid.json"));
75 const ContentCapabilitiesInfo& info = ContentCapabilitiesInfo::Get(
76 extension.get());
77 EXPECT_EQ(1u, info.url_patterns.size());
78 EXPECT_FALSE(info.url_patterns.MatchesURL(GURL("http://valid.example.com/")));
79 EXPECT_FALSE(info.url_patterns.MatchesURL(GURL("https://foo.example.com/")));
80 EXPECT_FALSE(info.url_patterns.MatchesURL(GURL("https://example.com/")));
81 EXPECT_TRUE(info.url_patterns.MatchesURL(GURL("https://valid.example.com/")));
82 EXPECT_EQ(3u, info.permissions.size());
83 EXPECT_EQ(1u, info.permissions.count(APIPermission::kClipboardRead));
84 EXPECT_EQ(1u, info.permissions.count(APIPermission::kClipboardWrite));
85 EXPECT_EQ(1u, info.permissions.count(APIPermission::kUnlimitedStorage));
86 EXPECT_EQ(0u, info.permissions.count(APIPermission::kUsb));
90 } // namespace extensions