OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 "base/run_loop.h" | |
6 #include "content/browser/fileapi/mock_url_request_delegate.h" | |
7 #include "content/browser/service_worker/embedded_worker_test_helper.h" | |
8 #include "content/browser/service_worker/service_worker_context_core.h" | |
9 #include "content/browser/service_worker/service_worker_context_request_handler. h" | |
10 #include "content/browser/service_worker/service_worker_provider_host.h" | |
11 #include "content/browser/service_worker/service_worker_registration.h" | |
12 #include "content/browser/service_worker/service_worker_utils.h" | |
13 #include "content/common/resource_request_body.h" | |
14 #include "content/public/test/mock_resource_context.h" | |
15 #include "content/public/test/test_browser_thread_bundle.h" | |
16 #include "net/base/load_flags.h" | |
17 #include "net/http/http_response_headers.h" | |
18 #include "net/url_request/url_request_context.h" | |
19 #include "net/url_request/url_request_job_factory_impl.h" | |
20 #include "net/url_request/url_request_test_job.h" | |
21 #include "net/url_request/url_request_test_util.h" | |
22 #include "storage/browser/blob/blob_storage_context.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 | |
25 namespace content { | |
26 | |
27 namespace { | |
28 | |
29 int kMockRenderProcessId = 1224; | |
30 int kMockProviderId = 1; | |
31 const char kHeaders[] = | |
32 "HTTP/1.1 200 OK\0" | |
33 "Content-Type: text/javascript\0" | |
34 "Expires: Thu, 1 Jan 2100 20:00:00 GMT\0" | |
35 "\0"; | |
36 const char kScriptCode[] = "// no script code\n"; | |
37 | |
38 void EmptyCallback() { | |
39 } | |
40 | |
41 net::URLRequestJob* CreateNormalURLRequestJob( | |
42 net::URLRequest* request, | |
43 net::NetworkDelegate* network_delegate) { | |
44 return new net::URLRequestTestJob(request, | |
45 network_delegate, | |
46 std::string(kHeaders, arraysize(kHeaders)), | |
47 kScriptCode, | |
48 true); | |
49 } | |
50 | |
51 net::URLRequestJob* CreateInvalidMimeTypeJob( | |
52 net::URLRequest* request, | |
53 net::NetworkDelegate* network_delegate) { | |
54 const char kPlainTextHeaders[] = | |
55 "HTTP/1.1 200 OK\0" | |
56 "Content-Type: text/plain\0" | |
57 "Expires: Thu, 1 Jan 2100 20:00:00 GMT\0" | |
58 "\0"; | |
59 return new net::URLRequestTestJob( | |
60 request, | |
61 network_delegate, | |
62 std::string(kPlainTextHeaders, arraysize(kPlainTextHeaders)), | |
63 kScriptCode, | |
64 true); | |
65 } | |
66 | |
67 class SSLCertificateErrorJob : public net::URLRequestTestJob { | |
68 public: | |
69 SSLCertificateErrorJob(net::URLRequest* request, | |
70 net::NetworkDelegate* network_delegate, | |
71 const std::string& response_headers, | |
72 const std::string& response_data, | |
73 bool auto_advance) | |
74 : net::URLRequestTestJob(request, | |
75 network_delegate, | |
76 response_headers, | |
77 response_data, | |
78 auto_advance), | |
79 weak_factory_(this) {} | |
80 virtual void Start() override { | |
81 base::MessageLoop::current()->PostTask( | |
82 FROM_HERE, | |
83 base::Bind(&SSLCertificateErrorJob::NotifyError, | |
84 weak_factory_.GetWeakPtr())); | |
85 } | |
86 void NotifyError() { | |
87 net::SSLInfo info; | |
88 info.cert_status = net::CERT_STATUS_DATE_INVALID; | |
89 NotifySSLCertificateError(info, true); | |
90 } | |
91 | |
92 protected: | |
93 virtual ~SSLCertificateErrorJob() {} | |
94 base::WeakPtrFactory<SSLCertificateErrorJob> weak_factory_; | |
95 }; | |
96 | |
97 net::URLRequestJob* CreateSSLCertificateErrorJob( | |
98 net::URLRequest* request, | |
99 net::NetworkDelegate* network_delegate) { | |
100 return new SSLCertificateErrorJob(request, | |
101 network_delegate, | |
102 std::string(kHeaders, arraysize(kHeaders)), | |
103 kScriptCode, | |
104 true); | |
105 } | |
106 | |
107 class CertStatusErrorJob : public net::URLRequestTestJob { | |
108 public: | |
109 CertStatusErrorJob(net::URLRequest* request, | |
110 net::NetworkDelegate* network_delegate, | |
111 const std::string& response_headers, | |
112 const std::string& response_data, | |
113 bool auto_advance) | |
114 : net::URLRequestTestJob(request, | |
115 network_delegate, | |
116 response_headers, | |
117 response_data, | |
118 auto_advance) {} | |
119 virtual void GetResponseInfo(net::HttpResponseInfo* info) override { | |
120 URLRequestTestJob::GetResponseInfo(info); | |
121 info->ssl_info.cert_status = net::CERT_STATUS_DATE_INVALID; | |
122 } | |
123 | |
124 protected: | |
125 virtual ~CertStatusErrorJob() {} | |
126 }; | |
127 | |
128 net::URLRequestJob* CreateCertStatusErrorJob( | |
129 net::URLRequest* request, | |
130 net::NetworkDelegate* network_delegate) { | |
131 return new CertStatusErrorJob(request, | |
132 network_delegate, | |
133 std::string(kHeaders, arraysize(kHeaders)), | |
134 kScriptCode, | |
135 true); | |
136 } | |
137 | |
138 class MockHttpProtocolHandler | |
139 : public net::URLRequestJobFactory::ProtocolHandler { | |
140 public: | |
141 typedef base::Callback< | |
142 net::URLRequestJob*(net::URLRequest*, net::NetworkDelegate*)> JobCallback; | |
143 | |
144 MockHttpProtocolHandler(ResourceContext* resource_context) | |
145 : resource_context_(resource_context) {} | |
146 virtual ~MockHttpProtocolHandler() {} | |
michaeln
2014/10/22 22:39:06
per the c++ 11 guidance for virtual, override, fin
horo
2014/10/23 04:46:50
Done.
| |
147 | |
148 virtual net::URLRequestJob* MaybeCreateJob( | |
149 net::URLRequest* request, | |
150 net::NetworkDelegate* network_delegate) const override { | |
151 ServiceWorkerRequestHandler* handler = | |
152 ServiceWorkerRequestHandler::GetHandler(request); | |
153 if (handler) { | |
154 return handler->MaybeCreateJob( | |
155 request, network_delegate, resource_context_); | |
156 } | |
157 return create_job_callback_.Run(request, network_delegate); | |
158 } | |
159 void setCreateJobCallback(const JobCallback& callback) { | |
michaeln
2014/10/22 22:39:06
cap S SetCreateJobCallbac
horo
2014/10/23 04:46:50
Done.
| |
160 create_job_callback_ = callback; | |
161 } | |
162 | |
163 private: | |
164 ResourceContext* resource_context_; | |
165 JobCallback create_job_callback_; | |
166 }; | |
167 } | |
168 | |
169 class ServiceWorkerWriteToCacheJobTest : public testing::Test { | |
170 public: | |
171 ServiceWorkerWriteToCacheJobTest() | |
172 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | |
173 mock_protocol_handler_(NULL) {} | |
174 virtual ~ServiceWorkerWriteToCacheJobTest() { | |
175 // URLRequestJobs may post clean-up tasks on destruction. | |
michaeln
2014/10/22 22:39:06
Might make sense to put this in the TearDown() met
horo
2014/10/23 04:46:51
Done.
| |
176 base::RunLoop().RunUntilIdle(); | |
177 } | |
178 | |
179 virtual void SetUp() override { | |
180 helper_.reset(new EmbeddedWorkerTestHelper(kMockRenderProcessId)); | |
181 | |
182 // A new unstored registration/version. | |
183 scope_ = GURL("https://host/scope/"); | |
184 script_url_ = GURL("https://host/script.js"); | |
185 registration_ = | |
186 new ServiceWorkerRegistration(scope_, 1L, context()->AsWeakPtr()); | |
187 version_ = new ServiceWorkerVersion( | |
188 registration_.get(), script_url_, 1L, context()->AsWeakPtr()); | |
189 | |
190 // An empty host. | |
191 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( | |
192 kMockRenderProcessId, kMockProviderId, context()->AsWeakPtr(), NULL)); | |
193 provider_host_ = host->AsWeakPtr(); | |
194 context()->AddProviderHost(host.Pass()); | |
195 provider_host_->running_hosted_version_ = version_; | |
196 | |
197 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback)); | |
198 base::RunLoop().RunUntilIdle(); | |
199 | |
200 url_request_context_.reset(new net::URLRequestContext); | |
201 mock_protocol_handler_ = new MockHttpProtocolHandler(&resource_context_); | |
202 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); | |
203 url_request_job_factory_->SetProtocolHandler("https", | |
204 mock_protocol_handler_); | |
205 url_request_context_->set_job_factory(url_request_job_factory_.get()); | |
206 | |
207 request_ = url_request_context_->CreateRequest( | |
208 script_url_, net::DEFAULT_PRIORITY, &url_request_delegate_, NULL); | |
209 ServiceWorkerRequestHandler::InitializeHandler( | |
210 request_.get(), | |
211 context_wrapper(), | |
212 &blob_storage_context_, | |
213 kMockRenderProcessId, | |
214 kMockProviderId, | |
215 false, | |
216 FETCH_REQUEST_MODE_NO_CORS, | |
217 FETCH_CREDENTIALS_MODE_OMIT, | |
218 RESOURCE_TYPE_SERVICE_WORKER, | |
219 REQUEST_CONTEXT_TYPE_SERVICE_WORKER, | |
220 REQUEST_CONTEXT_FRAME_TYPE_NONE, | |
221 scoped_refptr<ResourceRequestBody>()); | |
222 } | |
223 | |
224 virtual void TearDown() override { | |
225 request_.reset(); | |
226 url_request_context_.reset(); | |
227 url_request_job_factory_.reset(); | |
228 mock_protocol_handler_ = NULL; | |
229 version_ = NULL; | |
230 registration_ = NULL; | |
231 helper_.reset(); | |
232 } | |
233 | |
234 ServiceWorkerContextCore* context() const { return helper_->context(); } | |
235 ServiceWorkerContextWrapper* context_wrapper() const { | |
236 return helper_->context_wrapper(); | |
237 } | |
238 | |
239 protected: | |
240 TestBrowserThreadBundle browser_thread_bundle_; | |
241 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | |
242 scoped_refptr<ServiceWorkerRegistration> registration_; | |
243 scoped_refptr<ServiceWorkerVersion> version_; | |
244 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; | |
245 scoped_ptr<net::URLRequestContext> url_request_context_; | |
246 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | |
247 scoped_ptr<net::URLRequest> request_; | |
248 MockHttpProtocolHandler* mock_protocol_handler_; | |
249 | |
250 storage::BlobStorageContext blob_storage_context_; | |
251 content::MockResourceContext resource_context_; | |
252 | |
253 MockURLRequestDelegate url_request_delegate_; | |
254 GURL scope_; | |
255 GURL script_url_; | |
256 }; | |
257 | |
258 TEST_F(ServiceWorkerWriteToCacheJobTest, Normal) { | |
259 mock_protocol_handler_->setCreateJobCallback( | |
260 base::Bind(&CreateNormalURLRequestJob)); | |
261 request_->Start(); | |
262 base::RunLoop().RunUntilIdle(); | |
263 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request_->status().status()); | |
michaeln
2014/10/22 22:39:05
Maybe also tests for an artifact of having written
horo
2014/10/23 04:46:51
Done.
| |
264 } | |
265 | |
266 TEST_F(ServiceWorkerWriteToCacheJobTest, InvalidMimeType) { | |
267 mock_protocol_handler_->setCreateJobCallback( | |
268 base::Bind(&CreateInvalidMimeTypeJob)); | |
269 request_->Start(); | |
270 base::RunLoop().RunUntilIdle(); | |
271 EXPECT_EQ(net::URLRequestStatus::FAILED, request_->status().status()); | |
272 EXPECT_EQ(net::ERR_INSECURE_RESPONSE, request_->status().error()); | |
michaeln
2014/10/22 22:39:06
Maybe also test for not having written the respons
horo
2014/10/23 04:46:51
Done.
| |
273 } | |
274 | |
275 TEST_F(ServiceWorkerWriteToCacheJobTest, SSLCertificateError) { | |
276 mock_protocol_handler_->setCreateJobCallback( | |
277 base::Bind(&CreateSSLCertificateErrorJob)); | |
278 request_->Start(); | |
279 base::RunLoop().RunUntilIdle(); | |
280 EXPECT_EQ(net::URLRequestStatus::FAILED, request_->status().status()); | |
281 EXPECT_EQ(net::ERR_INSECURE_RESPONSE, request_->status().error()); | |
282 } | |
283 | |
284 TEST_F(ServiceWorkerWriteToCacheJobTest, CertStatusError) { | |
285 mock_protocol_handler_->setCreateJobCallback( | |
286 base::Bind(&CreateCertStatusErrorJob)); | |
287 request_->Start(); | |
288 base::RunLoop().RunUntilIdle(); | |
289 EXPECT_EQ(net::URLRequestStatus::FAILED, request_->status().status()); | |
290 EXPECT_EQ(net::ERR_INSECURE_RESPONSE, request_->status().error()); | |
291 } | |
292 | |
293 } // namespace content | |
OLD | NEW |