Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(503)

Side by Side Diff: ppapi/tests/test_url_loader.cc

Issue 11417145: Provide a safer URLLoader ReadResponseBody API (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ppapi/tests/test_url_loader.h ('k') | ppapi/thunk/interfaces_ppb_public_stable.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 instance_->AppendError("FileIOTrusted interface is supported by NaCl"); 90 instance_->AppendError("FileIOTrusted interface is supported by NaCl");
91 if (url_loader_trusted_interface_) 91 if (url_loader_trusted_interface_)
92 instance_->AppendError("URLLoaderTrusted interface is supported by NaCl"); 92 instance_->AppendError("URLLoaderTrusted interface is supported by NaCl");
93 #endif 93 #endif
94 } 94 }
95 return EnsureRunningOverHTTP(); 95 return EnsureRunningOverHTTP();
96 } 96 }
97 97
98 void TestURLLoader::RunTests(const std::string& filter) { 98 void TestURLLoader::RunTests(const std::string& filter) {
99 RUN_CALLBACK_TEST(TestURLLoader, BasicGET, filter); 99 RUN_CALLBACK_TEST(TestURLLoader, BasicGET, filter);
100 RUN_CALLBACK_TEST(TestURLLoader, BasicGET_ToArray, filter);
100 RUN_CALLBACK_TEST(TestURLLoader, BasicPOST, filter); 101 RUN_CALLBACK_TEST(TestURLLoader, BasicPOST, filter);
101 RUN_CALLBACK_TEST(TestURLLoader, BasicFilePOST, filter); 102 RUN_CALLBACK_TEST(TestURLLoader, BasicFilePOST, filter);
102 RUN_CALLBACK_TEST(TestURLLoader, BasicFileRangePOST, filter); 103 RUN_CALLBACK_TEST(TestURLLoader, BasicFileRangePOST, filter);
103 RUN_CALLBACK_TEST(TestURLLoader, CompoundBodyPOST, filter); 104 RUN_CALLBACK_TEST(TestURLLoader, CompoundBodyPOST, filter);
104 RUN_CALLBACK_TEST(TestURLLoader, EmptyDataPOST, filter); 105 RUN_CALLBACK_TEST(TestURLLoader, EmptyDataPOST, filter);
105 RUN_CALLBACK_TEST(TestURLLoader, BinaryDataPOST, filter); 106 RUN_CALLBACK_TEST(TestURLLoader, BinaryDataPOST, filter);
106 RUN_CALLBACK_TEST(TestURLLoader, CustomRequestHeader, filter); 107 RUN_CALLBACK_TEST(TestURLLoader, CustomRequestHeader, filter);
107 RUN_CALLBACK_TEST(TestURLLoader, FailsBogusContentLength, filter); 108 RUN_CALLBACK_TEST(TestURLLoader, FailsBogusContentLength, filter);
108 RUN_CALLBACK_TEST(TestURLLoader, StreamToFile, filter); 109 RUN_CALLBACK_TEST(TestURLLoader, StreamToFile, filter);
109 RUN_CALLBACK_TEST(TestURLLoader, UntrustedSameOriginRestriction, filter); 110 RUN_CALLBACK_TEST(TestURLLoader, UntrustedSameOriginRestriction, filter);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 if (callback.result() < 0) 152 if (callback.result() < 0)
152 return ReportError("URLLoader::ReadResponseBody", callback.result()); 153 return ReportError("URLLoader::ReadResponseBody", callback.result());
153 if (callback.result() == 0) 154 if (callback.result() == 0)
154 break; 155 break;
155 body->append(buf, callback.result()); 156 body->append(buf, callback.result());
156 } 157 }
157 158
158 PASS(); 159 PASS();
159 } 160 }
160 161
162 std::string TestURLLoader::ReadEntireResponseBodyToArray(pp::URLLoader* loader,
163 std::string* body) {
164 TestCompletionCallbackWithOutput< std::vector<char> > callback(
165 instance_->pp_instance(), callback_type());
166
167 for (;;) {
168 // Read just a little bit so that multiple reads are needed.
169 callback.WaitForResult(loader->ReadResponseBody(2, callback));
170 int32_t rv = callback.result();
171 if (rv < 0)
172 return ReportError("URLLoader::ReadResponseBody", rv);
173 if (rv == 0)
174 break;
175 assert(rv == static_cast<int32_t>(callback.output().size()));
176 body->append(callback.output().begin(), callback.output().end());
177 }
178
179 PASS();
180 }
181
161 std::string TestURLLoader::LoadAndCompareBody( 182 std::string TestURLLoader::LoadAndCompareBody(
162 const pp::URLRequestInfo& request, 183 const pp::URLRequestInfo& request,
163 const std::string& expected_body) { 184 const std::string& expected_body) {
164 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); 185 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
165 186
166 pp::URLLoader loader(instance_); 187 pp::URLLoader loader(instance_);
167 callback.WaitForResult(loader.Open(request, callback)); 188 callback.WaitForResult(loader.Open(request, callback));
168 CHECK_CALLBACK_BEHAVIOR(callback); 189 CHECK_CALLBACK_BEHAVIOR(callback);
169 ASSERT_EQ(PP_OK, callback.result()); 190 ASSERT_EQ(PP_OK, callback.result());
170 191
(...skipping 10 matching lines...) Expand all
181 return error; 202 return error;
182 203
183 if (body.size() != expected_body.size()) 204 if (body.size() != expected_body.size())
184 return "URLLoader::ReadResponseBody returned unexpected content length"; 205 return "URLLoader::ReadResponseBody returned unexpected content length";
185 if (body != expected_body) 206 if (body != expected_body)
186 return "URLLoader::ReadResponseBody returned unexpected content"; 207 return "URLLoader::ReadResponseBody returned unexpected content";
187 208
188 PASS(); 209 PASS();
189 } 210 }
190 211
212 // Exact the same test of LoadAndCompareBody, but for ReadResponseBodyToArray.
213 std::string TestURLLoader::LoadAndCompareBody_ToArray(
214 const pp::URLRequestInfo& request,
215 const std::string& expected_body) {
216 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
217
218 pp::URLLoader loader(instance_);
219 callback.WaitForResult(loader.Open(request, callback));
220 CHECK_CALLBACK_BEHAVIOR(callback);
221 ASSERT_EQ(PP_OK, callback.result());
222
223 pp::URLResponseInfo response_info(loader.GetResponseInfo());
224 if (response_info.is_null())
225 return "URLLoader::GetResponseInfo returned null";
226 int32_t status_code = response_info.GetStatusCode();
227 if (status_code != 200)
228 return "Unexpected HTTP status code";
229
230 std::string body;
231 std::string error = ReadEntireResponseBodyToArray(&loader, &body);
232 if (!error.empty())
233 return error;
234
235 if (body.size() != expected_body.size())
236 return "URLLoader::ReadResponseBodyToArray returned unexpected content "
237 "length";
238 if (body != expected_body)
239 return "URLLoader::ReadResponseBodyToArray returned unexpected content";
240
241 PASS();
242 }
243
191 int32_t TestURLLoader::OpenFileSystem(pp::FileSystem* file_system, 244 int32_t TestURLLoader::OpenFileSystem(pp::FileSystem* file_system,
192 std::string* message) { 245 std::string* message) {
193 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); 246 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
194 callback.WaitForResult(file_system->Open(1024, callback)); 247 callback.WaitForResult(file_system->Open(1024, callback));
195 if (callback.failed()) { 248 if (callback.failed()) {
196 message->assign(callback.errors()); 249 message->assign(callback.errors());
197 return callback.result(); 250 return callback.result();
198 } 251 }
199 if (callback.result() != PP_OK) { 252 if (callback.result() != PP_OK) {
200 message->assign("FileSystem::Open"); 253 message->assign("FileSystem::Open");
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 callback.WaitForResult(loader.Open(request, callback)); 353 callback.WaitForResult(loader.Open(request, callback));
301 return callback.result(); 354 return callback.result();
302 } 355 }
303 356
304 std::string TestURLLoader::TestBasicGET() { 357 std::string TestURLLoader::TestBasicGET() {
305 pp::URLRequestInfo request(instance_); 358 pp::URLRequestInfo request(instance_);
306 request.SetURL("test_url_loader_data/hello.txt"); 359 request.SetURL("test_url_loader_data/hello.txt");
307 return LoadAndCompareBody(request, "hello\n"); 360 return LoadAndCompareBody(request, "hello\n");
308 } 361 }
309 362
363 // A variance of BasicGET but use ReadResponseBodyToArray internally.
364 std::string TestURLLoader::TestBasicGET_ToArray() {
365 pp::URLRequestInfo request(instance_);
366 request.SetURL("test_url_loader_data/hello.txt");
367 return LoadAndCompareBody_ToArray(request, "hello\n");
368 }
369
310 std::string TestURLLoader::TestBasicPOST() { 370 std::string TestURLLoader::TestBasicPOST() {
311 pp::URLRequestInfo request(instance_); 371 pp::URLRequestInfo request(instance_);
312 request.SetURL("/echo"); 372 request.SetURL("/echo");
313 request.SetMethod("POST"); 373 request.SetMethod("POST");
314 std::string postdata("postdata"); 374 std::string postdata("postdata");
315 request.AppendDataToBody(postdata.data(), postdata.length()); 375 request.AppendDataToBody(postdata.data(), postdata.length());
316 return LoadAndCompareBody(request, postdata); 376 return LoadAndCompareBody(request, postdata);
317 } 377 }
318 378
319 std::string TestURLLoader::TestBasicFilePOST() { 379 std::string TestURLLoader::TestBasicFilePOST() {
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 if (rv != PP_ERROR_FAILED) { 896 if (rv != PP_ERROR_FAILED) {
837 return ReportError("The lower buffer value was higher than the upper but " 897 return ReportError("The lower buffer value was higher than the upper but "
838 "the URLLoader did not fail.", rv); 898 "the URLLoader did not fail.", rv);
839 } 899 }
840 900
841 PASS(); 901 PASS();
842 } 902 }
843 903
844 // TODO(viettrungluu): Add tests for Get{Upload,Download}Progress, Close 904 // TODO(viettrungluu): Add tests for Get{Upload,Download}Progress, Close
845 // (including abort tests if applicable). 905 // (including abort tests if applicable).
OLDNEW
« no previous file with comments | « ppapi/tests/test_url_loader.h ('k') | ppapi/thunk/interfaces_ppb_public_stable.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698