Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / media / webrtc_identity_service_host_unittest.cc
blob9e336d1db627d4bad87771d134f66a60a1449aae
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/browser/child_process_security_policy_impl.h"
8 #include "content/browser/media/webrtc_identity_store.h"
9 #include "content/browser/renderer_host/media/webrtc_identity_service_host.h"
10 #include "content/common/media/webrtc_identity_messages.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "ipc/ipc_message.h"
13 #include "net/base/net_errors.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace content {
18 namespace {
20 const char FAKE_ORIGIN[] = "http://fake.com";
21 const char FAKE_IDENTITY_NAME[] = "fake identity";
22 const char FAKE_COMMON_NAME[] = "fake common name";
23 const char FAKE_CERTIFICATE[] = "fake cert";
24 const char FAKE_PRIVATE_KEY[] = "fake private key";
25 const int FAKE_RENDERER_ID = 10;
26 const int FAKE_SEQUENCE_NUMBER = 1;
28 class MockWebRTCIdentityStore : public WebRTCIdentityStore {
29 public:
30 MockWebRTCIdentityStore() : WebRTCIdentityStore(base::FilePath(), NULL) {}
32 virtual base::Closure RequestIdentity(
33 const GURL& origin,
34 const std::string& identity_name,
35 const std::string& common_name,
36 const CompletionCallback& callback) OVERRIDE {
37 EXPECT_TRUE(callback_.is_null());
39 callback_ = callback;
40 return base::Bind(&MockWebRTCIdentityStore::OnCancel,
41 base::Unretained(this));
44 bool HasPendingRequest() const { return !callback_.is_null(); }
46 void RunCompletionCallback(int error,
47 const std::string& cert,
48 const std::string& key) {
49 callback_.Run(error, cert, key);
50 callback_.Reset();
53 private:
54 virtual ~MockWebRTCIdentityStore() {}
56 void OnCancel() { callback_.Reset(); }
58 CompletionCallback callback_;
61 class WebRTCIdentityServiceHostForTest : public WebRTCIdentityServiceHost {
62 public:
63 explicit WebRTCIdentityServiceHostForTest(WebRTCIdentityStore* identity_store)
64 : WebRTCIdentityServiceHost(FAKE_RENDERER_ID, identity_store) {
65 ChildProcessSecurityPolicyImpl* policy =
66 ChildProcessSecurityPolicyImpl::GetInstance();
67 policy->Add(FAKE_RENDERER_ID);
70 virtual bool Send(IPC::Message* message) OVERRIDE {
71 messages_.push_back(*message);
72 delete message;
73 return true;
76 virtual bool OnMessageReceived(const IPC::Message& message,
77 bool* message_was_ok) OVERRIDE {
78 return WebRTCIdentityServiceHost::OnMessageReceived(message,
79 message_was_ok);
82 IPC::Message GetLastMessage() { return messages_.back(); }
84 int GetNumberOfMessages() { return messages_.size(); }
86 void ClearMessages() { messages_.clear(); }
88 private:
89 virtual ~WebRTCIdentityServiceHostForTest() {
90 ChildProcessSecurityPolicyImpl* policy =
91 ChildProcessSecurityPolicyImpl::GetInstance();
92 policy->Remove(FAKE_RENDERER_ID);
95 std::deque<IPC::Message> messages_;
98 class WebRTCIdentityServiceHostTest : public ::testing::Test {
99 public:
100 WebRTCIdentityServiceHostTest()
101 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
102 store_(new MockWebRTCIdentityStore()),
103 host_(new WebRTCIdentityServiceHostForTest(store_.get())) {}
105 void SendRequestToHost() {
106 bool ok;
107 host_->OnMessageReceived(
108 WebRTCIdentityMsg_RequestIdentity(FAKE_SEQUENCE_NUMBER,
109 GURL(FAKE_ORIGIN),
110 FAKE_IDENTITY_NAME,
111 FAKE_COMMON_NAME),
112 &ok);
113 ASSERT_TRUE(ok);
116 void SendCancelRequestToHost() {
117 bool ok;
118 host_->OnMessageReceived(WebRTCIdentityMsg_CancelRequest(), &ok);
119 ASSERT_TRUE(ok);
122 void VerifyRequestFailedMessage(int error) {
123 EXPECT_EQ(1, host_->GetNumberOfMessages());
124 IPC::Message ipc = host_->GetLastMessage();
125 EXPECT_EQ(ipc.type(), WebRTCIdentityHostMsg_RequestFailed::ID);
127 Tuple2<int, int> error_in_message;
128 WebRTCIdentityHostMsg_RequestFailed::Read(&ipc, &error_in_message);
129 EXPECT_EQ(FAKE_SEQUENCE_NUMBER, error_in_message.a);
130 EXPECT_EQ(error, error_in_message.b);
133 void VerifyIdentityReadyMessage(const std::string& cert,
134 const std::string& key) {
135 EXPECT_EQ(1, host_->GetNumberOfMessages());
136 IPC::Message ipc = host_->GetLastMessage();
137 EXPECT_EQ(ipc.type(), WebRTCIdentityHostMsg_IdentityReady::ID);
139 Tuple3<int, std::string, std::string> identity_in_message;
140 WebRTCIdentityHostMsg_IdentityReady::Read(&ipc, &identity_in_message);
141 EXPECT_EQ(FAKE_SEQUENCE_NUMBER, identity_in_message.a);
142 EXPECT_EQ(cert, identity_in_message.b);
143 EXPECT_EQ(key, identity_in_message.c);
146 protected:
147 TestBrowserThreadBundle browser_thread_bundle_;
148 scoped_refptr<MockWebRTCIdentityStore> store_;
149 scoped_refptr<WebRTCIdentityServiceHostForTest> host_;
152 } // namespace
154 TEST_F(WebRTCIdentityServiceHostTest, TestSendAndCancelRequest) {
155 SendRequestToHost();
156 EXPECT_TRUE(store_->HasPendingRequest());
157 SendCancelRequestToHost();
158 EXPECT_FALSE(store_->HasPendingRequest());
161 TEST_F(WebRTCIdentityServiceHostTest, TestOnlyOneRequestAllowed) {
162 SendRequestToHost();
163 EXPECT_TRUE(store_->HasPendingRequest());
164 EXPECT_EQ(0, host_->GetNumberOfMessages());
165 SendRequestToHost();
167 VerifyRequestFailedMessage(net::ERR_INSUFFICIENT_RESOURCES);
170 TEST_F(WebRTCIdentityServiceHostTest, TestOnIdentityReady) {
171 SendRequestToHost();
172 store_->RunCompletionCallback(net::OK, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY);
173 VerifyIdentityReadyMessage(FAKE_CERTIFICATE, FAKE_PRIVATE_KEY);
176 TEST_F(WebRTCIdentityServiceHostTest, TestOnRequestFailed) {
177 SendRequestToHost();
178 store_->RunCompletionCallback(net::ERR_KEY_GENERATION_FAILED, "", "");
179 VerifyRequestFailedMessage(net::ERR_KEY_GENERATION_FAILED);
182 TEST_F(WebRTCIdentityServiceHostTest, TestOriginAccessDenied) {
183 ChildProcessSecurityPolicyImpl* policy =
184 ChildProcessSecurityPolicyImpl::GetInstance();
185 policy->Remove(FAKE_RENDERER_ID);
187 SendRequestToHost();
188 VerifyRequestFailedMessage(net::ERR_ACCESS_DENIED);
191 // Verifies that we do not crash if we try to cancel a completed request.
192 TEST_F(WebRTCIdentityServiceHostTest, TestCancelAfterRequestCompleted) {
193 SendRequestToHost();
194 store_->RunCompletionCallback(net::OK, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY);
195 SendCancelRequestToHost();
198 } // namespace content