| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(StreamToFile); | 52 RUN_TEST(StreamToFile); |
| 53 RUN_TEST(AuditURLRedirect); | 53 RUN_TEST(AuditURLRedirect); |
| 54 RUN_TEST(AbortCalls); | 54 RUN_TEST(AbortCalls); |
| 55 } | 55 } |
| 56 | 56 |
| 57 std::string TestURLLoader::ReadEntireFile(pp::FileIO_Dev* file_io, | 57 std::string TestURLLoader::ReadEntireFile(pp::FileIO_Dev* file_io, |
| 58 std::string* data) { | 58 std::string* data) { |
| 59 TestCompletionCallback callback; | 59 TestCompletionCallback callback(instance_->pp_instance()); |
| 60 char buf[256]; | 60 char buf[256]; |
| 61 int64_t offset = 0; | 61 int64_t offset = 0; |
| 62 | 62 |
| 63 for (;;) { | 63 for (;;) { |
| 64 int32_t rv = file_io->Read(offset, buf, sizeof(buf), callback); | 64 int32_t rv = file_io->Read(offset, buf, sizeof(buf), callback); |
| 65 if (rv == PP_ERROR_WOULDBLOCK) | 65 if (rv == PP_ERROR_WOULDBLOCK) |
| 66 rv = callback.WaitForResult(); | 66 rv = callback.WaitForResult(); |
| 67 if (rv < 0) | 67 if (rv < 0) |
| 68 return ReportError("FileIO::Read", rv); | 68 return ReportError("FileIO::Read", rv); |
| 69 if (rv == 0) | 69 if (rv == 0) |
| 70 break; | 70 break; |
| 71 offset += rv; | 71 offset += rv; |
| 72 data->append(buf, rv); | 72 data->append(buf, rv); |
| 73 } | 73 } |
| 74 | 74 |
| 75 PASS(); | 75 PASS(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader, | 78 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader, |
| 79 std::string* body) { | 79 std::string* body) { |
| 80 TestCompletionCallback callback; | 80 TestCompletionCallback callback(instance_->pp_instance()); |
| 81 char buf[2]; // Small so that multiple reads are needed. | 81 char buf[2]; // Small so that multiple reads are needed. |
| 82 | 82 |
| 83 for (;;) { | 83 for (;;) { |
| 84 int32_t rv = loader->ReadResponseBody(buf, sizeof(buf), callback); | 84 int32_t rv = loader->ReadResponseBody(buf, sizeof(buf), callback); |
| 85 if (rv == PP_ERROR_WOULDBLOCK) | 85 if (rv == PP_ERROR_WOULDBLOCK) |
| 86 rv = callback.WaitForResult(); | 86 rv = callback.WaitForResult(); |
| 87 if (rv < 0) | 87 if (rv < 0) |
| 88 return ReportError("URLLoader::ReadResponseBody", rv); | 88 return ReportError("URLLoader::ReadResponseBody", rv); |
| 89 if (rv == 0) | 89 if (rv == 0) |
| 90 break; | 90 break; |
| 91 body->append(buf, rv); | 91 body->append(buf, rv); |
| 92 } | 92 } |
| 93 | 93 |
| 94 PASS(); | 94 PASS(); |
| 95 } | 95 } |
| 96 | 96 |
| 97 std::string TestURLLoader::LoadAndCompareBody( | 97 std::string TestURLLoader::LoadAndCompareBody( |
| 98 const pp::URLRequestInfo& request, | 98 const pp::URLRequestInfo& request, |
| 99 const std::string& expected_body) { | 99 const std::string& expected_body) { |
| 100 TestCompletionCallback callback; | 100 TestCompletionCallback callback(instance_->pp_instance()); |
| 101 | 101 |
| 102 pp::URLLoader loader(*instance_); | 102 pp::URLLoader loader(*instance_); |
| 103 int32_t rv = loader.Open(request, callback); | 103 int32_t rv = loader.Open(request, callback); |
| 104 if (rv == PP_ERROR_WOULDBLOCK) | 104 if (rv == PP_ERROR_WOULDBLOCK) |
| 105 rv = callback.WaitForResult(); | 105 rv = callback.WaitForResult(); |
| 106 if (rv != PP_OK) | 106 if (rv != PP_OK) |
| 107 return ReportError("URLLoader::Open", rv); | 107 return ReportError("URLLoader::Open", rv); |
| 108 | 108 |
| 109 pp::URLResponseInfo response_info(loader.GetResponseInfo()); | 109 pp::URLResponseInfo response_info(loader.GetResponseInfo()); |
| 110 if (response_info.is_null()) | 110 if (response_info.is_null()) |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 std::string postdata("postdata"); | 187 std::string postdata("postdata"); |
| 188 request.AppendDataToBody(postdata.data(), postdata.length()); | 188 request.AppendDataToBody(postdata.data(), postdata.length()); |
| 189 return LoadAndCompareBody(request, postdata); | 189 return LoadAndCompareBody(request, postdata); |
| 190 } | 190 } |
| 191 | 191 |
| 192 std::string TestURLLoader::TestStreamToFile() { | 192 std::string TestURLLoader::TestStreamToFile() { |
| 193 pp::URLRequestInfo request; | 193 pp::URLRequestInfo request; |
| 194 request.SetURL("test_url_loader_data/hello.txt"); | 194 request.SetURL("test_url_loader_data/hello.txt"); |
| 195 request.SetStreamToFile(true); | 195 request.SetStreamToFile(true); |
| 196 | 196 |
| 197 TestCompletionCallback callback; | 197 TestCompletionCallback callback(instance_->pp_instance()); |
| 198 | 198 |
| 199 pp::URLLoader loader(*instance_); | 199 pp::URLLoader loader(*instance_); |
| 200 int32_t rv = loader.Open(request, callback); | 200 int32_t rv = loader.Open(request, callback); |
| 201 if (rv == PP_ERROR_WOULDBLOCK) | 201 if (rv == PP_ERROR_WOULDBLOCK) |
| 202 rv = callback.WaitForResult(); | 202 rv = callback.WaitForResult(); |
| 203 if (rv != PP_OK) | 203 if (rv != PP_OK) |
| 204 return ReportError("URLLoader::Open", rv); | 204 return ReportError("URLLoader::Open", rv); |
| 205 | 205 |
| 206 pp::URLResponseInfo response_info(loader.GetResponseInfo()); | 206 pp::URLResponseInfo response_info(loader.GetResponseInfo()); |
| 207 if (response_info.is_null()) | 207 if (response_info.is_null()) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 if (file_descriptor < 0) | 244 if (file_descriptor < 0) |
| 245 return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; | 245 return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; |
| 246 | 246 |
| 247 PASS(); | 247 PASS(); |
| 248 } | 248 } |
| 249 | 249 |
| 250 std::string TestURLLoader::TestSameOriginRestriction() { | 250 std::string TestURLLoader::TestSameOriginRestriction() { |
| 251 pp::URLRequestInfo request; | 251 pp::URLRequestInfo request; |
| 252 request.SetURL("http://www.google.com/"); | 252 request.SetURL("http://www.google.com/"); |
| 253 | 253 |
| 254 TestCompletionCallback callback; | 254 TestCompletionCallback callback(instance_->pp_instance()); |
| 255 | 255 |
| 256 pp::URLLoader loader(*instance_); | 256 pp::URLLoader loader(*instance_); |
| 257 int32_t rv = loader.Open(request, callback); | 257 int32_t rv = loader.Open(request, callback); |
| 258 if (rv == PP_ERROR_WOULDBLOCK) | 258 if (rv == PP_ERROR_WOULDBLOCK) |
| 259 rv = callback.WaitForResult(); | 259 rv = callback.WaitForResult(); |
| 260 | 260 |
| 261 // We expect a failure. | 261 // We expect a failure. |
| 262 if (rv != PP_ERROR_NOACCESS) { | 262 if (rv != PP_ERROR_NOACCESS) { |
| 263 if (rv == PP_OK) { | 263 if (rv == PP_OK) { |
| 264 return "URLLoader::Open() failed to block a cross-origin request."; | 264 return "URLLoader::Open() failed to block a cross-origin request."; |
| 265 } else { | 265 } else { |
| 266 return ReportError("URLLoader::Open()", rv); | 266 return ReportError("URLLoader::Open()", rv); |
| 267 } | 267 } |
| 268 } | 268 } |
| 269 | 269 |
| 270 PASS(); | 270 PASS(); |
| 271 } | 271 } |
| 272 | 272 |
| 273 // This test should cause a redirect and ensure that the loader runs | 273 // This test should cause a redirect and ensure that the loader runs |
| 274 // the callback, rather than following the redirect. | 274 // the callback, rather than following the redirect. |
| 275 std::string TestURLLoader::TestAuditURLRedirect() { | 275 std::string TestURLLoader::TestAuditURLRedirect() { |
| 276 pp::URLRequestInfo request; | 276 pp::URLRequestInfo request; |
| 277 // This path will cause the server to return a 301 redirect. | 277 // This path will cause the server to return a 301 redirect. |
| 278 request.SetURL("/server-redirect?www.google.com"); | 278 request.SetURL("/server-redirect?www.google.com"); |
| 279 request.SetFollowRedirects(false); | 279 request.SetFollowRedirects(false); |
| 280 | 280 |
| 281 TestCompletionCallback callback; | 281 TestCompletionCallback callback(instance_->pp_instance()); |
| 282 | 282 |
| 283 pp::URLLoader loader(*instance_); | 283 pp::URLLoader loader(*instance_); |
| 284 int32_t rv = loader.Open(request, callback); | 284 int32_t rv = loader.Open(request, callback); |
| 285 if (rv == PP_ERROR_WOULDBLOCK) | 285 if (rv == PP_ERROR_WOULDBLOCK) |
| 286 rv = callback.WaitForResult(); | 286 rv = callback.WaitForResult(); |
| 287 if (rv != PP_OK) | 287 if (rv != PP_OK) |
| 288 return ReportError("URLLoader::Open", rv); | 288 return ReportError("URLLoader::Open", rv); |
| 289 | 289 |
| 290 // Checks that the response indicates a redirect, and that the URL | 290 // Checks that the response indicates a redirect, and that the URL |
| 291 // is correct. | 291 // is correct. |
| 292 pp::URLResponseInfo response_info(loader.GetResponseInfo()); | 292 pp::URLResponseInfo response_info(loader.GetResponseInfo()); |
| 293 if (response_info.is_null()) | 293 if (response_info.is_null()) |
| 294 return "URLLoader::GetResponseInfo returned null"; | 294 return "URLLoader::GetResponseInfo returned null"; |
| 295 int32_t status_code = response_info.GetStatusCode(); | 295 int32_t status_code = response_info.GetStatusCode(); |
| 296 if (status_code != 301) | 296 if (status_code != 301) |
| 297 return "Response status should be 301"; | 297 return "Response status should be 301"; |
| 298 if (response_info.GetRedirectURL().AsString() != "www.google.com") | 298 if (response_info.GetRedirectURL().AsString() != "www.google.com") |
| 299 return "Redirect URL should be www.google.com"; | 299 return "Redirect URL should be www.google.com"; |
| 300 | 300 |
| 301 PASS(); | 301 PASS(); |
| 302 } | 302 } |
| 303 | 303 |
| 304 std::string TestURLLoader::TestAbortCalls() { | 304 std::string TestURLLoader::TestAbortCalls() { |
| 305 pp::URLRequestInfo request; | 305 pp::URLRequestInfo request; |
| 306 request.SetURL("test_url_loader_data/hello.txt"); | 306 request.SetURL("test_url_loader_data/hello.txt"); |
| 307 | 307 |
| 308 TestCompletionCallback callback; | 308 TestCompletionCallback callback(instance_->pp_instance()); |
| 309 int32_t rv; | 309 int32_t rv; |
| 310 | 310 |
| 311 // Abort |Open()|. | 311 // Abort |Open()|. |
| 312 { | 312 { |
| 313 callback.reset_run_count(); | 313 callback.reset_run_count(); |
| 314 rv = pp::URLLoader(*instance_).Open(request, callback); | 314 rv = pp::URLLoader(*instance_).Open(request, callback); |
| 315 if (callback.run_count() > 0) | 315 if (callback.run_count() > 0) |
| 316 return "URLLoader::Open ran callback synchronously."; | 316 return "URLLoader::Open ran callback synchronously."; |
| 317 if (rv == PP_ERROR_WOULDBLOCK) { | 317 if (rv == PP_ERROR_WOULDBLOCK) { |
| 318 rv = callback.WaitForResult(); | 318 rv = callback.WaitForResult(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 | 354 |
| 355 // TODO(viettrungluu): More abort tests (but add basic tests first). | 355 // TODO(viettrungluu): More abort tests (but add basic tests first). |
| 356 // Also test that Close() aborts properly. crbug.com/69457 | 356 // Also test that Close() aborts properly. crbug.com/69457 |
| 357 | 357 |
| 358 PASS(); | 358 PASS(); |
| 359 } | 359 } |
| 360 | 360 |
| 361 // TODO(viettrungluu): Add tests for FollowRedirect, | 361 // TODO(viettrungluu): Add tests for FollowRedirect, |
| 362 // Get{Upload,Download}Progress, Close (including abort tests if applicable). | 362 // Get{Upload,Download}Progress, Close (including abort tests if applicable). |
| 363 // TODO(darin): Add a test for GrantUniversalAccess. | 363 // TODO(darin): Add a test for GrantUniversalAccess. |
| OLD | NEW |