File: credential_manager_client_browsertest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (250 lines) | stat: -rw-r--r-- 8,169 bytes parent folder | download
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/password_manager/content/renderer/credential_manager_client.h"

#include <stdint.h>

#include <memory>
#include <tuple>

#include "base/location.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_view.h"
#include "content/public/test/render_view_test.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/platform/WebCredential.h"
#include "third_party/WebKit/public/platform/WebCredentialManagerClient.h"
#include "third_party/WebKit/public/platform/WebCredentialManagerError.h"
#include "third_party/WebKit/public/platform/WebPasswordCredential.h"

namespace password_manager {

namespace {

const char kTestCredentialPassword[] = "https://password.com/";
const char kTestCredentialEmpty[] = "https://empty.com/";
const char kTestCredentialReject[] = "https://reject.com/";

class FakeCredentialManager : public mojom::CredentialManager {
 public:
  FakeCredentialManager() {}
  ~FakeCredentialManager() override {}

  void BindRequest(mojom::CredentialManagerRequest request) {
    bindings_.AddBinding(this, std::move(request));
  }

 private:
  // mojom::CredentialManager methods:
  void Store(const CredentialInfo& credential,
             const StoreCallback& callback) override {
    callback.Run();
  }

  void RequireUserMediation(
      const RequireUserMediationCallback& callback) override {
    callback.Run();
  }

  void Get(bool zero_click_only,
           bool include_passwords,
           const std::vector<GURL>& federations,
           const GetCallback& callback) override {
    const std::string& url = federations[0].spec();

    if (url == kTestCredentialPassword) {
      CredentialInfo info;
      info.type = CredentialType::CREDENTIAL_TYPE_PASSWORD;
      callback.Run(mojom::CredentialManagerError::SUCCESS, info);
    } else if (url == kTestCredentialEmpty) {
      callback.Run(mojom::CredentialManagerError::SUCCESS, CredentialInfo());
    } else if (url == kTestCredentialReject) {
      callback.Run(mojom::CredentialManagerError::PASSWORDSTOREUNAVAILABLE,
                   base::nullopt);
    }
  }

  mojo::BindingSet<mojom::CredentialManager> bindings_;
};

class CredentialManagerClientTest : public content::RenderViewTest {
 public:
  CredentialManagerClientTest()
      : callback_errored_(false), callback_succeeded_(false) {}
  ~CredentialManagerClientTest() override {}

  void SetUp() override {
    content::RenderViewTest::SetUp();
    client_.reset(new CredentialManagerClient(view_));

    service_manager::InterfaceProvider* remote_interfaces =
        view_->GetMainRenderFrame()->GetRemoteInterfaces();
    service_manager::InterfaceProvider::TestApi test_api(remote_interfaces);
    test_api.SetBinderForName(
        mojom::CredentialManager::Name_,
        base::Bind(&CredentialManagerClientTest::BindCredentialManager,
                   base::Unretained(this)));
  }

  void TearDown() override {
    credential_.reset();
    client_.reset();
    content::RenderViewTest::TearDown();
  }

  bool callback_errored() const { return callback_errored_; }
  void set_callback_errored(bool state) { callback_errored_ = state; }
  bool callback_succeeded() const { return callback_succeeded_; }
  void set_callback_succeeded(bool state) { callback_succeeded_ = state; }

  void BindCredentialManager(mojo::ScopedMessagePipeHandle handle) {
    fake_cm_.BindRequest(
        mojo::MakeRequest<mojom::CredentialManager>(std::move(handle)));
  }

  std::unique_ptr<blink::WebPasswordCredential> credential_;
  blink::WebCredentialManagerError error_;

 protected:
  std::unique_ptr<CredentialManagerClient> client_;

  FakeCredentialManager fake_cm_;

  // True if a message's callback's 'onSuccess'/'onError' methods were called,
  // false otherwise. We put these on the test object rather than on the
  // Test*Callbacks objects because ownership of those objects passes into the
  // client, which destroys the callbacks after calling them to resolve the
  // pending Blink-side Promise.
  bool callback_errored_;
  bool callback_succeeded_;
};

class TestNotificationCallbacks
    : public blink::WebCredentialManagerClient::NotificationCallbacks {
 public:
  explicit TestNotificationCallbacks(CredentialManagerClientTest* test)
      : test_(test) {}

  ~TestNotificationCallbacks() override {}

  void onSuccess() override { test_->set_callback_succeeded(true); }

  void onError(blink::WebCredentialManagerError reason) override {
    test_->set_callback_errored(true);
  }

 private:
  CredentialManagerClientTest* test_;
};

class TestRequestCallbacks
    : public blink::WebCredentialManagerClient::RequestCallbacks {
 public:
  explicit TestRequestCallbacks(CredentialManagerClientTest* test)
      : test_(test) {}

  ~TestRequestCallbacks() override {}

  void onSuccess(std::unique_ptr<blink::WebCredential> credential) override {
    test_->set_callback_succeeded(true);

    blink::WebCredential* ptr = credential.release();
    test_->credential_.reset(static_cast<blink::WebPasswordCredential*>(ptr));
  }

  void onError(blink::WebCredentialManagerError reason) override {
    test_->set_callback_errored(true);
    test_->credential_.reset();
    test_->error_ = reason;
  }

 private:
  CredentialManagerClientTest* test_;
};

void RunAllPendingTasks() {
  base::RunLoop run_loop;
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
  run_loop.Run();
}

}  // namespace

TEST_F(CredentialManagerClientTest, SendStore) {
  credential_.reset(new blink::WebPasswordCredential("", "", "", GURL()));
  std::unique_ptr<TestNotificationCallbacks> callbacks(
      new TestNotificationCallbacks(this));
  client_->dispatchStore(*credential_, callbacks.release());

  RunAllPendingTasks();

  EXPECT_TRUE(callback_succeeded());
  EXPECT_FALSE(callback_errored());
}

TEST_F(CredentialManagerClientTest, SendRequestUserMediation) {
  std::unique_ptr<TestNotificationCallbacks> callbacks(
      new TestNotificationCallbacks(this));
  client_->dispatchRequireUserMediation(callbacks.release());

  RunAllPendingTasks();

  EXPECT_TRUE(callback_succeeded());
  EXPECT_FALSE(callback_errored());
}

TEST_F(CredentialManagerClientTest, SendRequestCredential) {
  std::unique_ptr<TestRequestCallbacks> callbacks(
      new TestRequestCallbacks(this));
  std::vector<GURL> federations;
  federations.push_back(GURL(kTestCredentialPassword));
  client_->dispatchGet(false, true, federations, callbacks.release());

  RunAllPendingTasks();

  EXPECT_TRUE(callback_succeeded());
  EXPECT_FALSE(callback_errored());
  EXPECT_TRUE(credential_);
  EXPECT_EQ("password", credential_->type());
}

TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) {
  std::unique_ptr<TestRequestCallbacks> callbacks(
      new TestRequestCallbacks(this));
  std::vector<GURL> federations;
  federations.push_back(GURL(kTestCredentialEmpty));
  client_->dispatchGet(false, true, federations, callbacks.release());

  RunAllPendingTasks();

  EXPECT_TRUE(callback_succeeded());
  EXPECT_FALSE(callback_errored());
  EXPECT_FALSE(credential_);
}

TEST_F(CredentialManagerClientTest, SendRequestCredentialReject) {
  std::unique_ptr<TestRequestCallbacks> callbacks(
      new TestRequestCallbacks(this));
  std::vector<GURL> federations;
  federations.push_back(GURL(kTestCredentialReject));
  client_->dispatchGet(false, true, federations, callbacks.release());

  RunAllPendingTasks();

  EXPECT_FALSE(callback_succeeded());
  EXPECT_TRUE(callback_errored());
  EXPECT_FALSE(credential_);
  EXPECT_EQ(blink::WebCredentialManagerError::
                WebCredentialManagerPasswordStoreUnavailableError,
            error_);
}

}  // namespace password_manager