| OLD | NEW |
| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 observed_content_(observed_content) { | 47 observed_content_(observed_content) { |
| 48 *open_result_ = ERR_IO_PENDING; | 48 *open_result_ = ERR_IO_PENDING; |
| 49 *seek_position_ = ERR_IO_PENDING; | 49 *seek_position_ = ERR_IO_PENDING; |
| 50 observed_content_->clear(); | 50 observed_content_->clear(); |
| 51 } | 51 } |
| 52 | 52 |
| 53 ~TestURLRequestFileJob() override {} | 53 ~TestURLRequestFileJob() override {} |
| 54 | 54 |
| 55 protected: | 55 protected: |
| 56 void OnOpenComplete(int result) override { | 56 void OnOpenComplete(int result) override { |
| 57 printf("T: OnOpenComplete: %d\n", result); |
| 57 // Should only be called once. | 58 // Should only be called once. |
| 58 ASSERT_EQ(ERR_IO_PENDING, *open_result_); | 59 ASSERT_EQ(ERR_IO_PENDING, *open_result_); |
| 59 *open_result_ = result; | 60 *open_result_ = result; |
| 60 } | 61 } |
| 61 | 62 |
| 62 void OnSeekComplete(int64_t result) override { | 63 void OnSeekComplete(int64_t result) override { |
| 64 printf("T: OnSeekComplete: %ld\n", result); |
| 63 // Should only call this if open succeeded. | 65 // Should only call this if open succeeded. |
| 64 EXPECT_EQ(OK, *open_result_); | 66 EXPECT_EQ(OK, *open_result_); |
| 65 // Should only be called once. | 67 // Should only be called once. |
| 66 ASSERT_EQ(ERR_IO_PENDING, *seek_position_); | 68 ASSERT_EQ(ERR_IO_PENDING, *seek_position_); |
| 67 *seek_position_ = result; | 69 *seek_position_ = result; |
| 68 } | 70 } |
| 69 | 71 |
| 70 void OnReadComplete(IOBuffer* buf, int result) override { | 72 void OnReadComplete(IOBuffer* buf, int result) override { |
| 73 printf("T: OnReadComplete: %d\n", result); |
| 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 |
| 76 int* const open_result_; | 79 int* const open_result_; |
| 77 int64_t* const seek_position_; | 80 int64_t* const seek_position_; |
| 78 std::string* const observed_content_; | 81 std::string* const observed_content_; |
| 79 }; | 82 }; |
| 80 | 83 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 // These are mutable because MaybeCreateJobWithProtocolHandler is const. | 146 // These are mutable because MaybeCreateJobWithProtocolHandler is const. |
| 144 mutable int* open_result_; | 147 mutable int* open_result_; |
| 145 mutable int64_t* seek_position_; | 148 mutable int64_t* seek_position_; |
| 146 mutable std::string* observed_content_; | 149 mutable std::string* observed_content_; |
| 147 }; | 150 }; |
| 148 | 151 |
| 149 // Helper function to create a file at |path| filled with |content|. | 152 // Helper function to create a file at |path| filled with |content|. |
| 150 // Returns true on success. | 153 // Returns true on success. |
| 151 bool CreateFileWithContent(const std::string& content, | 154 bool CreateFileWithContent(const std::string& content, |
| 152 const base::FilePath& path) { | 155 const base::FilePath& path) { |
| 153 return base::WriteFile(path, content.c_str(), content.length()); | 156 return base::WriteFile(path, content.c_str(), content.length()) != -1; |
| 154 } | 157 } |
| 155 | 158 |
| 156 // A simple holder for start/end used in http range requests. | 159 // A simple holder for start/end used in http range requests. |
| 157 struct Range { | 160 struct Range { |
| 158 int start; | 161 int start; |
| 159 int end; | 162 int end; |
| 160 | 163 |
| 161 Range() { | 164 Range() { |
| 162 start = 0; | 165 start = 0; |
| 163 end = 0; | 166 end = 0; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 for (int i = 0; i < size; i++) { | 304 for (int i = 0; i < size; i++) { |
| 302 result.append(1, static_cast<char>(i % 256)); | 305 result.append(1, static_cast<char>(i % 256)); |
| 303 } | 306 } |
| 304 return result; | 307 return result; |
| 305 } | 308 } |
| 306 | 309 |
| 307 TEST_F(URLRequestFileJobEventsTest, TinyFile) { | 310 TEST_F(URLRequestFileJobEventsTest, TinyFile) { |
| 308 RunSuccessfulRequestWithString(std::string("hello world"), NULL); | 311 RunSuccessfulRequestWithString(std::string("hello world"), NULL); |
| 309 } | 312 } |
| 310 | 313 |
| 314 TEST_F(URLRequestFileJobEventsTest, ZeroFile) { |
| 315 RunSuccessfulRequestWithString(std::string(""), NULL); |
| 316 } |
| 317 |
| 311 TEST_F(URLRequestFileJobEventsTest, SmallFile) { | 318 TEST_F(URLRequestFileJobEventsTest, SmallFile) { |
| 312 RunSuccessfulRequestWithString(MakeContentOfSize(17 * 1024), NULL); | 319 RunSuccessfulRequestWithString(MakeContentOfSize(17 * 1024), NULL); |
| 313 } | 320 } |
| 314 | 321 |
| 315 TEST_F(URLRequestFileJobEventsTest, BigFile) { | 322 TEST_F(URLRequestFileJobEventsTest, BigFile) { |
| 316 RunSuccessfulRequestWithString(MakeContentOfSize(3 * 1024 * 1024), NULL); | 323 RunSuccessfulRequestWithString(MakeContentOfSize(3 * 1024 * 1024), NULL); |
| 317 } | 324 } |
| 318 | 325 |
| 319 TEST_F(URLRequestFileJobEventsTest, Range) { | 326 TEST_F(URLRequestFileJobEventsTest, Range) { |
| 320 // Use a 15KB content file and read a range chosen somewhat arbitrarily but | 327 // Use a 15KB content file and read a range chosen somewhat arbitrarily but |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 | 408 |
| 402 EXPECT_EQ(OK, open_result); | 409 EXPECT_EQ(OK, open_result); |
| 403 EXPECT_EQ(0, seek_position); | 410 EXPECT_EQ(0, seek_position); |
| 404 EXPECT_EQ("hello\n", observed_content); | 411 EXPECT_EQ("hello\n", observed_content); |
| 405 EXPECT_FALSE(delegate_.request_failed()); | 412 EXPECT_FALSE(delegate_.request_failed()); |
| 406 } | 413 } |
| 407 | 414 |
| 408 } // namespace | 415 } // namespace |
| 409 | 416 |
| 410 } // namespace net | 417 } // namespace net |
| OLD | NEW |