Update V8 to version 4.6.55.
[chromium-blink-merge.git] / chrome / common / pepper_permission_util_unittest.cc
blob12bab4803efcd5604b44a5354bb5965521c87dc3
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 "chrome/common/pepper_permission_util.h"
7 #include <set>
8 #include <string>
10 #include "chrome/common/extensions/features/feature_channel.h"
11 #include "components/crx_file/id_util.h"
12 #include "components/version_info/version_info.h"
13 #include "extensions/common/extension_builder.h"
14 #include "extensions/common/extension_set.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using chrome::IsExtensionOrSharedModuleWhitelisted;
19 namespace extensions {
21 namespace {
23 // Return an extension with |id| which imports a module with the given
24 // |import_id|.
25 scoped_refptr<Extension> CreateExtensionImportingModule(
26 const std::string& import_id,
27 const std::string& id) {
28 scoped_ptr<base::DictionaryValue> manifest =
29 DictionaryBuilder()
30 .Set("name", "Has Dependent Modules")
31 .Set("version", "1.0")
32 .Set("manifest_version", 2)
33 .Set("import",
34 ListBuilder().Append(DictionaryBuilder().Set("id", import_id)))
35 .Build();
37 return ExtensionBuilder()
38 .SetManifest(manifest.Pass())
39 .AddFlags(Extension::FROM_WEBSTORE)
40 .SetID(id)
41 .Build();
44 } // namespace
46 TEST(PepperPermissionUtilTest, ExtensionWhitelisting) {
47 ScopedCurrentChannel current_channel(version_info::Channel::UNKNOWN);
48 ExtensionSet extensions;
49 std::string whitelisted_id =
50 crx_file::id_util::GenerateId("whitelisted_extension");
51 scoped_ptr<base::DictionaryValue> manifest =
52 DictionaryBuilder()
53 .Set("name", "Whitelisted Extension")
54 .Set("version", "1.0")
55 .Set("manifest_version", 2)
56 .Build();
57 scoped_refptr<Extension> ext = ExtensionBuilder()
58 .SetManifest(manifest.Pass())
59 .SetID(whitelisted_id)
60 .Build();
61 extensions.Insert(ext);
62 std::set<std::string> whitelist;
63 std::string url = std::string("chrome-extension://") + whitelisted_id +
64 std::string("/manifest.nmf");
65 std::string bad_scheme_url =
66 std::string("http://") + whitelisted_id + std::string("/manifest.nmf");
67 std::string bad_host_url = std::string("chrome-extension://") +
68 crx_file::id_util::GenerateId("bad_host");
69 std::string("/manifest.nmf");
71 EXPECT_FALSE(
72 IsExtensionOrSharedModuleWhitelisted(GURL(url), &extensions, whitelist));
73 whitelist.insert(whitelisted_id);
74 EXPECT_TRUE(
75 IsExtensionOrSharedModuleWhitelisted(GURL(url), &extensions, whitelist));
76 EXPECT_FALSE(IsExtensionOrSharedModuleWhitelisted(
77 GURL(bad_scheme_url), &extensions, whitelist));
78 EXPECT_FALSE(IsExtensionOrSharedModuleWhitelisted(
79 GURL(bad_host_url), &extensions, whitelist));
82 TEST(PepperPermissionUtilTest, SharedModuleWhitelisting) {
83 ScopedCurrentChannel current_channel(version_info::Channel::UNKNOWN);
84 ExtensionSet extensions;
85 std::string whitelisted_id = crx_file::id_util::GenerateId("extension_id");
86 std::string bad_id = crx_file::id_util::GenerateId("bad_id");
88 scoped_ptr<base::DictionaryValue> shared_module_manifest =
89 DictionaryBuilder()
90 .Set("name", "Whitelisted Shared Module")
91 .Set("version", "1.0")
92 .Set("manifest_version", 2)
93 .Set("export",
94 DictionaryBuilder()
95 .Set("resources", ListBuilder().Append("*"))
96 // Add the extension to the whitelist. This
97 // restricts import to |whitelisted_id| only.
98 .Set("whitelist", ListBuilder().Append(whitelisted_id)))
99 .Build();
100 scoped_refptr<Extension> shared_module =
101 ExtensionBuilder().SetManifest(shared_module_manifest.Pass()).Build();
103 scoped_refptr<Extension> ext =
104 CreateExtensionImportingModule(shared_module->id(), whitelisted_id);
105 std::string extension_url =
106 std::string("chrome-extension://") + ext->id() + std::string("/foo.html");
108 std::set<std::string> whitelist;
109 // Important: whitelist *only* the shared module.
110 whitelist.insert(shared_module->id());
112 extensions.Insert(ext);
113 // This should fail because shared_module is not in the set of extensions.
114 EXPECT_FALSE(IsExtensionOrSharedModuleWhitelisted(
115 GURL(extension_url), &extensions, whitelist));
116 extensions.Insert(shared_module);
117 EXPECT_TRUE(IsExtensionOrSharedModuleWhitelisted(
118 GURL(extension_url), &extensions, whitelist));
119 scoped_refptr<Extension> not_in_sm_whitelist =
120 CreateExtensionImportingModule(shared_module->id(), bad_id);
121 std::string not_in_sm_whitelist_url = std::string("chrome-extension://") +
122 not_in_sm_whitelist->id() +
123 std::string("/foo.html");
125 extensions.Insert(not_in_sm_whitelist);
126 // This should succeed, even though |not_in_sm_whitelist| is not whitelisted
127 // to use shared_module, because the pepper permission utility does not care
128 // about that whitelist. It is possible to install against the whitelist as
129 // an unpacked extension.
130 EXPECT_TRUE(IsExtensionOrSharedModuleWhitelisted(
131 GURL(not_in_sm_whitelist_url), &extensions, whitelist));
133 // Note that the whitelist should be empty after this call, so tests checking
134 // for failure to import will fail because of this.
135 whitelist.erase(shared_module->id());
136 EXPECT_FALSE(IsExtensionOrSharedModuleWhitelisted(
137 GURL(extension_url), &extensions, whitelist));
140 } // namespace extensions