OLD | NEW |
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/file_stream.h" | 5 #include "net/base/file_stream.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/callback.h" | 10 #include "base/callback.h" |
9 #include "base/files/file.h" | 11 #include "base/files/file.h" |
10 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
11 #include "base/macros.h" | 13 #include "base/macros.h" |
12 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
13 #include "base/path_service.h" | 15 #include "base/path_service.h" |
14 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
15 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
16 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 TestInt64CompletionCallback callback64; | 112 TestInt64CompletionCallback callback64; |
111 // 1. Test reading with a file handle. | 113 // 1. Test reading with a file handle. |
112 ASSERT_EQ(kTestDataSize, | 114 ASSERT_EQ(kTestDataSize, |
113 base::WriteFile(temp_file_path(), kTestData, kTestDataSize)); | 115 base::WriteFile(temp_file_path(), kTestData, kTestDataSize)); |
114 int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | | 116 int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | |
115 base::File::FLAG_ASYNC; | 117 base::File::FLAG_ASYNC; |
116 base::File file(temp_file_path(), flags); | 118 base::File file(temp_file_path(), flags); |
117 | 119 |
118 // Seek to the beginning of the file and read. | 120 // Seek to the beginning of the file and read. |
119 scoped_ptr<FileStream> read_stream( | 121 scoped_ptr<FileStream> read_stream( |
120 new FileStream(file.Pass(), base::ThreadTaskRunnerHandle::Get())); | 122 new FileStream(std::move(file), base::ThreadTaskRunnerHandle::Get())); |
121 ASSERT_EQ(ERR_IO_PENDING, read_stream->Seek(0, callback64.callback())); | 123 ASSERT_EQ(ERR_IO_PENDING, read_stream->Seek(0, callback64.callback())); |
122 ASSERT_EQ(0, callback64.WaitForResult()); | 124 ASSERT_EQ(0, callback64.WaitForResult()); |
123 // Read into buffer and compare. | 125 // Read into buffer and compare. |
124 scoped_refptr<IOBufferWithSize> read_buffer = | 126 scoped_refptr<IOBufferWithSize> read_buffer = |
125 new IOBufferWithSize(kTestDataSize); | 127 new IOBufferWithSize(kTestDataSize); |
126 rv = read_stream->Read(read_buffer.get(), kTestDataSize, callback.callback()); | 128 rv = read_stream->Read(read_buffer.get(), kTestDataSize, callback.callback()); |
127 ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); | 129 ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); |
128 ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); | 130 ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); |
129 read_stream.reset(); | 131 read_stream.reset(); |
130 | 132 |
131 // 2. Test writing with a file handle. | 133 // 2. Test writing with a file handle. |
132 base::DeleteFile(temp_file_path(), false); | 134 base::DeleteFile(temp_file_path(), false); |
133 flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE | | 135 flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE | |
134 base::File::FLAG_ASYNC; | 136 base::File::FLAG_ASYNC; |
135 file.Initialize(temp_file_path(), flags); | 137 file.Initialize(temp_file_path(), flags); |
136 | 138 |
137 scoped_ptr<FileStream> write_stream( | 139 scoped_ptr<FileStream> write_stream( |
138 new FileStream(file.Pass(), base::ThreadTaskRunnerHandle::Get())); | 140 new FileStream(std::move(file), base::ThreadTaskRunnerHandle::Get())); |
139 ASSERT_EQ(ERR_IO_PENDING, write_stream->Seek(0, callback64.callback())); | 141 ASSERT_EQ(ERR_IO_PENDING, write_stream->Seek(0, callback64.callback())); |
140 ASSERT_EQ(0, callback64.WaitForResult()); | 142 ASSERT_EQ(0, callback64.WaitForResult()); |
141 scoped_refptr<IOBufferWithSize> write_buffer = CreateTestDataBuffer(); | 143 scoped_refptr<IOBufferWithSize> write_buffer = CreateTestDataBuffer(); |
142 rv = write_stream->Write(write_buffer.get(), kTestDataSize, | 144 rv = write_stream->Write(write_buffer.get(), kTestDataSize, |
143 callback.callback()); | 145 callback.callback()); |
144 ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); | 146 ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); |
145 write_stream.reset(); | 147 write_stream.reset(); |
146 | 148 |
147 // Read into buffer and compare to make sure the handle worked fine. | 149 // Read into buffer and compare to make sure the handle worked fine. |
148 ASSERT_EQ(kTestDataSize, | 150 ASSERT_EQ(kTestDataSize, |
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
745 // Verify that Write() errors are mapped correctly. | 747 // Verify that Write() errors are mapped correctly. |
746 TEST_F(FileStreamTest, WriteError) { | 748 TEST_F(FileStreamTest, WriteError) { |
747 // Try opening file as read-only and then writing to it using FileStream. | 749 // Try opening file as read-only and then writing to it using FileStream. |
748 uint32_t flags = | 750 uint32_t flags = |
749 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC; | 751 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC; |
750 | 752 |
751 base::File file(temp_file_path(), flags); | 753 base::File file(temp_file_path(), flags); |
752 ASSERT_TRUE(file.IsValid()); | 754 ASSERT_TRUE(file.IsValid()); |
753 | 755 |
754 scoped_ptr<FileStream> stream( | 756 scoped_ptr<FileStream> stream( |
755 new FileStream(file.Pass(), base::ThreadTaskRunnerHandle::Get())); | 757 new FileStream(std::move(file), base::ThreadTaskRunnerHandle::Get())); |
756 | 758 |
757 scoped_refptr<IOBuffer> buf = new IOBuffer(1); | 759 scoped_refptr<IOBuffer> buf = new IOBuffer(1); |
758 buf->data()[0] = 0; | 760 buf->data()[0] = 0; |
759 | 761 |
760 TestCompletionCallback callback; | 762 TestCompletionCallback callback; |
761 int rv = stream->Write(buf.get(), 1, callback.callback()); | 763 int rv = stream->Write(buf.get(), 1, callback.callback()); |
762 if (rv == ERR_IO_PENDING) | 764 if (rv == ERR_IO_PENDING) |
763 rv = callback.WaitForResult(); | 765 rv = callback.WaitForResult(); |
764 EXPECT_LT(rv, 0); | 766 EXPECT_LT(rv, 0); |
765 | 767 |
766 stream.reset(); | 768 stream.reset(); |
767 base::RunLoop().RunUntilIdle(); | 769 base::RunLoop().RunUntilIdle(); |
768 } | 770 } |
769 | 771 |
770 // Verify that Read() errors are mapped correctly. | 772 // Verify that Read() errors are mapped correctly. |
771 TEST_F(FileStreamTest, ReadError) { | 773 TEST_F(FileStreamTest, ReadError) { |
772 // Try opening file for write and then reading from it using FileStream. | 774 // Try opening file for write and then reading from it using FileStream. |
773 uint32_t flags = | 775 uint32_t flags = |
774 base::File::FLAG_OPEN | base::File::FLAG_WRITE | base::File::FLAG_ASYNC; | 776 base::File::FLAG_OPEN | base::File::FLAG_WRITE | base::File::FLAG_ASYNC; |
775 | 777 |
776 base::File file(temp_file_path(), flags); | 778 base::File file(temp_file_path(), flags); |
777 ASSERT_TRUE(file.IsValid()); | 779 ASSERT_TRUE(file.IsValid()); |
778 | 780 |
779 scoped_ptr<FileStream> stream( | 781 scoped_ptr<FileStream> stream( |
780 new FileStream(file.Pass(), base::ThreadTaskRunnerHandle::Get())); | 782 new FileStream(std::move(file), base::ThreadTaskRunnerHandle::Get())); |
781 | 783 |
782 scoped_refptr<IOBuffer> buf = new IOBuffer(1); | 784 scoped_refptr<IOBuffer> buf = new IOBuffer(1); |
783 TestCompletionCallback callback; | 785 TestCompletionCallback callback; |
784 int rv = stream->Read(buf.get(), 1, callback.callback()); | 786 int rv = stream->Read(buf.get(), 1, callback.callback()); |
785 if (rv == ERR_IO_PENDING) | 787 if (rv == ERR_IO_PENDING) |
786 rv = callback.WaitForResult(); | 788 rv = callback.WaitForResult(); |
787 EXPECT_LT(rv, 0); | 789 EXPECT_LT(rv, 0); |
788 | 790 |
789 stream.reset(); | 791 stream.reset(); |
790 base::RunLoop().RunUntilIdle(); | 792 base::RunLoop().RunUntilIdle(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
831 total_bytes_read += rv; | 833 total_bytes_read += rv; |
832 data_read.append(buf->data(), rv); | 834 data_read.append(buf->data(), rv); |
833 } | 835 } |
834 EXPECT_EQ(file_size, total_bytes_read); | 836 EXPECT_EQ(file_size, total_bytes_read); |
835 } | 837 } |
836 #endif | 838 #endif |
837 | 839 |
838 } // namespace | 840 } // namespace |
839 | 841 |
840 } // namespace net | 842 } // namespace net |
OLD | NEW |