File: certificate_selector_browsertest.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 (163 lines) | stat: -rw-r--r-- 5,541 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
158
159
160
161
162
163
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/certificate_selector.h"

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/cert/x509_certificate.h"
#include "net/ssl/client_cert_identity_test_util.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_data_directory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/models/table_model.h"
#include "ui/views/controls/label.h"

namespace {

class TestCertificateSelector : public CertificateSelector {
 public:
  TestCertificateSelector(net::ClientCertIdentityList certificates,
                          content::WebContents* web_contents)
      : CertificateSelector(std::move(certificates), web_contents) {}

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

  ~TestCertificateSelector() override {
    if (!on_destroy_.is_null()) {
      std::move(on_destroy_).Run();
    }
  }

  void Init() {
    InitWithText(std::make_unique<views::Label>(u"some arbitrary text"));
  }

  void AcceptCertificate(
      std::unique_ptr<net::ClientCertIdentity> identity) override {
    if (accepted_) {
      *accepted_ = true;
    }
  }

  bool Cancel() override {
    if (canceled_) {
      *canceled_ = true;
    }
    return CertificateSelector::Cancel();
  }

  void TrackState(bool* accepted, bool* canceled) {
    accepted_ = accepted;
    canceled_ = canceled;
  }

  using CertificateSelector::table_model_for_testing;

  void set_on_destroy(base::OnceClosure on_destroy) {
    on_destroy_ = std::move(on_destroy);
  }

 private:
  raw_ptr<bool> accepted_ = nullptr;
  raw_ptr<bool> canceled_ = nullptr;
  base::OnceClosure on_destroy_;
};

class CertificateSelectorTest : public InProcessBrowserTest {
 public:
  void SetUpInProcessBrowserTestFixture() override {
    client_1_ =
        net::ImportCertFromFile(net::GetTestCertsDirectory(), "client_1.pem");
    ASSERT_TRUE(client_1_);

    client_2_ =
        net::ImportCertFromFile(net::GetTestCertsDirectory(), "client_2.pem");
    ASSERT_TRUE(client_2_);
  }

  void SetUpOnMainThread() override {
    ASSERT_TRUE(content::WaitForLoadStop(
        browser()->tab_strip_model()->GetActiveWebContents()));

    selector_ = new TestCertificateSelector(
        net::FakeClientCertIdentityListFromCertificateList(
            {client_1_, client_2_}),
        browser()->tab_strip_model()->GetActiveWebContents());
    selector_->Init();
    selector_->Show();
  }

 protected:
  scoped_refptr<net::X509Certificate> client_1_;
  scoped_refptr<net::X509Certificate> client_2_;

  // The selector will be owned by the Views hierarchy and will at latest be
  // deleted during the browser shutdown.
  raw_ptr<TestCertificateSelector, AcrossTasksDanglingUntriaged> selector_ =
      nullptr;
};

}  // namespace

IN_PROC_BROWSER_TEST_F(CertificateSelectorTest, GetRowText) {
  ui::TableModel* model = selector_->table_model_for_testing();
  EXPECT_EQ(u"Client Cert A",
            model->GetText(0, IDS_CERT_SELECTOR_SUBJECT_COLUMN));
  EXPECT_EQ(u"B CA", model->GetText(0, IDS_CERT_SELECTOR_ISSUER_COLUMN));
  EXPECT_EQ(std::u16string(),
            model->GetText(0, IDS_CERT_SELECTOR_PROVIDER_COLUMN));
  EXPECT_EQ(u"1000", model->GetText(0, IDS_CERT_SELECTOR_SERIAL_COLUMN));

  EXPECT_EQ(u"Client Cert D",
            model->GetText(1, IDS_CERT_SELECTOR_SUBJECT_COLUMN));
  EXPECT_EQ(u"E CA", model->GetText(1, IDS_CERT_SELECTOR_ISSUER_COLUMN));
  EXPECT_EQ(std::u16string(),
            model->GetText(1, IDS_CERT_SELECTOR_PROVIDER_COLUMN));
  EXPECT_EQ(u"1002", model->GetText(1, IDS_CERT_SELECTOR_SERIAL_COLUMN));
}

IN_PROC_BROWSER_TEST_F(CertificateSelectorTest, GetSelectedCert) {
  ASSERT_TRUE(selector_->GetSelectedCert());
  EXPECT_EQ(client_1_.get(), selector_->GetSelectedCert()->certificate());
  EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_DOWN, false,
                                              false, false, false));
  ASSERT_TRUE(selector_->GetSelectedCert());
  EXPECT_EQ(client_2_.get(), selector_->GetSelectedCert()->certificate());
  EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_UP, false,
                                              false, false, false));
  ASSERT_TRUE(selector_->GetSelectedCert());
  EXPECT_EQ(client_1_.get(), selector_->GetSelectedCert()->certificate());
}

IN_PROC_BROWSER_TEST_F(CertificateSelectorTest, DoubleClick) {
  bool accepted = false;
  bool canceled = false;
  selector_->TrackState(&accepted, &canceled);

  base::RunLoop loop;
  selector_->set_on_destroy(loop.QuitClosure());

  // Simulate double clicking on an entry in the certificate list.
  selector_->OnDoubleClick();

  // Wait for the dialog to be closed and destroyed.
  loop.Run();

  // Closing the dialog through a double click must call only the Accept()
  // function and not Cancel().
  EXPECT_TRUE(accepted);
  EXPECT_FALSE(canceled);
}