OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <deque> | |
6 #include <tuple> | |
7 | |
8 #include "content/browser/child_process_security_policy_impl.h" | |
9 #include "content/browser/media/webrtc/webrtc_identity_store.h" | |
10 #include "content/browser/renderer_host/media/webrtc_identity_service_host.h" | |
11 #include "content/common/media/webrtc_identity_messages.h" | |
12 #include "content/public/test/mock_resource_context.h" | |
13 #include "content/public/test/test_browser_thread_bundle.h" | |
14 #include "content/test/test_content_browser_client.h" | |
15 #include "ipc/ipc_message.h" | |
16 #include "net/base/net_errors.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace content { | |
20 | |
21 namespace { | |
22 | |
23 const char kFakeUrl[] = "http://www.fake.com"; | |
24 const char kFakeSite[] = "http://fake.com"; | |
25 const char kOtherSite[] = "https://other.com"; | |
26 const char kFakeFirstPartyUrl[] = "http://fake.firstparty.com"; | |
27 const char kFakeidentityName[] = "fake identity"; | |
28 const char kFakeCommonName[] = "fake common name"; | |
29 const char kFakeCertificate[] = "fake cert"; | |
30 const char kFakePrivateKey[] = "fake private key"; | |
31 const int kFakeRendererId = 10; | |
32 const int kFakeRequestId = 1; | |
33 | |
34 class WebRTCIdentityServiceHostTestBrowserClient | |
35 : public TestContentBrowserClient { | |
36 public: | |
37 WebRTCIdentityServiceHostTestBrowserClient() : allow_cache_(true) {} | |
38 | |
39 void set_allow_cache(bool allow) { allow_cache_ = allow; } | |
40 | |
41 bool AllowWebRTCIdentityCache(const GURL& url, | |
42 const GURL& first_party_url, | |
43 ResourceContext* context) override { | |
44 url_ = url; | |
45 first_party_url_ = first_party_url; | |
46 return allow_cache_; | |
47 } | |
48 | |
49 GURL url() const { return url_; } | |
50 GURL first_party_url() const { return first_party_url_; } | |
51 | |
52 private: | |
53 bool allow_cache_; | |
54 GURL url_; | |
55 GURL first_party_url_; | |
56 }; | |
57 | |
58 class MockWebRTCIdentityStore : public WebRTCIdentityStore { | |
59 public: | |
60 MockWebRTCIdentityStore() | |
61 : WebRTCIdentityStore(base::FilePath(), NULL), enable_cache_(true) {} | |
62 | |
63 base::Closure RequestIdentity(const GURL& origin, | |
64 const std::string& identity_name, | |
65 const std::string& common_name, | |
66 const CompletionCallback& callback, | |
67 bool enable_cache) override { | |
68 EXPECT_TRUE(callback_.is_null()); | |
69 | |
70 callback_ = callback; | |
71 enable_cache_ = enable_cache; | |
72 return base::Bind(&MockWebRTCIdentityStore::OnCancel, | |
73 base::Unretained(this)); | |
74 } | |
75 | |
76 bool HasPendingRequest() const { return !callback_.is_null(); } | |
77 | |
78 void RunCompletionCallback(int error, | |
79 const std::string& cert, | |
80 const std::string& key) { | |
81 callback_.Run(error, cert, key); | |
82 callback_.Reset(); | |
83 } | |
84 | |
85 bool enable_cache() const { return enable_cache_; } | |
86 | |
87 private: | |
88 ~MockWebRTCIdentityStore() override {} | |
89 | |
90 void OnCancel() { callback_.Reset(); } | |
91 | |
92 CompletionCallback callback_; | |
93 bool enable_cache_; | |
94 }; | |
95 | |
96 class WebRTCIdentityServiceHostForTest : public WebRTCIdentityServiceHost { | |
97 public: | |
98 WebRTCIdentityServiceHostForTest(WebRTCIdentityStore* identity_store, | |
99 ResourceContext* resource_context) | |
100 : WebRTCIdentityServiceHost(kFakeRendererId, | |
101 identity_store, | |
102 resource_context) { | |
103 ChildProcessSecurityPolicyImpl* policy = | |
104 ChildProcessSecurityPolicyImpl::GetInstance(); | |
105 policy->Add(kFakeRendererId); | |
106 } | |
107 | |
108 bool Send(IPC::Message* message) override { | |
109 messages_.push_back(*message); | |
110 delete message; | |
111 return true; | |
112 } | |
113 | |
114 bool OnMessageReceived(const IPC::Message& message) override { | |
115 return WebRTCIdentityServiceHost::OnMessageReceived(message); | |
116 } | |
117 | |
118 IPC::Message GetLastMessage() { return messages_.back(); } | |
119 | |
120 int GetNumberOfMessages() { return messages_.size(); } | |
121 | |
122 void ClearMessages() { messages_.clear(); } | |
123 | |
124 private: | |
125 ~WebRTCIdentityServiceHostForTest() override { | |
126 ChildProcessSecurityPolicyImpl* policy = | |
127 ChildProcessSecurityPolicyImpl::GetInstance(); | |
128 policy->Remove(kFakeRendererId); | |
129 } | |
130 | |
131 std::deque<IPC::Message> messages_; | |
132 }; | |
133 | |
134 class WebRTCIdentityServiceHostTest : public ::testing::Test { | |
135 public: | |
136 WebRTCIdentityServiceHostTest() | |
137 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | |
138 mock_resource_context_(new MockResourceContext()), | |
139 store_(new MockWebRTCIdentityStore()), | |
140 host_(new WebRTCIdentityServiceHostForTest( | |
141 store_.get(), | |
142 mock_resource_context_.get())) {} | |
143 | |
144 void SendRequestToHost() { | |
145 WebRTCIdentityMsg_RequestIdentity_Params params; | |
146 params.request_id = kFakeRequestId; | |
147 params.url = GURL(kFakeUrl); | |
148 params.first_party_for_cookies = GURL(kFakeFirstPartyUrl); | |
149 params.identity_name = kFakeidentityName; | |
150 params.common_name = kFakeCommonName; | |
151 host_->OnMessageReceived(WebRTCIdentityMsg_RequestIdentity(params)); | |
152 } | |
153 | |
154 void SendCancelRequestToHost() { | |
155 host_->OnMessageReceived(WebRTCIdentityMsg_CancelRequest()); | |
156 } | |
157 | |
158 void VerifyRequestFailedMessage(int error) { | |
159 EXPECT_EQ(1, host_->GetNumberOfMessages()); | |
160 IPC::Message ipc = host_->GetLastMessage(); | |
161 EXPECT_EQ(ipc.type(), WebRTCIdentityHostMsg_RequestFailed::ID); | |
162 | |
163 std::tuple<int, int> error_in_message; | |
164 WebRTCIdentityHostMsg_RequestFailed::Read(&ipc, &error_in_message); | |
165 EXPECT_EQ(kFakeRequestId, std::get<0>(error_in_message)); | |
166 EXPECT_EQ(error, std::get<1>(error_in_message)); | |
167 } | |
168 | |
169 void VerifyIdentityReadyMessage(const std::string& cert, | |
170 const std::string& key) { | |
171 EXPECT_EQ(1, host_->GetNumberOfMessages()); | |
172 IPC::Message ipc = host_->GetLastMessage(); | |
173 EXPECT_EQ(ipc.type(), WebRTCIdentityHostMsg_IdentityReady::ID); | |
174 | |
175 std::tuple<int, std::string, std::string> identity_in_message; | |
176 WebRTCIdentityHostMsg_IdentityReady::Read(&ipc, &identity_in_message); | |
177 EXPECT_EQ(kFakeRequestId, std::get<0>(identity_in_message)); | |
178 EXPECT_EQ(cert, std::get<1>(identity_in_message)); | |
179 EXPECT_EQ(key, std::get<2>(identity_in_message)); | |
180 } | |
181 | |
182 protected: | |
183 TestBrowserThreadBundle browser_thread_bundle_; | |
184 std::unique_ptr<MockResourceContext> mock_resource_context_; | |
185 scoped_refptr<MockWebRTCIdentityStore> store_; | |
186 scoped_refptr<WebRTCIdentityServiceHostForTest> host_; | |
187 }; | |
188 | |
189 } // namespace | |
190 | |
191 TEST_F(WebRTCIdentityServiceHostTest, TestCacheDisabled) { | |
192 WebRTCIdentityServiceHostTestBrowserClient test_client; | |
193 test_client.set_allow_cache(false); | |
194 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); | |
195 | |
196 SendRequestToHost(); | |
197 EXPECT_TRUE(store_->HasPendingRequest()); | |
198 EXPECT_FALSE(store_->enable_cache()); | |
199 EXPECT_EQ(GURL(kFakeUrl), test_client.url()); | |
200 EXPECT_EQ(GURL(kFakeFirstPartyUrl), test_client.first_party_url()); | |
201 | |
202 // Restore the original content browser client. | |
203 SetBrowserClientForTesting(old_client); | |
204 } | |
205 | |
206 TEST_F(WebRTCIdentityServiceHostTest, TestSendAndCancelRequest) { | |
207 SendRequestToHost(); | |
208 EXPECT_TRUE(store_->HasPendingRequest()); | |
209 SendCancelRequestToHost(); | |
210 EXPECT_FALSE(store_->HasPendingRequest()); | |
211 } | |
212 | |
213 TEST_F(WebRTCIdentityServiceHostTest, TestOnlyOneRequestAllowed) { | |
214 SendRequestToHost(); | |
215 EXPECT_TRUE(store_->HasPendingRequest()); | |
216 EXPECT_EQ(0, host_->GetNumberOfMessages()); | |
217 SendRequestToHost(); | |
218 | |
219 VerifyRequestFailedMessage(net::ERR_INSUFFICIENT_RESOURCES); | |
220 } | |
221 | |
222 TEST_F(WebRTCIdentityServiceHostTest, TestOnIdentityReady) { | |
223 SendRequestToHost(); | |
224 store_->RunCompletionCallback(net::OK, kFakeCertificate, kFakePrivateKey); | |
225 VerifyIdentityReadyMessage(kFakeCertificate, kFakePrivateKey); | |
226 } | |
227 | |
228 TEST_F(WebRTCIdentityServiceHostTest, TestOnRequestFailed) { | |
229 SendRequestToHost(); | |
230 store_->RunCompletionCallback(net::ERR_KEY_GENERATION_FAILED, "", ""); | |
231 VerifyRequestFailedMessage(net::ERR_KEY_GENERATION_FAILED); | |
232 } | |
233 | |
234 TEST_F(WebRTCIdentityServiceHostTest, TestOriginAccessDenied) { | |
235 ChildProcessSecurityPolicyImpl* policy = | |
236 ChildProcessSecurityPolicyImpl::GetInstance(); | |
237 policy->LockToOrigin(kFakeRendererId, GURL(kOtherSite)); | |
238 | |
239 SendRequestToHost(); | |
240 VerifyRequestFailedMessage(net::ERR_ACCESS_DENIED); | |
241 } | |
242 | |
243 TEST_F(WebRTCIdentityServiceHostTest, TestOriginAccessAllowed) { | |
244 ChildProcessSecurityPolicyImpl* policy = | |
245 ChildProcessSecurityPolicyImpl::GetInstance(); | |
246 policy->LockToOrigin(kFakeRendererId, GURL(kFakeSite)); | |
247 | |
248 SendRequestToHost(); | |
249 store_->RunCompletionCallback(net::OK, kFakeCertificate, kFakePrivateKey); | |
250 VerifyIdentityReadyMessage(kFakeCertificate, kFakePrivateKey); | |
251 } | |
252 | |
253 // Verifies that we do not crash if we try to cancel a completed request. | |
254 TEST_F(WebRTCIdentityServiceHostTest, TestCancelAfterRequestCompleted) { | |
255 SendRequestToHost(); | |
256 store_->RunCompletionCallback(net::OK, kFakeCertificate, kFakePrivateKey); | |
257 SendCancelRequestToHost(); | |
258 } | |
259 | |
260 } // namespace content | |
OLD | NEW |