OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/loader/upload_data_stream_builder.h" | 5 #include "content/browser/loader/upload_data_stream_builder.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | |
8 | 9 |
9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
12 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
15 #include "content/common/resource_request_body.h" | 16 #include "content/common/resource_request_body.h" |
17 #include "net/base/io_buffer.h" | |
18 #include "net/base/net_errors.h" | |
19 #include "net/base/test_completion_callback.h" | |
16 #include "net/base/upload_bytes_element_reader.h" | 20 #include "net/base/upload_bytes_element_reader.h" |
17 #include "net/base/upload_data_stream.h" | 21 #include "net/base/upload_data_stream.h" |
18 #include "net/base/upload_file_element_reader.h" | 22 #include "net/base/upload_file_element_reader.h" |
19 #include "storage/browser/blob/blob_data_builder.h" | 23 #include "storage/browser/blob/blob_data_builder.h" |
20 #include "storage/browser/blob/blob_storage_context.h" | 24 #include "storage/browser/blob/blob_storage_context.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "url/gurl.h" | 26 #include "url/gurl.h" |
23 | 27 |
24 using storage::BlobDataBuilder; | 28 using storage::BlobDataBuilder; |
25 using storage::BlobDataHandle; | 29 using storage::BlobDataHandle; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 // Setup blob data for testing. | 105 // Setup blob data for testing. |
102 base::Time time1, time2; | 106 base::Time time1, time2; |
103 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); | 107 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); |
104 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2); | 108 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2); |
105 | 109 |
106 BlobStorageContext blob_storage_context; | 110 BlobStorageContext blob_storage_context; |
107 | 111 |
108 const std::string blob_id0("id-0"); | 112 const std::string blob_id0("id-0"); |
109 scoped_ptr<BlobDataBuilder> blob_data_builder( | 113 scoped_ptr<BlobDataBuilder> blob_data_builder( |
110 new BlobDataBuilder(blob_id0)); | 114 new BlobDataBuilder(blob_id0)); |
111 scoped_ptr<BlobDataHandle> handle1 = | |
112 blob_storage_context.AddFinishedBlob(blob_data_builder.get()); | |
michaeln
2015/03/11 00:10:25
looks like this handle in the existing test actual
| |
113 | 115 |
114 const std::string blob_id1("id-1"); | 116 const std::string blob_id1("id-1"); |
115 const std::string kBlobData = "BlobData"; | 117 const std::string kBlobData = "BlobData"; |
116 blob_data_builder.reset(new BlobDataBuilder(blob_id1)); | 118 blob_data_builder.reset(new BlobDataBuilder(blob_id1)); |
117 blob_data_builder->AppendData(kBlobData); | 119 blob_data_builder->AppendData(kBlobData); |
118 blob_data_builder->AppendFile( | 120 blob_data_builder->AppendFile( |
119 base::FilePath(FILE_PATH_LITERAL("BlobFile.txt")), 0, 20, time1); | 121 base::FilePath(FILE_PATH_LITERAL("BlobFile.txt")), 0, 20, time1); |
120 scoped_ptr<BlobDataHandle> handle2 = | 122 scoped_ptr<BlobDataHandle> handle2 = |
121 blob_storage_context.AddFinishedBlob(blob_data_builder.get()); | 123 blob_storage_context.AddFinishedBlob(blob_data_builder.get()); |
122 | 124 |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 *(*upload->GetElementReaders())[5], blob_element1)); | 305 *(*upload->GetElementReaders())[5], blob_element1)); |
304 EXPECT_TRUE(AreElementsEqual( | 306 EXPECT_TRUE(AreElementsEqual( |
305 *(*upload->GetElementReaders())[6], blob_element2)); | 307 *(*upload->GetElementReaders())[6], blob_element2)); |
306 EXPECT_TRUE(AreElementsEqual( | 308 EXPECT_TRUE(AreElementsEqual( |
307 *(*upload->GetElementReaders())[7], upload_element2)); | 309 *(*upload->GetElementReaders())[7], upload_element2)); |
308 } | 310 } |
309 // Clean up for ASAN. | 311 // Clean up for ASAN. |
310 base::RunLoop().RunUntilIdle(); | 312 base::RunLoop().RunUntilIdle(); |
311 } | 313 } |
312 | 314 |
315 TEST(UploadDataStreamBuilderTest, | |
316 WriteUploadDataStreamWithEmptyFileBackedBlob) { | |
317 base::MessageLoop message_loop; | |
318 { | |
319 base::FilePath test_blob_path; | |
320 ASSERT_TRUE(base::CreateTemporaryFile(&test_blob_path)); | |
321 | |
322 const uint64_t kZeroLength = 0; | |
323 base::Time blob_time; | |
324 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &blob_time); | |
325 ASSERT_TRUE(base::TouchFile(test_blob_path, blob_time, blob_time)); | |
326 | |
327 BlobStorageContext blob_storage_context; | |
328 | |
329 // First an empty blob | |
330 scoped_ptr<BlobDataBuilder> blob_data_builder(new BlobDataBuilder("id-0")); | |
331 scoped_ptr<BlobDataHandle> handle = | |
332 blob_storage_context.AddFinishedBlob(blob_data_builder.get()); | |
333 | |
334 // Then a blob created from an empty file added several times. | |
335 const std::string blob_id1("id-1"); | |
336 blob_data_builder.reset(new BlobDataBuilder(blob_id1)); | |
337 blob_data_builder->AppendFile(test_blob_path, 0, kZeroLength, blob_time); | |
338 scoped_ptr<BlobDataHandle> handle2 = | |
339 blob_storage_context.AddFinishedBlob(blob_data_builder.get()); | |
340 | |
341 ResourceRequestBody::Element blob_element; | |
342 blob_element.SetToFilePathRange(test_blob_path, 0, kZeroLength, blob_time); | |
343 | |
344 scoped_refptr<ResourceRequestBody> request_body(new ResourceRequestBody()); | |
345 scoped_ptr<net::UploadDataStream> upload(UploadDataStreamBuilder::Build( | |
346 request_body.get(), &blob_storage_context, NULL, | |
347 base::MessageLoopProxy::current().get())); | |
348 | |
349 request_body = new ResourceRequestBody(); | |
350 request_body->AppendBlob(blob_id1); | |
351 request_body->AppendBlob(blob_id1); | |
352 request_body->AppendBlob(blob_id1); | |
353 | |
354 upload = UploadDataStreamBuilder::Build( | |
355 request_body.get(), &blob_storage_context, NULL, | |
356 base::MessageLoopProxy::current().get()); | |
357 ASSERT_TRUE(upload->GetElementReaders()); | |
358 const auto& readers = *upload->GetElementReaders(); | |
359 ASSERT_EQ(3U, readers.size()); | |
360 EXPECT_TRUE(AreElementsEqual(*readers[0], blob_element)); | |
361 EXPECT_TRUE(AreElementsEqual(*readers[1], blob_element)); | |
362 EXPECT_TRUE(AreElementsEqual(*readers[2], blob_element)); | |
363 | |
364 net::TestCompletionCallback init_callback; | |
365 ASSERT_EQ(net::ERR_IO_PENDING, upload->Init(init_callback.callback())); | |
366 EXPECT_EQ(net::OK, init_callback.WaitForResult()); | |
367 | |
368 EXPECT_EQ(kZeroLength, upload->size()); | |
369 | |
370 // Purposely (try to) read more than what is in the stream. If we try to | |
371 // read zero bytes then UploadDataStream::Read will fail a DCHECK. | |
372 int kBufferLength = kZeroLength + 1; | |
373 scoped_ptr<char[]> buffer(new char[kBufferLength]); | |
374 scoped_refptr<net::IOBuffer> io_buffer = | |
375 new net::WrappedIOBuffer(buffer.get()); | |
376 net::TestCompletionCallback read_callback; | |
377 int result = | |
378 upload->Read(io_buffer.get(), kBufferLength, read_callback.callback()); | |
379 EXPECT_EQ(static_cast<int>(kZeroLength), read_callback.GetResult(result)); | |
380 | |
381 base::DeleteFile(test_blob_path, false); | |
382 } | |
383 // Clean up for ASAN. | |
384 base::RunLoop().RunUntilIdle(); | |
385 } | |
313 } // namespace content | 386 } // namespace content |
OLD | NEW |