| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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> | |
| 9 #include <string> | 8 #include <string> |
| 10 | 9 |
| 11 #include "ppapi/c/dev/ppb_file_io_dev.h" | 10 #include "ppapi/c/dev/ppb_file_io_dev.h" |
| 12 #include "ppapi/c/dev/ppb_file_io_trusted_dev.h" | 11 #include "ppapi/c/dev/ppb_file_io_trusted_dev.h" |
| 13 #include "ppapi/c/dev/ppb_testing_dev.h" | 12 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 14 #include "ppapi/c/pp_errors.h" | 13 #include "ppapi/c/pp_errors.h" |
| 15 #include "ppapi/c/ppb_url_loader.h" | 14 #include "ppapi/c/ppb_url_loader.h" |
| 16 #include "ppapi/cpp/dev/file_io_dev.h" | 15 #include "ppapi/cpp/dev/file_io_dev.h" |
| 17 #include "ppapi/cpp/dev/file_ref_dev.h" | 16 #include "ppapi/cpp/dev/file_ref_dev.h" |
| 18 #include "ppapi/cpp/dev/file_system_dev.h" | 17 #include "ppapi/cpp/dev/file_system_dev.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 44 RUN_TEST(BasicGET); | 43 RUN_TEST(BasicGET); |
| 45 RUN_TEST(BasicPOST); | 44 RUN_TEST(BasicPOST); |
| 46 RUN_TEST(CompoundBodyPOST); | 45 RUN_TEST(CompoundBodyPOST); |
| 47 RUN_TEST(EmptyDataPOST); | 46 RUN_TEST(EmptyDataPOST); |
| 48 RUN_TEST(BinaryDataPOST); | 47 RUN_TEST(BinaryDataPOST); |
| 49 RUN_TEST(CustomRequestHeader); | 48 RUN_TEST(CustomRequestHeader); |
| 50 RUN_TEST(IgnoresBogusContentLength); | 49 RUN_TEST(IgnoresBogusContentLength); |
| 51 RUN_TEST(SameOriginRestriction); | 50 RUN_TEST(SameOriginRestriction); |
| 52 RUN_TEST(StreamToFile); | 51 RUN_TEST(StreamToFile); |
| 53 RUN_TEST(AuditURLRedirect); | 52 RUN_TEST(AuditURLRedirect); |
| 54 RUN_TEST(AbortCalls); | |
| 55 } | 53 } |
| 56 | 54 |
| 57 std::string TestURLLoader::ReadEntireFile(pp::FileIO_Dev* file_io, | 55 std::string TestURLLoader::ReadEntireFile(pp::FileIO_Dev* file_io, |
| 58 std::string* data) { | 56 std::string* data) { |
| 59 TestCompletionCallback callback; | 57 TestCompletionCallback callback; |
| 60 char buf[256]; | 58 char buf[256]; |
| 61 int64_t offset = 0; | 59 int64_t offset = 0; |
| 62 | 60 |
| 63 for (;;) { | 61 for (;;) { |
| 64 int32_t rv = file_io->Read(offset, buf, sizeof(buf), callback); | 62 int32_t rv = file_io->Read(offset, buf, sizeof(buf), callback); |
| 65 if (rv == PP_ERROR_WOULDBLOCK) | 63 if (rv == PP_ERROR_WOULDBLOCK) |
| 66 rv = callback.WaitForResult(); | 64 rv = callback.WaitForResult(); |
| 67 if (rv < 0) | 65 if (rv < 0) |
| 68 return ReportError("FileIO::Read", rv); | 66 return ReportError("FileIO::Read", rv); |
| 69 if (rv == 0) | 67 if (rv == 0) |
| 70 break; | 68 break; |
| 71 offset += rv; | 69 offset += rv; |
| 72 data->append(buf, rv); | 70 data->append(buf, rv); |
| 73 } | 71 } |
| 74 | 72 |
| 75 PASS(); | 73 return ""; |
| 76 } | 74 } |
| 77 | 75 |
| 78 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader, | 76 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader, |
| 79 std::string* body) { | 77 std::string* body) { |
| 80 TestCompletionCallback callback; | 78 TestCompletionCallback callback; |
| 81 char buf[2]; // Small so that multiple reads are needed. | 79 char buf[256]; |
| 82 | 80 |
| 83 for (;;) { | 81 for (;;) { |
| 84 int32_t rv = loader->ReadResponseBody(buf, sizeof(buf), callback); | 82 int32_t rv = loader->ReadResponseBody(buf, sizeof(buf), callback); |
| 85 if (rv == PP_ERROR_WOULDBLOCK) | 83 if (rv == PP_ERROR_WOULDBLOCK) |
| 86 rv = callback.WaitForResult(); | 84 rv = callback.WaitForResult(); |
| 87 if (rv < 0) | 85 if (rv < 0) |
| 88 return ReportError("URLLoader::ReadResponseBody", rv); | 86 return ReportError("URLLoader::ReadResponseBody", rv); |
| 89 if (rv == 0) | 87 if (rv == 0) |
| 90 break; | 88 break; |
| 91 body->append(buf, rv); | 89 body->append(buf, rv); |
| 92 } | 90 } |
| 93 | 91 |
| 94 PASS(); | 92 return ""; |
| 95 } | 93 } |
| 96 | 94 |
| 97 std::string TestURLLoader::LoadAndCompareBody( | 95 std::string TestURLLoader::LoadAndCompareBody( |
| 98 const pp::URLRequestInfo& request, | 96 const pp::URLRequestInfo& request, |
| 99 const std::string& expected_body) { | 97 const std::string& expected_body) { |
| 100 TestCompletionCallback callback; | 98 TestCompletionCallback callback; |
| 101 | 99 |
| 102 pp::URLLoader loader(*instance_); | 100 pp::URLLoader loader(*instance_); |
| 103 int32_t rv = loader.Open(request, callback); | 101 int32_t rv = loader.Open(request, callback); |
| 104 if (rv == PP_ERROR_WOULDBLOCK) | 102 if (rv == PP_ERROR_WOULDBLOCK) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 116 std::string body; | 114 std::string body; |
| 117 std::string error = ReadEntireResponseBody(&loader, &body); | 115 std::string error = ReadEntireResponseBody(&loader, &body); |
| 118 if (!error.empty()) | 116 if (!error.empty()) |
| 119 return error; | 117 return error; |
| 120 | 118 |
| 121 if (body.size() != expected_body.size()) | 119 if (body.size() != expected_body.size()) |
| 122 return "URLLoader::ReadResponseBody returned unexpected content length"; | 120 return "URLLoader::ReadResponseBody returned unexpected content length"; |
| 123 if (body != expected_body) | 121 if (body != expected_body) |
| 124 return "URLLoader::ReadResponseBody returned unexpected content"; | 122 return "URLLoader::ReadResponseBody returned unexpected content"; |
| 125 | 123 |
| 126 PASS(); | 124 return ""; |
| 127 } | 125 } |
| 128 | 126 |
| 129 std::string TestURLLoader::TestBasicGET() { | 127 std::string TestURLLoader::TestBasicGET() { |
| 130 pp::URLRequestInfo request; | 128 pp::URLRequestInfo request; |
| 131 request.SetURL("test_url_loader_data/hello.txt"); | 129 request.SetURL("test_url_loader_data/hello.txt"); |
| 132 return LoadAndCompareBody(request, "hello\n"); | 130 return LoadAndCompareBody(request, "hello\n"); |
| 133 } | 131 } |
| 134 | 132 |
| 135 std::string TestURLLoader::TestBasicPOST() { | 133 std::string TestURLLoader::TestBasicPOST() { |
| 136 pp::URLRequestInfo request; | 134 pp::URLRequestInfo request; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 // is correct. | 289 // is correct. |
| 292 pp::URLResponseInfo response_info(loader.GetResponseInfo()); | 290 pp::URLResponseInfo response_info(loader.GetResponseInfo()); |
| 293 if (response_info.is_null()) | 291 if (response_info.is_null()) |
| 294 return "URLLoader::GetResponseInfo returned null"; | 292 return "URLLoader::GetResponseInfo returned null"; |
| 295 int32_t status_code = response_info.GetStatusCode(); | 293 int32_t status_code = response_info.GetStatusCode(); |
| 296 if (status_code != 301) | 294 if (status_code != 301) |
| 297 return "Response status should be 301"; | 295 return "Response status should be 301"; |
| 298 if (response_info.GetRedirectURL().AsString() != "www.google.com") | 296 if (response_info.GetRedirectURL().AsString() != "www.google.com") |
| 299 return "Redirect URL should be www.google.com"; | 297 return "Redirect URL should be www.google.com"; |
| 300 | 298 |
| 301 PASS(); | 299 return ""; |
| 302 } | 300 } |
| 303 | 301 |
| 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). | |
| 363 // TODO(darin): Add a test for GrantUniversalAccess. | 302 // TODO(darin): Add a test for GrantUniversalAccess. |
| OLD | NEW |