Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(943)

Side by Side Diff: content/browser/loader/upload_data_stream_builder_unittest.cc

Issue 942633004: IndexedDB: Fixed support for empty blobs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Early return in BlobDataBuilder::AppendData Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 *(*upload->GetElementReaders())[5], blob_element1)); 307 *(*upload->GetElementReaders())[5], blob_element1));
304 EXPECT_TRUE(AreElementsEqual( 308 EXPECT_TRUE(AreElementsEqual(
305 *(*upload->GetElementReaders())[6], blob_element2)); 309 *(*upload->GetElementReaders())[6], blob_element2));
306 EXPECT_TRUE(AreElementsEqual( 310 EXPECT_TRUE(AreElementsEqual(
307 *(*upload->GetElementReaders())[7], upload_element2)); 311 *(*upload->GetElementReaders())[7], upload_element2));
308 } 312 }
309 // Clean up for ASAN. 313 // Clean up for ASAN.
310 base::RunLoop().RunUntilIdle(); 314 base::RunLoop().RunUntilIdle();
311 } 315 }
312 316
317 TEST(UploadDataStreamBuilderTest,
318 WriteUploadDataStreamWithEmptyFileBackedBlob) {
319 base::MessageLoop message_loop;
320 {
321 base::FilePath test_blob_path;
322 ASSERT_TRUE(base::CreateTemporaryFile(&test_blob_path));
323
324 const uint64_t kZeroLength = 0;
325 base::Time blob_time;
326 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &blob_time);
327 ASSERT_TRUE(base::TouchFile(test_blob_path, blob_time, blob_time));
328
329 BlobStorageContext blob_storage_context;
330
331 // First an empty blob
332 const std::string blob_id0("id-0");
333 scoped_ptr<BlobDataBuilder> blob_data_builder(
334 new BlobDataBuilder(blob_id0));
335 scoped_ptr<BlobDataHandle> handle1 =
michaeln 2015/03/09 22:34:06 for clarity, i'd make the id and handle vars use t
cmumford 2015/03/09 23:00:10 Done. I "cargo culted" that from the test above (R
michaeln 2015/03/11 00:10:25 But the block about "// First an empty blob" is st
336 blob_storage_context.AddFinishedBlob(blob_data_builder.get());
337
338 // Then a blob created from an empty file added several times.
339 const std::string blob_id1("id-1");
340 blob_data_builder.reset(new BlobDataBuilder(blob_id1));
341 blob_data_builder->AppendFile(test_blob_path, 0, kZeroLength, blob_time);
342 scoped_ptr<BlobDataHandle> handle2 =
343 blob_storage_context.AddFinishedBlob(blob_data_builder.get());
344
345 ResourceRequestBody::Element blob_element;
346 blob_element.SetToFilePathRange(test_blob_path, 0, kZeroLength, blob_time);
347
348 scoped_refptr<ResourceRequestBody> request_body(new ResourceRequestBody());
349 scoped_ptr<net::UploadDataStream> upload(UploadDataStreamBuilder::Build(
350 request_body.get(), &blob_storage_context, NULL,
351 base::MessageLoopProxy::current().get()));
352
353 request_body = new ResourceRequestBody();
354 request_body->AppendBlob(blob_id1);
355 request_body->AppendBlob(blob_id1);
356 request_body->AppendBlob(blob_id1);
357
358 upload = UploadDataStreamBuilder::Build(
359 request_body.get(), &blob_storage_context, NULL,
360 base::MessageLoopProxy::current().get());
361 ASSERT_TRUE(upload->GetElementReaders());
362 const auto& readers = *upload->GetElementReaders();
363 ASSERT_EQ(3U, readers.size());
364 EXPECT_TRUE(AreElementsEqual(*readers[0], blob_element));
365 EXPECT_TRUE(AreElementsEqual(*readers[1], blob_element));
366 EXPECT_TRUE(AreElementsEqual(*readers[2], blob_element));
367
368 net::TestCompletionCallback init_callback;
369 ASSERT_EQ(net::ERR_IO_PENDING, upload->Init(init_callback.callback()));
370 EXPECT_EQ(net::OK, init_callback.WaitForResult());
371
372 const size_t kExpectedStreamLength = 3U * kZeroLength;
michaeln 2015/03/09 22:34:06 nit: maybe use the zero constant here and below
373 EXPECT_EQ(kExpectedStreamLength, upload->size());
374
375 // Purposely (try to) read more than what is in the stream. If we try to
376 // read zero bytes then UploadDataStream::Read will fail a DCHECK.
377 int kAttemptedReadLength = kExpectedStreamLength + 1;
michaeln 2015/03/09 22:34:06 nit: kBufferLength or kBufferSize is more typical
378 scoped_ptr<char[]> buffer(new char[kAttemptedReadLength]);
379 scoped_refptr<net::IOBuffer> write_buff =
michaeln 2015/03/09 22:34:06 nit: read_buf or io_buffer?
cmumford 2015/03/09 23:00:10 Well (if I'm not mistaken). Read is reading from t
380 new net::WrappedIOBuffer(buffer.get());
381 net::TestCompletionCallback read_callback;
382 int result = upload->Read(write_buff.get(), kAttemptedReadLength,
383 read_callback.callback());
384 EXPECT_EQ(static_cast<int>(kExpectedStreamLength),
385 read_callback.GetResult(result));
386
387 base::DeleteFile(test_blob_path, false);
388 }
389 // Clean up for ASAN.
390 base::RunLoop().RunUntilIdle();
391 }
313 } // namespace content 392 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698