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 |
28 using base::File; | |
24 using storage::BlobDataBuilder; | 29 using storage::BlobDataBuilder; |
25 using storage::BlobDataHandle; | 30 using storage::BlobDataHandle; |
26 using storage::BlobStorageContext; | 31 using storage::BlobStorageContext; |
27 | 32 |
28 namespace content { | 33 namespace content { |
29 namespace { | 34 namespace { |
30 | 35 |
31 bool AreElementsEqual(const net::UploadElementReader& reader, | 36 bool AreElementsEqual(const net::UploadElementReader& reader, |
32 const ResourceRequestBody::Element& element) { | 37 const ResourceRequestBody::Element& element) { |
33 switch(element.type()) { | 38 switch(element.type()) { |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 *(*upload->GetElementReaders())[5], blob_element1)); | 308 *(*upload->GetElementReaders())[5], blob_element1)); |
304 EXPECT_TRUE(AreElementsEqual( | 309 EXPECT_TRUE(AreElementsEqual( |
305 *(*upload->GetElementReaders())[6], blob_element2)); | 310 *(*upload->GetElementReaders())[6], blob_element2)); |
306 EXPECT_TRUE(AreElementsEqual( | 311 EXPECT_TRUE(AreElementsEqual( |
307 *(*upload->GetElementReaders())[7], upload_element2)); | 312 *(*upload->GetElementReaders())[7], upload_element2)); |
308 } | 313 } |
309 // Clean up for ASAN. | 314 // Clean up for ASAN. |
310 base::RunLoop().RunUntilIdle(); | 315 base::RunLoop().RunUntilIdle(); |
311 } | 316 } |
312 | 317 |
318 TEST(UploadDataStreamBuilderTest, | |
319 WriteUploadDataStreamWithEmptyFileBackedBlob) { | |
320 base::MessageLoop message_loop; | |
321 { | |
322 base::Time blob_time; | |
323 base::FilePath test_blob_path; | |
324 ASSERT_TRUE(base::CreateTemporaryFile(&test_blob_path)); | |
325 | |
326 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
| |
327 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.
| |
328 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.
| |
329 File f(test_blob_path, File::FLAG_OPEN | File::FLAG_WRITE); | |
330 ASSERT_EQ(static_cast<int>(kBlobLength), | |
331 f.WriteAtCurrentPos(kBlobFileData.c_str(), kBlobLength)); | |
332 base::File::Info info; | |
333 ASSERT_TRUE(f.GetInfo(&info)); | |
334 blob_time = info.last_modified; | |
335 } else { | |
336 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &blob_time); | |
337 ASSERT_TRUE(base::TouchFile(test_blob_path, blob_time, blob_time)); | |
338 } | |
339 | |
340 BlobStorageContext blob_storage_context; | |
341 | |
342 // First an empty blob | |
343 const std::string blob_id0("id-0"); | |
344 scoped_ptr<BlobDataBuilder> blob_data_builder( | |
345 new BlobDataBuilder(blob_id0)); | |
346 scoped_ptr<BlobDataHandle> handle1 = | |
347 blob_storage_context.AddFinishedBlob(blob_data_builder.get()); | |
348 | |
349 // Then a blob created from an empty file added several times. | |
350 const std::string blob_id1("id-1"); | |
351 blob_data_builder.reset(new BlobDataBuilder(blob_id1)); | |
352 blob_data_builder->AppendFile(test_blob_path, 0, kBlobLength, blob_time); | |
353 scoped_ptr<BlobDataHandle> handle2 = | |
354 blob_storage_context.AddFinishedBlob(blob_data_builder.get()); | |
355 | |
356 ResourceRequestBody::Element blob_element; | |
357 blob_element.SetToFilePathRange(test_blob_path, 0, kBlobLength, blob_time); | |
358 | |
359 scoped_refptr<ResourceRequestBody> request_body(new ResourceRequestBody()); | |
360 scoped_ptr<net::UploadDataStream> upload(UploadDataStreamBuilder::Build( | |
361 request_body.get(), &blob_storage_context, NULL, | |
362 base::MessageLoopProxy::current().get())); | |
363 | |
364 request_body = new ResourceRequestBody(); | |
365 request_body->AppendBlob(blob_id1); | |
366 request_body->AppendBlob(blob_id1); | |
367 request_body->AppendBlob(blob_id1); | |
368 | |
369 upload = UploadDataStreamBuilder::Build( | |
370 request_body.get(), &blob_storage_context, NULL, | |
371 base::MessageLoopProxy::current().get()); | |
372 ASSERT_TRUE(upload->GetElementReaders()); | |
373 const auto& readers = *upload->GetElementReaders(); | |
374 ASSERT_EQ(3U, readers.size()); | |
375 EXPECT_TRUE(AreElementsEqual(*readers[0], blob_element)); | |
376 EXPECT_TRUE(AreElementsEqual(*readers[1], blob_element)); | |
377 EXPECT_TRUE(AreElementsEqual(*readers[2], blob_element)); | |
378 | |
379 net::TestCompletionCallback init_callback; | |
380 ASSERT_EQ(net::ERR_IO_PENDING, upload->Init(init_callback.callback())); | |
381 EXPECT_EQ(net::OK, init_callback.WaitForResult()); | |
382 | |
383 const size_t kExpectedStreamLength = 3U * kBlobLength; | |
384 EXPECT_EQ(kExpectedStreamLength, upload->size()); | |
385 | |
386 scoped_ptr<char[]> buffer(new char[kExpectedStreamLength]); | |
387 scoped_refptr<net::IOBuffer> write_buff = | |
388 new net::WrappedIOBuffer(buffer.get()); | |
389 net::TestCompletionCallback read_callback; | |
390 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.
| |
391 read_callback.callback()); | |
392 EXPECT_EQ(static_cast<int>(kExpectedStreamLength), | |
393 read_callback.GetResult(result)); | |
394 | |
395 base::DeleteFile(test_blob_path, false); | |
396 } | |
397 // Clean up for ASAN. | |
398 base::RunLoop().RunUntilIdle(); | |
399 } | |
313 } // namespace content | 400 } // namespace content |
OLD | NEW |