| 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 10 matching lines...) Expand all Loading... |
| 21 #include "ppapi/cpp/instance.h" | 21 #include "ppapi/cpp/instance.h" |
| 22 #include "ppapi/cpp/module.h" | 22 #include "ppapi/cpp/module.h" |
| 23 #include "ppapi/cpp/url_loader.h" | 23 #include "ppapi/cpp/url_loader.h" |
| 24 #include "ppapi/cpp/url_request_info.h" | 24 #include "ppapi/cpp/url_request_info.h" |
| 25 #include "ppapi/cpp/url_response_info.h" | 25 #include "ppapi/cpp/url_response_info.h" |
| 26 #include "ppapi/tests/test_utils.h" | 26 #include "ppapi/tests/test_utils.h" |
| 27 #include "ppapi/tests/testing_instance.h" | 27 #include "ppapi/tests/testing_instance.h" |
| 28 | 28 |
| 29 REGISTER_TEST_CASE(URLLoader); | 29 REGISTER_TEST_CASE(URLLoader); |
| 30 | 30 |
| 31 namespace { |
| 32 |
| 33 int32_t WriteEntireBuffer(PP_Instance instance, |
| 34 pp::FileIO* file_io, |
| 35 int32_t offset, |
| 36 const std::string& data) { |
| 37 TestCompletionCallback callback(instance); |
| 38 int32_t write_offset = offset; |
| 39 const char* buf = data.c_str(); |
| 40 int32_t size = data.size(); |
| 41 |
| 42 while (write_offset < offset + size) { |
| 43 int32_t rv = file_io->Write(write_offset, &buf[write_offset - offset], |
| 44 size - write_offset + offset, callback); |
| 45 if (rv == PP_OK_COMPLETIONPENDING) |
| 46 rv = callback.WaitForResult(); |
| 47 if (rv < 0) |
| 48 return rv; |
| 49 if (rv == 0) |
| 50 return PP_ERROR_FAILED; |
| 51 write_offset += rv; |
| 52 } |
| 53 |
| 54 return PP_OK; |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 31 TestURLLoader::TestURLLoader(TestingInstance* instance) | 59 TestURLLoader::TestURLLoader(TestingInstance* instance) |
| 32 : TestCase(instance), | 60 : TestCase(instance), |
| 33 file_io_trusted_interface_(NULL) { | 61 file_io_trusted_interface_(NULL) { |
| 34 } | 62 } |
| 35 | 63 |
| 36 bool TestURLLoader::Init() { | 64 bool TestURLLoader::Init() { |
| 37 file_io_trusted_interface_ = static_cast<const PPB_FileIOTrusted*>( | 65 file_io_trusted_interface_ = static_cast<const PPB_FileIOTrusted*>( |
| 38 pp::Module::Get()->GetBrowserInterface(PPB_FILEIOTRUSTED_INTERFACE)); | 66 pp::Module::Get()->GetBrowserInterface(PPB_FILEIOTRUSTED_INTERFACE)); |
| 39 if (!file_io_trusted_interface_) { | 67 if (!file_io_trusted_interface_) { |
| 40 instance_->AppendError("FileIOTrusted interface not available"); | 68 instance_->AppendError("FileIOTrusted interface not available"); |
| 41 } | 69 } |
| 42 return InitTestingInterface() && EnsureRunningOverHTTP(); | 70 return InitTestingInterface() && EnsureRunningOverHTTP(); |
| 43 } | 71 } |
| 44 | 72 |
| 45 void TestURLLoader::RunTest() { | 73 void TestURLLoader::RunTest() { |
| 46 RUN_TEST_FORCEASYNC_AND_NOT(BasicGET); | 74 RUN_TEST_FORCEASYNC_AND_NOT(BasicGET); |
| 47 RUN_TEST_FORCEASYNC_AND_NOT(BasicPOST); | 75 RUN_TEST_FORCEASYNC_AND_NOT(BasicPOST); |
| 76 RUN_TEST_FORCEASYNC_AND_NOT(BasicFilePOST); |
| 77 RUN_TEST_FORCEASYNC_AND_NOT(BasicFileRangePOST); |
| 48 RUN_TEST_FORCEASYNC_AND_NOT(CompoundBodyPOST); | 78 RUN_TEST_FORCEASYNC_AND_NOT(CompoundBodyPOST); |
| 49 RUN_TEST_FORCEASYNC_AND_NOT(EmptyDataPOST); | 79 RUN_TEST_FORCEASYNC_AND_NOT(EmptyDataPOST); |
| 50 RUN_TEST_FORCEASYNC_AND_NOT(BinaryDataPOST); | 80 RUN_TEST_FORCEASYNC_AND_NOT(BinaryDataPOST); |
| 51 RUN_TEST_FORCEASYNC_AND_NOT(CustomRequestHeader); | 81 RUN_TEST_FORCEASYNC_AND_NOT(CustomRequestHeader); |
| 52 RUN_TEST_FORCEASYNC_AND_NOT(IgnoresBogusContentLength); | 82 RUN_TEST_FORCEASYNC_AND_NOT(IgnoresBogusContentLength); |
| 53 RUN_TEST_FORCEASYNC_AND_NOT(SameOriginRestriction); | 83 RUN_TEST_FORCEASYNC_AND_NOT(SameOriginRestriction); |
| 54 RUN_TEST_FORCEASYNC_AND_NOT(JavascriptURLRestriction); | 84 RUN_TEST_FORCEASYNC_AND_NOT(JavascriptURLRestriction); |
| 55 RUN_TEST_FORCEASYNC_AND_NOT(CrossOriginRequest); | 85 RUN_TEST_FORCEASYNC_AND_NOT(CrossOriginRequest); |
| 56 RUN_TEST_FORCEASYNC_AND_NOT(StreamToFile); | 86 RUN_TEST_FORCEASYNC_AND_NOT(StreamToFile); |
| 57 RUN_TEST_FORCEASYNC_AND_NOT(AuditURLRedirect); | 87 RUN_TEST_FORCEASYNC_AND_NOT(AuditURLRedirect); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 return error; | 160 return error; |
| 131 | 161 |
| 132 if (body.size() != expected_body.size()) | 162 if (body.size() != expected_body.size()) |
| 133 return "URLLoader::ReadResponseBody returned unexpected content length"; | 163 return "URLLoader::ReadResponseBody returned unexpected content length"; |
| 134 if (body != expected_body) | 164 if (body != expected_body) |
| 135 return "URLLoader::ReadResponseBody returned unexpected content"; | 165 return "URLLoader::ReadResponseBody returned unexpected content"; |
| 136 | 166 |
| 137 PASS(); | 167 PASS(); |
| 138 } | 168 } |
| 139 | 169 |
| 170 int32_t TestURLLoader::OpenFileSystem(pp::FileSystem* file_system, |
| 171 std::string* message) { |
| 172 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 173 int32_t rv = file_system->Open(1024, callback); |
| 174 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) { |
| 175 message->assign("FileSystem::Open force_async"); |
| 176 return rv; |
| 177 } |
| 178 if (rv == PP_OK_COMPLETIONPENDING) |
| 179 rv = callback.WaitForResult(); |
| 180 if (rv != PP_OK) { |
| 181 message->assign("FileSystem::Open"); |
| 182 return rv; |
| 183 } |
| 184 return rv; |
| 185 } |
| 186 |
| 187 int32_t TestURLLoader::PrepareFileForPost( |
| 188 const pp::FileRef& file_ref, |
| 189 const std::string& data, |
| 190 std::string* message) { |
| 191 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 192 pp::FileIO file_io(instance_); |
| 193 int32_t rv = file_io.Open(file_ref, |
| 194 PP_FILEOPENFLAG_CREATE | |
| 195 PP_FILEOPENFLAG_TRUNCATE | |
| 196 PP_FILEOPENFLAG_WRITE, |
| 197 callback); |
| 198 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) { |
| 199 message->assign("FileIO::Open force_async"); |
| 200 return rv; |
| 201 } |
| 202 if (rv == PP_OK_COMPLETIONPENDING) |
| 203 rv = callback.WaitForResult(); |
| 204 if (rv != PP_OK) { |
| 205 message->assign("FileIO::Open"); |
| 206 return rv; |
| 207 } |
| 208 |
| 209 rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, data); |
| 210 if (rv != PP_OK) { |
| 211 message->assign("FileIO::Write"); |
| 212 return rv; |
| 213 } |
| 214 |
| 215 return rv; |
| 216 } |
| 217 |
| 140 std::string TestURLLoader::TestBasicGET() { | 218 std::string TestURLLoader::TestBasicGET() { |
| 141 pp::URLRequestInfo request(instance_); | 219 pp::URLRequestInfo request(instance_); |
| 142 request.SetURL("test_url_loader_data/hello.txt"); | 220 request.SetURL("test_url_loader_data/hello.txt"); |
| 143 return LoadAndCompareBody(request, "hello\n"); | 221 return LoadAndCompareBody(request, "hello\n"); |
| 144 } | 222 } |
| 145 | 223 |
| 146 std::string TestURLLoader::TestBasicPOST() { | 224 std::string TestURLLoader::TestBasicPOST() { |
| 147 pp::URLRequestInfo request(instance_); | 225 pp::URLRequestInfo request(instance_); |
| 148 request.SetURL("/echo"); | 226 request.SetURL("/echo"); |
| 149 request.SetMethod("POST"); | 227 request.SetMethod("POST"); |
| 150 std::string postdata("postdata"); | 228 std::string postdata("postdata"); |
| 151 request.AppendDataToBody(postdata.data(), postdata.length()); | 229 request.AppendDataToBody(postdata.data(), postdata.length()); |
| 152 return LoadAndCompareBody(request, postdata); | 230 return LoadAndCompareBody(request, postdata); |
| 153 } | 231 } |
| 154 | 232 |
| 233 std::string TestURLLoader::TestBasicFilePOST() { |
| 234 std::string message; |
| 235 |
| 236 pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); |
| 237 int32_t rv = OpenFileSystem(&file_system, &message); |
| 238 if (rv != PP_OK) |
| 239 return ReportError(message.c_str(), rv); |
| 240 |
| 241 pp::FileRef file_ref(file_system, "/file_post_test"); |
| 242 std::string postdata("postdata"); |
| 243 rv = PrepareFileForPost(file_ref, postdata, &message); |
| 244 if (rv != PP_OK) |
| 245 return ReportError(message.c_str(), rv); |
| 246 |
| 247 pp::URLRequestInfo request(instance_); |
| 248 request.SetURL("/echo"); |
| 249 request.SetMethod("POST"); |
| 250 request.AppendFileToBody(file_ref, 0); |
| 251 return LoadAndCompareBody(request, postdata); |
| 252 } |
| 253 |
| 254 std::string TestURLLoader::TestBasicFileRangePOST() { |
| 255 std::string message; |
| 256 |
| 257 pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); |
| 258 int32_t rv = OpenFileSystem(&file_system, &message); |
| 259 if (rv != PP_OK) |
| 260 return ReportError(message.c_str(), rv); |
| 261 |
| 262 pp::FileRef file_ref(file_system, "/file_range_post_test"); |
| 263 std::string postdata("postdatapostdata"); |
| 264 rv = PrepareFileForPost(file_ref, postdata, &message); |
| 265 if (rv != PP_OK) |
| 266 return ReportError(message.c_str(), rv); |
| 267 |
| 268 pp::URLRequestInfo request(instance_); |
| 269 request.SetURL("/echo"); |
| 270 request.SetMethod("POST"); |
| 271 request.AppendFileRangeToBody(file_ref, 4, 12, 0); |
| 272 return LoadAndCompareBody(request, postdata.substr(4, 12)); |
| 273 } |
| 274 |
| 155 std::string TestURLLoader::TestCompoundBodyPOST() { | 275 std::string TestURLLoader::TestCompoundBodyPOST() { |
| 156 pp::URLRequestInfo request(instance_); | 276 pp::URLRequestInfo request(instance_); |
| 157 request.SetURL("/echo"); | 277 request.SetURL("/echo"); |
| 158 request.SetMethod("POST"); | 278 request.SetMethod("POST"); |
| 159 std::string postdata1("post"); | 279 std::string postdata1("post"); |
| 160 request.AppendDataToBody(postdata1.data(), postdata1.length()); | 280 request.AppendDataToBody(postdata1.data(), postdata1.length()); |
| 161 std::string postdata2("data"); | 281 std::string postdata2("data"); |
| 162 request.AppendDataToBody(postdata2.data(), postdata2.length()); | 282 request.AppendDataToBody(postdata2.data(), postdata2.length()); |
| 163 return LoadAndCompareBody(request, postdata1 + postdata2); | 283 return LoadAndCompareBody(request, postdata1 + postdata2); |
| 164 } | 284 } |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 return error; | 601 return error; |
| 482 if (body != "hello\n") | 602 if (body != "hello\n") |
| 483 return ReportError("Couldn't read data", rv); | 603 return ReportError("Couldn't read data", rv); |
| 484 | 604 |
| 485 PASS(); | 605 PASS(); |
| 486 } | 606 } |
| 487 | 607 |
| 488 // TODO(viettrungluu): Add tests for FollowRedirect, | 608 // TODO(viettrungluu): Add tests for FollowRedirect, |
| 489 // Get{Upload,Download}Progress, Close (including abort tests if applicable). | 609 // Get{Upload,Download}Progress, Close (including abort tests if applicable). |
| 490 // TODO(darin): Add a test for GrantUniversalAccess. | 610 // TODO(darin): Add a test for GrantUniversalAccess. |
| OLD | NEW |