OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_dir_job.h" | 5 #include "net/url_request/url_request_file_dir_job.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/test/scoped_task_scheduler.h" | 17 #include "base/test/scoped_task_scheduler.h" |
18 #include "net/base/filename_util.h" | 18 #include "net/base/filename_util.h" |
19 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
21 #include "net/test/gtest_util.h" | 21 #include "net/test/gtest_util.h" |
| 22 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
22 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
23 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 using net::test::IsError; | 28 using net::test::IsError; |
28 using net::test::IsOk; | 29 using net::test::IsOk; |
29 | 30 |
30 namespace net { | 31 namespace net { |
31 | 32 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 TEST_F(URLRequestFileDirTest, ListCompletionOnNoPending) { | 109 TEST_F(URLRequestFileDirTest, ListCompletionOnNoPending) { |
109 base::ScopedTempDir directory; | 110 base::ScopedTempDir directory; |
110 // It is necessary to pass an existing directory to UrlRequest object, | 111 // It is necessary to pass an existing directory to UrlRequest object, |
111 // but it will be deleted for testing purpose after request is started. | 112 // but it will be deleted for testing purpose after request is started. |
112 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 113 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
113 TestJobFactory factory(directory.GetPath()); | 114 TestJobFactory factory(directory.GetPath()); |
114 context_.set_job_factory(&factory); | 115 context_.set_job_factory(&factory); |
115 std::unique_ptr<URLRequest> request(context_.CreateRequest( | 116 std::unique_ptr<URLRequest> request(context_.CreateRequest( |
116 FilePathToFileURL( | 117 FilePathToFileURL( |
117 directory.GetPath().AppendASCII("this_path_does_not_exist")), | 118 directory.GetPath().AppendASCII("this_path_does_not_exist")), |
118 DEFAULT_PRIORITY, &delegate_)); | 119 DEFAULT_PRIORITY, &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS)); |
119 | 120 |
120 request->Start(); | 121 request->Start(); |
121 ASSERT_TRUE(directory.Delete()); | 122 ASSERT_TRUE(directory.Delete()); |
122 | 123 |
123 // Since the DirectoryLister is running on the network thread, this | 124 // Since the DirectoryLister is running on the network thread, this |
124 // will spin the message loop until the read error is returned to the | 125 // will spin the message loop until the read error is returned to the |
125 // URLRequestFileDirJob. | 126 // URLRequestFileDirJob. |
126 base::RunLoop().RunUntilIdle(); | 127 base::RunLoop().RunUntilIdle(); |
127 ASSERT_TRUE(delegate_.got_response_started()); | 128 ASSERT_TRUE(delegate_.got_response_started()); |
128 | 129 |
129 int bytes_read = request->Read(buffer_.get(), kBufferSize); | 130 int bytes_read = request->Read(buffer_.get(), kBufferSize); |
130 | 131 |
131 // The URLRequestFileDirJobShould return the cached read error synchronously. | 132 // The URLRequestFileDirJobShould return the cached read error synchronously. |
132 // If it's not returned synchronously, the code path this is intended to test | 133 // If it's not returned synchronously, the code path this is intended to test |
133 // was not executed. | 134 // was not executed. |
134 EXPECT_THAT(bytes_read, IsError(ERR_FILE_NOT_FOUND)); | 135 EXPECT_THAT(bytes_read, IsError(ERR_FILE_NOT_FOUND)); |
135 } | 136 } |
136 | 137 |
137 // Test the case where reading the response completes synchronously. | 138 // Test the case where reading the response completes synchronously. |
138 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileSync) { | 139 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileSync) { |
139 base::ScopedTempDir directory; | 140 base::ScopedTempDir directory; |
140 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 141 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
141 base::FilePath path; | 142 base::FilePath path; |
142 base::CreateTemporaryFileInDir(directory.GetPath(), &path); | 143 base::CreateTemporaryFileInDir(directory.GetPath(), &path); |
143 | 144 |
144 TestJobFactory factory(directory.GetPath()); | 145 TestJobFactory factory(directory.GetPath()); |
145 context_.set_job_factory(&factory); | 146 context_.set_job_factory(&factory); |
146 | 147 |
147 std::unique_ptr<URLRequest> request(context_.CreateRequest( | 148 std::unique_ptr<URLRequest> request( |
148 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_)); | 149 context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 150 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS)); |
149 request->Start(); | 151 request->Start(); |
150 EXPECT_TRUE(request->is_pending()); | 152 EXPECT_TRUE(request->is_pending()); |
151 | 153 |
152 // Since the DirectoryLister is running on the network thread, this will spin | 154 // Since the DirectoryLister is running on the network thread, this will spin |
153 // the message loop until the URLRequetsFileDirJob has received the | 155 // the message loop until the URLRequetsFileDirJob has received the |
154 // entire directory listing and cached it. | 156 // entire directory listing and cached it. |
155 base::RunLoop().RunUntilIdle(); | 157 base::RunLoop().RunUntilIdle(); |
156 | 158 |
157 // This will complete synchronously, since the URLRequetsFileDirJob had | 159 // This will complete synchronously, since the URLRequetsFileDirJob had |
158 // directory listing cached in memory. | 160 // directory listing cached in memory. |
(...skipping 11 matching lines...) Expand all Loading... |
170 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileAsync) { | 172 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileAsync) { |
171 base::ScopedTempDir directory; | 173 base::ScopedTempDir directory; |
172 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 174 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
173 base::FilePath path; | 175 base::FilePath path; |
174 base::CreateTemporaryFileInDir(directory.GetPath(), &path); | 176 base::CreateTemporaryFileInDir(directory.GetPath(), &path); |
175 | 177 |
176 TestJobFactory factory(directory.GetPath()); | 178 TestJobFactory factory(directory.GetPath()); |
177 context_.set_job_factory(&factory); | 179 context_.set_job_factory(&factory); |
178 | 180 |
179 TestDelegate delegate; | 181 TestDelegate delegate; |
180 std::unique_ptr<URLRequest> request(context_.CreateRequest( | 182 std::unique_ptr<URLRequest> request( |
181 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate)); | 183 context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 184 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
182 request->Start(); | 185 request->Start(); |
183 EXPECT_TRUE(request->is_pending()); | 186 EXPECT_TRUE(request->is_pending()); |
184 | 187 |
185 base::RunLoop().Run(); | 188 base::RunLoop().Run(); |
186 | 189 |
187 ASSERT_GT(delegate.bytes_received(), 0); | 190 ASSERT_GT(delegate.bytes_received(), 0); |
188 ASSERT_LE(delegate.bytes_received(), kBufferSize); | 191 ASSERT_LE(delegate.bytes_received(), kBufferSize); |
189 EXPECT_TRUE(delegate.data_received().find( | 192 EXPECT_TRUE(delegate.data_received().find( |
190 directory.GetPath().BaseName().MaybeAsASCII()) != | 193 directory.GetPath().BaseName().MaybeAsASCII()) != |
191 std::string::npos); | 194 std::string::npos); |
(...skipping 10 matching lines...) Expand all Loading... |
202 FILE_PATH_LITERAL("CreateNewSubDirectoryInDirectory"), | 205 FILE_PATH_LITERAL("CreateNewSubDirectoryInDirectory"), |
203 &sub_dir); | 206 &sub_dir); |
204 | 207 |
205 base::FilePath path; | 208 base::FilePath path; |
206 base::CreateTemporaryFileInDir(directory.GetPath(), &path); | 209 base::CreateTemporaryFileInDir(directory.GetPath(), &path); |
207 | 210 |
208 TestJobFactory factory(directory.GetPath()); | 211 TestJobFactory factory(directory.GetPath()); |
209 context_.set_job_factory(&factory); | 212 context_.set_job_factory(&factory); |
210 | 213 |
211 TestDelegate delegate; | 214 TestDelegate delegate; |
212 std::unique_ptr<URLRequest> request(context_.CreateRequest( | 215 std::unique_ptr<URLRequest> request( |
213 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate)); | 216 context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 217 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
214 request->Start(); | 218 request->Start(); |
215 EXPECT_TRUE(request->is_pending()); | 219 EXPECT_TRUE(request->is_pending()); |
216 | 220 |
217 base::RunLoop().Run(); | 221 base::RunLoop().Run(); |
218 | 222 |
219 ASSERT_GT(delegate.bytes_received(), 0); | 223 ASSERT_GT(delegate.bytes_received(), 0); |
220 ASSERT_LE(delegate.bytes_received(), kBufferSize); | 224 ASSERT_LE(delegate.bytes_received(), kBufferSize); |
221 EXPECT_TRUE(delegate.data_received().find( | 225 EXPECT_TRUE(delegate.data_received().find( |
222 directory.GetPath().BaseName().MaybeAsASCII()) != | 226 directory.GetPath().BaseName().MaybeAsASCII()) != |
223 std::string::npos); | 227 std::string::npos); |
224 EXPECT_TRUE(delegate.data_received().find( | 228 EXPECT_TRUE(delegate.data_received().find( |
225 sub_dir.BaseName().MaybeAsASCII()) != std::string::npos); | 229 sub_dir.BaseName().MaybeAsASCII()) != std::string::npos); |
226 EXPECT_TRUE(delegate.data_received().find(path.BaseName().MaybeAsASCII()) != | 230 EXPECT_TRUE(delegate.data_received().find(path.BaseName().MaybeAsASCII()) != |
227 std::string::npos); | 231 std::string::npos); |
228 } | 232 } |
229 | 233 |
230 TEST_F(URLRequestFileDirTest, EmptyDirectory) { | 234 TEST_F(URLRequestFileDirTest, EmptyDirectory) { |
231 base::ScopedTempDir directory; | 235 base::ScopedTempDir directory; |
232 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 236 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
233 | 237 |
234 TestJobFactory factory(directory.GetPath()); | 238 TestJobFactory factory(directory.GetPath()); |
235 context_.set_job_factory(&factory); | 239 context_.set_job_factory(&factory); |
236 | 240 |
237 TestDelegate delegate; | 241 TestDelegate delegate; |
238 std::unique_ptr<URLRequest> request(context_.CreateRequest( | 242 std::unique_ptr<URLRequest> request(context_.CreateRequest( |
239 FilePathToFileURL(directory.GetPath()), DEFAULT_PRIORITY, &delegate)); | 243 FilePathToFileURL(directory.GetPath()), DEFAULT_PRIORITY, &delegate, |
| 244 TRAFFIC_ANNOTATION_FOR_TESTS)); |
240 request->Start(); | 245 request->Start(); |
241 EXPECT_TRUE(request->is_pending()); | 246 EXPECT_TRUE(request->is_pending()); |
242 | 247 |
243 base::RunLoop().Run(); | 248 base::RunLoop().Run(); |
244 | 249 |
245 ASSERT_GT(delegate.bytes_received(), 0); | 250 ASSERT_GT(delegate.bytes_received(), 0); |
246 ASSERT_LE(delegate.bytes_received(), kBufferSize); | 251 ASSERT_LE(delegate.bytes_received(), kBufferSize); |
247 EXPECT_TRUE(delegate.data_received().find( | 252 EXPECT_TRUE(delegate.data_received().find( |
248 directory.GetPath().BaseName().MaybeAsASCII()) != | 253 directory.GetPath().BaseName().MaybeAsASCII()) != |
249 std::string::npos); | 254 std::string::npos); |
250 } | 255 } |
251 | 256 |
252 } // namespace | 257 } // namespace |
253 | 258 |
254 } // namespace net | 259 } // namespace net |
OLD | NEW |