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

Side by Side Diff: net/url_request/url_request_file_job_unittest.cc

Issue 2776873002: Modify tests to ensure URLRequestJob::DoneReading is always called. (Closed)
Patch Set: Ready for review. Created 3 years, 8 months 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "net/url_request/url_request_file_job.h" 5 #include "net/url_request/url_request_file_job.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 19 matching lines...) Expand all
30 public: 30 public:
31 // |seek_position| will be set to the value passed in to OnSeekComplete. 31 // |seek_position| will be set to the value passed in to OnSeekComplete.
32 // |observed_content| will be set to the concatenated data from all calls to 32 // |observed_content| will be set to the concatenated data from all calls to
33 // OnReadComplete. 33 // OnReadComplete.
34 TestURLRequestFileJob(URLRequest* request, 34 TestURLRequestFileJob(URLRequest* request,
35 NetworkDelegate* network_delegate, 35 NetworkDelegate* network_delegate,
36 const base::FilePath& file_path, 36 const base::FilePath& file_path,
37 const scoped_refptr<base::TaskRunner>& file_task_runner, 37 const scoped_refptr<base::TaskRunner>& file_task_runner,
38 int* open_result, 38 int* open_result,
39 int64_t* seek_position, 39 int64_t* seek_position,
40 bool* done_reading,
40 std::string* observed_content) 41 std::string* observed_content)
41 : URLRequestFileJob(request, 42 : URLRequestFileJob(request,
42 network_delegate, 43 network_delegate,
43 file_path, 44 file_path,
44 file_task_runner), 45 file_task_runner),
45 open_result_(open_result), 46 open_result_(open_result),
46 seek_position_(seek_position), 47 seek_position_(seek_position),
48 done_reading_(done_reading),
47 observed_content_(observed_content) { 49 observed_content_(observed_content) {
48 *open_result_ = ERR_IO_PENDING; 50 *open_result_ = ERR_IO_PENDING;
49 *seek_position_ = ERR_IO_PENDING; 51 *seek_position_ = ERR_IO_PENDING;
52 *done_reading_ = false;
50 observed_content_->clear(); 53 observed_content_->clear();
51 } 54 }
52 55
53 ~TestURLRequestFileJob() override {} 56 ~TestURLRequestFileJob() override {}
54 57
55 protected: 58 protected:
56 void OnOpenComplete(int result) override { 59 void OnOpenComplete(int result) override {
57 // Should only be called once. 60 // Should only be called once.
58 ASSERT_EQ(ERR_IO_PENDING, *open_result_); 61 ASSERT_EQ(ERR_IO_PENDING, *open_result_);
59 *open_result_ = result; 62 *open_result_ = result;
60 } 63 }
61 64
62 void OnSeekComplete(int64_t result) override { 65 void OnSeekComplete(int64_t result) override {
63 // Should only call this if open succeeded. 66 // Should only call this if open succeeded.
64 EXPECT_EQ(OK, *open_result_); 67 EXPECT_EQ(OK, *open_result_);
65 // Should only be called once. 68 // Should only be called once.
66 ASSERT_EQ(ERR_IO_PENDING, *seek_position_); 69 ASSERT_EQ(ERR_IO_PENDING, *seek_position_);
67 *seek_position_ = result; 70 *seek_position_ = result;
68 } 71 }
69 72
70 void OnReadComplete(IOBuffer* buf, int result) override { 73 void OnReadComplete(IOBuffer* buf, int result) override {
71 // Should only call this if seek succeeded. 74 // Should only call this if seek succeeded.
72 EXPECT_GE(*seek_position_, 0); 75 EXPECT_GE(*seek_position_, 0);
73 observed_content_->append(std::string(buf->data(), result)); 76 observed_content_->append(std::string(buf->data(), result));
74 } 77 }
75 78
79 void DoneReading() override { *done_reading_ = true; }
80
76 int* const open_result_; 81 int* const open_result_;
77 int64_t* const seek_position_; 82 int64_t* const seek_position_;
83 bool* done_reading_;
78 std::string* const observed_content_; 84 std::string* const observed_content_;
79 }; 85 };
80 86
81 // A URLRequestJobFactory that will return TestURLRequestFileJob instances for 87 // A URLRequestJobFactory that will return TestURLRequestFileJob instances for
82 // file:// scheme URLs. Can only be used to create a single job. 88 // file:// scheme URLs. Can only be used to create a single job.
83 class TestJobFactory : public URLRequestJobFactory { 89 class TestJobFactory : public URLRequestJobFactory {
84 public: 90 public:
85 TestJobFactory(const base::FilePath& path, 91 TestJobFactory(const base::FilePath& path,
86 int* open_result, 92 int* open_result,
87 int64_t* seek_position, 93 int64_t* seek_position,
94 bool* done_reading,
88 std::string* observed_content) 95 std::string* observed_content)
89 : path_(path), 96 : path_(path),
90 open_result_(open_result), 97 open_result_(open_result),
91 seek_position_(seek_position), 98 seek_position_(seek_position),
99 done_reading_(done_reading),
92 observed_content_(observed_content) { 100 observed_content_(observed_content) {
93 CHECK(open_result_); 101 CHECK(open_result_);
94 CHECK(seek_position_); 102 CHECK(seek_position_);
103 CHECK(done_reading_);
95 CHECK(observed_content_); 104 CHECK(observed_content_);
96 } 105 }
97 106
98 ~TestJobFactory() override {} 107 ~TestJobFactory() override {}
99 108
100 URLRequestJob* MaybeCreateJobWithProtocolHandler( 109 URLRequestJob* MaybeCreateJobWithProtocolHandler(
101 const std::string& scheme, 110 const std::string& scheme,
102 URLRequest* request, 111 URLRequest* request,
103 NetworkDelegate* network_delegate) const override { 112 NetworkDelegate* network_delegate) const override {
104 CHECK(open_result_); 113 CHECK(open_result_);
105 CHECK(seek_position_); 114 CHECK(seek_position_);
115 CHECK(done_reading_);
106 CHECK(observed_content_); 116 CHECK(observed_content_);
107 URLRequestJob* job = new TestURLRequestFileJob( 117 URLRequestJob* job = new TestURLRequestFileJob(
108 request, network_delegate, path_, base::ThreadTaskRunnerHandle::Get(), 118 request, network_delegate, path_, base::ThreadTaskRunnerHandle::Get(),
109 open_result_, seek_position_, observed_content_); 119 open_result_, seek_position_, done_reading_, observed_content_);
110 open_result_ = nullptr; 120 open_result_ = nullptr;
111 seek_position_ = nullptr; 121 seek_position_ = nullptr;
122 done_reading_ = nullptr;
112 observed_content_ = nullptr; 123 observed_content_ = nullptr;
113 return job; 124 return job;
114 } 125 }
115 126
116 URLRequestJob* MaybeInterceptRedirect(URLRequest* request, 127 URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
117 NetworkDelegate* network_delegate, 128 NetworkDelegate* network_delegate,
118 const GURL& location) const override { 129 const GURL& location) const override {
119 return nullptr; 130 return nullptr;
120 } 131 }
121 132
(...skipping 14 matching lines...) Expand all
136 bool IsSafeRedirectTarget(const GURL& location) const override { 147 bool IsSafeRedirectTarget(const GURL& location) const override {
137 return false; 148 return false;
138 } 149 }
139 150
140 private: 151 private:
141 const base::FilePath path_; 152 const base::FilePath path_;
142 153
143 // These are mutable because MaybeCreateJobWithProtocolHandler is const. 154 // These are mutable because MaybeCreateJobWithProtocolHandler is const.
144 mutable int* open_result_; 155 mutable int* open_result_;
145 mutable int64_t* seek_position_; 156 mutable int64_t* seek_position_;
157 mutable bool* done_reading_;
146 mutable std::string* observed_content_; 158 mutable std::string* observed_content_;
147 }; 159 };
148 160
149 // Helper function to create a file at |path| filled with |content|. 161 // Helper function to create a file at |path| filled with |content|.
150 // Returns true on success. 162 // Returns true on success.
151 bool CreateFileWithContent(const std::string& content, 163 bool CreateFileWithContent(const std::string& content,
152 const base::FilePath& path) { 164 const base::FilePath& path) {
153 return base::WriteFile(path, content.c_str(), content.length()); 165 return base::WriteFile(path, content.c_str(), content.length()) != -1;
154 } 166 }
155 167
156 // A simple holder for start/end used in http range requests. 168 // A simple holder for start/end used in http range requests.
157 struct Range { 169 struct Range {
158 int start; 170 int start;
159 int end; 171 int end;
160 172
161 Range() { 173 Range() {
162 start = 0; 174 start = 0;
163 end = 0; 175 end = 0;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 const base::FilePath::StringPieceType& file_extension, 208 const base::FilePath::StringPieceType& file_extension,
197 const Range* range); 209 const Range* range);
198 210
199 // Creates and runs a TestURLRequestFileJob job to read from file provided by 211 // Creates and runs a TestURLRequestFileJob job to read from file provided by
200 // |path|. If |range| value is provided, it will be passed in the range 212 // |path|. If |range| value is provided, it will be passed in the range
201 // header. 213 // header.
202 void RunRequestWithPath(const base::FilePath& path, 214 void RunRequestWithPath(const base::FilePath& path,
203 const std::string& range, 215 const std::string& range,
204 int* open_result, 216 int* open_result,
205 int64_t* seek_position, 217 int64_t* seek_position,
218 bool* done_reading,
206 std::string* observed_content); 219 std::string* observed_content);
207 220
208 TestURLRequestContext context_; 221 TestURLRequestContext context_;
209 TestDelegate delegate_; 222 TestDelegate delegate_;
210 }; 223 };
211 224
212 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {} 225 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {}
213 226
214 void URLRequestFileJobEventsTest::TearDown() { 227 void URLRequestFileJobEventsTest::TearDown() {
215 // Gives a chance to close the opening file. 228 // Gives a chance to close the opening file.
(...skipping 24 matching lines...) Expand all
240 ASSERT_GE(range->start, 0); 253 ASSERT_GE(range->start, 0);
241 ASSERT_GE(range->end, 0); 254 ASSERT_GE(range->end, 0);
242 ASSERT_LE(range->start, range->end); 255 ASSERT_LE(range->start, range->end);
243 ASSERT_LT(static_cast<unsigned int>(range->end), expected_content.length()); 256 ASSERT_LT(static_cast<unsigned int>(range->end), expected_content.length());
244 range_value = base::StringPrintf("bytes=%d-%d", range->start, range->end); 257 range_value = base::StringPrintf("bytes=%d-%d", range->start, range->end);
245 } 258 }
246 259
247 { 260 {
248 int open_result; 261 int open_result;
249 int64_t seek_position; 262 int64_t seek_position;
263 bool done_reading;
250 std::string observed_content; 264 std::string observed_content;
251 RunRequestWithPath(path, range_value, &open_result, &seek_position, 265 RunRequestWithPath(path, range_value, &open_result, &seek_position,
252 &observed_content); 266 &done_reading, &observed_content);
253 267
254 EXPECT_EQ(OK, open_result); 268 EXPECT_EQ(OK, open_result);
255 EXPECT_FALSE(delegate_.request_failed()); 269 EXPECT_FALSE(delegate_.request_failed());
256 int expected_length = 270 int expected_length =
257 range ? (range->end - range->start + 1) : expected_content.length(); 271 range ? (range->end - range->start + 1) : expected_content.length();
258 EXPECT_EQ(delegate_.bytes_received(), expected_length); 272 EXPECT_EQ(delegate_.bytes_received(), expected_length);
259 273
260 std::string expected_data_received; 274 std::string expected_data_received;
261 if (range) { 275 if (range) {
262 expected_data_received.insert(0, expected_content, range->start, 276 expected_data_received.insert(0, expected_content, range->start,
263 expected_length); 277 expected_length);
264 EXPECT_EQ(expected_data_received, observed_content); 278 EXPECT_EQ(expected_data_received, observed_content);
265 } else { 279 } else {
266 expected_data_received = expected_content; 280 expected_data_received = expected_content;
267 EXPECT_EQ(raw_content, observed_content); 281 EXPECT_EQ(raw_content, observed_content);
268 } 282 }
269 283
270 EXPECT_EQ(expected_data_received, delegate_.data_received()); 284 EXPECT_EQ(expected_data_received, delegate_.data_received());
271 EXPECT_EQ(seek_position, range ? range->start : 0); 285 EXPECT_EQ(seek_position, range ? range->start : 0);
286 EXPECT_TRUE(done_reading);
272 } 287 }
273 } 288 }
274 289
275 void URLRequestFileJobEventsTest::RunRequestWithPath( 290 void URLRequestFileJobEventsTest::RunRequestWithPath(
276 const base::FilePath& path, 291 const base::FilePath& path,
277 const std::string& range, 292 const std::string& range,
278 int* open_result, 293 int* open_result,
279 int64_t* seek_position, 294 int64_t* seek_position,
295 bool* done_reading,
280 std::string* observed_content) { 296 std::string* observed_content) {
281 TestJobFactory factory(path, open_result, seek_position, observed_content); 297 TestJobFactory factory(path, open_result, seek_position, done_reading,
298 observed_content);
282 context_.set_job_factory(&factory); 299 context_.set_job_factory(&factory);
283 300
284 std::unique_ptr<URLRequest> request(context_.CreateRequest( 301 std::unique_ptr<URLRequest> request(context_.CreateRequest(
285 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_)); 302 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_));
286 if (!range.empty()) { 303 if (!range.empty()) {
287 request->SetExtraRequestHeaderByName(HttpRequestHeaders::kRange, range, 304 request->SetExtraRequestHeaderByName(HttpRequestHeaders::kRange, range,
288 true /*overwrite*/); 305 true /*overwrite*/);
289 } 306 }
290 request->Start(); 307 request->Start();
291 308
292 base::RunLoop().Run(); 309 base::RunLoop().Run();
293 } 310 }
294 311
295 // Helper function to make a character array filled with |size| bytes of 312 // Helper function to make a character array filled with |size| bytes of
296 // test content. 313 // test content.
297 std::string MakeContentOfSize(int size) { 314 std::string MakeContentOfSize(int size) {
298 EXPECT_GE(size, 0); 315 EXPECT_GE(size, 0);
299 std::string result; 316 std::string result;
300 result.reserve(size); 317 result.reserve(size);
301 for (int i = 0; i < size; i++) { 318 for (int i = 0; i < size; i++) {
302 result.append(1, static_cast<char>(i % 256)); 319 result.append(1, static_cast<char>(i % 256));
303 } 320 }
304 return result; 321 return result;
305 } 322 }
306 323
324 TEST_F(URLRequestFileJobEventsTest, ZeroByteFile) {
325 RunSuccessfulRequestWithString(std::string(""), nullptr);
326 }
327
307 TEST_F(URLRequestFileJobEventsTest, TinyFile) { 328 TEST_F(URLRequestFileJobEventsTest, TinyFile) {
308 RunSuccessfulRequestWithString(std::string("hello world"), NULL); 329 RunSuccessfulRequestWithString(std::string("hello world"), NULL);
309 } 330 }
310 331
311 TEST_F(URLRequestFileJobEventsTest, SmallFile) { 332 TEST_F(URLRequestFileJobEventsTest, SmallFile) {
312 RunSuccessfulRequestWithString(MakeContentOfSize(17 * 1024), NULL); 333 RunSuccessfulRequestWithString(MakeContentOfSize(17 * 1024), NULL);
313 } 334 }
314 335
315 TEST_F(URLRequestFileJobEventsTest, BigFile) { 336 TEST_F(URLRequestFileJobEventsTest, BigFile) {
316 RunSuccessfulRequestWithString(MakeContentOfSize(3 * 1024 * 1024), NULL); 337 RunSuccessfulRequestWithString(MakeContentOfSize(3 * 1024 * 1024), NULL);
(...skipping 21 matching lines...) Expand all
338 } 359 }
339 360
340 TEST_F(URLRequestFileJobEventsTest, OpenNonExistentFile) { 361 TEST_F(URLRequestFileJobEventsTest, OpenNonExistentFile) {
341 base::FilePath path; 362 base::FilePath path;
342 PathService::Get(base::DIR_SOURCE_ROOT, &path); 363 PathService::Get(base::DIR_SOURCE_ROOT, &path);
343 path = path.Append( 364 path = path.Append(
344 FILE_PATH_LITERAL("net/data/url_request_unittest/non-existent.txt")); 365 FILE_PATH_LITERAL("net/data/url_request_unittest/non-existent.txt"));
345 366
346 int open_result; 367 int open_result;
347 int64_t seek_position; 368 int64_t seek_position;
369 bool done_reading;
348 std::string observed_content; 370 std::string observed_content;
349 RunRequestWithPath(path, std::string(), &open_result, &seek_position, 371 RunRequestWithPath(path, std::string(), &open_result, &seek_position,
350 &observed_content); 372 &done_reading, &observed_content);
351 373
352 EXPECT_EQ(ERR_FILE_NOT_FOUND, open_result); 374 EXPECT_EQ(ERR_FILE_NOT_FOUND, open_result);
375 EXPECT_FALSE(done_reading);
353 EXPECT_TRUE(delegate_.request_failed()); 376 EXPECT_TRUE(delegate_.request_failed());
354 } 377 }
355 378
356 TEST_F(URLRequestFileJobEventsTest, MultiRangeRequestNotSupported) { 379 TEST_F(URLRequestFileJobEventsTest, MultiRangeRequestNotSupported) {
357 base::FilePath path; 380 base::FilePath path;
358 PathService::Get(base::DIR_SOURCE_ROOT, &path); 381 PathService::Get(base::DIR_SOURCE_ROOT, &path);
359 path = path.Append( 382 path = path.Append(
360 FILE_PATH_LITERAL("net/data/url_request_unittest/BullRunSpeech.txt")); 383 FILE_PATH_LITERAL("net/data/url_request_unittest/BullRunSpeech.txt"));
361 384
362 int open_result; 385 int open_result;
363 int64_t seek_position; 386 int64_t seek_position;
387 bool done_reading;
364 std::string observed_content; 388 std::string observed_content;
365 RunRequestWithPath(path, "bytes=1-5,20-30", &open_result, &seek_position, 389 RunRequestWithPath(path, "bytes=1-5,20-30", &open_result, &seek_position,
366 &observed_content); 390 &done_reading, &observed_content);
367 391
368 EXPECT_EQ(OK, open_result); 392 EXPECT_EQ(OK, open_result);
369 EXPECT_EQ(ERR_REQUEST_RANGE_NOT_SATISFIABLE, seek_position); 393 EXPECT_EQ(ERR_REQUEST_RANGE_NOT_SATISFIABLE, seek_position);
394 EXPECT_FALSE(done_reading);
370 EXPECT_TRUE(delegate_.request_failed()); 395 EXPECT_TRUE(delegate_.request_failed());
371 } 396 }
372 397
373 TEST_F(URLRequestFileJobEventsTest, RangeExceedingFileSize) { 398 TEST_F(URLRequestFileJobEventsTest, RangeExceedingFileSize) {
374 base::FilePath path; 399 base::FilePath path;
375 PathService::Get(base::DIR_SOURCE_ROOT, &path); 400 PathService::Get(base::DIR_SOURCE_ROOT, &path);
376 path = path.Append( 401 path = path.Append(
377 FILE_PATH_LITERAL("net/data/url_request_unittest/BullRunSpeech.txt")); 402 FILE_PATH_LITERAL("net/data/url_request_unittest/BullRunSpeech.txt"));
378 403
379 int open_result; 404 int open_result;
380 int64_t seek_position; 405 int64_t seek_position;
406 bool done_reading;
381 std::string observed_content; 407 std::string observed_content;
382 RunRequestWithPath(path, "bytes=50000-", &open_result, &seek_position, 408 RunRequestWithPath(path, "bytes=50000-", &open_result, &seek_position,
383 &observed_content); 409 &done_reading, &observed_content);
384 410
385 EXPECT_EQ(OK, open_result); 411 EXPECT_EQ(OK, open_result);
386 EXPECT_EQ(ERR_REQUEST_RANGE_NOT_SATISFIABLE, seek_position); 412 EXPECT_EQ(ERR_REQUEST_RANGE_NOT_SATISFIABLE, seek_position);
413 EXPECT_FALSE(done_reading);
387 EXPECT_TRUE(delegate_.request_failed()); 414 EXPECT_TRUE(delegate_.request_failed());
388 } 415 }
389 416
390 TEST_F(URLRequestFileJobEventsTest, IgnoreRangeParsingError) { 417 TEST_F(URLRequestFileJobEventsTest, IgnoreRangeParsingError) {
391 base::FilePath path; 418 base::FilePath path;
392 PathService::Get(base::DIR_SOURCE_ROOT, &path); 419 PathService::Get(base::DIR_SOURCE_ROOT, &path);
393 path = path.Append( 420 path = path.Append(
394 FILE_PATH_LITERAL("net/data/url_request_unittest/simple.html")); 421 FILE_PATH_LITERAL("net/data/url_request_unittest/simple.html"));
395 422
396 int open_result; 423 int open_result;
397 int64_t seek_position; 424 int64_t seek_position;
425 bool done_reading;
398 std::string observed_content; 426 std::string observed_content;
399 RunRequestWithPath(path, "bytes=3-z", &open_result, &seek_position, 427 RunRequestWithPath(path, "bytes=3-z", &open_result, &seek_position,
400 &observed_content); 428 &done_reading, &observed_content);
401 429
402 EXPECT_EQ(OK, open_result); 430 EXPECT_EQ(OK, open_result);
403 EXPECT_EQ(0, seek_position); 431 EXPECT_EQ(0, seek_position);
404 EXPECT_EQ("hello\n", observed_content); 432 EXPECT_EQ("hello\n", observed_content);
433 EXPECT_TRUE(done_reading);
405 EXPECT_FALSE(delegate_.request_failed()); 434 EXPECT_FALSE(delegate_.request_failed());
406 } 435 }
407 436
408 } // namespace 437 } // namespace
409 438
410 } // namespace net 439 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698