Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / webkit / browser / blob / local_file_stream_reader_unittest.cc
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
3 // found in the LICENSE file.
4
5 #include "webkit/browser/blob/local_file_stream_reader.h"
6
7 #include <string>
8
9 #include "base/file_util.h"
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/run_loop.h"
15 #include "base/threading/thread.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/test_completion_callback.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace webkit_blob {
22
23 namespace {
24
25 const char kTestData[] = "0123456789";
26 const int kTestDataSize = arraysize(kTestData) - 1;
27
28 void ReadFromReader(LocalFileStreamReader* reader,
29                     std::string* data, size_t size,
30                     int* result) {
31   ASSERT_TRUE(reader != NULL);
32   ASSERT_TRUE(result != NULL);
33   *result = net::OK;
34   net::TestCompletionCallback callback;
35   size_t total_bytes_read = 0;
36   while (total_bytes_read < size) {
37     scoped_refptr<net::IOBufferWithSize> buf(
38         new net::IOBufferWithSize(size - total_bytes_read));
39     int rv = reader->Read(buf.get(), buf->size(), callback.callback());
40     if (rv == net::ERR_IO_PENDING)
41       rv = callback.WaitForResult();
42     if (rv < 0)
43       *result = rv;
44     if (rv <= 0)
45       break;
46     total_bytes_read += rv;
47     data->append(buf->data(), rv);
48   }
49 }
50
51 void NeverCalled(int) { ADD_FAILURE(); }
52 void EmptyCallback() {}
53
54 void QuitLoop() {
55   base::MessageLoop::current()->Quit();
56 }
57
58 }  // namespace
59
60 class LocalFileStreamReaderTest : public testing::Test {
61  public:
62   LocalFileStreamReaderTest()
63       : file_thread_("FileUtilProxyTestFileThread") {}
64
65   virtual void SetUp() OVERRIDE {
66     ASSERT_TRUE(file_thread_.Start());
67     ASSERT_TRUE(dir_.CreateUniqueTempDir());
68
69     file_util::WriteFile(test_path(), kTestData, kTestDataSize);
70     base::File::Info info;
71     ASSERT_TRUE(base::GetFileInfo(test_path(), &info));
72     test_file_modification_time_ = info.last_modified;
73   }
74
75   virtual void TearDown() OVERRIDE {
76     // Give another chance for deleted streams to perform Close.
77     base::RunLoop().RunUntilIdle();
78     file_thread_.Stop();
79     base::RunLoop().RunUntilIdle();
80   }
81
82  protected:
83   LocalFileStreamReader* CreateFileReader(
84       const base::FilePath& path,
85       int64 initial_offset,
86       const base::Time& expected_modification_time) {
87     return new LocalFileStreamReader(
88             file_task_runner(),
89             path,
90             initial_offset,
91             expected_modification_time);
92   }
93
94   void TouchTestFile() {
95     base::Time new_modified_time =
96         test_file_modification_time() - base::TimeDelta::FromSeconds(1);
97     ASSERT_TRUE(base::TouchFile(test_path(),
98                                 test_file_modification_time(),
99                                 new_modified_time));
100   }
101
102   base::MessageLoopProxy* file_task_runner() const {
103     return file_thread_.message_loop_proxy().get();
104   }
105
106   base::FilePath test_dir() const { return dir_.path(); }
107   base::FilePath test_path() const { return dir_.path().AppendASCII("test"); }
108   base::Time test_file_modification_time() const {
109     return test_file_modification_time_;
110   }
111
112   void EnsureFileTaskFinished() {
113     file_task_runner()->PostTaskAndReply(
114         FROM_HERE, base::Bind(&EmptyCallback), base::Bind(&QuitLoop));
115     base::MessageLoop::current()->Run();
116   }
117
118  private:
119   base::MessageLoopForIO message_loop_;
120   base::Thread file_thread_;
121   base::ScopedTempDir dir_;
122   base::Time test_file_modification_time_;
123 };
124
125 TEST_F(LocalFileStreamReaderTest, NonExistent) {
126   base::FilePath nonexistent_path = test_dir().AppendASCII("nonexistent");
127   scoped_ptr<LocalFileStreamReader> reader(
128       CreateFileReader(nonexistent_path, 0, base::Time()));
129   int result = 0;
130   std::string data;
131   ReadFromReader(reader.get(), &data, 10, &result);
132   ASSERT_EQ(net::ERR_FILE_NOT_FOUND, result);
133   ASSERT_EQ(0U, data.size());
134 }
135
136 TEST_F(LocalFileStreamReaderTest, Empty) {
137   base::FilePath empty_path = test_dir().AppendASCII("empty");
138   base::File file(empty_path, base::File::FLAG_CREATE | base::File::FLAG_READ);
139   ASSERT_TRUE(file.IsValid());
140   file.Close();
141
142   scoped_ptr<LocalFileStreamReader> reader(
143       CreateFileReader(empty_path, 0, base::Time()));
144   int result = 0;
145   std::string data;
146   ReadFromReader(reader.get(), &data, 10, &result);
147   ASSERT_EQ(net::OK, result);
148   ASSERT_EQ(0U, data.size());
149
150   net::TestInt64CompletionCallback callback;
151   int64 length_result = reader->GetLength(callback.callback());
152   if (length_result == net::ERR_IO_PENDING)
153     length_result = callback.WaitForResult();
154   ASSERT_EQ(0, result);
155 }
156
157 TEST_F(LocalFileStreamReaderTest, GetLengthNormal) {
158   scoped_ptr<LocalFileStreamReader> reader(
159       CreateFileReader(test_path(), 0, test_file_modification_time()));
160   net::TestInt64CompletionCallback callback;
161   int64 result = reader->GetLength(callback.callback());
162   if (result == net::ERR_IO_PENDING)
163     result = callback.WaitForResult();
164   ASSERT_EQ(kTestDataSize, result);
165 }
166
167 TEST_F(LocalFileStreamReaderTest, GetLengthAfterModified) {
168   // Touch file so that the file's modification time becomes different
169   // from what we expect.
170   TouchTestFile();
171
172   scoped_ptr<LocalFileStreamReader> reader(
173       CreateFileReader(test_path(), 0, test_file_modification_time()));
174   net::TestInt64CompletionCallback callback;
175   int64 result = reader->GetLength(callback.callback());
176   if (result == net::ERR_IO_PENDING)
177     result = callback.WaitForResult();
178   ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result);
179
180   // With NULL expected modification time this should work.
181   reader.reset(CreateFileReader(test_path(), 0, base::Time()));
182   result = reader->GetLength(callback.callback());
183   if (result == net::ERR_IO_PENDING)
184     result = callback.WaitForResult();
185   ASSERT_EQ(kTestDataSize, result);
186 }
187
188 TEST_F(LocalFileStreamReaderTest, GetLengthWithOffset) {
189   scoped_ptr<LocalFileStreamReader> reader(
190       CreateFileReader(test_path(), 3, base::Time()));
191   net::TestInt64CompletionCallback callback;
192   int64 result = reader->GetLength(callback.callback());
193   if (result == net::ERR_IO_PENDING)
194     result = callback.WaitForResult();
195   // Initial offset does not affect the result of GetLength.
196   ASSERT_EQ(kTestDataSize, result);
197 }
198
199 TEST_F(LocalFileStreamReaderTest, ReadNormal) {
200   scoped_ptr<LocalFileStreamReader> reader(
201       CreateFileReader(test_path(), 0, test_file_modification_time()));
202   int result = 0;
203   std::string data;
204   ReadFromReader(reader.get(), &data, kTestDataSize, &result);
205   ASSERT_EQ(net::OK, result);
206   ASSERT_EQ(kTestData, data);
207 }
208
209 TEST_F(LocalFileStreamReaderTest, ReadAfterModified) {
210   // Touch file so that the file's modification time becomes different
211   // from what we expect.
212   TouchTestFile();
213
214   scoped_ptr<LocalFileStreamReader> reader(
215       CreateFileReader(test_path(), 0, test_file_modification_time()));
216   int result = 0;
217   std::string data;
218   ReadFromReader(reader.get(), &data, kTestDataSize, &result);
219   ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result);
220   ASSERT_EQ(0U, data.size());
221
222   // With NULL expected modification time this should work.
223   data.clear();
224   reader.reset(CreateFileReader(test_path(), 0, base::Time()));
225   ReadFromReader(reader.get(), &data, kTestDataSize, &result);
226   ASSERT_EQ(net::OK, result);
227   ASSERT_EQ(kTestData, data);
228 }
229
230 TEST_F(LocalFileStreamReaderTest, ReadWithOffset) {
231   scoped_ptr<LocalFileStreamReader> reader(
232       CreateFileReader(test_path(), 3, base::Time()));
233   int result = 0;
234   std::string data;
235   ReadFromReader(reader.get(), &data, kTestDataSize, &result);
236   ASSERT_EQ(net::OK, result);
237   ASSERT_EQ(&kTestData[3], data);
238 }
239
240 TEST_F(LocalFileStreamReaderTest, DeleteWithUnfinishedRead) {
241   scoped_ptr<LocalFileStreamReader> reader(
242       CreateFileReader(test_path(), 0, base::Time()));
243
244   net::TestCompletionCallback callback;
245   scoped_refptr<net::IOBufferWithSize> buf(
246       new net::IOBufferWithSize(kTestDataSize));
247   int rv = reader->Read(buf.get(), buf->size(), base::Bind(&NeverCalled));
248   ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0);
249
250   // Delete immediately.
251   // Should not crash; nor should NeverCalled be callback.
252   reader.reset();
253   EnsureFileTaskFinished();
254 }
255
256 }  // namespace webkit_blob