| OLD | NEW |
| 1 // Copyright (c) 2010 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> | 9 #include <string> |
| 9 | 10 |
| 10 #include "ppapi/c/dev/ppb_file_io_dev.h" | 11 #include "ppapi/c/dev/ppb_file_io_dev.h" |
| 11 #include "ppapi/c/dev/ppb_file_io_trusted_dev.h" | 12 #include "ppapi/c/dev/ppb_file_io_trusted_dev.h" |
| 12 #include "ppapi/c/dev/ppb_testing_dev.h" | 13 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 13 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
| 14 #include "ppapi/c/ppb_url_loader.h" | 15 #include "ppapi/c/ppb_url_loader.h" |
| 15 #include "ppapi/cpp/dev/file_io_dev.h" | 16 #include "ppapi/cpp/dev/file_io_dev.h" |
| 16 #include "ppapi/cpp/dev/file_ref_dev.h" | 17 #include "ppapi/cpp/dev/file_ref_dev.h" |
| 17 #include "ppapi/cpp/dev/file_system_dev.h" | 18 #include "ppapi/cpp/dev/file_system_dev.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 43 RUN_TEST(BasicGET); | 44 RUN_TEST(BasicGET); |
| 44 RUN_TEST(BasicPOST); | 45 RUN_TEST(BasicPOST); |
| 45 RUN_TEST(CompoundBodyPOST); | 46 RUN_TEST(CompoundBodyPOST); |
| 46 RUN_TEST(EmptyDataPOST); | 47 RUN_TEST(EmptyDataPOST); |
| 47 RUN_TEST(BinaryDataPOST); | 48 RUN_TEST(BinaryDataPOST); |
| 48 RUN_TEST(CustomRequestHeader); | 49 RUN_TEST(CustomRequestHeader); |
| 49 RUN_TEST(IgnoresBogusContentLength); | 50 RUN_TEST(IgnoresBogusContentLength); |
| 50 RUN_TEST(SameOriginRestriction); | 51 RUN_TEST(SameOriginRestriction); |
| 51 RUN_TEST(StreamToFile); | 52 RUN_TEST(StreamToFile); |
| 52 RUN_TEST(AuditURLRedirect); | 53 RUN_TEST(AuditURLRedirect); |
| 54 RUN_TEST(AbortCalls); |
| 53 } | 55 } |
| 54 | 56 |
| 55 std::string TestURLLoader::ReadEntireFile(pp::FileIO_Dev* file_io, | 57 std::string TestURLLoader::ReadEntireFile(pp::FileIO_Dev* file_io, |
| 56 std::string* data) { | 58 std::string* data) { |
| 57 TestCompletionCallback callback; | 59 TestCompletionCallback callback; |
| 58 char buf[256]; | 60 char buf[256]; |
| 59 int64_t offset = 0; | 61 int64_t offset = 0; |
| 60 | 62 |
| 61 for (;;) { | 63 for (;;) { |
| 62 int32_t rv = file_io->Read(offset, buf, sizeof(buf), callback); | 64 int32_t rv = file_io->Read(offset, buf, sizeof(buf), callback); |
| 63 if (rv == PP_ERROR_WOULDBLOCK) | 65 if (rv == PP_ERROR_WOULDBLOCK) |
| 64 rv = callback.WaitForResult(); | 66 rv = callback.WaitForResult(); |
| 65 if (rv < 0) | 67 if (rv < 0) |
| 66 return ReportError("FileIO::Read", rv); | 68 return ReportError("FileIO::Read", rv); |
| 67 if (rv == 0) | 69 if (rv == 0) |
| 68 break; | 70 break; |
| 69 offset += rv; | 71 offset += rv; |
| 70 data->append(buf, rv); | 72 data->append(buf, rv); |
| 71 } | 73 } |
| 72 | 74 |
| 73 return ""; | 75 PASS(); |
| 74 } | 76 } |
| 75 | 77 |
| 76 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader, | 78 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader, |
| 77 std::string* body) { | 79 std::string* body) { |
| 78 TestCompletionCallback callback; | 80 TestCompletionCallback callback; |
| 79 char buf[256]; | 81 char buf[2]; // Small so that multiple reads are needed. |
| 80 | 82 |
| 81 for (;;) { | 83 for (;;) { |
| 82 int32_t rv = loader->ReadResponseBody(buf, sizeof(buf), callback); | 84 int32_t rv = loader->ReadResponseBody(buf, sizeof(buf), callback); |
| 83 if (rv == PP_ERROR_WOULDBLOCK) | 85 if (rv == PP_ERROR_WOULDBLOCK) |
| 84 rv = callback.WaitForResult(); | 86 rv = callback.WaitForResult(); |
| 85 if (rv < 0) | 87 if (rv < 0) |
| 86 return ReportError("URLLoader::ReadResponseBody", rv); | 88 return ReportError("URLLoader::ReadResponseBody", rv); |
| 87 if (rv == 0) | 89 if (rv == 0) |
| 88 break; | 90 break; |
| 89 body->append(buf, rv); | 91 body->append(buf, rv); |
| 90 } | 92 } |
| 91 | 93 |
| 92 return ""; | 94 PASS(); |
| 93 } | 95 } |
| 94 | 96 |
| 95 std::string TestURLLoader::LoadAndCompareBody( | 97 std::string TestURLLoader::LoadAndCompareBody( |
| 96 const pp::URLRequestInfo& request, | 98 const pp::URLRequestInfo& request, |
| 97 const std::string& expected_body) { | 99 const std::string& expected_body) { |
| 98 TestCompletionCallback callback; | 100 TestCompletionCallback callback; |
| 99 | 101 |
| 100 pp::URLLoader loader(*instance_); | 102 pp::URLLoader loader(*instance_); |
| 101 int32_t rv = loader.Open(request, callback); | 103 int32_t rv = loader.Open(request, callback); |
| 102 if (rv == PP_ERROR_WOULDBLOCK) | 104 if (rv == PP_ERROR_WOULDBLOCK) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 114 std::string body; | 116 std::string body; |
| 115 std::string error = ReadEntireResponseBody(&loader, &body); | 117 std::string error = ReadEntireResponseBody(&loader, &body); |
| 116 if (!error.empty()) | 118 if (!error.empty()) |
| 117 return error; | 119 return error; |
| 118 | 120 |
| 119 if (body.size() != expected_body.size()) | 121 if (body.size() != expected_body.size()) |
| 120 return "URLLoader::ReadResponseBody returned unexpected content length"; | 122 return "URLLoader::ReadResponseBody returned unexpected content length"; |
| 121 if (body != expected_body) | 123 if (body != expected_body) |
| 122 return "URLLoader::ReadResponseBody returned unexpected content"; | 124 return "URLLoader::ReadResponseBody returned unexpected content"; |
| 123 | 125 |
| 124 return ""; | 126 PASS(); |
| 125 } | 127 } |
| 126 | 128 |
| 127 std::string TestURLLoader::TestBasicGET() { | 129 std::string TestURLLoader::TestBasicGET() { |
| 128 pp::URLRequestInfo request; | 130 pp::URLRequestInfo request; |
| 129 request.SetURL("test_url_loader_data/hello.txt"); | 131 request.SetURL("test_url_loader_data/hello.txt"); |
| 130 return LoadAndCompareBody(request, "hello\n"); | 132 return LoadAndCompareBody(request, "hello\n"); |
| 131 } | 133 } |
| 132 | 134 |
| 133 std::string TestURLLoader::TestBasicPOST() { | 135 std::string TestURLLoader::TestBasicPOST() { |
| 134 pp::URLRequestInfo request; | 136 pp::URLRequestInfo request; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 // is correct. | 291 // is correct. |
| 290 pp::URLResponseInfo response_info(loader.GetResponseInfo()); | 292 pp::URLResponseInfo response_info(loader.GetResponseInfo()); |
| 291 if (response_info.is_null()) | 293 if (response_info.is_null()) |
| 292 return "URLLoader::GetResponseInfo returned null"; | 294 return "URLLoader::GetResponseInfo returned null"; |
| 293 int32_t status_code = response_info.GetStatusCode(); | 295 int32_t status_code = response_info.GetStatusCode(); |
| 294 if (status_code != 301) | 296 if (status_code != 301) |
| 295 return "Response status should be 301"; | 297 return "Response status should be 301"; |
| 296 if (response_info.GetRedirectURL().AsString() != "www.google.com") | 298 if (response_info.GetRedirectURL().AsString() != "www.google.com") |
| 297 return "Redirect URL should be www.google.com"; | 299 return "Redirect URL should be www.google.com"; |
| 298 | 300 |
| 299 return ""; | 301 PASS(); |
| 300 } | 302 } |
| 301 | 303 |
| 304 std::string TestURLLoader::TestAbortCalls() { |
| 305 pp::URLRequestInfo request; |
| 306 request.SetURL("test_url_loader_data/hello.txt"); |
| 307 |
| 308 TestCompletionCallback callback; |
| 309 int32_t rv; |
| 310 |
| 311 // Abort |Open()|. |
| 312 { |
| 313 callback.reset_run_count(); |
| 314 rv = pp::URLLoader(*instance_).Open(request, callback); |
| 315 if (callback.run_count() > 0) |
| 316 return "URLLoader::Open ran callback synchronously."; |
| 317 if (rv == PP_ERROR_WOULDBLOCK) { |
| 318 rv = callback.WaitForResult(); |
| 319 if (rv != PP_ERROR_ABORTED) |
| 320 return "URLLoader::Open not aborted."; |
| 321 } else if (rv != PP_OK) { |
| 322 return ReportError("URLLoader::Open", rv); |
| 323 } |
| 324 } |
| 325 |
| 326 // Abort |ReadResponseBody()|. |
| 327 { |
| 328 char buf[2] = { 0 }; |
| 329 { |
| 330 pp::URLLoader loader(*instance_); |
| 331 rv = loader.Open(request, callback); |
| 332 if (rv == PP_ERROR_WOULDBLOCK) |
| 333 rv = callback.WaitForResult(); |
| 334 if (rv != PP_OK) |
| 335 return ReportError("URLLoader::Open", rv); |
| 336 |
| 337 callback.reset_run_count(); |
| 338 rv = loader.ReadResponseBody(buf, sizeof(buf), callback); |
| 339 } // Destroy |loader|. |
| 340 if (rv == PP_ERROR_WOULDBLOCK) { |
| 341 // Save a copy and make sure |buf| doesn't get written to. |
| 342 char buf_copy[2]; |
| 343 memcpy(&buf_copy, &buf, sizeof(buf)); |
| 344 rv = callback.WaitForResult(); |
| 345 if (rv != PP_ERROR_ABORTED) |
| 346 return "URLLoader::ReadResponseBody not aborted."; |
| 347 if (memcmp(&buf_copy, &buf, sizeof(buf)) != 0) |
| 348 return "URLLoader::ReadResponseBody wrote data after resource " |
| 349 "destruction."; |
| 350 } else if (rv != PP_OK) { |
| 351 return ReportError("URLLoader::ReadResponseBody", rv); |
| 352 } |
| 353 } |
| 354 |
| 355 // TODO(viettrungluu): More abort tests (but add basic tests first). |
| 356 // Also test that Close() aborts properly. crbug.com/69457 |
| 357 |
| 358 PASS(); |
| 359 } |
| 360 |
| 361 // TODO(viettrungluu): Add tests for FollowRedirect, |
| 362 // Get{Upload,Download}Progress, Close (including abort tests if applicable). |
| 302 // TODO(darin): Add a test for GrantUniversalAccess. | 363 // TODO(darin): Add a test for GrantUniversalAccess. |
| OLD | NEW |