1// Copyright 2013 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 "chrome/browser/chromeos/drive/local_file_reader.h"
6
7#include <string>
8
9#include "base/files/file_path.h"
10#include "base/files/scoped_temp_dir.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/message_loop/message_loop.h"
14#include "base/rand_util.h"
15#include "base/threading/thread.h"
16#include "chrome/browser/chromeos/drive/test_util.h"
17#include "google_apis/drive/test_util.h"
18#include "net/base/io_buffer.h"
19#include "net/base/test_completion_callback.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
22namespace drive {
23namespace util {
24namespace {
25
26// An adapter to use test_util::ReadAllData.
27class LocalFileReaderAdapter {
28 public:
29  explicit LocalFileReaderAdapter(LocalFileReader* reader) : reader_(reader) {}
30  int Read(net::IOBuffer* buffer,
31           int buffer_length,
32           const net::CompletionCallback& callback) {
33    reader_->Read(buffer, buffer_length, callback);
34    // As LocalFileReader::Read always works asynchronously,
35    // return ERR_IO_PENDING.
36    return net::ERR_IO_PENDING;
37  }
38
39 private:
40  LocalFileReader* reader_;
41};
42
43}  // namespace
44
45class LocalFileReaderTest : public ::testing::Test {
46 protected:
47  virtual void SetUp() OVERRIDE {
48    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
49    worker_thread_.reset(new base::Thread("LocalFileReaderTest"));
50    ASSERT_TRUE(worker_thread_->Start());
51    file_reader_.reset(
52        new LocalFileReader(worker_thread_->message_loop_proxy().get()));
53  }
54
55  base::MessageLoop message_loop_;
56  base::ScopedTempDir temp_dir_;
57  scoped_ptr<base::Thread> worker_thread_;
58  scoped_ptr<LocalFileReader> file_reader_;
59};
60
61TEST_F(LocalFileReaderTest, NonExistingFile) {
62  const base::FilePath kTestFile =
63      temp_dir_.path().AppendASCII("non-existing");
64
65  net::TestCompletionCallback callback;
66  file_reader_->Open(kTestFile, 0, callback.callback());
67  EXPECT_EQ(net::ERR_FILE_NOT_FOUND, callback.WaitForResult());
68}
69
70TEST_F(LocalFileReaderTest, FullRead) {
71  base::FilePath test_file;
72  std::string expected_content;
73  ASSERT_TRUE(google_apis::test_util::CreateFileOfSpecifiedSize(
74      temp_dir_.path(), 1024, &test_file, &expected_content));
75
76  net::TestCompletionCallback callback;
77  file_reader_->Open(test_file, 0, callback.callback());
78  ASSERT_EQ(net::OK, callback.WaitForResult());
79
80  LocalFileReaderAdapter adapter(file_reader_.get());
81  std::string content;
82  ASSERT_EQ(net::OK, test_util::ReadAllData(&adapter, &content));
83  EXPECT_EQ(expected_content, content);
84}
85
86TEST_F(LocalFileReaderTest, OpenWithOffset) {
87  base::FilePath test_file;
88  std::string expected_content;
89  ASSERT_TRUE(google_apis::test_util::CreateFileOfSpecifiedSize(
90      temp_dir_.path(), 1024, &test_file, &expected_content));
91
92  size_t offset = expected_content.size() / 2;
93  expected_content.erase(0, offset);
94
95  net::TestCompletionCallback callback;
96  file_reader_->Open(
97      test_file, static_cast<int64>(offset), callback.callback());
98  ASSERT_EQ(net::OK, callback.WaitForResult());
99
100  LocalFileReaderAdapter adapter(file_reader_.get());
101  std::string content;
102  ASSERT_EQ(net::OK, test_util::ReadAllData(&adapter, &content));
103  EXPECT_EQ(expected_content, content);
104}
105
106}  // namespace util
107}  // namespace drive
108