OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/url_request/url_request_file_job.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/threading/sequenced_worker_pool.h" |
| 12 #include "net/base/filename_util.h" |
| 13 #include "net/base/net_util.h" |
| 14 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 // A URLRequestFileJob for testing OnSeekComplete / OnReadComplete callbacks. |
| 20 class URLRequestFileJobWithCallbacks : public net::URLRequestFileJob { |
| 21 public: |
| 22 URLRequestFileJobWithCallbacks( |
| 23 net::URLRequest* request, |
| 24 net::NetworkDelegate* network_delegate, |
| 25 const base::FilePath& file_path, |
| 26 const scoped_refptr<base::TaskRunner>& file_task_runner) |
| 27 : URLRequestFileJob(request, |
| 28 network_delegate, |
| 29 file_path, |
| 30 file_task_runner), |
| 31 seek_position_(0) { |
| 32 } |
| 33 |
| 34 int64 seek_position() { return seek_position_; } |
| 35 const std::vector<std::string>& data_chunks() { return data_chunks_; } |
| 36 |
| 37 protected: |
| 38 virtual ~URLRequestFileJobWithCallbacks() {} |
| 39 |
| 40 virtual void OnSeekComplete(int64 result) OVERRIDE { |
| 41 ASSERT_EQ(seek_position_, 0); |
| 42 seek_position_ = result; |
| 43 } |
| 44 |
| 45 virtual void OnReadComplete(net::IOBuffer* buf, int result) OVERRIDE { |
| 46 data_chunks_.push_back(std::string(buf->data(), result)); |
| 47 } |
| 48 |
| 49 int64 seek_position_; |
| 50 std::vector<std::string> data_chunks_; |
| 51 }; |
| 52 |
| 53 // A URLRequestJobFactory that will return URLRequestFileJobWithCallbacks |
| 54 // instances for file:// scheme URLs. |
| 55 class CallbacksJobFactory : public net::URLRequestJobFactory { |
| 56 public: |
| 57 class JobObserver { |
| 58 public: |
| 59 virtual void OnJobCreated(URLRequestFileJobWithCallbacks* job) = 0; |
| 60 }; |
| 61 |
| 62 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) |
| 63 : path_(path), observer_(observer) { |
| 64 } |
| 65 |
| 66 virtual ~CallbacksJobFactory() {} |
| 67 |
| 68 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 69 const std::string& scheme, |
| 70 net::URLRequest* request, |
| 71 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 72 URLRequestFileJobWithCallbacks* job = new URLRequestFileJobWithCallbacks( |
| 73 request, |
| 74 network_delegate, |
| 75 path_, |
| 76 base::MessageLoop::current()->message_loop_proxy()); |
| 77 observer_->OnJobCreated(job); |
| 78 return job; |
| 79 } |
| 80 |
| 81 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { |
| 82 return scheme == "file"; |
| 83 } |
| 84 |
| 85 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { |
| 86 return IsHandledProtocol(url.scheme()); |
| 87 } |
| 88 |
| 89 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { |
| 90 return false; |
| 91 } |
| 92 |
| 93 private: |
| 94 base::FilePath path_; |
| 95 JobObserver* observer_; |
| 96 }; |
| 97 |
| 98 // Helper function to create a file in |directory| filled with |
| 99 // |content|. Returns true on succes and fills in |path| with the full path to |
| 100 // the file. |
| 101 bool CreateTempFileWithContent(const std::string& content, |
| 102 const base::ScopedTempDir& directory, |
| 103 base::FilePath* path) { |
| 104 if (!directory.IsValid()) |
| 105 return false; |
| 106 |
| 107 if (!base::CreateTemporaryFileInDir(directory.path(), path)) |
| 108 return false; |
| 109 |
| 110 return base::WriteFile(*path, content.c_str(), content.length()); |
| 111 } |
| 112 |
| 113 class JobObserverImpl : public CallbacksJobFactory::JobObserver { |
| 114 public: |
| 115 virtual void OnJobCreated(URLRequestFileJobWithCallbacks* job) OVERRIDE { |
| 116 jobs_.push_back(job); |
| 117 } |
| 118 |
| 119 typedef std::vector<scoped_refptr<URLRequestFileJobWithCallbacks> > JobList; |
| 120 |
| 121 const JobList& jobs() { return jobs_; } |
| 122 |
| 123 protected: |
| 124 JobList jobs_; |
| 125 }; |
| 126 |
| 127 // A simple holder for start/end used in http range requests. |
| 128 struct Range { |
| 129 int start; |
| 130 int end; |
| 131 |
| 132 Range() { |
| 133 start = 0; |
| 134 end = 0; |
| 135 } |
| 136 |
| 137 Range(int start, int end) { |
| 138 this->start = start; |
| 139 this->end = end; |
| 140 } |
| 141 }; |
| 142 |
| 143 // A superclass for tests of the OnSeekComplete / OnReadComplete functions of |
| 144 // URLRequestFileJob. |
| 145 class URLRequestFileJobEventsTest : public testing::Test { |
| 146 public: |
| 147 URLRequestFileJobEventsTest(); |
| 148 |
| 149 protected: |
| 150 // 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, |
| 152 // 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 |
| 154 // the request and verify that only the bytes in that range (inclusive) were |
| 155 // observed. |
| 156 void RunRequest(const std::string& content, const Range* range); |
| 157 |
| 158 JobObserverImpl observer_; |
| 159 net::TestURLRequestContext context_; |
| 160 net::TestDelegate delegate_; |
| 161 }; |
| 162 |
| 163 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {} |
| 164 |
| 165 void URLRequestFileJobEventsTest::RunRequest(const std::string& content, |
| 166 const Range* range) { |
| 167 base::ScopedTempDir directory; |
| 168 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
| 169 base::FilePath path; |
| 170 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path)); |
| 171 CallbacksJobFactory factory(path, &observer_); |
| 172 context_.set_job_factory(&factory); |
| 173 |
| 174 net::TestURLRequest request(net::FilePathToFileURL(path), |
| 175 net::DEFAULT_PRIORITY, |
| 176 &delegate_, |
| 177 &context_); |
| 178 if (range) { |
| 179 ASSERT_GE(range->start, 0); |
| 180 ASSERT_GE(range->end, 0); |
| 181 ASSERT_LE(range->start, range->end); |
| 182 ASSERT_LT(static_cast<unsigned int>(range->end), content.length()); |
| 183 std::string range_value = |
| 184 base::StringPrintf("bytes=%d-%d", range->start, range->end); |
| 185 request.SetExtraRequestHeaderByName( |
| 186 net::HttpRequestHeaders::kRange, range_value, true /*overwrite*/); |
| 187 } |
| 188 request.Start(); |
| 189 |
| 190 base::RunLoop loop; |
| 191 loop.Run(); |
| 192 |
| 193 EXPECT_FALSE(delegate_.request_failed()); |
| 194 int expected_length = |
| 195 range ? (range->end - range->start + 1) : content.length(); |
| 196 EXPECT_EQ(delegate_.bytes_received(), expected_length); |
| 197 |
| 198 std::string expected_content; |
| 199 if (range) { |
| 200 expected_content.insert(0, content, range->start, expected_length); |
| 201 } else { |
| 202 expected_content = content; |
| 203 } |
| 204 EXPECT_TRUE(delegate_.data_received() == expected_content); |
| 205 |
| 206 ASSERT_EQ(observer_.jobs().size(), 1u); |
| 207 ASSERT_EQ(observer_.jobs().at(0)->seek_position(), range ? range->start : 0); |
| 208 |
| 209 std::string observed_content; |
| 210 const std::vector<std::string>& chunks = |
| 211 observer_.jobs().at(0)->data_chunks(); |
| 212 for (std::vector<std::string>::const_iterator i = chunks.begin(); |
| 213 i != chunks.end(); |
| 214 ++i) { |
| 215 observed_content.append(*i); |
| 216 } |
| 217 EXPECT_EQ(expected_content, observed_content); |
| 218 EXPECT_TRUE(expected_content == observed_content); |
| 219 } |
| 220 |
| 221 // Helper function to make a character array filled with |size| bytes of |
| 222 // test content. |
| 223 std::string MakeContentOfSize(int size) { |
| 224 EXPECT_GE(size, 0); |
| 225 std::string result; |
| 226 result.reserve(size); |
| 227 for (int i = 0; i < size; i++) { |
| 228 result.append(1, static_cast<char>(i % 256)); |
| 229 } |
| 230 return result; |
| 231 } |
| 232 |
| 233 } // namespace |
| 234 |
| 235 TEST_F(URLRequestFileJobEventsTest, TinyFile) { |
| 236 RunRequest(std::string("hello world"), NULL); |
| 237 } |
| 238 |
| 239 TEST_F(URLRequestFileJobEventsTest, SmallFile) { |
| 240 RunRequest(MakeContentOfSize(17 * 1024), NULL); |
| 241 } |
| 242 |
| 243 TEST_F(URLRequestFileJobEventsTest, BigFile) { |
| 244 RunRequest(MakeContentOfSize(3 * 1024 * 1024), NULL); |
| 245 } |
| 246 |
| 247 TEST_F(URLRequestFileJobEventsTest, Range) { |
| 248 // Use a 15KB content file and read a range chosen somewhat arbitrarily but |
| 249 // not aligned on any likely page boundaries. |
| 250 int size = 15 * 1024; |
| 251 Range range(1701, (6 * 1024) + 3); |
| 252 RunRequest(MakeContentOfSize(size), &range); |
| 253 } |
OLD | NEW |