File: btm_service_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (102 lines) | stat: -rw-r--r-- 4,267 bytes parent folder | download | duplicates (2)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/browser/btm_service.h"

#include "base/memory/raw_ptr.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/tracking_protection_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/btm_service_test_utils.h"
#include "net/http/http_status_code.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {
bool Has3pcException(BrowserContext* browser_context,
                     WebContents* web_contents,
                     const GURL& url,
                     const GURL& initial_url,
                     const GURL& final_url) {
  BtmRedirectInfoPtr redirect = BtmRedirectInfo::CreateForServer(
      UrlAndSourceId(url, ukm::kInvalidSourceId), BtmDataAccessType::kWrite,
      base::Time::Now(), false, net::HTTP_FOUND, base::TimeDelta());
  Populate3PcExceptions(browser_context, web_contents, initial_url, final_url,
                        base::span_from_ref(redirect));
  return redirect->has_3pc_exception.value();
}

class BtmService3pcExceptionsTest : public testing::Test {
 public:
  BtmService3pcExceptionsTest()
      : cookie_settings_(
            CookieSettingsFactory::GetForProfile(&profile_).get()) {}

  TestingProfile* GetProfile() { return &profile_; }

 protected:
  BrowserTaskEnvironment task_environment_;
  TestingProfile profile_;
  raw_ptr<content_settings::CookieSettings> cookie_settings_;

  void SetUp() override {
    GetProfile()->GetPrefs()->SetBoolean(prefs::kTrackingProtection3pcdEnabled,
                                         true);
    ASSERT_FALSE(Are3PcsGenerallyEnabled());
  }

  // Add an exception to the third-party cookie blocking rule for
  // `third_party_url` embedded by `first_party_url`.
  void Add3PCException(const GURL& first_party_url,
                       const GURL& third_party_url) {
    cookie_settings_->SetTemporaryCookieGrantForHeuristic(
        third_party_url, first_party_url, base::Days(1),
        /*use_schemeless_patterns=*/false);

    auto* client = GetContentClientForTesting()->browser();
    ASSERT_TRUE(client->IsFullCookieAccessAllowed(
        &profile_, nullptr, third_party_url,
        blink::StorageKey::CreateFirstParty(
            url::Origin::Create(first_party_url)),
        /*overrides=*/{}));
    ASSERT_FALSE(client->IsFullCookieAccessAllowed(
        &profile_, nullptr, first_party_url,
        blink::StorageKey::CreateFirstParty(
            url::Origin::Create(third_party_url)),
        /*overrides=*/{}));
  }

  bool Are3PcsGenerallyEnabled() {
    return ::content::Are3PcsGenerallyEnabled(&profile_, nullptr);
  }
};

// Verifies that redirect chains that start or end on embedder URLs do not
// have 3PC exceptions.
TEST_F(BtmService3pcExceptionsTest, EmbedderUrl_DoesNotHaveException) {
  GURL non_web_url(chrome::kChromeUINewTabURL);
  GURL redirect_url("https://redirect.com");

  EXPECT_FALSE(Has3pcException(GetProfile(), nullptr, redirect_url, non_web_url,
                               GURL("https://final.com")));
  EXPECT_FALSE(Has3pcException(GetProfile(), nullptr, redirect_url,
                               GURL("https://initial.com"), non_web_url));

  // Verify that exception logic is working as expected for HTTP(S) URLs.
  EXPECT_FALSE(Has3pcException(GetProfile(), nullptr, redirect_url,
                               GURL("https://initial.com"),
                               GURL("https://final.com")));
  Add3PCException(GURL("https://initial.com"), redirect_url);
  EXPECT_TRUE(Has3pcException(GetProfile(), nullptr, redirect_url,
                              GURL("https://initial.com"),
                              GURL("https://final.com")));
}

}  // namespace content