diff options
author | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-09 22:26:41 +0000 |
---|---|---|
committer | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-09 22:26:41 +0000 |
commit | 92aad5227a8e7b00f7fbc3e409037bf23cccda0c (patch) | |
tree | 13ec2aa09ed2e376a6c78524c3489f24d8e2d9a4 /net/base/file_stream_unittest.cc | |
parent | af416dfd64790147f1b25c634b4054907e54ba12 (diff) | |
download | chromium_src-92aad5227a8e7b00f7fbc3e409037bf23cccda0c.zip chromium_src-92aad5227a8e7b00f7fbc3e409037bf23cccda0c.tar.gz chromium_src-92aad5227a8e7b00f7fbc3e409037bf23cccda0c.tar.bz2 |
Add constructor to net::FileStream to create it with a base::PlatformFile handle.
Constructor added to both _win and _posix implementations and provided a unit test.
Review URL: http://codereview.chromium.org/20137
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9428 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/file_stream_unittest.cc')
-rw-r--r-- | net/base/file_stream_unittest.cc | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/net/base/file_stream_unittest.cc b/net/base/file_stream_unittest.cc index abee897..f96d57f 100644 --- a/net/base/file_stream_unittest.cc +++ b/net/base/file_stream_unittest.cc @@ -4,6 +4,7 @@ #include "base/file_util.h" #include "base/path_service.h" +#include "base/platform_file.h" #include "net/base/file_stream.h" #include "net/base/net_errors.h" #include "net/base/test_completion_callback.h" @@ -39,6 +40,44 @@ TEST_F(FileStreamTest, BasicOpenClose) { EXPECT_EQ(net::OK, rv); } +// Test the use of FileStream with a file handle provided at construction. +TEST_F(FileStreamTest, UseFileHandle) { + bool created = false; + + // 1. Test reading with a file handle. + ASSERT_EQ(kTestDataSize, + file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); + int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; + base::PlatformFile file = base::CreatePlatformFile( + temp_file_path().ToWStringHack(), flags, &created); + + // Seek to the beginning of the file and read. + net::FileStream read_stream(file, flags); + ASSERT_EQ(0, read_stream.Seek(net::FROM_BEGIN, 0)); + ASSERT_EQ(kTestDataSize, read_stream.Available()); + // Read into buffer and compare. + char buffer[kTestDataSize]; + ASSERT_EQ(kTestDataSize, read_stream.Read(buffer, kTestDataSize, NULL)); + ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); + read_stream.Close(); + + // 2. Test writing with a file handle. + file_util::Delete(temp_file_path(), false); + flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE; + file = base::CreatePlatformFile(temp_file_path().ToWStringHack(), + flags, &created); + + net::FileStream write_stream(file, flags); + ASSERT_EQ(0, write_stream.Seek(net::FROM_BEGIN, 0)); + ASSERT_EQ(kTestDataSize, write_stream.Write(kTestData, kTestDataSize, NULL)); + write_stream.Close(); + + // Read into buffer and compare to make sure the handle worked fine. + ASSERT_EQ(kTestDataSize, + file_util::ReadFile(temp_file_path(), buffer, kTestDataSize)); + ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); +} + TEST_F(FileStreamTest, UseClosedStream) { net::FileStream stream; |