File: extension_util_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (106 lines) | stat: -rw-r--r-- 4,549 bytes parent folder | download | duplicates (3)
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
// Copyright 2019 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/extension_util.h"

#include "base/memory/ref_counted.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "build/android_buildflags.h"
#include "content/public/browser/site_instance.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_content_client_initializer.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/extension_paths.h"
#include "extensions/common/extension_set.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/url_constants.h"

namespace extensions {
namespace {
// Returns a barebones test Extension object with the given name.
static scoped_refptr<const Extension> CreateExtension(const std::string& name) {
  base::FilePath path;
  base::PathService::Get(DIR_TEST_DATA, &path);

  return ExtensionBuilder(name).SetPath(path.AppendASCII(name)).Build();
}
}  // namespace

// Tests that extension URLs are properly mapped to local file paths.
TEST(ExtensionUtilTest, MapUrlToLocalFilePath) {
  scoped_refptr<const Extension> app(CreateExtension("platform_app"));
  ExtensionSet extensions;
  extensions.Insert(app);

  // Non-extension URLs don't map to anything.
  base::FilePath non_extension_path;
  GURL non_extension_url("http://not-an-extension.com/");
  EXPECT_FALSE(util::MapUrlToLocalFilePath(&extensions, non_extension_url,
                                           false, &non_extension_path));
  EXPECT_TRUE(non_extension_path.empty());

  // Valid resources return a valid path.
  base::FilePath valid_path;
  GURL valid_url = app->ResolveExtensionURL("manifest.json");
  EXPECT_TRUE(util::MapUrlToLocalFilePath(
      &extensions, valid_url, true /* use_blocking_api */, &valid_path));
  EXPECT_FALSE(valid_path.empty());

  // A file must exist to be mapped to a path using the blocking API.
  base::FilePath does_not_exist_path;
  GURL does_not_exist_url = app->ResolveExtensionURL("does-not-exist.html");
  EXPECT_FALSE(util::MapUrlToLocalFilePath(&extensions, does_not_exist_url,
                                           true /* use_blocking_api */,
                                           &does_not_exist_path));
  EXPECT_TRUE(does_not_exist_path.empty());

  // A file does not need to exist to be mapped to a path with the non-blocking
  // API. This avoids hitting the disk to see if it exists.
  EXPECT_TRUE(util::MapUrlToLocalFilePath(&extensions, does_not_exist_url,
                                          false /* use_blocking_api */,
                                          &does_not_exist_path));
  EXPECT_FALSE(does_not_exist_path.empty());
}

// TODO(https://crbug.com/356905053):Strict site isolation is not enabled on
// Android, so this test is disabled on desktop android.
#if BUILDFLAG(IS_DESKTOP_ANDROID)
#define MAYBE_ExtensionIdForSiteInstance DISABLED_ExtensionIdForSiteInstance
#else
#define MAYBE_ExtensionIdForSiteInstance ExtensionIdForSiteInstance
#endif
TEST(ExtensionUtilTest, MAYBE_ExtensionIdForSiteInstance) {
  content::BrowserTaskEnvironment test_environment;
  content::TestBrowserContext test_context;

  // Extension.
  const ExtensionId kExtensionId1(32, 'a');
  scoped_refptr<content::SiteInstance> extension_site_instance =
      content::SiteInstance::CreateForURL(
          &test_context, Extension::GetBaseURLFromExtensionId(kExtensionId1));
  EXPECT_EQ(kExtensionId1,
            util::GetExtensionIdForSiteInstance(*extension_site_instance));

  // GuestView.
  const ExtensionId kExtensionId2(32, 'b');
  scoped_refptr<content::SiteInstance> guest_site_instance =
      content::SiteInstance::CreateForGuest(
          &test_context,
          content::StoragePartitionConfig::Create(&test_context, kExtensionId2,
                                                  "fake_storage_partition_id",
                                                  true /* in_memory */));
  EXPECT_EQ(kExtensionId2,
            util::GetExtensionIdForSiteInstance(*guest_site_instance));

  // Http.
  scoped_refptr<content::SiteInstance> https_site_instance =
      content::SiteInstance::CreateForURL(&test_context,
                                          GURL("https://example.com"));
  EXPECT_EQ("", util::GetExtensionIdForSiteInstance(*https_site_instance));
}

}  // namespace extensions