| OLD | NEW |
| 1 // Copyright (c) 2010 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> | 8 #include <string> |
| 9 | 9 |
| 10 #include "ppapi/c/dev/ppb_file_io_dev.h" | 10 #include "ppapi/c/dev/ppb_file_io_dev.h" |
| 11 #include "ppapi/c/dev/ppb_testing_dev.h" | 11 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 12 #include "ppapi/c/dev/ppb_url_loader_dev.h" | |
| 13 #include "ppapi/c/pp_errors.h" | 12 #include "ppapi/c/pp_errors.h" |
| 13 #include "ppapi/c/ppb_url_loader.h" |
| 14 #include "ppapi/cpp/dev/file_io_dev.h" | 14 #include "ppapi/cpp/dev/file_io_dev.h" |
| 15 #include "ppapi/cpp/dev/file_ref_dev.h" | 15 #include "ppapi/cpp/dev/file_ref_dev.h" |
| 16 #include "ppapi/cpp/dev/file_system_dev.h" | 16 #include "ppapi/cpp/dev/file_system_dev.h" |
| 17 #include "ppapi/cpp/dev/url_loader_dev.h" | |
| 18 #include "ppapi/cpp/dev/url_request_info_dev.h" | |
| 19 #include "ppapi/cpp/dev/url_response_info_dev.h" | |
| 20 #include "ppapi/cpp/instance.h" | 17 #include "ppapi/cpp/instance.h" |
| 21 #include "ppapi/cpp/module.h" | 18 #include "ppapi/cpp/module.h" |
| 19 #include "ppapi/cpp/url_loader.h" |
| 20 #include "ppapi/cpp/url_request_info.h" |
| 21 #include "ppapi/cpp/url_response_info.h" |
| 22 #include "ppapi/tests/test_utils.h" | 22 #include "ppapi/tests/test_utils.h" |
| 23 #include "ppapi/tests/testing_instance.h" | 23 #include "ppapi/tests/testing_instance.h" |
| 24 | 24 |
| 25 REGISTER_TEST_CASE(URLLoader); | 25 REGISTER_TEST_CASE(URLLoader); |
| 26 | 26 |
| 27 bool TestURLLoader::Init() { | 27 bool TestURLLoader::Init() { |
| 28 return InitTestingInterface() && EnsureRunningOverHTTP(); | 28 return InitTestingInterface() && EnsureRunningOverHTTP(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void TestURLLoader::RunTest() { | 31 void TestURLLoader::RunTest() { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 55 return ReportError("FileIO::Read", rv); | 55 return ReportError("FileIO::Read", rv); |
| 56 if (rv == 0) | 56 if (rv == 0) |
| 57 break; | 57 break; |
| 58 offset += rv; | 58 offset += rv; |
| 59 data->append(buf, rv); | 59 data->append(buf, rv); |
| 60 } | 60 } |
| 61 | 61 |
| 62 return ""; | 62 return ""; |
| 63 } | 63 } |
| 64 | 64 |
| 65 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader_Dev* loader, | 65 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader, |
| 66 std::string* body) { | 66 std::string* body) { |
| 67 TestCompletionCallback callback; | 67 TestCompletionCallback callback; |
| 68 char buf[256]; | 68 char buf[256]; |
| 69 | 69 |
| 70 for (;;) { | 70 for (;;) { |
| 71 int32_t rv = loader->ReadResponseBody(buf, sizeof(buf), callback); | 71 int32_t rv = loader->ReadResponseBody(buf, sizeof(buf), callback); |
| 72 if (rv == PP_ERROR_WOULDBLOCK) | 72 if (rv == PP_ERROR_WOULDBLOCK) |
| 73 rv = callback.WaitForResult(); | 73 rv = callback.WaitForResult(); |
| 74 if (rv < 0) | 74 if (rv < 0) |
| 75 return ReportError("URLLoader::ReadResponseBody", rv); | 75 return ReportError("URLLoader::ReadResponseBody", rv); |
| 76 if (rv == 0) | 76 if (rv == 0) |
| 77 break; | 77 break; |
| 78 body->append(buf, rv); | 78 body->append(buf, rv); |
| 79 } | 79 } |
| 80 | 80 |
| 81 return ""; | 81 return ""; |
| 82 } | 82 } |
| 83 | 83 |
| 84 std::string TestURLLoader::LoadAndCompareBody( | 84 std::string TestURLLoader::LoadAndCompareBody( |
| 85 const pp::URLRequestInfo_Dev& request, | 85 const pp::URLRequestInfo& request, |
| 86 const std::string& expected_body) { | 86 const std::string& expected_body) { |
| 87 TestCompletionCallback callback; | 87 TestCompletionCallback callback; |
| 88 | 88 |
| 89 pp::URLLoader_Dev loader(*instance_); | 89 pp::URLLoader loader(*instance_); |
| 90 int32_t rv = loader.Open(request, callback); | 90 int32_t rv = loader.Open(request, callback); |
| 91 if (rv == PP_ERROR_WOULDBLOCK) | 91 if (rv == PP_ERROR_WOULDBLOCK) |
| 92 rv = callback.WaitForResult(); | 92 rv = callback.WaitForResult(); |
| 93 if (rv != PP_OK) | 93 if (rv != PP_OK) |
| 94 return ReportError("URLLoader::Open", rv); | 94 return ReportError("URLLoader::Open", rv); |
| 95 | 95 |
| 96 pp::URLResponseInfo_Dev response_info(loader.GetResponseInfo()); | 96 pp::URLResponseInfo response_info(loader.GetResponseInfo()); |
| 97 if (response_info.is_null()) | 97 if (response_info.is_null()) |
| 98 return "URLLoader::GetResponseInfo returned null"; | 98 return "URLLoader::GetResponseInfo returned null"; |
| 99 int32_t status_code = response_info.GetStatusCode(); | 99 int32_t status_code = response_info.GetStatusCode(); |
| 100 if (status_code != 200) | 100 if (status_code != 200) |
| 101 return "Unexpected HTTP status code"; | 101 return "Unexpected HTTP status code"; |
| 102 | 102 |
| 103 std::string body; | 103 std::string body; |
| 104 std::string error = ReadEntireResponseBody(&loader, &body); | 104 std::string error = ReadEntireResponseBody(&loader, &body); |
| 105 if (!error.empty()) | 105 if (!error.empty()) |
| 106 return error; | 106 return error; |
| 107 | 107 |
| 108 if (body.size() != expected_body.size()) | 108 if (body.size() != expected_body.size()) |
| 109 return "URLLoader::ReadResponseBody returned unexpected content length"; | 109 return "URLLoader::ReadResponseBody returned unexpected content length"; |
| 110 if (body != expected_body) | 110 if (body != expected_body) |
| 111 return "URLLoader::ReadResponseBody returned unexpected content"; | 111 return "URLLoader::ReadResponseBody returned unexpected content"; |
| 112 | 112 |
| 113 return ""; | 113 return ""; |
| 114 } | 114 } |
| 115 | 115 |
| 116 std::string TestURLLoader::TestBasicGET() { | 116 std::string TestURLLoader::TestBasicGET() { |
| 117 pp::URLRequestInfo_Dev request; | 117 pp::URLRequestInfo request; |
| 118 request.SetURL("test_url_loader_data/hello.txt"); | 118 request.SetURL("test_url_loader_data/hello.txt"); |
| 119 return LoadAndCompareBody(request, "hello\n"); | 119 return LoadAndCompareBody(request, "hello\n"); |
| 120 } | 120 } |
| 121 | 121 |
| 122 std::string TestURLLoader::TestBasicPOST() { | 122 std::string TestURLLoader::TestBasicPOST() { |
| 123 pp::URLRequestInfo_Dev request; | 123 pp::URLRequestInfo request; |
| 124 request.SetURL("/echo"); | 124 request.SetURL("/echo"); |
| 125 request.SetMethod("POST"); | 125 request.SetMethod("POST"); |
| 126 std::string postdata("postdata"); | 126 std::string postdata("postdata"); |
| 127 request.AppendDataToBody(postdata.data(), postdata.length()); | 127 request.AppendDataToBody(postdata.data(), postdata.length()); |
| 128 return LoadAndCompareBody(request, postdata); | 128 return LoadAndCompareBody(request, postdata); |
| 129 } | 129 } |
| 130 | 130 |
| 131 std::string TestURLLoader::TestCompoundBodyPOST() { | 131 std::string TestURLLoader::TestCompoundBodyPOST() { |
| 132 pp::URLRequestInfo_Dev request; | 132 pp::URLRequestInfo request; |
| 133 request.SetURL("/echo"); | 133 request.SetURL("/echo"); |
| 134 request.SetMethod("POST"); | 134 request.SetMethod("POST"); |
| 135 std::string postdata1("post"); | 135 std::string postdata1("post"); |
| 136 request.AppendDataToBody(postdata1.data(), postdata1.length()); | 136 request.AppendDataToBody(postdata1.data(), postdata1.length()); |
| 137 std::string postdata2("data"); | 137 std::string postdata2("data"); |
| 138 request.AppendDataToBody(postdata2.data(), postdata2.length()); | 138 request.AppendDataToBody(postdata2.data(), postdata2.length()); |
| 139 return LoadAndCompareBody(request, postdata1 + postdata2); | 139 return LoadAndCompareBody(request, postdata1 + postdata2); |
| 140 } | 140 } |
| 141 | 141 |
| 142 std::string TestURLLoader::TestEmptyDataPOST() { | 142 std::string TestURLLoader::TestEmptyDataPOST() { |
| 143 pp::URLRequestInfo_Dev request; | 143 pp::URLRequestInfo request; |
| 144 request.SetURL("/echo"); | 144 request.SetURL("/echo"); |
| 145 request.SetMethod("POST"); | 145 request.SetMethod("POST"); |
| 146 request.AppendDataToBody("", 0); | 146 request.AppendDataToBody("", 0); |
| 147 return LoadAndCompareBody(request, ""); | 147 return LoadAndCompareBody(request, ""); |
| 148 } | 148 } |
| 149 | 149 |
| 150 std::string TestURLLoader::TestBinaryDataPOST() { | 150 std::string TestURLLoader::TestBinaryDataPOST() { |
| 151 pp::URLRequestInfo_Dev request; | 151 pp::URLRequestInfo request; |
| 152 request.SetURL("/echo"); | 152 request.SetURL("/echo"); |
| 153 request.SetMethod("POST"); | 153 request.SetMethod("POST"); |
| 154 const char postdata_chars[] = | 154 const char postdata_chars[] = |
| 155 "\x00\x01\x02\x03\x04\x05postdata\xfa\xfb\xfc\xfd\xfe\xff"; | 155 "\x00\x01\x02\x03\x04\x05postdata\xfa\xfb\xfc\xfd\xfe\xff"; |
| 156 std::string postdata(postdata_chars, | 156 std::string postdata(postdata_chars, |
| 157 sizeof(postdata_chars) / sizeof(postdata_chars[0])); | 157 sizeof(postdata_chars) / sizeof(postdata_chars[0])); |
| 158 request.AppendDataToBody(postdata.data(), postdata.length()); | 158 request.AppendDataToBody(postdata.data(), postdata.length()); |
| 159 return LoadAndCompareBody(request, postdata); | 159 return LoadAndCompareBody(request, postdata); |
| 160 } | 160 } |
| 161 | 161 |
| 162 std::string TestURLLoader::TestCustomRequestHeader() { | 162 std::string TestURLLoader::TestCustomRequestHeader() { |
| 163 pp::URLRequestInfo_Dev request; | 163 pp::URLRequestInfo request; |
| 164 request.SetURL("/echoheader?Foo"); | 164 request.SetURL("/echoheader?Foo"); |
| 165 request.SetHeaders("Foo: 1"); | 165 request.SetHeaders("Foo: 1"); |
| 166 return LoadAndCompareBody(request, "1"); | 166 return LoadAndCompareBody(request, "1"); |
| 167 } | 167 } |
| 168 | 168 |
| 169 std::string TestURLLoader::TestIgnoresBogusContentLength() { | 169 std::string TestURLLoader::TestIgnoresBogusContentLength() { |
| 170 pp::URLRequestInfo_Dev request; | 170 pp::URLRequestInfo request; |
| 171 request.SetURL("/echo"); | 171 request.SetURL("/echo"); |
| 172 request.SetMethod("POST"); | 172 request.SetMethod("POST"); |
| 173 request.SetHeaders("Content-Length: 400"); | 173 request.SetHeaders("Content-Length: 400"); |
| 174 std::string postdata("postdata"); | 174 std::string postdata("postdata"); |
| 175 request.AppendDataToBody(postdata.data(), postdata.length()); | 175 request.AppendDataToBody(postdata.data(), postdata.length()); |
| 176 return LoadAndCompareBody(request, postdata); | 176 return LoadAndCompareBody(request, postdata); |
| 177 } | 177 } |
| 178 | 178 |
| 179 std::string TestURLLoader::TestStreamToFile() { | 179 std::string TestURLLoader::TestStreamToFile() { |
| 180 pp::URLRequestInfo_Dev request; | 180 pp::URLRequestInfo request; |
| 181 request.SetURL("test_url_loader_data/hello.txt"); | 181 request.SetURL("test_url_loader_data/hello.txt"); |
| 182 request.SetStreamToFile(true); | 182 request.SetStreamToFile(true); |
| 183 | 183 |
| 184 TestCompletionCallback callback; | 184 TestCompletionCallback callback; |
| 185 | 185 |
| 186 pp::URLLoader_Dev loader(*instance_); | 186 pp::URLLoader loader(*instance_); |
| 187 int32_t rv = loader.Open(request, callback); | 187 int32_t rv = loader.Open(request, callback); |
| 188 if (rv == PP_ERROR_WOULDBLOCK) | 188 if (rv == PP_ERROR_WOULDBLOCK) |
| 189 rv = callback.WaitForResult(); | 189 rv = callback.WaitForResult(); |
| 190 if (rv != PP_OK) | 190 if (rv != PP_OK) |
| 191 return ReportError("URLLoader::Open", rv); | 191 return ReportError("URLLoader::Open", rv); |
| 192 | 192 |
| 193 pp::URLResponseInfo_Dev response_info(loader.GetResponseInfo()); | 193 pp::URLResponseInfo response_info(loader.GetResponseInfo()); |
| 194 if (response_info.is_null()) | 194 if (response_info.is_null()) |
| 195 return "URLLoader::GetResponseInfo returned null"; | 195 return "URLLoader::GetResponseInfo returned null"; |
| 196 int32_t status_code = response_info.GetStatusCode(); | 196 int32_t status_code = response_info.GetStatusCode(); |
| 197 if (status_code != 200) | 197 if (status_code != 200) |
| 198 return "Unexpected HTTP status code"; | 198 return "Unexpected HTTP status code"; |
| 199 | 199 |
| 200 pp::FileRef_Dev body(response_info.GetBody()); | 200 pp::FileRef_Dev body(response_info.GetBodyAsFileRef()); |
| 201 if (body.is_null()) | 201 if (body.is_null()) |
| 202 return "URLResponseInfo::GetBody returned null"; | 202 return "URLResponseInfo::GetBody returned null"; |
| 203 | 203 |
| 204 rv = loader.FinishStreamingToFile(callback); | 204 rv = loader.FinishStreamingToFile(callback); |
| 205 if (rv == PP_ERROR_WOULDBLOCK) | 205 if (rv == PP_ERROR_WOULDBLOCK) |
| 206 rv = callback.WaitForResult(); | 206 rv = callback.WaitForResult(); |
| 207 if (rv != PP_OK) | 207 if (rv != PP_OK) |
| 208 return ReportError("URLLoader::FinishStreamingToFile", rv); | 208 return ReportError("URLLoader::FinishStreamingToFile", rv); |
| 209 | 209 |
| 210 | 210 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 227 return "ReadEntireFile returned unexpected content"; | 227 return "ReadEntireFile returned unexpected content"; |
| 228 | 228 |
| 229 int32_t file_descriptor = reader.GetOSFileDescriptor(); | 229 int32_t file_descriptor = reader.GetOSFileDescriptor(); |
| 230 if (file_descriptor < 0) | 230 if (file_descriptor < 0) |
| 231 return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; | 231 return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; |
| 232 | 232 |
| 233 return ""; | 233 return ""; |
| 234 } | 234 } |
| 235 | 235 |
| 236 std::string TestURLLoader::TestSameOriginRestriction() { | 236 std::string TestURLLoader::TestSameOriginRestriction() { |
| 237 pp::URLRequestInfo_Dev request; | 237 pp::URLRequestInfo request; |
| 238 request.SetURL("http://www.google.com/"); | 238 request.SetURL("http://www.google.com/"); |
| 239 | 239 |
| 240 TestCompletionCallback callback; | 240 TestCompletionCallback callback; |
| 241 | 241 |
| 242 pp::URLLoader_Dev loader(*instance_); | 242 pp::URLLoader loader(*instance_); |
| 243 int32_t rv = loader.Open(request, callback); | 243 int32_t rv = loader.Open(request, callback); |
| 244 if (rv == PP_ERROR_WOULDBLOCK) | 244 if (rv == PP_ERROR_WOULDBLOCK) |
| 245 rv = callback.WaitForResult(); | 245 rv = callback.WaitForResult(); |
| 246 | 246 |
| 247 // We expect a failure. | 247 // We expect a failure. |
| 248 if (rv != PP_ERROR_NOACCESS) { | 248 if (rv != PP_ERROR_NOACCESS) { |
| 249 if (rv == PP_OK) { | 249 if (rv == PP_OK) { |
| 250 return "URLLoader::Open() failed to block a cross-origin request."; | 250 return "URLLoader::Open() failed to block a cross-origin request."; |
| 251 } else { | 251 } else { |
| 252 return ReportError("URLLoader::Open()", rv); | 252 return ReportError("URLLoader::Open()", rv); |
| 253 } | 253 } |
| 254 } | 254 } |
| 255 | 255 |
| 256 return ""; | 256 return ""; |
| 257 } | 257 } |
| 258 | 258 |
| 259 // This test should cause a redirect and ensure that the loader runs | 259 // This test should cause a redirect and ensure that the loader runs |
| 260 // the callback, rather than following the redirect. | 260 // the callback, rather than following the redirect. |
| 261 std::string TestURLLoader::TestAuditURLRedirect() { | 261 std::string TestURLLoader::TestAuditURLRedirect() { |
| 262 pp::URLRequestInfo_Dev request; | 262 pp::URLRequestInfo request; |
| 263 // This path will cause the server to return a 301 redirect. | 263 // This path will cause the server to return a 301 redirect. |
| 264 request.SetURL("/server-redirect?www.google.com"); | 264 request.SetURL("/server-redirect?www.google.com"); |
| 265 request.SetFollowRedirects(false); | 265 request.SetFollowRedirects(false); |
| 266 | 266 |
| 267 TestCompletionCallback callback; | 267 TestCompletionCallback callback; |
| 268 | 268 |
| 269 pp::URLLoader_Dev loader(*instance_); | 269 pp::URLLoader loader(*instance_); |
| 270 int32_t rv = loader.Open(request, callback); | 270 int32_t rv = loader.Open(request, callback); |
| 271 if (rv == PP_ERROR_WOULDBLOCK) | 271 if (rv == PP_ERROR_WOULDBLOCK) |
| 272 rv = callback.WaitForResult(); | 272 rv = callback.WaitForResult(); |
| 273 if (rv != PP_OK) | 273 if (rv != PP_OK) |
| 274 return ReportError("URLLoader::Open", rv); | 274 return ReportError("URLLoader::Open", rv); |
| 275 | 275 |
| 276 // Checks that the response indicates a redirect, and that the URL | 276 // Checks that the response indicates a redirect, and that the URL |
| 277 // is correct. | 277 // is correct. |
| 278 pp::URLResponseInfo_Dev response_info(loader.GetResponseInfo()); | 278 pp::URLResponseInfo response_info(loader.GetResponseInfo()); |
| 279 if (response_info.is_null()) | 279 if (response_info.is_null()) |
| 280 return "URLLoader::GetResponseInfo returned null"; | 280 return "URLLoader::GetResponseInfo returned null"; |
| 281 int32_t status_code = response_info.GetStatusCode(); | 281 int32_t status_code = response_info.GetStatusCode(); |
| 282 if (status_code != 301) | 282 if (status_code != 301) |
| 283 return "Response status should be 301"; | 283 return "Response status should be 301"; |
| 284 if (response_info.GetRedirectURL().AsString() != "www.google.com") | 284 if (response_info.GetRedirectURL().AsString() != "www.google.com") |
| 285 return "Redirect URL should be www.google.com"; | 285 return "Redirect URL should be www.google.com"; |
| 286 | 286 |
| 287 return ""; | 287 return ""; |
| 288 } | 288 } |
| 289 | 289 |
| 290 // TODO(darin): Add a test for GrantUniversalAccess. | 290 // TODO(darin): Add a test for GrantUniversalAccess. |
| OLD | NEW |