Chromium Code Reviews| 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 27 matching lines...) Expand all Loading... | |
| 38 pp::Module::Get()->GetBrowserInterface(PPB_FILEIOTRUSTED_INTERFACE)); | 38 pp::Module::Get()->GetBrowserInterface(PPB_FILEIOTRUSTED_INTERFACE)); |
| 39 if (!file_io_trusted_interface_) { | 39 if (!file_io_trusted_interface_) { |
| 40 instance_->AppendError("FileIOTrusted interface not available"); | 40 instance_->AppendError("FileIOTrusted interface not available"); |
| 41 } | 41 } |
| 42 return InitTestingInterface() && EnsureRunningOverHTTP(); | 42 return InitTestingInterface() && EnsureRunningOverHTTP(); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void TestURLLoader::RunTest() { | 45 void TestURLLoader::RunTest() { |
| 46 RUN_TEST_FORCEASYNC_AND_NOT(BasicGET); | 46 RUN_TEST_FORCEASYNC_AND_NOT(BasicGET); |
| 47 RUN_TEST_FORCEASYNC_AND_NOT(BasicPOST); | 47 RUN_TEST_FORCEASYNC_AND_NOT(BasicPOST); |
| 48 RUN_TEST_FORCEASYNC_AND_NOT(BasicFilePOST); | |
| 49 RUN_TEST_FORCEASYNC_AND_NOT(BasicFileRangePOST); | |
| 48 RUN_TEST_FORCEASYNC_AND_NOT(CompoundBodyPOST); | 50 RUN_TEST_FORCEASYNC_AND_NOT(CompoundBodyPOST); |
| 49 RUN_TEST_FORCEASYNC_AND_NOT(EmptyDataPOST); | 51 RUN_TEST_FORCEASYNC_AND_NOT(EmptyDataPOST); |
| 50 RUN_TEST_FORCEASYNC_AND_NOT(BinaryDataPOST); | 52 RUN_TEST_FORCEASYNC_AND_NOT(BinaryDataPOST); |
| 51 RUN_TEST_FORCEASYNC_AND_NOT(CustomRequestHeader); | 53 RUN_TEST_FORCEASYNC_AND_NOT(CustomRequestHeader); |
| 52 RUN_TEST_FORCEASYNC_AND_NOT(IgnoresBogusContentLength); | 54 RUN_TEST_FORCEASYNC_AND_NOT(IgnoresBogusContentLength); |
| 53 RUN_TEST_FORCEASYNC_AND_NOT(SameOriginRestriction); | 55 RUN_TEST_FORCEASYNC_AND_NOT(SameOriginRestriction); |
| 54 RUN_TEST_FORCEASYNC_AND_NOT(JavascriptURLRestriction); | 56 RUN_TEST_FORCEASYNC_AND_NOT(JavascriptURLRestriction); |
| 55 RUN_TEST_FORCEASYNC_AND_NOT(CrossOriginRequest); | 57 RUN_TEST_FORCEASYNC_AND_NOT(CrossOriginRequest); |
| 56 RUN_TEST_FORCEASYNC_AND_NOT(StreamToFile); | 58 RUN_TEST_FORCEASYNC_AND_NOT(StreamToFile); |
| 57 RUN_TEST_FORCEASYNC_AND_NOT(AuditURLRedirect); | 59 RUN_TEST_FORCEASYNC_AND_NOT(AuditURLRedirect); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 75 return ReportError("FileIO::Read", rv); | 77 return ReportError("FileIO::Read", rv); |
| 76 if (rv == 0) | 78 if (rv == 0) |
| 77 break; | 79 break; |
| 78 offset += rv; | 80 offset += rv; |
| 79 data->append(buf, rv); | 81 data->append(buf, rv); |
| 80 } | 82 } |
| 81 | 83 |
| 82 PASS(); | 84 PASS(); |
| 83 } | 85 } |
| 84 | 86 |
| 87 int32_t WriteEntireBuffer(PP_Instance instance, | |
|
michaeln
2011/08/17 00:16:24
should this be static or in an anon namespace or m
kinuko
2011/08/17 07:55:28
Put this in an anon namespace and placed at top ov
| |
| 88 pp::FileIO* file_io, | |
| 89 int32_t offset, | |
| 90 const std::string& data) { | |
| 91 TestCompletionCallback callback(instance); | |
| 92 int32_t write_offset = offset; | |
| 93 const char* buf = data.c_str(); | |
| 94 int32_t size = data.size(); | |
| 95 | |
| 96 while (write_offset < offset + size) { | |
| 97 int32_t rv = file_io->Write(write_offset, &buf[write_offset - offset], | |
| 98 size - write_offset + offset, callback); | |
| 99 if (rv == PP_OK_COMPLETIONPENDING) | |
| 100 rv = callback.WaitForResult(); | |
| 101 if (rv < 0) | |
| 102 return rv; | |
| 103 if (rv == 0) | |
| 104 return PP_ERROR_FAILED; | |
| 105 write_offset += rv; | |
| 106 } | |
| 107 | |
| 108 return PP_OK; | |
| 109 } | |
| 110 | |
| 85 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader, | 111 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader, |
| 86 std::string* body) { | 112 std::string* body) { |
| 87 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 113 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 88 char buf[2]; // Small so that multiple reads are needed. | 114 char buf[2]; // Small so that multiple reads are needed. |
| 89 | 115 |
| 90 for (;;) { | 116 for (;;) { |
| 91 int32_t rv = loader->ReadResponseBody(buf, sizeof(buf), callback); | 117 int32_t rv = loader->ReadResponseBody(buf, sizeof(buf), callback); |
| 92 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | 118 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 93 return ReportError("URLLoader::ReadResponseBody force_async", rv); | 119 return ReportError("URLLoader::ReadResponseBody force_async", rv); |
| 94 if (rv == PP_OK_COMPLETIONPENDING) | 120 if (rv == PP_OK_COMPLETIONPENDING) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 | 171 |
| 146 std::string TestURLLoader::TestBasicPOST() { | 172 std::string TestURLLoader::TestBasicPOST() { |
| 147 pp::URLRequestInfo request(instance_); | 173 pp::URLRequestInfo request(instance_); |
| 148 request.SetURL("/echo"); | 174 request.SetURL("/echo"); |
| 149 request.SetMethod("POST"); | 175 request.SetMethod("POST"); |
| 150 std::string postdata("postdata"); | 176 std::string postdata("postdata"); |
| 151 request.AppendDataToBody(postdata.data(), postdata.length()); | 177 request.AppendDataToBody(postdata.data(), postdata.length()); |
| 152 return LoadAndCompareBody(request, postdata); | 178 return LoadAndCompareBody(request, postdata); |
| 153 } | 179 } |
| 154 | 180 |
| 181 std::string TestURLLoader::TestBasicFilePOST() { | |
| 182 TestCompletionCallback callback(instance_->pp_instance(), false); | |
| 183 pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); | |
|
darin (slow to review)
2011/08/16 17:28:36
it looks like TestBasicFilePOST and TestBasicFileR
ericu
2011/08/17 01:14:31
+1
kinuko
2011/08/17 07:55:28
Done.
| |
| 184 pp::FileRef file_ref(file_system, "/file_post_test"); | |
| 185 int32_t rv = file_system.Open(1024, callback); | |
| 186 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | |
| 187 return ReportError("FileSystem::Open force_async", rv); | |
| 188 if (rv == PP_OK_COMPLETIONPENDING) | |
| 189 rv = callback.WaitForResult(); | |
| 190 if (rv != PP_OK) | |
| 191 return ReportError("FileSystem::Open", rv); | |
| 192 | |
| 193 pp::FileIO file_io(instance_); | |
| 194 rv = file_io.Open(file_ref, | |
| 195 PP_FILEOPENFLAG_CREATE | | |
| 196 PP_FILEOPENFLAG_TRUNCATE | | |
| 197 PP_FILEOPENFLAG_READ | | |
| 198 PP_FILEOPENFLAG_WRITE, | |
| 199 callback); | |
| 200 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | |
| 201 return ReportError("FileIO::Open force_async", rv); | |
| 202 if (rv == PP_OK_COMPLETIONPENDING) | |
| 203 rv = callback.WaitForResult(); | |
| 204 if (rv != PP_OK) | |
| 205 return ReportError("FileIO::Open", rv); | |
| 206 | |
| 207 std::string postdata("postdata"); | |
| 208 rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, postdata); | |
| 209 if (rv != PP_OK) | |
| 210 return ReportError("FileIO::Write", rv); | |
| 211 | |
| 212 pp::URLRequestInfo request(instance_); | |
| 213 request.SetURL("/echo"); | |
| 214 request.SetMethod("POST"); | |
| 215 request.AppendFileToBody(file_ref, 0); | |
| 216 return LoadAndCompareBody(request, postdata); | |
| 217 } | |
| 218 | |
| 219 std::string TestURLLoader::TestBasicFileRangePOST() { | |
| 220 TestCompletionCallback callback(instance_->pp_instance(), false); | |
| 221 pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); | |
| 222 pp::FileRef file_ref(file_system, "/file_post_test"); | |
| 223 int32_t rv = file_system.Open(1024, callback); | |
| 224 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | |
| 225 return ReportError("FileSystem::Open force_async", rv); | |
| 226 if (rv == PP_OK_COMPLETIONPENDING) | |
| 227 rv = callback.WaitForResult(); | |
| 228 if (rv != PP_OK) | |
| 229 return ReportError("FileSystem::Open", rv); | |
| 230 | |
| 231 pp::FileIO file_io(instance_); | |
| 232 rv = file_io.Open(file_ref, | |
| 233 PP_FILEOPENFLAG_CREATE | | |
| 234 PP_FILEOPENFLAG_TRUNCATE | | |
| 235 PP_FILEOPENFLAG_READ | | |
| 236 PP_FILEOPENFLAG_WRITE, | |
| 237 callback); | |
| 238 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | |
| 239 return ReportError("FileIO::Open force_async", rv); | |
| 240 if (rv == PP_OK_COMPLETIONPENDING) | |
| 241 rv = callback.WaitForResult(); | |
| 242 if (rv != PP_OK) | |
| 243 return ReportError("FileIO::Open", rv); | |
| 244 | |
| 245 std::string postdata("postdatapostdata"); | |
| 246 rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, postdata); | |
| 247 if (rv != PP_OK) | |
| 248 return ReportError("FileIO::Write", rv); | |
| 249 | |
| 250 pp::URLRequestInfo request(instance_); | |
| 251 request.SetURL("/echo"); | |
| 252 request.SetMethod("POST"); | |
| 253 request.AppendFileRangeToBody(file_ref, 4, 12, 0); | |
| 254 return LoadAndCompareBody(request, postdata.substr(4, 12)); | |
| 255 } | |
| 256 | |
| 155 std::string TestURLLoader::TestCompoundBodyPOST() { | 257 std::string TestURLLoader::TestCompoundBodyPOST() { |
| 156 pp::URLRequestInfo request(instance_); | 258 pp::URLRequestInfo request(instance_); |
| 157 request.SetURL("/echo"); | 259 request.SetURL("/echo"); |
| 158 request.SetMethod("POST"); | 260 request.SetMethod("POST"); |
| 159 std::string postdata1("post"); | 261 std::string postdata1("post"); |
| 160 request.AppendDataToBody(postdata1.data(), postdata1.length()); | 262 request.AppendDataToBody(postdata1.data(), postdata1.length()); |
| 161 std::string postdata2("data"); | 263 std::string postdata2("data"); |
| 162 request.AppendDataToBody(postdata2.data(), postdata2.length()); | 264 request.AppendDataToBody(postdata2.data(), postdata2.length()); |
| 163 return LoadAndCompareBody(request, postdata1 + postdata2); | 265 return LoadAndCompareBody(request, postdata1 + postdata2); |
| 164 } | 266 } |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 481 return error; | 583 return error; |
| 482 if (body != "hello\n") | 584 if (body != "hello\n") |
| 483 return ReportError("Couldn't read data", rv); | 585 return ReportError("Couldn't read data", rv); |
| 484 | 586 |
| 485 PASS(); | 587 PASS(); |
| 486 } | 588 } |
| 487 | 589 |
| 488 // TODO(viettrungluu): Add tests for FollowRedirect, | 590 // TODO(viettrungluu): Add tests for FollowRedirect, |
| 489 // Get{Upload,Download}Progress, Close (including abort tests if applicable). | 591 // Get{Upload,Download}Progress, Close (including abort tests if applicable). |
| 490 // TODO(darin): Add a test for GrantUniversalAccess. | 592 // TODO(darin): Add a test for GrantUniversalAccess. |
| OLD | NEW |