| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 } | 40 } |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 class FileStreamTest : public PlatformTest { | 44 class FileStreamTest : public PlatformTest { |
| 45 public: | 45 public: |
| 46 virtual void SetUp() { | 46 virtual void SetUp() { |
| 47 PlatformTest::SetUp(); | 47 PlatformTest::SetUp(); |
| 48 | 48 |
| 49 base::CreateTemporaryFile(&temp_file_path_); | 49 base::CreateTemporaryFile(&temp_file_path_); |
| 50 file_util::WriteFile(temp_file_path_, kTestData, kTestDataSize); | 50 base::WriteFile(temp_file_path_, kTestData, kTestDataSize); |
| 51 } | 51 } |
| 52 virtual void TearDown() { | 52 virtual void TearDown() { |
| 53 EXPECT_TRUE(base::DeleteFile(temp_file_path_, false)); | 53 EXPECT_TRUE(base::DeleteFile(temp_file_path_, false)); |
| 54 | 54 |
| 55 // FileStreamContexts must be asynchronously closed on the file task runner | 55 // FileStreamContexts must be asynchronously closed on the file task runner |
| 56 // before they can be deleted. Pump the RunLoop to avoid leaks. | 56 // before they can be deleted. Pump the RunLoop to avoid leaks. |
| 57 base::RunLoop().RunUntilIdle(); | 57 base::RunLoop().RunUntilIdle(); |
| 58 PlatformTest::TearDown(); | 58 PlatformTest::TearDown(); |
| 59 } | 59 } |
| 60 | 60 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info)); | 138 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info)); |
| 139 base::RunLoop runloop; | 139 base::RunLoop runloop; |
| 140 runloop.RunUntilIdle(); | 140 runloop.RunUntilIdle(); |
| 141 // The file should now be closed, though the callback has not been called. | 141 // The file should now be closed, though the callback has not been called. |
| 142 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); | 142 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 TEST_F(FileStreamTest, FileHandleNotLeftOpen) { | 145 TEST_F(FileStreamTest, FileHandleNotLeftOpen) { |
| 146 bool created = false; | 146 bool created = false; |
| 147 ASSERT_EQ(kTestDataSize, | 147 ASSERT_EQ(kTestDataSize, |
| 148 file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); | 148 base::WriteFile(temp_file_path(), kTestData, kTestDataSize)); |
| 149 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; | 149 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; |
| 150 base::PlatformFile file = base::CreatePlatformFile( | 150 base::PlatformFile file = base::CreatePlatformFile( |
| 151 temp_file_path(), flags, &created, NULL); | 151 temp_file_path(), flags, &created, NULL); |
| 152 | 152 |
| 153 { | 153 { |
| 154 // Seek to the beginning of the file and read. | 154 // Seek to the beginning of the file and read. |
| 155 FileStream read_stream(file, flags, NULL, | 155 FileStream read_stream(file, flags, NULL, |
| 156 base::MessageLoopProxy::current()); | 156 base::MessageLoopProxy::current()); |
| 157 EXPECT_TRUE(read_stream.IsOpen()); | 157 EXPECT_TRUE(read_stream.IsOpen()); |
| 158 } | 158 } |
| 159 | 159 |
| 160 EXPECT_NE(base::kInvalidPlatformFileValue, file); | 160 EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 161 base::PlatformFileInfo info; | 161 base::PlatformFileInfo info; |
| 162 // The file should be closed. | 162 // The file should be closed. |
| 163 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); | 163 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); |
| 164 } | 164 } |
| 165 | 165 |
| 166 // Test the use of FileStream with a file handle provided at construction. | 166 // Test the use of FileStream with a file handle provided at construction. |
| 167 TEST_F(FileStreamTest, UseFileHandle) { | 167 TEST_F(FileStreamTest, UseFileHandle) { |
| 168 bool created = false; | 168 bool created = false; |
| 169 | 169 |
| 170 // 1. Test reading with a file handle. | 170 // 1. Test reading with a file handle. |
| 171 ASSERT_EQ(kTestDataSize, | 171 ASSERT_EQ(kTestDataSize, |
| 172 file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); | 172 base::WriteFile(temp_file_path(), kTestData, kTestDataSize)); |
| 173 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; | 173 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; |
| 174 base::PlatformFile file = base::CreatePlatformFile( | 174 base::PlatformFile file = base::CreatePlatformFile( |
| 175 temp_file_path(), flags, &created, NULL); | 175 temp_file_path(), flags, &created, NULL); |
| 176 | 176 |
| 177 // Seek to the beginning of the file and read. | 177 // Seek to the beginning of the file and read. |
| 178 scoped_ptr<FileStream> read_stream( | 178 scoped_ptr<FileStream> read_stream( |
| 179 new FileStream(file, flags, NULL, base::MessageLoopProxy::current())); | 179 new FileStream(file, flags, NULL, base::MessageLoopProxy::current())); |
| 180 ASSERT_EQ(0, read_stream->SeekSync(FROM_BEGIN, 0)); | 180 ASSERT_EQ(0, read_stream->SeekSync(FROM_BEGIN, 0)); |
| 181 ASSERT_EQ(kTestDataSize, read_stream->Available()); | 181 ASSERT_EQ(kTestDataSize, read_stream->Available()); |
| 182 // Read into buffer and compare. | 182 // Read into buffer and compare. |
| (...skipping 1039 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1222 total_bytes_read += rv; | 1222 total_bytes_read += rv; |
| 1223 data_read.append(buf->data(), rv); | 1223 data_read.append(buf->data(), rv); |
| 1224 } | 1224 } |
| 1225 EXPECT_EQ(file_size, total_bytes_read); | 1225 EXPECT_EQ(file_size, total_bytes_read); |
| 1226 } | 1226 } |
| 1227 #endif | 1227 #endif |
| 1228 | 1228 |
| 1229 } // namespace | 1229 } // namespace |
| 1230 | 1230 |
| 1231 } // namespace net | 1231 } // namespace net |
| OLD | NEW |