Chromium Code Reviews| Index: content/browser/loader/upload_data_stream_builder_unittest.cc |
| diff --git a/content/browser/loader/upload_data_stream_builder_unittest.cc b/content/browser/loader/upload_data_stream_builder_unittest.cc |
| index a946e651e1f9700d673107635f070c2c6bf959be..cb04e59438c980aa57763d696d0f39f76d0cc48c 100644 |
| --- a/content/browser/loader/upload_data_stream_builder_unittest.cc |
| +++ b/content/browser/loader/upload_data_stream_builder_unittest.cc |
| @@ -5,6 +5,7 @@ |
| #include "content/browser/loader/upload_data_stream_builder.h" |
| #include <algorithm> |
| +#include <string> |
| #include "base/files/file_path.h" |
| #include "base/files/file_util.h" |
| @@ -13,6 +14,9 @@ |
| #include "base/run_loop.h" |
| #include "base/time/time.h" |
| #include "content/common/resource_request_body.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/test_completion_callback.h" |
| #include "net/base/upload_bytes_element_reader.h" |
| #include "net/base/upload_data_stream.h" |
| #include "net/base/upload_file_element_reader.h" |
| @@ -21,6 +25,7 @@ |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "url/gurl.h" |
| +using base::File; |
| using storage::BlobDataBuilder; |
| using storage::BlobDataHandle; |
| using storage::BlobStorageContext; |
| @@ -310,4 +315,86 @@ TEST(UploadDataStreamBuilderTest, ResolveBlobAndCreateUploadDataStream) { |
| base::RunLoop().RunUntilIdle(); |
| } |
| +TEST(UploadDataStreamBuilderTest, |
| + WriteUploadDataStreamWithEmptyFileBackedBlob) { |
| + base::MessageLoop message_loop; |
| + { |
| + base::Time blob_time; |
| + base::FilePath test_blob_path; |
| + ASSERT_TRUE(base::CreateTemporaryFile(&test_blob_path)); |
| + |
| + const std::string kBlobFileData = ""; |
|
michaeln
2015/03/09 21:15:18
dont think we need this
cmumford
2015/03/09 22:05:11
Yeah, it was handy when writing the test to make t
|
| + const uint64_t kBlobLength = kBlobFileData.size(); |
|
michaeln
2015/03/09 21:15:18
since its always zero, kZeroLength would be more c
cmumford
2015/03/09 22:05:10
Done.
|
| + if (!kBlobFileData.empty()) { |
|
michaeln
2015/03/09 21:15:18
its always empty so we don't need the if
cmumford
2015/03/09 22:05:10
Done.
|
| + File f(test_blob_path, File::FLAG_OPEN | File::FLAG_WRITE); |
| + ASSERT_EQ(static_cast<int>(kBlobLength), |
| + f.WriteAtCurrentPos(kBlobFileData.c_str(), kBlobLength)); |
| + base::File::Info info; |
| + ASSERT_TRUE(f.GetInfo(&info)); |
| + blob_time = info.last_modified; |
| + } else { |
| + base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &blob_time); |
| + ASSERT_TRUE(base::TouchFile(test_blob_path, blob_time, blob_time)); |
| + } |
| + |
| + BlobStorageContext blob_storage_context; |
| + |
| + // First an empty blob |
| + const std::string blob_id0("id-0"); |
| + scoped_ptr<BlobDataBuilder> blob_data_builder( |
| + new BlobDataBuilder(blob_id0)); |
| + scoped_ptr<BlobDataHandle> handle1 = |
| + blob_storage_context.AddFinishedBlob(blob_data_builder.get()); |
| + |
| + // Then a blob created from an empty file added several times. |
| + const std::string blob_id1("id-1"); |
| + blob_data_builder.reset(new BlobDataBuilder(blob_id1)); |
| + blob_data_builder->AppendFile(test_blob_path, 0, kBlobLength, blob_time); |
| + scoped_ptr<BlobDataHandle> handle2 = |
| + blob_storage_context.AddFinishedBlob(blob_data_builder.get()); |
| + |
| + ResourceRequestBody::Element blob_element; |
| + blob_element.SetToFilePathRange(test_blob_path, 0, kBlobLength, blob_time); |
| + |
| + scoped_refptr<ResourceRequestBody> request_body(new ResourceRequestBody()); |
| + scoped_ptr<net::UploadDataStream> upload(UploadDataStreamBuilder::Build( |
| + request_body.get(), &blob_storage_context, NULL, |
| + base::MessageLoopProxy::current().get())); |
| + |
| + request_body = new ResourceRequestBody(); |
| + request_body->AppendBlob(blob_id1); |
| + request_body->AppendBlob(blob_id1); |
| + request_body->AppendBlob(blob_id1); |
| + |
| + upload = UploadDataStreamBuilder::Build( |
| + request_body.get(), &blob_storage_context, NULL, |
| + base::MessageLoopProxy::current().get()); |
| + ASSERT_TRUE(upload->GetElementReaders()); |
| + const auto& readers = *upload->GetElementReaders(); |
| + ASSERT_EQ(3U, readers.size()); |
| + EXPECT_TRUE(AreElementsEqual(*readers[0], blob_element)); |
| + EXPECT_TRUE(AreElementsEqual(*readers[1], blob_element)); |
| + EXPECT_TRUE(AreElementsEqual(*readers[2], blob_element)); |
| + |
| + net::TestCompletionCallback init_callback; |
| + ASSERT_EQ(net::ERR_IO_PENDING, upload->Init(init_callback.callback())); |
| + EXPECT_EQ(net::OK, init_callback.WaitForResult()); |
| + |
| + const size_t kExpectedStreamLength = 3U * kBlobLength; |
| + EXPECT_EQ(kExpectedStreamLength, upload->size()); |
| + |
| + scoped_ptr<char[]> buffer(new char[kExpectedStreamLength]); |
| + scoped_refptr<net::IOBuffer> write_buff = |
| + new net::WrappedIOBuffer(buffer.get()); |
| + net::TestCompletionCallback read_callback; |
| + int result = upload->Read(write_buff.get(), kExpectedStreamLength, |
|
michaeln
2015/03/09 21:15:18
Ah, I see what tripped the dcheck in the stream re
cmumford
2015/03/09 22:05:10
Done.
|
| + read_callback.callback()); |
| + EXPECT_EQ(static_cast<int>(kExpectedStreamLength), |
| + read_callback.GetResult(result)); |
| + |
| + base::DeleteFile(test_blob_path, false); |
| + } |
| + // Clean up for ASAN. |
| + base::RunLoop().RunUntilIdle(); |
| +} |
| } // namespace content |