Revert of Revert of Use BoringSSL in the implementation of ClearKey for Chromecast...
[chromium-blink-merge.git] / net / url_request / url_request_file_job_unittest.cc
blob427ed3d011453d833d7c711a5257d20a3042a0b0
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.
5 #include "net/url_request/url_request_file_job.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "net/base/filename_util.h"
14 #include "net/base/net_util.h"
15 #include "net/url_request/url_request.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace net {
21 namespace {
23 // A URLRequestFileJob for testing OnSeekComplete / OnReadComplete callbacks.
24 class URLRequestFileJobWithCallbacks : public URLRequestFileJob {
25 public:
26 URLRequestFileJobWithCallbacks(
27 URLRequest* request,
28 NetworkDelegate* network_delegate,
29 const base::FilePath& file_path,
30 const scoped_refptr<base::TaskRunner>& file_task_runner)
31 : URLRequestFileJob(request,
32 network_delegate,
33 file_path,
34 file_task_runner),
35 seek_position_(0) {
38 int64 seek_position() { return seek_position_; }
39 const std::vector<std::string>& data_chunks() { return data_chunks_; }
41 protected:
42 ~URLRequestFileJobWithCallbacks() override {}
44 void OnSeekComplete(int64 result) override {
45 ASSERT_EQ(seek_position_, 0);
46 seek_position_ = result;
49 void OnReadComplete(IOBuffer* buf, int result) override {
50 data_chunks_.push_back(std::string(buf->data(), result));
53 int64 seek_position_;
54 std::vector<std::string> data_chunks_;
57 // A URLRequestJobFactory that will return URLRequestFileJobWithCallbacks
58 // instances for file:// scheme URLs.
59 class CallbacksJobFactory : public URLRequestJobFactory {
60 public:
61 class JobObserver {
62 public:
63 virtual void OnJobCreated(URLRequestFileJobWithCallbacks* job) = 0;
66 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer)
67 : path_(path), observer_(observer) {
70 ~CallbacksJobFactory() override {}
72 URLRequestJob* MaybeCreateJobWithProtocolHandler(
73 const std::string& scheme,
74 URLRequest* request,
75 NetworkDelegate* network_delegate) const override {
76 URLRequestFileJobWithCallbacks* job = new URLRequestFileJobWithCallbacks(
77 request,
78 network_delegate,
79 path_,
80 base::MessageLoop::current()->message_loop_proxy());
81 observer_->OnJobCreated(job);
82 return job;
85 URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
86 NetworkDelegate* network_delegate,
87 const GURL& location) const override {
88 return nullptr;
91 URLRequestJob* MaybeInterceptResponse(
92 URLRequest* request,
93 NetworkDelegate* network_delegate) const override {
94 return nullptr;
97 bool IsHandledProtocol(const std::string& scheme) const override {
98 return scheme == "file";
101 bool IsHandledURL(const GURL& url) const override {
102 return IsHandledProtocol(url.scheme());
105 bool IsSafeRedirectTarget(const GURL& location) const override {
106 return false;
109 private:
110 base::FilePath path_;
111 JobObserver* observer_;
114 // Helper function to create a file in |directory| filled with
115 // |content|. Returns true on succes and fills in |path| with the full path to
116 // the file.
117 bool CreateTempFileWithContent(const std::string& content,
118 const base::ScopedTempDir& directory,
119 base::FilePath* path) {
120 if (!directory.IsValid())
121 return false;
123 if (!base::CreateTemporaryFileInDir(directory.path(), path))
124 return false;
126 return base::WriteFile(*path, content.c_str(), content.length());
129 class JobObserverImpl : public CallbacksJobFactory::JobObserver {
130 public:
131 void OnJobCreated(URLRequestFileJobWithCallbacks* job) override {
132 jobs_.push_back(job);
135 typedef std::vector<scoped_refptr<URLRequestFileJobWithCallbacks> > JobList;
137 const JobList& jobs() { return jobs_; }
139 protected:
140 JobList jobs_;
143 // A simple holder for start/end used in http range requests.
144 struct Range {
145 int start;
146 int end;
148 Range() {
149 start = 0;
150 end = 0;
153 Range(int start, int end) {
154 this->start = start;
155 this->end = end;
159 // A superclass for tests of the OnSeekComplete / OnReadComplete functions of
160 // URLRequestFileJob.
161 class URLRequestFileJobEventsTest : public testing::Test {
162 public:
163 URLRequestFileJobEventsTest();
165 protected:
166 // This creates a file with |content| as the contents, and then creates and
167 // runs a URLRequestFileJobWithCallbacks job to get the contents out of it,
168 // and makes sure that the callbacks observed the correct bytes. If a Range
169 // is provided, this function will add the appropriate Range http header to
170 // the request and verify that only the bytes in that range (inclusive) were
171 // observed.
172 void RunRequest(const std::string& content, const Range* range);
174 JobObserverImpl observer_;
175 TestURLRequestContext context_;
176 TestDelegate delegate_;
179 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {}
181 void URLRequestFileJobEventsTest::RunRequest(const std::string& content,
182 const Range* range) {
183 base::ScopedTempDir directory;
184 ASSERT_TRUE(directory.CreateUniqueTempDir());
185 base::FilePath path;
186 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path));
187 CallbacksJobFactory factory(path, &observer_);
188 context_.set_job_factory(&factory);
190 scoped_ptr<URLRequest> request(context_.CreateRequest(
191 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_));
192 if (range) {
193 ASSERT_GE(range->start, 0);
194 ASSERT_GE(range->end, 0);
195 ASSERT_LE(range->start, range->end);
196 ASSERT_LT(static_cast<unsigned int>(range->end), content.length());
197 std::string range_value =
198 base::StringPrintf("bytes=%d-%d", range->start, range->end);
199 request->SetExtraRequestHeaderByName(
200 HttpRequestHeaders::kRange, range_value, true /*overwrite*/);
202 request->Start();
204 base::RunLoop loop;
205 loop.Run();
207 EXPECT_FALSE(delegate_.request_failed());
208 int expected_length =
209 range ? (range->end - range->start + 1) : content.length();
210 EXPECT_EQ(delegate_.bytes_received(), expected_length);
212 std::string expected_content;
213 if (range) {
214 expected_content.insert(0, content, range->start, expected_length);
215 } else {
216 expected_content = content;
218 EXPECT_TRUE(delegate_.data_received() == expected_content);
220 ASSERT_EQ(observer_.jobs().size(), 1u);
221 ASSERT_EQ(observer_.jobs().at(0)->seek_position(), range ? range->start : 0);
223 std::string observed_content;
224 const std::vector<std::string>& chunks =
225 observer_.jobs().at(0)->data_chunks();
226 for (std::vector<std::string>::const_iterator i = chunks.begin();
227 i != chunks.end();
228 ++i) {
229 observed_content.append(*i);
231 EXPECT_EQ(expected_content, observed_content);
234 // Helper function to make a character array filled with |size| bytes of
235 // test content.
236 std::string MakeContentOfSize(int size) {
237 EXPECT_GE(size, 0);
238 std::string result;
239 result.reserve(size);
240 for (int i = 0; i < size; i++) {
241 result.append(1, static_cast<char>(i % 256));
243 return result;
246 TEST_F(URLRequestFileJobEventsTest, TinyFile) {
247 RunRequest(std::string("hello world"), NULL);
250 TEST_F(URLRequestFileJobEventsTest, SmallFile) {
251 RunRequest(MakeContentOfSize(17 * 1024), NULL);
254 TEST_F(URLRequestFileJobEventsTest, BigFile) {
255 RunRequest(MakeContentOfSize(3 * 1024 * 1024), NULL);
258 TEST_F(URLRequestFileJobEventsTest, Range) {
259 // Use a 15KB content file and read a range chosen somewhat arbitrarily but
260 // not aligned on any likely page boundaries.
261 int size = 15 * 1024;
262 Range range(1701, (6 * 1024) + 3);
263 RunRequest(MakeContentOfSize(size), &range);
266 } // namespace
268 } // namespace net