Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 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/fileapi/local_file_stream_writer.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include "base/callback.h" |
| 10 | #include "base/file_util.h" |
| 11 | #include "base/files/scoped_temp_dir.h" |
| 12 | #include "base/logging.h" |
| 13 | #include "base/memory/scoped_ptr.h" |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 15 | #include "base/run_loop.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 16 | #include "base/threading/thread.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 17 | #include "net/base/io_buffer.h" |
| 18 | #include "net/base/test_completion_callback.h" |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 21 | namespace fileapi { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 22 | |
| 23 | class LocalFileStreamWriterTest : public testing::Test { |
| 24 | public: |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 25 | LocalFileStreamWriterTest() |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame^] | 26 | : file_thread_("FileUtilProxyTestFileThread") {} |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 27 | |
| 28 | virtual void SetUp() OVERRIDE { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 29 | ASSERT_TRUE(file_thread_.Start()); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 30 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 31 | } |
| 32 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 33 | virtual void TearDown() OVERRIDE { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 34 | // Give another chance for deleted streams to perform Close. |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 35 | base::RunLoop().RunUntilIdle(); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 36 | file_thread_.Stop(); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 37 | base::RunLoop().RunUntilIdle(); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 40 | protected: |
| 41 | base::FilePath Path(const std::string& name) { |
| 42 | return temp_dir_.path().AppendASCII(name); |
| 43 | } |
| 44 | |
| 45 | int WriteStringToWriter(LocalFileStreamWriter* writer, |
| 46 | const std::string& data) { |
| 47 | scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data)); |
| 48 | scoped_refptr<net::DrainableIOBuffer> drainable( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 49 | new net::DrainableIOBuffer(buffer.get(), buffer->size())); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 50 | |
| 51 | while (drainable->BytesRemaining() > 0) { |
| 52 | net::TestCompletionCallback callback; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 53 | int result = writer->Write( |
| 54 | drainable.get(), drainable->BytesRemaining(), callback.callback()); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 55 | if (result == net::ERR_IO_PENDING) |
| 56 | result = callback.WaitForResult(); |
| 57 | if (result <= 0) |
| 58 | return result; |
| 59 | drainable->DidConsume(result); |
| 60 | } |
| 61 | return net::OK; |
| 62 | } |
| 63 | |
| 64 | std::string GetFileContent(const base::FilePath& path) { |
| 65 | std::string content; |
Torne (Richard Coles) | 58537e2 | 2013-09-12 12:10:22 +0100 | [diff] [blame] | 66 | base::ReadFileToString(path, &content); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 67 | return content; |
| 68 | } |
| 69 | |
| 70 | base::FilePath CreateFileWithContent(const std::string& name, |
| 71 | const std::string& data) { |
| 72 | base::FilePath path = Path(name); |
| 73 | file_util::WriteFile(path, data.c_str(), data.size()); |
| 74 | return path; |
| 75 | } |
| 76 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 77 | base::MessageLoopProxy* file_task_runner() const { |
| 78 | return file_thread_.message_loop_proxy().get(); |
| 79 | } |
| 80 | |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 81 | LocalFileStreamWriter* CreateWriter(const base::FilePath& path, |
| 82 | int64 offset) { |
| 83 | return new LocalFileStreamWriter(file_task_runner(), path, offset); |
| 84 | } |
| 85 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 86 | private: |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame^] | 87 | base::MessageLoopForIO message_loop_; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 88 | base::Thread file_thread_; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 89 | base::ScopedTempDir temp_dir_; |
| 90 | }; |
| 91 | |
| 92 | void NeverCalled(int unused) { |
| 93 | ADD_FAILURE(); |
| 94 | } |
| 95 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 96 | TEST_F(LocalFileStreamWriterTest, Write) { |
| 97 | base::FilePath path = CreateFileWithContent("file_a", std::string()); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 98 | scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 99 | EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); |
| 100 | EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "bar")); |
| 101 | writer.reset(); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 102 | base::RunLoop().RunUntilIdle(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 103 | EXPECT_TRUE(base::PathExists(path)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 104 | EXPECT_EQ("foobar", GetFileContent(path)); |
| 105 | } |
| 106 | |
| 107 | TEST_F(LocalFileStreamWriterTest, WriteMiddle) { |
| 108 | base::FilePath path = CreateFileWithContent("file_a", "foobar"); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 109 | scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 2)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 110 | EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
| 111 | writer.reset(); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 112 | base::RunLoop().RunUntilIdle(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 113 | EXPECT_TRUE(base::PathExists(path)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 114 | EXPECT_EQ("foxxxr", GetFileContent(path)); |
| 115 | } |
| 116 | |
| 117 | TEST_F(LocalFileStreamWriterTest, WriteEnd) { |
| 118 | base::FilePath path = CreateFileWithContent("file_a", "foobar"); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 119 | scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 6)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 120 | EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
| 121 | writer.reset(); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 122 | base::RunLoop().RunUntilIdle(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 123 | EXPECT_TRUE(base::PathExists(path)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 124 | EXPECT_EQ("foobarxxx", GetFileContent(path)); |
| 125 | } |
| 126 | |
| 127 | TEST_F(LocalFileStreamWriterTest, WriteFailForNonexistingFile) { |
| 128 | base::FilePath path = Path("file_a"); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 129 | ASSERT_FALSE(base::PathExists(path)); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 130 | scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 131 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, WriteStringToWriter(writer.get(), "foo")); |
| 132 | writer.reset(); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 133 | base::RunLoop().RunUntilIdle(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 134 | EXPECT_FALSE(base::PathExists(path)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | TEST_F(LocalFileStreamWriterTest, CancelBeforeOperation) { |
| 138 | base::FilePath path = Path("file_a"); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 139 | scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 140 | // Cancel immediately fails when there's no in-flight operation. |
| 141 | int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); |
| 142 | EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); |
| 143 | } |
| 144 | |
| 145 | TEST_F(LocalFileStreamWriterTest, CancelAfterFinishedOperation) { |
| 146 | base::FilePath path = CreateFileWithContent("file_a", std::string()); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 147 | scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 148 | EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); |
| 149 | |
| 150 | // Cancel immediately fails when there's no in-flight operation. |
| 151 | int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); |
| 152 | EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); |
| 153 | |
| 154 | writer.reset(); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 155 | base::RunLoop().RunUntilIdle(); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 156 | // Write operation is already completed. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 157 | EXPECT_TRUE(base::PathExists(path)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 158 | EXPECT_EQ("foo", GetFileContent(path)); |
| 159 | } |
| 160 | |
| 161 | TEST_F(LocalFileStreamWriterTest, CancelWrite) { |
| 162 | base::FilePath path = CreateFileWithContent("file_a", "foobar"); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 163 | scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 164 | |
| 165 | scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx")); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 166 | int result = |
| 167 | writer->Write(buffer.get(), buffer->size(), base::Bind(&NeverCalled)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 168 | ASSERT_EQ(net::ERR_IO_PENDING, result); |
| 169 | |
| 170 | net::TestCompletionCallback callback; |
| 171 | writer->Cancel(callback.callback()); |
| 172 | int cancel_result = callback.WaitForResult(); |
| 173 | EXPECT_EQ(net::OK, cancel_result); |
| 174 | } |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 175 | |
| 176 | } // namespace fileapi |