Chromium Code Reviews| 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 "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 11 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
| 12 #include "net/base/filename_util.h" | 13 #include "net/base/filename_util.h" |
| 13 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 15 #include "net/url_request/url_request.h" | |
| 14 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| 19 namespace net { | |
| 20 | |
| 17 namespace { | 21 namespace { |
| 18 | 22 |
| 19 // A URLRequestFileJob for testing OnSeekComplete / OnReadComplete callbacks. | 23 // A URLRequestFileJob for testing OnSeekComplete / OnReadComplete callbacks. |
| 20 class URLRequestFileJobWithCallbacks : public net::URLRequestFileJob { | 24 class URLRequestFileJobWithCallbacks : public URLRequestFileJob { |
| 21 public: | 25 public: |
| 22 URLRequestFileJobWithCallbacks( | 26 URLRequestFileJobWithCallbacks( |
| 23 net::URLRequest* request, | 27 URLRequest* request, |
| 24 net::NetworkDelegate* network_delegate, | 28 NetworkDelegate* network_delegate, |
| 25 const base::FilePath& file_path, | 29 const base::FilePath& file_path, |
| 26 const scoped_refptr<base::TaskRunner>& file_task_runner) | 30 const scoped_refptr<base::TaskRunner>& file_task_runner) |
| 27 : URLRequestFileJob(request, | 31 : URLRequestFileJob(request, |
| 28 network_delegate, | 32 network_delegate, |
| 29 file_path, | 33 file_path, |
| 30 file_task_runner), | 34 file_task_runner), |
| 31 seek_position_(0) { | 35 seek_position_(0) { |
| 32 } | 36 } |
| 33 | 37 |
| 34 int64 seek_position() { return seek_position_; } | 38 int64 seek_position() { return seek_position_; } |
| 35 const std::vector<std::string>& data_chunks() { return data_chunks_; } | 39 const std::vector<std::string>& data_chunks() { return data_chunks_; } |
| 36 | 40 |
| 37 protected: | 41 protected: |
| 38 virtual ~URLRequestFileJobWithCallbacks() {} | 42 virtual ~URLRequestFileJobWithCallbacks() {} |
| 39 | 43 |
| 40 virtual void OnSeekComplete(int64 result) OVERRIDE { | 44 virtual void OnSeekComplete(int64 result) OVERRIDE { |
| 41 ASSERT_EQ(seek_position_, 0); | 45 ASSERT_EQ(seek_position_, 0); |
| 42 seek_position_ = result; | 46 seek_position_ = result; |
| 43 } | 47 } |
| 44 | 48 |
| 45 virtual void OnReadComplete(net::IOBuffer* buf, int result) OVERRIDE { | 49 virtual void OnReadComplete(IOBuffer* buf, int result) OVERRIDE { |
| 46 data_chunks_.push_back(std::string(buf->data(), result)); | 50 data_chunks_.push_back(std::string(buf->data(), result)); |
| 47 } | 51 } |
| 48 | 52 |
| 49 int64 seek_position_; | 53 int64 seek_position_; |
| 50 std::vector<std::string> data_chunks_; | 54 std::vector<std::string> data_chunks_; |
| 51 }; | 55 }; |
| 52 | 56 |
| 53 // A URLRequestJobFactory that will return URLRequestFileJobWithCallbacks | 57 // A URLRequestJobFactory that will return URLRequestFileJobWithCallbacks |
| 54 // instances for file:// scheme URLs. | 58 // instances for file:// scheme URLs. |
| 55 class CallbacksJobFactory : public net::URLRequestJobFactory { | 59 class CallbacksJobFactory : public URLRequestJobFactory { |
| 56 public: | 60 public: |
| 57 class JobObserver { | 61 class JobObserver { |
| 58 public: | 62 public: |
| 59 virtual void OnJobCreated(URLRequestFileJobWithCallbacks* job) = 0; | 63 virtual void OnJobCreated(URLRequestFileJobWithCallbacks* job) = 0; |
| 60 }; | 64 }; |
| 61 | 65 |
| 62 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) | 66 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) |
| 63 : path_(path), observer_(observer) { | 67 : path_(path), observer_(observer) { |
| 64 } | 68 } |
| 65 | 69 |
| 66 virtual ~CallbacksJobFactory() {} | 70 virtual ~CallbacksJobFactory() {} |
| 67 | 71 |
| 68 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | 72 virtual URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 69 const std::string& scheme, | 73 const std::string& scheme, |
| 70 net::URLRequest* request, | 74 URLRequest* request, |
| 71 net::NetworkDelegate* network_delegate) const OVERRIDE { | 75 NetworkDelegate* network_delegate) const OVERRIDE { |
| 72 URLRequestFileJobWithCallbacks* job = new URLRequestFileJobWithCallbacks( | 76 URLRequestFileJobWithCallbacks* job = new URLRequestFileJobWithCallbacks( |
| 73 request, | 77 request, |
| 74 network_delegate, | 78 network_delegate, |
| 75 path_, | 79 path_, |
| 76 base::MessageLoop::current()->message_loop_proxy()); | 80 base::MessageLoop::current()->message_loop_proxy()); |
| 77 observer_->OnJobCreated(job); | 81 observer_->OnJobCreated(job); |
| 78 return job; | 82 return job; |
| 79 } | 83 } |
| 80 | 84 |
| 81 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { | 85 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 protected: | 153 protected: |
| 150 // This creates a file with |content| as the contents, and then creates and | 154 // This creates a file with |content| as the contents, and then creates and |
| 151 // runs a URLRequestFileJobWithCallbacks job to get the contents out of it, | 155 // runs a URLRequestFileJobWithCallbacks job to get the contents out of it, |
| 152 // and makes sure that the callbacks observed the correct bytes. If a Range | 156 // and makes sure that the callbacks observed the correct bytes. If a Range |
| 153 // is provided, this function will add the appropriate Range http header to | 157 // is provided, this function will add the appropriate Range http header to |
| 154 // the request and verify that only the bytes in that range (inclusive) were | 158 // the request and verify that only the bytes in that range (inclusive) were |
| 155 // observed. | 159 // observed. |
| 156 void RunRequest(const std::string& content, const Range* range); | 160 void RunRequest(const std::string& content, const Range* range); |
| 157 | 161 |
| 158 JobObserverImpl observer_; | 162 JobObserverImpl observer_; |
| 159 net::TestURLRequestContext context_; | 163 TestURLRequestContext context_; |
| 160 net::TestDelegate delegate_; | 164 TestDelegate delegate_; |
| 161 }; | 165 }; |
| 162 | 166 |
| 163 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {} | 167 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {} |
| 164 | 168 |
| 165 void URLRequestFileJobEventsTest::RunRequest(const std::string& content, | 169 void URLRequestFileJobEventsTest::RunRequest(const std::string& content, |
| 166 const Range* range) { | 170 const Range* range) { |
| 167 base::ScopedTempDir directory; | 171 base::ScopedTempDir directory; |
| 168 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 172 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
| 169 base::FilePath path; | 173 base::FilePath path; |
| 170 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path)); | 174 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path)); |
| 171 CallbacksJobFactory factory(path, &observer_); | 175 CallbacksJobFactory factory(path, &observer_); |
| 172 context_.set_job_factory(&factory); | 176 context_.set_job_factory(&factory); |
| 173 | 177 |
| 174 net::TestURLRequest request(net::FilePathToFileURL(path), | 178 scoped_ptr<URLRequest> request(context_.CreateRequest( |
| 175 net::DEFAULT_PRIORITY, | 179 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_, NULL)); |
| 176 &delegate_, | |
| 177 &context_); | |
| 178 if (range) { | 180 if (range) { |
| 179 ASSERT_GE(range->start, 0); | 181 ASSERT_GE(range->start, 0); |
| 180 ASSERT_GE(range->end, 0); | 182 ASSERT_GE(range->end, 0); |
| 181 ASSERT_LE(range->start, range->end); | 183 ASSERT_LE(range->start, range->end); |
| 182 ASSERT_LT(static_cast<unsigned int>(range->end), content.length()); | 184 ASSERT_LT(static_cast<unsigned int>(range->end), content.length()); |
| 183 std::string range_value = | 185 std::string range_value = |
| 184 base::StringPrintf("bytes=%d-%d", range->start, range->end); | 186 base::StringPrintf("bytes=%d-%d", range->start, range->end); |
| 185 request.SetExtraRequestHeaderByName( | 187 request->SetExtraRequestHeaderByName( |
| 186 net::HttpRequestHeaders::kRange, range_value, true /*overwrite*/); | 188 HttpRequestHeaders::kRange, range_value, true /*overwrite*/); |
| 187 } | 189 } |
| 188 request.Start(); | 190 request->Start(); |
| 189 | 191 |
| 190 base::RunLoop loop; | 192 base::RunLoop loop; |
| 191 loop.Run(); | 193 loop.Run(); |
| 192 | 194 |
| 193 EXPECT_FALSE(delegate_.request_failed()); | 195 EXPECT_FALSE(delegate_.request_failed()); |
| 194 int expected_length = | 196 int expected_length = |
| 195 range ? (range->end - range->start + 1) : content.length(); | 197 range ? (range->end - range->start + 1) : content.length(); |
| 196 EXPECT_EQ(delegate_.bytes_received(), expected_length); | 198 EXPECT_EQ(delegate_.bytes_received(), expected_length); |
| 197 | 199 |
| 198 std::string expected_content; | 200 std::string expected_content; |
| 199 if (range) { | 201 if (range) { |
| 200 expected_content.insert(0, content, range->start, expected_length); | 202 expected_content.insert(0, content, range->start, expected_length); |
| 201 } else { | 203 } else { |
| 202 expected_content = content; | 204 expected_content = content; |
| 203 } | 205 } |
| 204 EXPECT_TRUE(delegate_.data_received() == expected_content); | 206 EXPECT_TRUE(delegate_.data_received() == expected_content); |
| 205 | 207 |
| 206 ASSERT_EQ(observer_.jobs().size(), 1u); | 208 ASSERT_EQ(observer_.jobs().size(), 1u); |
| 207 ASSERT_EQ(observer_.jobs().at(0)->seek_position(), range ? range->start : 0); | 209 ASSERT_EQ(observer_.jobs().at(0)->seek_position(), range ? range->start : 0); |
| 208 | 210 |
| 209 std::string observed_content; | 211 std::string observed_content; |
| 210 const std::vector<std::string>& chunks = | 212 const std::vector<std::string>& chunks = |
| 211 observer_.jobs().at(0)->data_chunks(); | 213 observer_.jobs().at(0)->data_chunks(); |
| 212 for (std::vector<std::string>::const_iterator i = chunks.begin(); | 214 for (std::vector<std::string>::const_iterator i = chunks.begin(); |
| 213 i != chunks.end(); | 215 i != chunks.end(); |
| 214 ++i) { | 216 ++i) { |
| 215 observed_content.append(*i); | 217 observed_content.append(*i); |
| 216 } | 218 } |
| 217 EXPECT_EQ(expected_content, observed_content); | 219 EXPECT_EQ(expected_content, observed_content); |
| 218 EXPECT_TRUE(expected_content == observed_content); | |
| 219 } | 220 } |
| 220 | 221 |
| 221 // Helper function to make a character array filled with |size| bytes of | 222 // Helper function to make a character array filled with |size| bytes of |
| 222 // test content. | 223 // test content. |
| 223 std::string MakeContentOfSize(int size) { | 224 std::string MakeContentOfSize(int size) { |
| 224 EXPECT_GE(size, 0); | 225 EXPECT_GE(size, 0); |
| 225 std::string result; | 226 std::string result; |
| 226 result.reserve(size); | 227 result.reserve(size); |
| 227 for (int i = 0; i < size; i++) { | 228 for (int i = 0; i < size; i++) { |
| 228 result.append(1, static_cast<char>(i % 256)); | 229 result.append(1, static_cast<char>(i % 256)); |
| 229 } | 230 } |
| 230 return result; | 231 return result; |
| 231 } | 232 } |
| 232 | 233 |
| 233 } // namespace | |
|
pauljensen
2014/08/28 15:48:39
nit: I liked this up here as I find it a nice divi
mmenke
2014/08/28 16:46:00
Each TEST_F does technically create a new class, w
| |
| 234 | |
| 235 TEST_F(URLRequestFileJobEventsTest, TinyFile) { | 234 TEST_F(URLRequestFileJobEventsTest, TinyFile) { |
| 236 RunRequest(std::string("hello world"), NULL); | 235 RunRequest(std::string("hello world"), NULL); |
| 237 } | 236 } |
| 238 | 237 |
| 239 TEST_F(URLRequestFileJobEventsTest, SmallFile) { | 238 TEST_F(URLRequestFileJobEventsTest, SmallFile) { |
| 240 RunRequest(MakeContentOfSize(17 * 1024), NULL); | 239 RunRequest(MakeContentOfSize(17 * 1024), NULL); |
| 241 } | 240 } |
| 242 | 241 |
| 243 TEST_F(URLRequestFileJobEventsTest, BigFile) { | 242 TEST_F(URLRequestFileJobEventsTest, BigFile) { |
| 244 RunRequest(MakeContentOfSize(3 * 1024 * 1024), NULL); | 243 RunRequest(MakeContentOfSize(3 * 1024 * 1024), NULL); |
| 245 } | 244 } |
| 246 | 245 |
| 247 TEST_F(URLRequestFileJobEventsTest, Range) { | 246 TEST_F(URLRequestFileJobEventsTest, Range) { |
| 248 // Use a 15KB content file and read a range chosen somewhat arbitrarily but | 247 // Use a 15KB content file and read a range chosen somewhat arbitrarily but |
| 249 // not aligned on any likely page boundaries. | 248 // not aligned on any likely page boundaries. |
| 250 int size = 15 * 1024; | 249 int size = 15 * 1024; |
| 251 Range range(1701, (6 * 1024) + 3); | 250 Range range(1701, (6 * 1024) + 3); |
| 252 RunRequest(MakeContentOfSize(size), &range); | 251 RunRequest(MakeContentOfSize(size), &range); |
| 253 } | 252 } |
| 253 | |
| 254 } // namespace | |
| 255 | |
| 256 } // namespace net | |
| OLD | NEW |