| 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/upload_data_stream.h" | 5 #include "net/base/upload_data_stream.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/threading/thread_restrictions.h" |
| 15 #include "base/time.h" | 16 #include "base/time.h" |
| 16 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/base/test_completion_callback.h" |
| 18 #include "net/base/upload_data.h" | 20 #include "net/base/upload_data.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
| 21 | 23 |
| 22 namespace net { | 24 namespace net { |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 const char kTestData[] = "0123456789"; | 28 const char kTestData[] = "0123456789"; |
| 27 const size_t kTestDataSize = arraysize(kTestData) - 1; | 29 const size_t kTestDataSize = arraysize(kTestData) - 1; |
| 28 const size_t kTestBufferSize = 1 << 14; // 16KB. | 30 const size_t kTestBufferSize = 1 << 14; // 16KB. |
| 29 | 31 |
| 30 } // namespace | 32 } // namespace |
| 31 | 33 |
| 32 class UploadDataStreamTest : public PlatformTest { | 34 class UploadDataStreamTest : public PlatformTest { |
| 33 public: | 35 public: |
| 34 UploadDataStreamTest() : upload_data_(new UploadData) { } | 36 UploadDataStreamTest() : upload_data_(new UploadData) { } |
| 35 | 37 |
| 36 void FileChangedHelper(const FilePath& file_path, | 38 void FileChangedHelper(const FilePath& file_path, |
| 37 const base::Time& time, | 39 const base::Time& time, |
| 38 bool error_expected); | 40 bool error_expected); |
| 39 | 41 |
| 42 virtual void SetUp() { |
| 43 // To ensure that file IO is not performed on the main thread in the |
| 44 // test (i.e. InitSync() will fail if file IO is performed). |
| 45 base::ThreadRestrictions::SetIOAllowed(false); |
| 46 } |
| 47 |
| 48 virtual void TearDown() { |
| 49 base::ThreadRestrictions::SetIOAllowed(true); |
| 50 } |
| 51 |
| 40 scoped_refptr<UploadData> upload_data_; | 52 scoped_refptr<UploadData> upload_data_; |
| 41 }; | 53 }; |
| 42 | 54 |
| 43 TEST_F(UploadDataStreamTest, EmptyUploadData) { | 55 TEST_F(UploadDataStreamTest, EmptyUploadData) { |
| 44 upload_data_->AppendBytes("", 0); | 56 upload_data_->AppendBytes("", 0); |
| 45 scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data_)); | 57 scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data_)); |
| 46 ASSERT_EQ(OK, stream->Init()); | 58 ASSERT_EQ(OK, stream->InitSync()); |
| 47 ASSERT_TRUE(stream.get()); | 59 ASSERT_TRUE(stream.get()); |
| 48 EXPECT_EQ(0U, stream->size()); | 60 EXPECT_EQ(0U, stream->size()); |
| 49 EXPECT_EQ(0U, stream->position()); | 61 EXPECT_EQ(0U, stream->position()); |
| 50 EXPECT_TRUE(stream->IsEOF()); | 62 EXPECT_TRUE(stream->IsEOF()); |
| 51 } | 63 } |
| 52 | 64 |
| 53 TEST_F(UploadDataStreamTest, ConsumeAll) { | 65 TEST_F(UploadDataStreamTest, ConsumeAll) { |
| 54 upload_data_->AppendBytes(kTestData, kTestDataSize); | 66 upload_data_->AppendBytes(kTestData, kTestDataSize); |
| 55 scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data_)); | 67 scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data_)); |
| 56 ASSERT_EQ(OK, stream->Init()); | 68 ASSERT_EQ(OK, stream->InitSync()); |
| 57 ASSERT_TRUE(stream.get()); | 69 ASSERT_TRUE(stream.get()); |
| 58 EXPECT_EQ(kTestDataSize, stream->size()); | 70 EXPECT_EQ(kTestDataSize, stream->size()); |
| 59 EXPECT_EQ(0U, stream->position()); | 71 EXPECT_EQ(0U, stream->position()); |
| 60 EXPECT_FALSE(stream->IsEOF()); | 72 EXPECT_FALSE(stream->IsEOF()); |
| 61 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); | 73 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); |
| 62 while (!stream->IsEOF()) { | 74 while (!stream->IsEOF()) { |
| 63 int bytes_read = stream->Read(buf, kTestBufferSize); | 75 int bytes_read = stream->Read(buf, kTestBufferSize); |
| 64 ASSERT_LE(0, bytes_read); // Not an error. | 76 ASSERT_LE(0, bytes_read); // Not an error. |
| 65 } | 77 } |
| 66 EXPECT_EQ(kTestDataSize, stream->position()); | 78 EXPECT_EQ(kTestDataSize, stream->position()); |
| 67 ASSERT_TRUE(stream->IsEOF()); | 79 ASSERT_TRUE(stream->IsEOF()); |
| 68 } | 80 } |
| 69 | 81 |
| 70 TEST_F(UploadDataStreamTest, FileSmallerThanLength) { | 82 TEST_F(UploadDataStreamTest, FileSmallerThanLength) { |
| 71 FilePath temp_file_path; | 83 FilePath temp_file_path; |
| 72 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); | 84 { |
| 73 ASSERT_EQ(static_cast<int>(kTestDataSize), | 85 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 74 file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); | 86 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); |
| 87 ASSERT_EQ(static_cast<int>(kTestDataSize), |
| 88 file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); |
| 89 } |
| 75 const uint64 kFakeSize = kTestDataSize*2; | 90 const uint64 kFakeSize = kTestDataSize*2; |
| 76 | 91 |
| 77 std::vector<UploadData::Element> elements; | 92 std::vector<UploadData::Element> elements; |
| 78 UploadData::Element element; | 93 UploadData::Element element; |
| 79 element.SetToFilePath(temp_file_path); | 94 element.SetToFilePath(temp_file_path); |
| 80 element.SetContentLength(kFakeSize); | 95 element.SetContentLength(kFakeSize); |
| 81 elements.push_back(element); | 96 elements.push_back(element); |
| 82 upload_data_->SetElements(elements); | 97 upload_data_->SetElements(elements); |
| 98 // Although upload_data_ carries a file, GetContentLengthSync() works here, |
| 99 // as the file size is overriden by SetContentLength(). |
| 83 EXPECT_EQ(kFakeSize, upload_data_->GetContentLengthSync()); | 100 EXPECT_EQ(kFakeSize, upload_data_->GetContentLengthSync()); |
| 84 | 101 |
| 85 scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data_)); | 102 scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data_)); |
| 86 ASSERT_EQ(OK, stream->Init()); | 103 // For the same reason, InitSync() works. |
| 104 ASSERT_EQ(OK, stream->InitSync()); |
| 87 ASSERT_TRUE(stream.get()); | 105 ASSERT_TRUE(stream.get()); |
| 88 EXPECT_EQ(kFakeSize, stream->size()); | 106 EXPECT_EQ(kFakeSize, stream->size()); |
| 89 EXPECT_EQ(0U, stream->position()); | 107 EXPECT_EQ(0U, stream->position()); |
| 90 EXPECT_FALSE(stream->IsEOF()); | 108 EXPECT_FALSE(stream->IsEOF()); |
| 91 uint64 read_counter = 0; | 109 uint64 read_counter = 0; |
| 92 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); | 110 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); |
| 93 while (!stream->IsEOF()) { | 111 while (!stream->IsEOF()) { |
| 94 int bytes_read = stream->Read(buf, kTestBufferSize); | 112 int bytes_read = stream->Read(buf, kTestBufferSize); |
| 95 ASSERT_LE(0, bytes_read); // Not an error. | 113 ASSERT_LE(0, bytes_read); // Not an error. |
| 96 read_counter += bytes_read; | 114 read_counter += bytes_read; |
| 97 EXPECT_EQ(read_counter, stream->position()); | 115 EXPECT_EQ(read_counter, stream->position()); |
| 98 } | 116 } |
| 99 // UpdateDataStream will pad out the file with 0 bytes so that the HTTP | 117 // UpdateDataStream will pad out the file with 0 bytes so that the HTTP |
| 100 // transaction doesn't hang. Therefore we expected the full size. | 118 // transaction doesn't hang. Therefore we expected the full size. |
| 101 EXPECT_EQ(kFakeSize, read_counter); | 119 EXPECT_EQ(kFakeSize, read_counter); |
| 102 EXPECT_EQ(read_counter, stream->position()); | 120 EXPECT_EQ(read_counter, stream->position()); |
| 103 | 121 |
| 104 file_util::Delete(temp_file_path, false); | 122 { |
| 123 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 124 file_util::Delete(temp_file_path, false); |
| 125 } |
| 105 } | 126 } |
| 106 | 127 |
| 107 void UploadDataStreamTest::FileChangedHelper(const FilePath& file_path, | 128 void UploadDataStreamTest::FileChangedHelper(const FilePath& file_path, |
| 108 const base::Time& time, | 129 const base::Time& time, |
| 109 bool error_expected) { | 130 bool error_expected) { |
| 110 std::vector<UploadData::Element> elements; | 131 std::vector<UploadData::Element> elements; |
| 111 UploadData::Element element; | 132 UploadData::Element element; |
| 112 element.SetToFilePathRange(file_path, 1, 2, time); | 133 element.SetToFilePathRange(file_path, 1, 2, time); |
| 113 elements.push_back(element); | 134 elements.push_back(element); |
| 114 // Don't use upload_data_ here, as this function is called twice, and | 135 // Don't use upload_data_ here, as this function is called twice, and |
| 115 // reusing upload_data_ is wrong. | 136 // reusing upload_data_ is wrong. |
| 116 scoped_refptr<UploadData> upload_data(new UploadData); | 137 scoped_refptr<UploadData> upload_data(new UploadData); |
| 117 upload_data->SetElements(elements); | 138 upload_data->SetElements(elements); |
| 118 | 139 |
| 119 scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data)); | 140 scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data)); |
| 120 int error_code = stream->Init(); | 141 TestCompletionCallback callback; |
| 142 int error_code = stream->Init(callback.callback()); |
| 143 ASSERT_EQ(ERR_IO_PENDING, error_code); |
| 144 error_code = callback.WaitForResult(); |
| 121 if (error_expected) | 145 if (error_expected) |
| 122 ASSERT_EQ(ERR_UPLOAD_FILE_CHANGED, error_code); | 146 ASSERT_EQ(ERR_UPLOAD_FILE_CHANGED, error_code); |
| 123 else | 147 else |
| 124 ASSERT_EQ(OK, error_code); | 148 ASSERT_EQ(OK, error_code); |
| 125 } | 149 } |
| 126 | 150 |
| 127 TEST_F(UploadDataStreamTest, FileChanged) { | 151 TEST_F(UploadDataStreamTest, FileChanged) { |
| 128 FilePath temp_file_path; | 152 FilePath temp_file_path; |
| 129 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); | |
| 130 ASSERT_EQ(static_cast<int>(kTestDataSize), | |
| 131 file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); | |
| 132 | |
| 133 base::PlatformFileInfo file_info; | 153 base::PlatformFileInfo file_info; |
| 134 ASSERT_TRUE(file_util::GetFileInfo(temp_file_path, &file_info)); | 154 { |
| 155 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 156 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); |
| 157 ASSERT_EQ(static_cast<int>(kTestDataSize), |
| 158 file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); |
| 159 ASSERT_TRUE(file_util::GetFileInfo(temp_file_path, &file_info)); |
| 160 } |
| 135 | 161 |
| 136 // Test file not changed. | 162 // Test file not changed. |
| 137 FileChangedHelper(temp_file_path, file_info.last_modified, false); | 163 FileChangedHelper(temp_file_path, file_info.last_modified, false); |
| 138 | 164 |
| 139 // Test file changed. | 165 // Test file changed. |
| 140 FileChangedHelper(temp_file_path, | 166 FileChangedHelper(temp_file_path, |
| 141 file_info.last_modified - base::TimeDelta::FromSeconds(1), | 167 file_info.last_modified - base::TimeDelta::FromSeconds(1), |
| 142 true); | 168 true); |
| 143 | 169 |
| 144 file_util::Delete(temp_file_path, false); | 170 { |
| 171 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 172 file_util::Delete(temp_file_path, false); |
| 173 } |
| 145 } | 174 } |
| 146 | 175 |
| 147 } // namespace net | 176 } // namespace net |
| OLD | NEW |