OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 15 matching lines...) Expand all Loading... |
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 { | 31 namespace { |
32 | 32 |
33 int32_t WriteEntireBuffer(PP_Instance instance, | 33 int32_t WriteEntireBuffer(PP_Instance instance, |
34 pp::FileIO* file_io, | 34 pp::FileIO* file_io, |
35 int32_t offset, | 35 int32_t offset, |
36 const std::string& data) { | 36 const std::string& data, |
37 TestCompletionCallback callback(instance); | 37 CallbackType callback_type) { |
| 38 TestCompletionCallback callback(instance, callback_type); |
38 int32_t write_offset = offset; | 39 int32_t write_offset = offset; |
39 const char* buf = data.c_str(); | 40 const char* buf = data.c_str(); |
40 int32_t size = data.size(); | 41 int32_t size = data.size(); |
41 | 42 |
42 while (write_offset < offset + size) { | 43 while (write_offset < offset + size) { |
43 callback.WaitForResult(file_io->Write(write_offset, | 44 callback.WaitForResult(file_io->Write(write_offset, |
44 &buf[write_offset - offset], | 45 &buf[write_offset - offset], |
45 size - write_offset + offset, | 46 size - write_offset + offset, |
46 callback)); | 47 callback)); |
47 if (callback.result() < 0) | 48 if (callback.result() < 0) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 if (file_io_trusted_interface_) | 89 if (file_io_trusted_interface_) |
89 instance_->AppendError("FileIOTrusted interface is supported by NaCl"); | 90 instance_->AppendError("FileIOTrusted interface is supported by NaCl"); |
90 if (url_loader_trusted_interface_) | 91 if (url_loader_trusted_interface_) |
91 instance_->AppendError("URLLoaderTrusted interface is supported by NaCl"); | 92 instance_->AppendError("URLLoaderTrusted interface is supported by NaCl"); |
92 #endif | 93 #endif |
93 } | 94 } |
94 return EnsureRunningOverHTTP(); | 95 return EnsureRunningOverHTTP(); |
95 } | 96 } |
96 | 97 |
97 void TestURLLoader::RunTests(const std::string& filter) { | 98 void TestURLLoader::RunTests(const std::string& filter) { |
98 RUN_TEST_FORCEASYNC_AND_NOT(BasicGET, filter); | 99 RUN_CALLBACK_TEST(TestURLLoader, BasicGET, filter); |
99 RUN_TEST_FORCEASYNC_AND_NOT(BasicPOST, filter); | 100 RUN_CALLBACK_TEST(TestURLLoader, BasicPOST, filter); |
100 RUN_TEST_FORCEASYNC_AND_NOT(BasicFilePOST, filter); | 101 RUN_CALLBACK_TEST(TestURLLoader, BasicFilePOST, filter); |
101 RUN_TEST_FORCEASYNC_AND_NOT(BasicFileRangePOST, filter); | 102 RUN_CALLBACK_TEST(TestURLLoader, BasicFileRangePOST, filter); |
102 RUN_TEST_FORCEASYNC_AND_NOT(CompoundBodyPOST, filter); | 103 RUN_CALLBACK_TEST(TestURLLoader, CompoundBodyPOST, filter); |
103 RUN_TEST_FORCEASYNC_AND_NOT(EmptyDataPOST, filter); | 104 RUN_CALLBACK_TEST(TestURLLoader, EmptyDataPOST, filter); |
104 RUN_TEST_FORCEASYNC_AND_NOT(BinaryDataPOST, filter); | 105 RUN_CALLBACK_TEST(TestURLLoader, BinaryDataPOST, filter); |
105 RUN_TEST_FORCEASYNC_AND_NOT(CustomRequestHeader, filter); | 106 RUN_CALLBACK_TEST(TestURLLoader, CustomRequestHeader, filter); |
106 RUN_TEST_FORCEASYNC_AND_NOT(FailsBogusContentLength, filter); | 107 RUN_CALLBACK_TEST(TestURLLoader, FailsBogusContentLength, filter); |
107 RUN_TEST_FORCEASYNC_AND_NOT(StreamToFile, filter); | 108 RUN_CALLBACK_TEST(TestURLLoader, StreamToFile, filter); |
108 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedSameOriginRestriction, filter); | 109 RUN_CALLBACK_TEST(TestURLLoader, UntrustedSameOriginRestriction, filter); |
109 RUN_TEST_FORCEASYNC_AND_NOT(TrustedSameOriginRestriction, filter); | 110 RUN_CALLBACK_TEST(TestURLLoader, TrustedSameOriginRestriction, filter); |
110 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedCrossOriginRequest, filter); | 111 RUN_CALLBACK_TEST(TestURLLoader, UntrustedCrossOriginRequest, filter); |
111 RUN_TEST_FORCEASYNC_AND_NOT(TrustedCrossOriginRequest, filter); | 112 RUN_CALLBACK_TEST(TestURLLoader, TrustedCrossOriginRequest, filter); |
112 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedJavascriptURLRestriction, filter); | 113 RUN_CALLBACK_TEST(TestURLLoader, UntrustedJavascriptURLRestriction, filter); |
113 RUN_TEST_FORCEASYNC_AND_NOT(TrustedJavascriptURLRestriction, filter); | 114 RUN_CALLBACK_TEST(TestURLLoader, TrustedJavascriptURLRestriction, filter); |
114 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedHttpRequests, filter); | 115 RUN_CALLBACK_TEST(TestURLLoader, UntrustedHttpRequests, filter); |
115 RUN_TEST_FORCEASYNC_AND_NOT(TrustedHttpRequests, filter); | 116 RUN_CALLBACK_TEST(TestURLLoader, TrustedHttpRequests, filter); |
116 RUN_TEST_FORCEASYNC_AND_NOT(FollowURLRedirect, filter); | 117 RUN_CALLBACK_TEST(TestURLLoader, FollowURLRedirect, filter); |
117 RUN_TEST_FORCEASYNC_AND_NOT(AuditURLRedirect, filter); | 118 RUN_CALLBACK_TEST(TestURLLoader, AuditURLRedirect, filter); |
118 RUN_TEST_FORCEASYNC_AND_NOT(AbortCalls, filter); | 119 RUN_CALLBACK_TEST(TestURLLoader, AbortCalls, filter); |
119 RUN_TEST_FORCEASYNC_AND_NOT(UntendedLoad, filter); | 120 RUN_CALLBACK_TEST(TestURLLoader, UntendedLoad, filter); |
120 RUN_TEST_FORCEASYNC_AND_NOT(PrefetchBufferThreshold, filter); | 121 RUN_CALLBACK_TEST(TestURLLoader, PrefetchBufferThreshold, filter); |
121 } | 122 } |
122 | 123 |
123 std::string TestURLLoader::ReadEntireFile(pp::FileIO* file_io, | 124 std::string TestURLLoader::ReadEntireFile(pp::FileIO* file_io, |
124 std::string* data) { | 125 std::string* data) { |
125 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); | 126 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
126 char buf[256]; | 127 char buf[256]; |
127 int64_t offset = 0; | 128 int64_t offset = 0; |
128 | 129 |
129 for (;;) { | 130 for (;;) { |
130 callback.WaitForResult(file_io->Read(offset, buf, sizeof(buf), callback)); | 131 callback.WaitForResult(file_io->Read(offset, buf, sizeof(buf), callback)); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 callback)); | 216 callback)); |
216 if (callback.failed()) { | 217 if (callback.failed()) { |
217 message->assign(callback.errors()); | 218 message->assign(callback.errors()); |
218 return callback.result(); | 219 return callback.result(); |
219 } | 220 } |
220 if (callback.result() != PP_OK) { | 221 if (callback.result() != PP_OK) { |
221 message->assign("FileIO::Open failed."); | 222 message->assign("FileIO::Open failed."); |
222 return callback.result(); | 223 return callback.result(); |
223 } | 224 } |
224 | 225 |
225 int32_t rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, data); | 226 int32_t rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, data, |
| 227 callback_type()); |
226 if (rv != PP_OK) { | 228 if (rv != PP_OK) { |
227 message->assign("FileIO::Write failed."); | 229 message->assign("FileIO::Write failed."); |
228 return rv; | 230 return rv; |
229 } | 231 } |
230 | 232 |
231 return rv; | 233 return rv; |
232 } | 234 } |
233 | 235 |
234 std::string TestURLLoader::GetReachableAbsoluteURL( | 236 std::string TestURLLoader::GetReachableAbsoluteURL( |
235 const std::string& file_name) { | 237 const std::string& file_name) { |
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 // another callback function, to make sure the loader works with no callback. | 782 // another callback function, to make sure the loader works with no callback. |
781 int64_t bytes_received = 0; | 783 int64_t bytes_received = 0; |
782 int64_t total_bytes_to_be_received = 0; | 784 int64_t total_bytes_to_be_received = 0; |
783 while (true) { | 785 while (true) { |
784 loader.GetDownloadProgress(&bytes_received, &total_bytes_to_be_received); | 786 loader.GetDownloadProgress(&bytes_received, &total_bytes_to_be_received); |
785 if (total_bytes_to_be_received <= 0) | 787 if (total_bytes_to_be_received <= 0) |
786 return ReportError("URLLoader::GetDownloadProgress total size", | 788 return ReportError("URLLoader::GetDownloadProgress total size", |
787 total_bytes_to_be_received); | 789 total_bytes_to_be_received); |
788 if (bytes_received == total_bytes_to_be_received) | 790 if (bytes_received == total_bytes_to_be_received) |
789 break; | 791 break; |
790 pp::Module::Get()->core()->CallOnMainThread(10, callback); | 792 // TODO(dmichael): This should probably compare pp::MessageLoop::GetCurrent |
791 callback.WaitForResult(); | 793 // with GetForMainThread. We only need to yield on the main |
| 794 // thread. |
| 795 if (callback_type() != PP_BLOCKING) { |
| 796 pp::Module::Get()->core()->CallOnMainThread(10, callback); |
| 797 callback.WaitForResult(); |
| 798 } |
792 } | 799 } |
793 | 800 |
794 // The loader should now have the data and have finished successfully. | 801 // The loader should now have the data and have finished successfully. |
795 std::string body; | 802 std::string body; |
796 std::string error = ReadEntireResponseBody(&loader, &body); | 803 std::string error = ReadEntireResponseBody(&loader, &body); |
797 if (!error.empty()) | 804 if (!error.empty()) |
798 return error; | 805 return error; |
799 if (body != "hello\n") | 806 if (body != "hello\n") |
800 return ReportError("Couldn't read data", callback.result()); | 807 return ReportError("Couldn't read data", callback.result()); |
801 | 808 |
(...skipping 27 matching lines...) Expand all Loading... |
829 if (rv != PP_ERROR_FAILED) { | 836 if (rv != PP_ERROR_FAILED) { |
830 return ReportError("The lower buffer value was higher than the upper but " | 837 return ReportError("The lower buffer value was higher than the upper but " |
831 "the URLLoader did not fail.", rv); | 838 "the URLLoader did not fail.", rv); |
832 } | 839 } |
833 | 840 |
834 PASS(); | 841 PASS(); |
835 } | 842 } |
836 | 843 |
837 // TODO(viettrungluu): Add tests for Get{Upload,Download}Progress, Close | 844 // TODO(viettrungluu): Add tests for Get{Upload,Download}Progress, Close |
838 // (including abort tests if applicable). | 845 // (including abort tests if applicable). |
OLD | NEW |