Simplify ChildProcessLauncher
[chromium-blink-merge.git] / content / browser / renderer_host / media / webrtc_identity_service_host_unittest.cc
blobc420942016967c746d629b8dc0d71132b8e3efe9
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 base::Closure RequestIdentity(const GURL& origin,
33 const std::string& identity_name,
34 const std::string& common_name,
35 const CompletionCallback& callback) override {
36 EXPECT_TRUE(callback_.is_null());
38 callback_ = callback;
39 return base::Bind(&MockWebRTCIdentityStore::OnCancel,
40 base::Unretained(this));
43 bool HasPendingRequest() const { return !callback_.is_null(); }
45 void RunCompletionCallback(int error,
46 const std::string& cert,
47 const std::string& key) {
48 callback_.Run(error, cert, key);
49 callback_.Reset();
52 private:
53 ~MockWebRTCIdentityStore() override {}
55 void OnCancel() { callback_.Reset(); }
57 CompletionCallback callback_;
60 class WebRTCIdentityServiceHostForTest : public WebRTCIdentityServiceHost {
61 public:
62 explicit WebRTCIdentityServiceHostForTest(WebRTCIdentityStore* identity_store)
63 : WebRTCIdentityServiceHost(FAKE_RENDERER_ID, identity_store) {
64 ChildProcessSecurityPolicyImpl* policy =
65 ChildProcessSecurityPolicyImpl::GetInstance();
66 policy->Add(FAKE_RENDERER_ID);
69 bool Send(IPC::Message* message) override {
70 messages_.push_back(*message);
71 delete message;
72 return true;
75 bool OnMessageReceived(const IPC::Message& message) override {
76 return WebRTCIdentityServiceHost::OnMessageReceived(message);
79 IPC::Message GetLastMessage() { return messages_.back(); }
81 int GetNumberOfMessages() { return messages_.size(); }
83 void ClearMessages() { messages_.clear(); }
85 private:
86 ~WebRTCIdentityServiceHostForTest() override {
87 ChildProcessSecurityPolicyImpl* policy =
88 ChildProcessSecurityPolicyImpl::GetInstance();
89 policy->Remove(FAKE_RENDERER_ID);
92 std::deque<IPC::Message> messages_;
95 class WebRTCIdentityServiceHostTest : public ::testing::Test {
96 public:
97 WebRTCIdentityServiceHostTest()
98 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
99 store_(new MockWebRTCIdentityStore()),
100 host_(new WebRTCIdentityServiceHostForTest(store_.get())) {}
102 void SendRequestToHost() {
103 host_->OnMessageReceived(
104 WebRTCIdentityMsg_RequestIdentity(FAKE_SEQUENCE_NUMBER,
105 GURL(FAKE_ORIGIN),
106 FAKE_IDENTITY_NAME,
107 FAKE_COMMON_NAME));
110 void SendCancelRequestToHost() {
111 host_->OnMessageReceived(WebRTCIdentityMsg_CancelRequest());
114 void VerifyRequestFailedMessage(int error) {
115 EXPECT_EQ(1, host_->GetNumberOfMessages());
116 IPC::Message ipc = host_->GetLastMessage();
117 EXPECT_EQ(ipc.type(), WebRTCIdentityHostMsg_RequestFailed::ID);
119 Tuple<int, int> error_in_message;
120 WebRTCIdentityHostMsg_RequestFailed::Read(&ipc, &error_in_message);
121 EXPECT_EQ(FAKE_SEQUENCE_NUMBER, get<0>(error_in_message));
122 EXPECT_EQ(error, get<1>(error_in_message));
125 void VerifyIdentityReadyMessage(const std::string& cert,
126 const std::string& key) {
127 EXPECT_EQ(1, host_->GetNumberOfMessages());
128 IPC::Message ipc = host_->GetLastMessage();
129 EXPECT_EQ(ipc.type(), WebRTCIdentityHostMsg_IdentityReady::ID);
131 Tuple<int, std::string, std::string> identity_in_message;
132 WebRTCIdentityHostMsg_IdentityReady::Read(&ipc, &identity_in_message);
133 EXPECT_EQ(FAKE_SEQUENCE_NUMBER, get<0>(identity_in_message));
134 EXPECT_EQ(cert, get<1>(identity_in_message));
135 EXPECT_EQ(key, get<2>(identity_in_message));
138 protected:
139 TestBrowserThreadBundle browser_thread_bundle_;
140 scoped_refptr<MockWebRTCIdentityStore> store_;
141 scoped_refptr<WebRTCIdentityServiceHostForTest> host_;
144 } // namespace
146 TEST_F(WebRTCIdentityServiceHostTest, TestSendAndCancelRequest) {
147 SendRequestToHost();
148 EXPECT_TRUE(store_->HasPendingRequest());
149 SendCancelRequestToHost();
150 EXPECT_FALSE(store_->HasPendingRequest());
153 TEST_F(WebRTCIdentityServiceHostTest, TestOnlyOneRequestAllowed) {
154 SendRequestToHost();
155 EXPECT_TRUE(store_->HasPendingRequest());
156 EXPECT_EQ(0, host_->GetNumberOfMessages());
157 SendRequestToHost();
159 VerifyRequestFailedMessage(net::ERR_INSUFFICIENT_RESOURCES);
162 TEST_F(WebRTCIdentityServiceHostTest, TestOnIdentityReady) {
163 SendRequestToHost();
164 store_->RunCompletionCallback(net::OK, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY);
165 VerifyIdentityReadyMessage(FAKE_CERTIFICATE, FAKE_PRIVATE_KEY);
168 TEST_F(WebRTCIdentityServiceHostTest, TestOnRequestFailed) {
169 SendRequestToHost();
170 store_->RunCompletionCallback(net::ERR_KEY_GENERATION_FAILED, "", "");
171 VerifyRequestFailedMessage(net::ERR_KEY_GENERATION_FAILED);
174 TEST_F(WebRTCIdentityServiceHostTest, TestOriginAccessDenied) {
175 ChildProcessSecurityPolicyImpl* policy =
176 ChildProcessSecurityPolicyImpl::GetInstance();
177 policy->Remove(FAKE_RENDERER_ID);
179 SendRequestToHost();
180 VerifyRequestFailedMessage(net::ERR_ACCESS_DENIED);
183 // Verifies that we do not crash if we try to cancel a completed request.
184 TEST_F(WebRTCIdentityServiceHostTest, TestCancelAfterRequestCompleted) {
185 SendRequestToHost();
186 store_->RunCompletionCallback(net::OK, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY);
187 SendCancelRequestToHost();
190 } // namespace content