1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/process_map.h"
#include "base/memory/ref_counted.h"
#include "base/values.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
namespace {
enum class TypeToCreate { kExtension, kHostedApp, kPlatformApp };
scoped_refptr<const Extension> CreateExtensionWithFlags(TypeToCreate type,
const std::string& id) {
auto manifest_builder = base::Value::Dict()
.Set("name", "Test extension")
.Set("version", "1.0")
.Set("manifest_version", 2);
switch (type) {
case TypeToCreate::kExtension:
manifest_builder.Set(
"background",
base::Value::Dict().Set("scripts",
base::Value::List().Append("background.js")));
break;
case TypeToCreate::kHostedApp:
manifest_builder.Set(
"app", base::Value::Dict().Set(
"launch", base::Value::Dict().Set("web_url",
"https://www.foo.bar")));
break;
case TypeToCreate::kPlatformApp:
manifest_builder.Set(
"app",
base::Value::Dict().Set(
"background",
base::Value::Dict().Set(
"scripts", base::Value::List().Append("background.js"))));
break;
}
return ExtensionBuilder()
.SetID(id)
.SetManifest(std::move(manifest_builder))
.Build();
}
} // namespace
} // namespace extensions
using extensions::ProcessMap;
TEST(ExtensionProcessMapTest, Test) {
ProcessMap map(/*browser_context=*/nullptr);
// Test behavior when empty.
EXPECT_FALSE(map.Contains("a", 1));
EXPECT_FALSE(map.Remove(1));
EXPECT_FALSE(map.ExtensionHasProcess("a"));
EXPECT_EQ(0u, map.size());
// Test insertion and behavior with one item.
EXPECT_TRUE(map.Insert("a", 1));
EXPECT_TRUE(map.Contains("a", 1));
EXPECT_FALSE(map.Contains("a", 2));
EXPECT_FALSE(map.Contains("b", 1));
EXPECT_TRUE(map.ExtensionHasProcess("a"));
EXPECT_FALSE(map.ExtensionHasProcess("b"));
EXPECT_EQ(1u, map.size());
// Test inserting a duplicate item.
EXPECT_FALSE(map.Insert("a", 1));
EXPECT_TRUE(map.Contains("a", 1));
EXPECT_EQ(1u, map.size());
// Insert some more items.
EXPECT_TRUE(map.Insert("a", 2));
EXPECT_TRUE(map.Insert("b", 3));
EXPECT_TRUE(map.Insert("b", 4));
EXPECT_EQ(4u, map.size());
EXPECT_TRUE(map.Contains("a", 1));
EXPECT_TRUE(map.Contains("a", 2));
EXPECT_TRUE(map.Contains("b", 3));
EXPECT_TRUE(map.Contains("b", 4));
EXPECT_FALSE(map.Contains("a", 3));
EXPECT_FALSE(map.Contains("b", 2));
EXPECT_FALSE(map.Contains("a", 5));
EXPECT_FALSE(map.Contains("c", 3));
EXPECT_TRUE(map.ExtensionHasProcess("a"));
EXPECT_TRUE(map.ExtensionHasProcess("b"));
// At this point we have {a,1}, {a,2}, {b,3}, and {b,4} in the map. Test
// removal of these processes.
EXPECT_EQ(1, map.Remove(1));
EXPECT_EQ(3u, map.size());
EXPECT_FALSE(map.Contains("a", 1));
EXPECT_TRUE(map.Contains("a", 2));
EXPECT_EQ(1, map.Remove(2));
EXPECT_EQ(2u, map.size());
EXPECT_EQ(0, map.Remove(2));
EXPECT_EQ(2u, map.size());
EXPECT_EQ(1, map.Remove(3));
EXPECT_EQ(1u, map.size());
EXPECT_EQ(0, map.Remove(3));
EXPECT_EQ(1u, map.size());
EXPECT_EQ(1, map.Remove(4));
EXPECT_EQ(0u, map.size());
EXPECT_EQ(0, map.Remove(4));
EXPECT_EQ(0u, map.size());
}
TEST(ExtensionProcessMapTest, GetMostLikelyContextType) {
ProcessMap map(/*browser_context=*/nullptr);
const GURL web_url("https://foo.example");
const GURL extension_url("chrome-extension://foobar");
const GURL untrusted_webui_url("chrome-untrusted://foo/index.html");
EXPECT_EQ(extensions::mojom::ContextType::kWebPage,
map.GetMostLikelyContextType(nullptr, 1, &web_url));
scoped_refptr<const extensions::Extension> extension =
CreateExtensionWithFlags(extensions::TypeToCreate::kExtension, "a");
EXPECT_EQ(extensions::mojom::ContextType::kContentScript,
map.GetMostLikelyContextType(extension.get(), 2, &extension_url));
EXPECT_EQ(extensions::mojom::ContextType::kContentScript,
map.GetMostLikelyContextType(extension.get(), 2, &web_url));
EXPECT_EQ(
extensions::mojom::ContextType::kContentScript,
map.GetMostLikelyContextType(extension.get(), 2, &untrusted_webui_url));
map.Insert("b", 3);
extension =
CreateExtensionWithFlags(extensions::TypeToCreate::kExtension, "b");
EXPECT_EQ(extensions::mojom::ContextType::kPrivilegedExtension,
map.GetMostLikelyContextType(extension.get(), 3, &extension_url));
map.Insert("c", 4);
extension =
CreateExtensionWithFlags(extensions::TypeToCreate::kPlatformApp, "c");
EXPECT_EQ(extensions::mojom::ContextType::kPrivilegedExtension,
map.GetMostLikelyContextType(extension.get(), 4, &extension_url));
map.Insert("d", 5);
extension =
CreateExtensionWithFlags(extensions::TypeToCreate::kHostedApp, "d");
EXPECT_EQ(extensions::mojom::ContextType::kPrivilegedWebPage,
map.GetMostLikelyContextType(extension.get(), 5, &web_url));
map.Insert("e", 6);
EXPECT_EQ(extensions::mojom::ContextType::kUntrustedWebUi,
map.GetMostLikelyContextType(/*extension=*/nullptr, 6,
&untrusted_webui_url));
map.Insert("f", 7);
EXPECT_EQ(extensions::mojom::ContextType::kWebPage,
map.GetMostLikelyContextType(/*extension=*/nullptr, 7, &web_url));
}
|