OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/tests/test_url_loader.h" | 5 #include "ppapi/tests/test_url_loader.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | 8 #include <string.h> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 RUN_TEST(BasicGET); | 44 RUN_TEST(BasicGET); |
45 RUN_TEST(BasicPOST); | 45 RUN_TEST(BasicPOST); |
46 RUN_TEST(CompoundBodyPOST); | 46 RUN_TEST(CompoundBodyPOST); |
47 RUN_TEST(EmptyDataPOST); | 47 RUN_TEST(EmptyDataPOST); |
48 RUN_TEST(BinaryDataPOST); | 48 RUN_TEST(BinaryDataPOST); |
49 RUN_TEST(CustomRequestHeader); | 49 RUN_TEST(CustomRequestHeader); |
50 RUN_TEST(IgnoresBogusContentLength); | 50 RUN_TEST(IgnoresBogusContentLength); |
51 RUN_TEST(SameOriginRestriction); | 51 RUN_TEST(SameOriginRestriction); |
52 RUN_TEST(CrossOriginRequest); | 52 RUN_TEST(CrossOriginRequest); |
53 RUN_TEST(StreamToFile); | 53 RUN_TEST(StreamToFile); |
| 54 RUN_TEST(StreamToFileForceAsync); |
54 RUN_TEST(AuditURLRedirect); | 55 RUN_TEST(AuditURLRedirect); |
55 RUN_TEST(AbortCalls); | 56 RUN_TEST(AbortCalls); |
56 } | 57 } |
57 | 58 |
58 std::string TestURLLoader::ReadEntireFile(pp::FileIO_Dev* file_io, | 59 std::string TestURLLoader::ReadEntireFile(pp::FileIO_Dev* file_io, |
59 std::string* data) { | 60 std::string* data, |
60 TestCompletionCallback callback(instance_->pp_instance()); | 61 bool force_async) { |
| 62 TestCompletionCallback callback(instance_->pp_instance(), force_async); |
61 char buf[256]; | 63 char buf[256]; |
62 int64_t offset = 0; | 64 int64_t offset = 0; |
63 | 65 |
64 for (;;) { | 66 for (;;) { |
65 int32_t rv = file_io->Read(offset, buf, sizeof(buf), callback); | 67 int32_t rv = file_io->Read(offset, buf, sizeof(buf), callback); |
| 68 if (force_async && rv != PP_OK_COMPLETIONPENDING) |
| 69 return ReportError("FileIO::Read", rv); |
66 if (rv == PP_OK_COMPLETIONPENDING) | 70 if (rv == PP_OK_COMPLETIONPENDING) |
67 rv = callback.WaitForResult(); | 71 rv = callback.WaitForResult(); |
68 if (rv < 0) | 72 if (rv < 0) |
69 return ReportError("FileIO::Read", rv); | 73 return ReportError("FileIO::Read", rv); |
70 if (rv == 0) | 74 if (rv == 0) |
71 break; | 75 break; |
72 offset += rv; | 76 offset += rv; |
73 data->append(buf, rv); | 77 data->append(buf, rv); |
74 } | 78 } |
75 | 79 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 std::string TestURLLoader::TestIgnoresBogusContentLength() { | 187 std::string TestURLLoader::TestIgnoresBogusContentLength() { |
184 pp::URLRequestInfo request; | 188 pp::URLRequestInfo request; |
185 request.SetURL("/echo"); | 189 request.SetURL("/echo"); |
186 request.SetMethod("POST"); | 190 request.SetMethod("POST"); |
187 request.SetHeaders("Content-Length: 400"); | 191 request.SetHeaders("Content-Length: 400"); |
188 std::string postdata("postdata"); | 192 std::string postdata("postdata"); |
189 request.AppendDataToBody(postdata.data(), postdata.length()); | 193 request.AppendDataToBody(postdata.data(), postdata.length()); |
190 return LoadAndCompareBody(request, postdata); | 194 return LoadAndCompareBody(request, postdata); |
191 } | 195 } |
192 | 196 |
193 std::string TestURLLoader::TestStreamToFile() { | 197 std::string TestURLLoader::StreamToFile(bool force_async) { |
194 pp::URLRequestInfo request; | 198 pp::URLRequestInfo request; |
195 request.SetURL("test_url_loader_data/hello.txt"); | 199 request.SetURL("test_url_loader_data/hello.txt"); |
196 request.SetStreamToFile(true); | 200 request.SetStreamToFile(true); |
197 | 201 TestCompletionCallback callback(instance_->pp_instance(), force_async); |
198 TestCompletionCallback callback(instance_->pp_instance()); | |
199 | 202 |
200 pp::URLLoader loader(*instance_); | 203 pp::URLLoader loader(*instance_); |
201 int32_t rv = loader.Open(request, callback); | 204 int32_t rv = loader.Open(request, callback); |
| 205 if (force_async && rv != PP_OK_COMPLETIONPENDING) |
| 206 // Why does triggering this crash the browser when testing manually? |
| 207 return ReportError("URLLoader::Open", rv); |
202 if (rv == PP_OK_COMPLETIONPENDING) | 208 if (rv == PP_OK_COMPLETIONPENDING) |
203 rv = callback.WaitForResult(); | 209 rv = callback.WaitForResult(); |
204 if (rv != PP_OK) | 210 if (rv != PP_OK) |
205 return ReportError("URLLoader::Open", rv); | 211 return ReportError("URLLoader::Open", rv); |
206 | 212 |
207 pp::URLResponseInfo response_info(loader.GetResponseInfo()); | 213 pp::URLResponseInfo response_info(loader.GetResponseInfo()); |
208 if (response_info.is_null()) | 214 if (response_info.is_null()) |
209 return "URLLoader::GetResponseInfo returned null"; | 215 return "URLLoader::GetResponseInfo returned null"; |
210 int32_t status_code = response_info.GetStatusCode(); | 216 int32_t status_code = response_info.GetStatusCode(); |
211 if (status_code != 200) | 217 if (status_code != 200) |
212 return "Unexpected HTTP status code"; | 218 return "Unexpected HTTP status code"; |
213 | 219 |
214 pp::FileRef_Dev body(response_info.GetBodyAsFileRef()); | 220 pp::FileRef_Dev body(response_info.GetBodyAsFileRef()); |
215 if (body.is_null()) | 221 if (body.is_null()) |
216 return "URLResponseInfo::GetBody returned null"; | 222 return "URLResponseInfo::GetBody returned null"; |
217 | 223 |
218 rv = loader.FinishStreamingToFile(callback); | 224 rv = loader.FinishStreamingToFile(callback); |
| 225 if (force_async && rv != PP_OK_COMPLETIONPENDING) |
| 226 return ReportError("URLLoader::FinishStreamingToFile", rv); |
219 if (rv == PP_OK_COMPLETIONPENDING) | 227 if (rv == PP_OK_COMPLETIONPENDING) |
220 rv = callback.WaitForResult(); | 228 rv = callback.WaitForResult(); |
221 if (rv != PP_OK) | 229 if (rv != PP_OK) |
222 return ReportError("URLLoader::FinishStreamingToFile", rv); | 230 return ReportError("URLLoader::FinishStreamingToFile", rv); |
223 | 231 |
224 | |
225 pp::FileIO_Dev reader; | 232 pp::FileIO_Dev reader; |
226 rv = reader.Open(body, PP_FILEOPENFLAG_READ, callback); | 233 rv = reader.Open(body, PP_FILEOPENFLAG_READ, callback); |
| 234 if (force_async && rv != PP_OK_COMPLETIONPENDING) |
| 235 return ReportError("FileIO::Open", rv); |
227 if (rv == PP_OK_COMPLETIONPENDING) | 236 if (rv == PP_OK_COMPLETIONPENDING) |
228 rv = callback.WaitForResult(); | 237 rv = callback.WaitForResult(); |
229 if (rv != PP_OK) | 238 if (rv != PP_OK) |
230 return ReportError("FileIO::Open", rv); | 239 return ReportError("FileIO::Open", rv); |
231 | 240 |
232 std::string data; | 241 std::string data; |
233 std::string error = ReadEntireFile(&reader, &data); | 242 std::string error = ReadEntireFile(&reader, &data, force_async); |
234 if (!error.empty()) | 243 if (!error.empty()) |
235 return error; | 244 return error; |
236 | 245 |
237 std::string expected_body = "hello\n"; | 246 std::string expected_body = "hello\n"; |
238 if (data.size() != expected_body.size()) | 247 if (data.size() != expected_body.size()) |
239 return "ReadEntireFile returned unexpected content length"; | 248 return "ReadEntireFile returned unexpected content length"; |
240 if (data != expected_body) | 249 if (data != expected_body) |
241 return "ReadEntireFile returned unexpected content"; | 250 return "ReadEntireFile returned unexpected content"; |
242 | 251 |
243 int32_t file_descriptor = file_io_trusted_interface_->GetOSFileDescriptor( | 252 int32_t file_descriptor = file_io_trusted_interface_->GetOSFileDescriptor( |
244 reader.pp_resource()); | 253 reader.pp_resource()); |
245 if (file_descriptor < 0) | 254 if (file_descriptor < 0) |
246 return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; | 255 return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; |
247 | 256 |
248 PASS(); | 257 PASS(); |
249 } | 258 } |
250 | 259 |
| 260 std::string TestURLLoader::TestStreamToFile() { |
| 261 return StreamToFile(false); |
| 262 } |
| 263 |
| 264 std::string TestURLLoader::TestStreamToFileForceAsync() { |
| 265 return StreamToFile(true); |
| 266 } |
| 267 |
251 std::string TestURLLoader::TestSameOriginRestriction() { | 268 std::string TestURLLoader::TestSameOriginRestriction() { |
252 pp::URLRequestInfo request; | 269 pp::URLRequestInfo request; |
253 request.SetURL("http://www.google.com/"); | 270 request.SetURL("http://www.google.com/"); |
254 | 271 |
255 TestCompletionCallback callback(instance_->pp_instance()); | 272 TestCompletionCallback callback(instance_->pp_instance()); |
256 | 273 |
257 pp::URLLoader loader(*instance_); | 274 pp::URLLoader loader(*instance_); |
258 int32_t rv = loader.Open(request, callback); | 275 int32_t rv = loader.Open(request, callback); |
259 if (rv == PP_OK_COMPLETIONPENDING) | 276 if (rv == PP_OK_COMPLETIONPENDING) |
260 rv = callback.WaitForResult(); | 277 rv = callback.WaitForResult(); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 | 392 |
376 // TODO(viettrungluu): More abort tests (but add basic tests first). | 393 // TODO(viettrungluu): More abort tests (but add basic tests first). |
377 // Also test that Close() aborts properly. crbug.com/69457 | 394 // Also test that Close() aborts properly. crbug.com/69457 |
378 | 395 |
379 PASS(); | 396 PASS(); |
380 } | 397 } |
381 | 398 |
382 // TODO(viettrungluu): Add tests for FollowRedirect, | 399 // TODO(viettrungluu): Add tests for FollowRedirect, |
383 // Get{Upload,Download}Progress, Close (including abort tests if applicable). | 400 // Get{Upload,Download}Progress, Close (including abort tests if applicable). |
384 // TODO(darin): Add a test for GrantUniversalAccess. | 401 // TODO(darin): Add a test for GrantUniversalAccess. |
OLD | NEW |