File: chrome_content_client_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 (157 lines) | stat: -rw-r--r-- 5,639 bytes parent folder | download | duplicates (6)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif

#include "chrome/common/chrome_content_client.h"

#include <string>

#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/test/scoped_command_line.h"
#include "base/threading/platform_thread.h"
#include "build/build_config.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/origin_util.h"
#include "content/public/test/test_utils.h"
#include "extensions/buildflags/buildflags.h"
#include "ppapi/buildflags/buildflags.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_util.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/common/constants.h"
#endif

namespace chrome_common {

TEST(ChromeContentClientTest, AdditionalSchemes) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
  EXPECT_TRUE(url::IsStandard(
      extensions::kExtensionScheme,
      url::Component(0, strlen(extensions::kExtensionScheme))));

  GURL extension_url(
      "chrome-extension://abcdefghijklmnopqrstuvwxyzabcdef/foo.html");
  url::Origin origin = url::Origin::Create(extension_url);
  EXPECT_EQ("chrome-extension://abcdefghijklmnopqrstuvwxyzabcdef",
            origin.Serialize());
#endif

  // IsUrlPotentiallyTrustworthy assertions test for https://crbug.com/734581.
  constexpr const char* kChromeLayerUrlsRegisteredAsSecure[] = {
    // The schemes below are registered both as secure and no-access.  Product
    // code needs to treat such URLs as trustworthy, even though no-access
    // schemes translate into an opaque origin (which is untrustworthy).
    "chrome-native://newtab/",
    "chrome-error://foo/",
    // The schemes below are registered as secure (but not as no-access).
    "chrome://foo/",
    "chrome-untrusted://foo/",
    "chrome-search://foo/",
    "isolated-app://foo/",
#if BUILDFLAG(ENABLE_EXTENSIONS)
    "chrome-extension://foo/",
#endif
    "devtools://foo/",
  };
  for (const std::string& str : kChromeLayerUrlsRegisteredAsSecure) {
    SCOPED_TRACE(str);
    GURL url(str);
    EXPECT_TRUE(base::Contains(url::GetSecureSchemes(), url.scheme()));
    EXPECT_TRUE(network::IsUrlPotentiallyTrustworthy(url));
  }

  GURL chrome_url(content::GetWebUIURL("dummyurl"));
  EXPECT_TRUE(network::IsUrlPotentiallyTrustworthy(chrome_url));
  EXPECT_TRUE(content::OriginCanAccessServiceWorkers(chrome_url));
  EXPECT_TRUE(
      network::IsOriginPotentiallyTrustworthy(url::Origin::Create(chrome_url)));
}

class OriginTrialInitializationTestThread
    : public base::PlatformThread::Delegate {
 public:
  explicit OriginTrialInitializationTestThread(
      ChromeContentClient* chrome_client)
      : chrome_client_(chrome_client) {}

  OriginTrialInitializationTestThread(
      const OriginTrialInitializationTestThread&) = delete;
  OriginTrialInitializationTestThread& operator=(
      const OriginTrialInitializationTestThread&) = delete;

  void ThreadMain() override { AccessPolicy(chrome_client_, &policy_objects_); }

  // Static helper which can also be called from the main thread.
  static void AccessPolicy(
      ChromeContentClient* content_client,
      std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>*
          policy_objects) {
    // Repeatedly access the lazily-created origin trial policy
    for (int i = 0; i < 20; i++) {
      blink::OriginTrialPolicy* policy = content_client->GetOriginTrialPolicy();
      policy_objects->push_back(policy);
      base::PlatformThread::YieldCurrentThread();
    }
  }

  const std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>*
  policy_objects() const {
    return &policy_objects_;
  }

 private:
  raw_ptr<ChromeContentClient> chrome_client_;
  std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>
      policy_objects_;
};

// Test that the lazy initialization of Origin Trial policy is resistant to
// races with concurrent access. Failures (especially flaky) indicate that the
// race prevention is no longer sufficient.
TEST(ChromeContentClientTest, OriginTrialPolicyConcurrentInitialization) {
  ChromeContentClient content_client;
  std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>
      policy_objects;
  OriginTrialInitializationTestThread thread(&content_client);
  base::PlatformThreadHandle handle;

  ASSERT_TRUE(base::PlatformThread::Create(0, &thread, &handle));

  // Repeatedly access the lazily-created origin trial policy
  OriginTrialInitializationTestThread::AccessPolicy(&content_client,
                                                    &policy_objects);

  base::PlatformThread::Join(handle);

  ASSERT_EQ(20UL, policy_objects.size());

  blink::OriginTrialPolicy* first_policy = policy_objects[0];

  const std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>*
      all_policy_objects[] = {
          &policy_objects,
          thread.policy_objects(),
      };

  for (const std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>*
           thread_policy_objects : all_policy_objects) {
    EXPECT_GE(20UL, thread_policy_objects->size());
    for (blink::OriginTrialPolicy* policy : *(thread_policy_objects)) {
      EXPECT_EQ(first_policy, policy);
    }
  }
}

}  // namespace chrome_common