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.
7 #include "content/common/media/webrtc_identity_messages.h"
8 #include "content/renderer/media/webrtc_identity_service.h"
9 #include "ipc/ipc_message.h"
10 #include "testing/gtest/include/gtest/gtest.h"
16 static const char FAKE_ORIGIN
[] = "http://fake.com";
17 static const char FAKE_IDENTITY_NAME
[] = "fake identity";
18 static const char FAKE_COMMON_NAME
[] = "fake common name";
19 static const char FAKE_CERTIFICATE
[] = "fake cert";
20 static const char FAKE_PRIVATE_KEY
[] = "fake private key";
21 static const int FAKE_ERROR
= 100;
23 class WebRTCIdentityServiceForTest
: public WebRTCIdentityService
{
25 bool Send(IPC::Message
* message
) override
{
26 messages_
.push_back(*message
);
31 bool OnControlMessageReceived(const IPC::Message
& message
) override
{
32 return WebRTCIdentityService::OnControlMessageReceived(message
);
35 IPC::Message
GetLastMessage() { return messages_
.back(); }
37 int GetNumberOfMessages() { return messages_
.size(); }
39 void ClearMessages() { messages_
.clear(); }
42 std::deque
<IPC::Message
> messages_
;
45 class WebRTCIdentityServiceTest
: public ::testing::Test
{
47 WebRTCIdentityServiceTest()
48 : service_(new WebRTCIdentityServiceForTest()), last_error_(0) {}
51 void OnIdentityReady(const std::string
& cert
, const std::string
& key
) {
52 last_certificate_
= cert
;
53 last_private_key_
= key
;
56 void OnRequestFailed(int error
) { last_error_
= error
; }
58 void ResetRequestResult() {
59 last_certificate_
= "";
60 last_private_key_
= "";
64 int RequestIdentity() {
65 return service_
->RequestIdentity(
69 base::Bind(&WebRTCIdentityServiceTest::OnIdentityReady
,
70 base::Unretained(this)),
71 base::Bind(&WebRTCIdentityServiceTest::OnRequestFailed
,
72 base::Unretained(this)));
75 scoped_ptr
<WebRTCIdentityServiceForTest
> service_
;
76 std::string last_certificate_
;
77 std::string last_private_key_
;
83 TEST_F(WebRTCIdentityServiceTest
, TestSendRequest
) {
86 IPC::Message ipc
= service_
->GetLastMessage();
87 EXPECT_EQ(ipc
.type(), WebRTCIdentityMsg_RequestIdentity::ID
);
90 TEST_F(WebRTCIdentityServiceTest
, TestSuccessCallback
) {
91 int id
= RequestIdentity();
93 service_
->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
94 id
, FAKE_CERTIFICATE
, FAKE_PRIVATE_KEY
));
95 EXPECT_EQ(FAKE_CERTIFICATE
, last_certificate_
);
96 EXPECT_EQ(FAKE_PRIVATE_KEY
, last_private_key_
);
99 TEST_F(WebRTCIdentityServiceTest
, TestFailureCallback
) {
100 int id
= RequestIdentity();
102 service_
->OnControlMessageReceived(
103 WebRTCIdentityHostMsg_RequestFailed(id
, FAKE_ERROR
));
104 EXPECT_EQ(FAKE_ERROR
, last_error_
);
107 TEST_F(WebRTCIdentityServiceTest
, TestCancelRequest
) {
108 int request_id
= RequestIdentity();
109 service_
->ClearMessages();
111 service_
->CancelRequest(request_id
);
113 IPC::Message ipc
= service_
->GetLastMessage();
114 EXPECT_EQ(ipc
.type(), WebRTCIdentityMsg_CancelRequest::ID
);
117 TEST_F(WebRTCIdentityServiceTest
, TestQueuedRequestSentAfterSuccess
) {
118 int id
= RequestIdentity();
120 EXPECT_EQ(1, service_
->GetNumberOfMessages());
121 service_
->ClearMessages();
123 service_
->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
124 id
, FAKE_CERTIFICATE
, FAKE_PRIVATE_KEY
));
126 IPC::Message ipc
= service_
->GetLastMessage();
127 EXPECT_EQ(ipc
.type(), WebRTCIdentityMsg_RequestIdentity::ID
);
130 TEST_F(WebRTCIdentityServiceTest
, TestQueuedRequestSentAfterFailure
) {
131 int id
= RequestIdentity();
133 EXPECT_EQ(1, service_
->GetNumberOfMessages());
134 service_
->ClearMessages();
136 service_
->OnControlMessageReceived(
137 WebRTCIdentityHostMsg_RequestFailed(id
, FAKE_ERROR
));
139 IPC::Message ipc
= service_
->GetLastMessage();
140 EXPECT_EQ(ipc
.type(), WebRTCIdentityMsg_RequestIdentity::ID
);
143 TEST_F(WebRTCIdentityServiceTest
, TestQueuedRequestSentAfterCancelOutstanding
) {
144 int outstand_request_id
= RequestIdentity();
147 EXPECT_EQ(1, service_
->GetNumberOfMessages());
148 service_
->ClearMessages();
150 service_
->CancelRequest(outstand_request_id
);
152 // Should have two messages sent: one for cancelling the outstanding request,
153 // one for requesting the queued request.
154 EXPECT_EQ(2, service_
->GetNumberOfMessages());
155 IPC::Message ipc
= service_
->GetLastMessage();
156 EXPECT_EQ(ipc
.type(), WebRTCIdentityMsg_RequestIdentity::ID
);
159 TEST_F(WebRTCIdentityServiceTest
, TestCancelQueuedRequest
) {
160 int sent_id
= RequestIdentity();
161 int queued_request_id
= RequestIdentity();
162 EXPECT_EQ(1, service_
->GetNumberOfMessages());
163 service_
->ClearMessages();
165 service_
->CancelRequest(queued_request_id
);
167 // Verifies that the queued request is not sent after the outstanding request
169 service_
->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
170 sent_id
, FAKE_CERTIFICATE
, FAKE_PRIVATE_KEY
));
172 EXPECT_EQ(0, service_
->GetNumberOfMessages());
175 TEST_F(WebRTCIdentityServiceTest
, TestQueuedRequestSuccessCallback
) {
176 int id1
= RequestIdentity();
177 int id2
= RequestIdentity();
179 // Completes the outstanding request.
180 service_
->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
181 id1
, FAKE_CERTIFICATE
, FAKE_PRIVATE_KEY
));
182 EXPECT_EQ(FAKE_CERTIFICATE
, last_certificate_
);
183 EXPECT_EQ(FAKE_PRIVATE_KEY
, last_private_key_
);
185 ResetRequestResult();
187 service_
->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
188 id2
, FAKE_CERTIFICATE
, FAKE_PRIVATE_KEY
));
189 EXPECT_EQ(FAKE_CERTIFICATE
, last_certificate_
);
190 EXPECT_EQ(FAKE_PRIVATE_KEY
, last_private_key_
);
193 TEST_F(WebRTCIdentityServiceTest
, TestQueuedRequestFailureCallback
) {
194 int id1
= RequestIdentity();
195 int id2
= RequestIdentity();
197 // Completes the outstanding request.
198 service_
->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
199 id1
, FAKE_CERTIFICATE
, FAKE_PRIVATE_KEY
));
200 EXPECT_EQ(FAKE_CERTIFICATE
, last_certificate_
);
201 EXPECT_EQ(FAKE_PRIVATE_KEY
, last_private_key_
);
203 ResetRequestResult();
205 service_
->OnControlMessageReceived(
206 WebRTCIdentityHostMsg_RequestFailed(id2
, FAKE_ERROR
));
207 EXPECT_EQ(FAKE_ERROR
, last_error_
);
210 // Verifies that receiving a response for a cancelled request does not incur the
212 TEST_F(WebRTCIdentityServiceTest
, TestRequestCompletedAfterCancelled
) {
213 int id1
= RequestIdentity();
215 service_
->CancelRequest(id1
);
217 service_
->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
218 id1
, FAKE_CERTIFICATE
, FAKE_PRIVATE_KEY
));
220 EXPECT_NE(FAKE_CERTIFICATE
, last_certificate_
);
221 EXPECT_NE(FAKE_PRIVATE_KEY
, last_private_key_
);
223 service_
->OnControlMessageReceived(
224 WebRTCIdentityHostMsg_RequestFailed(id1
, FAKE_ERROR
));
225 EXPECT_NE(FAKE_ERROR
, last_error_
);
228 } // namespace content