Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: net/base/upload_file_element_reader_unittest.cc

Issue 140733002: Revert 244982 "Revert 244857 "net: Remove classes which were use..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: rebase Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/upload_file_element_reader.cc ('k') | net/net.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/base/upload_file_element_reader.h" 5 #include "net/base/upload_file_element_reader.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/message_loop/message_loop_proxy.h" 9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 new UploadFileElementReader(base::MessageLoopProxy::current().get(), 227 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
228 wrong_path, 228 wrong_path,
229 0, 229 0,
230 kuint64max, 230 kuint64max,
231 base::Time())); 231 base::Time()));
232 TestCompletionCallback init_callback; 232 TestCompletionCallback init_callback;
233 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); 233 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback()));
234 EXPECT_EQ(ERR_FILE_NOT_FOUND, init_callback.WaitForResult()); 234 EXPECT_EQ(ERR_FILE_NOT_FOUND, init_callback.WaitForResult());
235 } 235 }
236 236
237
238 class UploadFileElementReaderSyncTest : public PlatformTest {
239 protected:
240 virtual void SetUp() OVERRIDE {
241 // Some tests (*.ReadPartially) rely on bytes_.size() being even.
242 const char kData[] = "123456789abcdefghi";
243 bytes_.assign(kData, kData + arraysize(kData) - 1);
244
245 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
246
247 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
248 &temp_file_path_));
249 ASSERT_EQ(
250 static_cast<int>(bytes_.size()),
251 file_util::WriteFile(temp_file_path_, &bytes_[0], bytes_.size()));
252
253 reader_.reset(new UploadFileElementReaderSync(
254 temp_file_path_, 0, kuint64max, base::Time()));
255 ASSERT_EQ(OK, reader_->Init(CompletionCallback()));
256 EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
257 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
258 EXPECT_FALSE(reader_->IsInMemory());
259 }
260
261 std::vector<char> bytes_;
262 scoped_ptr<UploadElementReader> reader_;
263 base::ScopedTempDir temp_dir_;
264 base::FilePath temp_file_path_;
265 };
266
267 TEST_F(UploadFileElementReaderSyncTest, ReadPartially) {
268 const size_t kHalfSize = bytes_.size() / 2;
269 ASSERT_EQ(bytes_.size(), kHalfSize * 2);
270 std::vector<char> buf(kHalfSize);
271 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
272 EXPECT_EQ(
273 static_cast<int>(buf.size()),
274 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
275 EXPECT_EQ(bytes_.size() - buf.size(), reader_->BytesRemaining());
276 EXPECT_EQ(std::vector<char>(bytes_.begin(), bytes_.begin() + kHalfSize), buf);
277
278 EXPECT_EQ(
279 static_cast<int>(buf.size()),
280 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
281 EXPECT_EQ(0U, reader_->BytesRemaining());
282 EXPECT_EQ(std::vector<char>(bytes_.begin() + kHalfSize, bytes_.end()), buf);
283 }
284
285 TEST_F(UploadFileElementReaderSyncTest, ReadAll) {
286 std::vector<char> buf(bytes_.size());
287 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
288 EXPECT_EQ(
289 static_cast<int>(buf.size()),
290 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
291 EXPECT_EQ(0U, reader_->BytesRemaining());
292 EXPECT_EQ(bytes_, buf);
293 // Try to read again.
294 EXPECT_EQ(
295 0, reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
296 }
297
298 TEST_F(UploadFileElementReaderSyncTest, ReadTooMuch) {
299 const size_t kTooLargeSize = bytes_.size() * 2;
300 std::vector<char> buf(kTooLargeSize);
301 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
302 EXPECT_EQ(
303 static_cast<int>(bytes_.size()),
304 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
305 EXPECT_EQ(0U, reader_->BytesRemaining());
306 buf.resize(bytes_.size()); // Resize to compare.
307 EXPECT_EQ(bytes_, buf);
308 }
309
310 TEST_F(UploadFileElementReaderSyncTest, MultipleInit) {
311 std::vector<char> buf(bytes_.size());
312 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
313
314 // Read all.
315 EXPECT_EQ(
316 static_cast<int>(buf.size()),
317 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
318 EXPECT_EQ(0U, reader_->BytesRemaining());
319 EXPECT_EQ(bytes_, buf);
320
321 // Call Init() again to reset the state.
322 ASSERT_EQ(OK, reader_->Init(CompletionCallback()));
323 EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
324 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
325
326 // Read again.
327 EXPECT_EQ(
328 static_cast<int>(buf.size()),
329 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
330 EXPECT_EQ(0U, reader_->BytesRemaining());
331 EXPECT_EQ(bytes_, buf);
332 }
333
334 TEST_F(UploadFileElementReaderSyncTest, Range) {
335 const uint64 kOffset = 2;
336 const uint64 kLength = bytes_.size() - kOffset * 3;
337 reader_.reset(new UploadFileElementReaderSync(
338 temp_file_path_, kOffset, kLength, base::Time()));
339 ASSERT_EQ(OK, reader_->Init(CompletionCallback()));
340 EXPECT_EQ(kLength, reader_->GetContentLength());
341 EXPECT_EQ(kLength, reader_->BytesRemaining());
342 std::vector<char> buf(kLength);
343 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
344 EXPECT_EQ(static_cast<int>(kLength),
345 reader_->Read(wrapped_buffer.get(), kLength, CompletionCallback()));
346 const std::vector<char> expected(bytes_.begin() + kOffset,
347 bytes_.begin() + kOffset + kLength);
348 EXPECT_EQ(expected, buf);
349 }
350
351 TEST_F(UploadFileElementReaderSyncTest, FileChanged) {
352 base::File::Info info;
353 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info));
354
355 // Expect one second before the actual modification time to simulate change.
356 const base::Time expected_modification_time =
357 info.last_modified - base::TimeDelta::FromSeconds(1);
358 reader_.reset(new UploadFileElementReaderSync(
359 temp_file_path_, 0, kuint64max, expected_modification_time));
360 EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, reader_->Init(CompletionCallback()));
361 }
362
363 TEST_F(UploadFileElementReaderSyncTest, WrongPath) {
364 const base::FilePath wrong_path(FILE_PATH_LITERAL("wrong_path"));
365 reader_.reset(new UploadFileElementReaderSync(
366 wrong_path, 0, kuint64max, base::Time()));
367 ASSERT_EQ(ERR_FILE_NOT_FOUND, reader_->Init(CompletionCallback()));
368 }
369
370 } // namespace net 237 } // namespace net
OLDNEW
« no previous file with comments | « net/base/upload_file_element_reader.cc ('k') | net/net.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698