Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / content / renderer / media / webrtc_identity_service_unittest.cc
blobcddbd0caf3f9669a34ed26a082c0474ece220e8d
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <deque>
7 #include "content/renderer/media/webrtc_identity_service.h"
8 #include "ipc/ipc_message.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace content {
13 namespace {
15 static const char FAKE_URL[] = "http://fake.com";
16 static const char FAKE_IDENTITY_NAME[] = "fake identity";
17 static const char FAKE_COMMON_NAME[] = "fake common name";
18 static const char FAKE_CERTIFICATE[] = "fake cert";
19 static const char FAKE_PRIVATE_KEY[] = "fake private key";
20 static const int FAKE_ERROR = 100;
22 class WebRtcIdentityServiceForTest : public WebRTCIdentityService {
23 public:
24 bool Send(IPC::Message* message) override {
25 messages_.push_back(*message);
26 delete message;
27 return true;
30 bool OnControlMessageReceived(const IPC::Message& message) override {
31 return WebRTCIdentityService::OnControlMessageReceived(message);
34 IPC::Message GetLastMessage() { return messages_.back(); }
36 int GetNumberOfMessages() { return messages_.size(); }
38 void ClearMessages() { messages_.clear(); }
40 private:
41 std::deque<IPC::Message> messages_;
44 class WebRtcIdentityServiceTest : public ::testing::Test {
45 public:
46 WebRtcIdentityServiceTest()
47 : service_(new WebRtcIdentityServiceForTest()), last_error_(0) {}
49 protected:
50 void OnIdentityReady(const std::string& cert, const std::string& key) {
51 last_certificate_ = cert;
52 last_private_key_ = key;
55 void OnRequestFailed(int error) { last_error_ = error; }
57 void ResetRequestResult() {
58 last_certificate_ = "";
59 last_private_key_ = "";
60 last_error_ = 0;
63 int RequestIdentity() {
64 return service_->RequestIdentity(
65 GURL(FAKE_URL), GURL(FAKE_URL), FAKE_IDENTITY_NAME, FAKE_COMMON_NAME,
66 base::Bind(&WebRtcIdentityServiceTest::OnIdentityReady,
67 base::Unretained(this)),
68 base::Bind(&WebRtcIdentityServiceTest::OnRequestFailed,
69 base::Unretained(this)));
72 scoped_ptr<WebRtcIdentityServiceForTest> service_;
73 std::string last_certificate_;
74 std::string last_private_key_;
75 int last_error_;
78 } // namespace
80 TEST_F(WebRtcIdentityServiceTest, TestSendRequest) {
81 RequestIdentity();
83 IPC::Message ipc = service_->GetLastMessage();
84 EXPECT_EQ(ipc.type(), WebRTCIdentityMsg_RequestIdentity::ID);
87 TEST_F(WebRtcIdentityServiceTest, TestSuccessCallback) {
88 int id = RequestIdentity();
90 service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
91 id, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
92 EXPECT_EQ(FAKE_CERTIFICATE, last_certificate_);
93 EXPECT_EQ(FAKE_PRIVATE_KEY, last_private_key_);
96 TEST_F(WebRtcIdentityServiceTest, TestFailureCallback) {
97 int id = RequestIdentity();
99 service_->OnControlMessageReceived(
100 WebRTCIdentityHostMsg_RequestFailed(id, FAKE_ERROR));
101 EXPECT_EQ(FAKE_ERROR, last_error_);
104 TEST_F(WebRtcIdentityServiceTest, TestCancelRequest) {
105 int request_id = RequestIdentity();
106 service_->ClearMessages();
108 service_->CancelRequest(request_id);
110 IPC::Message ipc = service_->GetLastMessage();
111 EXPECT_EQ(ipc.type(), WebRTCIdentityMsg_CancelRequest::ID);
114 TEST_F(WebRtcIdentityServiceTest, TestQueuedRequestSentAfterSuccess) {
115 int id = RequestIdentity();
116 RequestIdentity();
117 EXPECT_EQ(1, service_->GetNumberOfMessages());
118 service_->ClearMessages();
120 service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
121 id, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
123 IPC::Message ipc = service_->GetLastMessage();
124 EXPECT_EQ(ipc.type(), WebRTCIdentityMsg_RequestIdentity::ID);
127 TEST_F(WebRtcIdentityServiceTest, TestQueuedRequestSentAfterFailure) {
128 int id = RequestIdentity();
129 RequestIdentity();
130 EXPECT_EQ(1, service_->GetNumberOfMessages());
131 service_->ClearMessages();
133 service_->OnControlMessageReceived(
134 WebRTCIdentityHostMsg_RequestFailed(id, FAKE_ERROR));
136 IPC::Message ipc = service_->GetLastMessage();
137 EXPECT_EQ(ipc.type(), WebRTCIdentityMsg_RequestIdentity::ID);
140 TEST_F(WebRtcIdentityServiceTest, TestQueuedRequestSentAfterCancelOutstanding) {
141 int outstand_request_id = RequestIdentity();
142 RequestIdentity();
144 EXPECT_EQ(1, service_->GetNumberOfMessages());
145 service_->ClearMessages();
147 service_->CancelRequest(outstand_request_id);
149 // Should have two messages sent: one for cancelling the outstanding request,
150 // one for requesting the queued request.
151 EXPECT_EQ(2, service_->GetNumberOfMessages());
152 IPC::Message ipc = service_->GetLastMessage();
153 EXPECT_EQ(ipc.type(), WebRTCIdentityMsg_RequestIdentity::ID);
156 TEST_F(WebRtcIdentityServiceTest, TestCancelQueuedRequest) {
157 int sent_id = RequestIdentity();
158 int queued_request_id = RequestIdentity();
159 EXPECT_EQ(1, service_->GetNumberOfMessages());
160 service_->ClearMessages();
162 service_->CancelRequest(queued_request_id);
164 // Verifies that the queued request is not sent after the outstanding request
165 // returns.
166 service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
167 sent_id, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
169 EXPECT_EQ(0, service_->GetNumberOfMessages());
172 TEST_F(WebRtcIdentityServiceTest, TestQueuedRequestSuccessCallback) {
173 int id1 = RequestIdentity();
174 int id2 = RequestIdentity();
176 // Completes the outstanding request.
177 service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
178 id1, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
179 EXPECT_EQ(FAKE_CERTIFICATE, last_certificate_);
180 EXPECT_EQ(FAKE_PRIVATE_KEY, last_private_key_);
182 ResetRequestResult();
184 service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
185 id2, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
186 EXPECT_EQ(FAKE_CERTIFICATE, last_certificate_);
187 EXPECT_EQ(FAKE_PRIVATE_KEY, last_private_key_);
190 TEST_F(WebRtcIdentityServiceTest, TestQueuedRequestFailureCallback) {
191 int id1 = RequestIdentity();
192 int id2 = RequestIdentity();
194 // Completes the outstanding request.
195 service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
196 id1, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
197 EXPECT_EQ(FAKE_CERTIFICATE, last_certificate_);
198 EXPECT_EQ(FAKE_PRIVATE_KEY, last_private_key_);
200 ResetRequestResult();
202 service_->OnControlMessageReceived(
203 WebRTCIdentityHostMsg_RequestFailed(id2, FAKE_ERROR));
204 EXPECT_EQ(FAKE_ERROR, last_error_);
207 // Verifies that receiving a response for a cancelled request does not incur the
208 // callbacks.
209 TEST_F(WebRtcIdentityServiceTest, TestRequestCompletedAfterCancelled) {
210 int id1 = RequestIdentity();
211 RequestIdentity();
212 service_->CancelRequest(id1);
214 service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
215 id1, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
217 EXPECT_NE(FAKE_CERTIFICATE, last_certificate_);
218 EXPECT_NE(FAKE_PRIVATE_KEY, last_private_key_);
220 service_->OnControlMessageReceived(
221 WebRTCIdentityHostMsg_RequestFailed(id1, FAKE_ERROR));
222 EXPECT_NE(FAKE_ERROR, last_error_);
225 } // namespace content