File: identity_registry_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (86 lines) | stat: -rw-r--r-- 2,676 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
// Copyright 2023 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/browser/webid/identity_registry.h"

#include "content/browser/webid/test/mock_modal_dialog_view_delegate.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"

using ::testing::NiceMock;

namespace content {

namespace {

constexpr char kRpUrl[] = "https://rp.example/";
constexpr char kIdpUrl[] = "https://idp.example/";

}  // namespace

class TestFederatedIdentityModalDialogViewDelegate
    : public NiceMock<MockModalDialogViewDelegate> {
 public:
  bool closed_{false};

  void OnClose() override { closed_ = true; }

  base::WeakPtr<TestFederatedIdentityModalDialogViewDelegate> GetWeakPtr() {
    return weak_ptr_factory_.GetWeakPtr();
  }

 private:
  base::WeakPtrFactory<TestFederatedIdentityModalDialogViewDelegate>
      weak_ptr_factory_{this};
};

class IdentityRegistryTest : public RenderViewHostImplTestHarness {
 protected:
  IdentityRegistryTest() = default;
  ~IdentityRegistryTest() override = default;

  void SetUp() override {
    RenderViewHostImplTestHarness::SetUp();

    test_delegate_ =
        std::make_unique<TestFederatedIdentityModalDialogViewDelegate>();

    IdentityRegistry::CreateForWebContents(
        web_contents(), test_delegate_->GetWeakPtr(), GURL(kIdpUrl));
    identity_registry_ = IdentityRegistry::FromWebContents(web_contents());

    static_cast<TestWebContents*>(web_contents())
        ->NavigateAndCommit(GURL(kRpUrl), ui::PAGE_TRANSITION_LINK);
  }
  void TearDown() override {
    identity_registry_ = nullptr;
    RenderViewHostImplTestHarness::TearDown();
  }

  std::unique_ptr<TestFederatedIdentityModalDialogViewDelegate> test_delegate_;
  raw_ptr<IdentityRegistry> identity_registry_;
};

// If notifier origin and registry origin are same-origin, modal dialog should
// be closed.
TEST_F(IdentityRegistryTest, NotifierAndRegistrySameOrigin) {
  EXPECT_FALSE(test_delegate_->closed_);
  identity_registry_->NotifyClose(url::Origin::Create(GURL(kIdpUrl)));
  EXPECT_TRUE(test_delegate_->closed_);
}

// If notifier origin and registry origin are cross-origin, modal dialog should
// remain open.
TEST_F(IdentityRegistryTest, NotifierAndRegistryCrossOrigin) {
  EXPECT_FALSE(test_delegate_->closed_);
  identity_registry_->NotifyClose(
      url::Origin::Create(GURL("https://cross-origin.example")));
  EXPECT_FALSE(test_delegate_->closed_);
}

}  // namespace content