1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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"
#include "base/run_loop.h"
#include "base/thread_task_runner_handle.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"
#include "storage/browser/blob/blob_data_builder.h"
#include "storage/browser/blob/blob_data_handle.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "storage/browser/blob/upload_blob_element_reader.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using storage::BlobDataBuilder;
using storage::BlobDataHandle;
using storage::BlobStorageContext;
namespace content {
TEST(UploadDataStreamBuilderTest, CreateUploadDataStream) {
base::MessageLoop message_loop;
{
scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody;
const std::string kBlob = "blobuuid";
const std::string kBlobData = "blobdata";
const char kData[] = "123";
const base::FilePath::StringType kFilePath = FILE_PATH_LITERAL("abc");
const uint64 kFileOffset = 10U;
const uint64 kFileLength = 100U;
const base::Time kFileTime = base::Time::FromDoubleT(999);
const int64 kIdentifier = 12345;
BlobStorageContext context;
BlobDataBuilder builder(kBlob);
builder.AppendData(kBlobData);
scoped_ptr<BlobDataHandle> handle = context.AddFinishedBlob(&builder);
request_body->AppendBytes(kData, arraysize(kData) - 1);
request_body->AppendFileRange(base::FilePath(kFilePath), kFileOffset,
kFileLength, kFileTime);
request_body->AppendBlob(kBlob);
request_body->set_identifier(kIdentifier);
scoped_ptr<net::UploadDataStream> upload(UploadDataStreamBuilder::Build(
request_body.get(), &context, NULL,
base::ThreadTaskRunnerHandle::Get().get()));
EXPECT_EQ(kIdentifier, upload->identifier());
ASSERT_TRUE(upload->GetElementReaders());
ASSERT_EQ(request_body->elements()->size(),
upload->GetElementReaders()->size());
const net::UploadBytesElementReader* r1 =
(*upload->GetElementReaders())[0]->AsBytesReader();
ASSERT_TRUE(r1);
EXPECT_EQ(kData, std::string(r1->bytes(), r1->length()));
const net::UploadFileElementReader* r2 =
(*upload->GetElementReaders())[1]->AsFileReader();
ASSERT_TRUE(r2);
EXPECT_EQ(kFilePath, r2->path().value());
EXPECT_EQ(kFileOffset, r2->range_offset());
EXPECT_EQ(kFileLength, r2->range_length());
EXPECT_EQ(kFileTime, r2->expected_modification_time());
const storage::UploadBlobElementReader* r3 =
static_cast<storage::UploadBlobElementReader*>(
(*upload->GetElementReaders())[2].get());
ASSERT_TRUE(r3);
EXPECT_EQ("blobuuid", r3->uuid());
}
// Clean up for ASAN.
base::RunLoop().RunUntilIdle();
}
TEST(UploadDataStreamBuilderTest,
WriteUploadDataStreamWithEmptyFileBackedBlob) {
base::MessageLoopForIO message_loop;
{
base::FilePath test_blob_path;
ASSERT_TRUE(base::CreateTemporaryFile(&test_blob_path));
const uint64_t kZeroLength = 0;
base::Time blob_time;
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;
// A blob created from an empty file added several times.
const std::string blob_id("id-0");
scoped_ptr<BlobDataBuilder> blob_data_builder(new BlobDataBuilder(blob_id));
blob_data_builder->AppendFile(test_blob_path, 0, kZeroLength, blob_time);
scoped_ptr<BlobDataHandle> handle =
blob_storage_context.AddFinishedBlob(blob_data_builder.get());
scoped_refptr<ResourceRequestBody> request_body(new ResourceRequestBody());
scoped_ptr<net::UploadDataStream> upload(UploadDataStreamBuilder::Build(
request_body.get(), &blob_storage_context, NULL,
base::ThreadTaskRunnerHandle::Get().get()));
request_body = new ResourceRequestBody();
request_body->AppendBlob(blob_id);
request_body->AppendBlob(blob_id);
request_body->AppendBlob(blob_id);
upload = UploadDataStreamBuilder::Build(
request_body.get(), &blob_storage_context, NULL,
base::ThreadTaskRunnerHandle::Get().get());
ASSERT_TRUE(upload->GetElementReaders());
const auto& readers = *upload->GetElementReaders();
ASSERT_EQ(3U, readers.size());
net::TestCompletionCallback init_callback;
ASSERT_EQ(net::ERR_IO_PENDING, upload->Init(init_callback.callback()));
EXPECT_EQ(net::OK, init_callback.WaitForResult());
EXPECT_EQ(kZeroLength, upload->size());
// Purposely (try to) read more than what is in the stream. If we try to
// read zero bytes then UploadDataStream::Read will fail a DCHECK.
int kBufferLength = kZeroLength + 1;
scoped_ptr<char[]> buffer(new char[kBufferLength]);
scoped_refptr<net::IOBuffer> io_buffer =
new net::WrappedIOBuffer(buffer.get());
net::TestCompletionCallback read_callback;
int result =
upload->Read(io_buffer.get(), kBufferLength, read_callback.callback());
EXPECT_EQ(static_cast<int>(kZeroLength), read_callback.GetResult(result));
base::DeleteFile(test_blob_path, false);
}
// Clean up for ASAN.
base::RunLoop().RunUntilIdle();
}
TEST(UploadDataStreamBuilderTest, ResetUploadStreamWithBlob) {
base::MessageLoopForIO message_loop;
{
scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody;
const std::string kBlob = "blobuuid";
const std::string kBlobData = "blobdata";
const int kBlobDataLength = 8;
const int64 kIdentifier = 12345;
BlobStorageContext blob_storage_context;
BlobDataBuilder builder(kBlob);
builder.AppendData(kBlobData);
scoped_ptr<BlobDataHandle> handle =
blob_storage_context.AddFinishedBlob(&builder);
request_body->AppendBlob(kBlob);
request_body->set_identifier(kIdentifier);
scoped_ptr<net::UploadDataStream> upload(UploadDataStreamBuilder::Build(
request_body.get(), &blob_storage_context, nullptr,
base::ThreadTaskRunnerHandle::Get().get()));
net::TestCompletionCallback init_callback;
ASSERT_EQ(net::OK, upload->Init(init_callback.callback()));
// Read part of the data.
const int kBufferLength = 4;
scoped_refptr<net::IOBufferWithSize> buffer(
new net::IOBufferWithSize(kBufferLength));
net::TestCompletionCallback read_callback;
int result =
upload->Read(buffer.get(), buffer->size(), read_callback.callback());
EXPECT_EQ(kBufferLength, read_callback.GetResult(result));
EXPECT_EQ(0,
std::memcmp(kBlobData.c_str(), buffer->data(), buffer->size()));
// Reset.
ASSERT_EQ(net::OK, upload->Init(init_callback.callback()));
// Read all the data.
buffer = new net::IOBufferWithSize(kBlobDataLength);
result =
upload->Read(buffer.get(), buffer->size(), read_callback.callback());
EXPECT_EQ(kBlobDataLength, read_callback.GetResult(result));
EXPECT_EQ(0,
std::memcmp(kBlobData.c_str(), buffer->data(), buffer->size()));
}
// Clean up for ASAN.
base::RunLoop().RunUntilIdle();
}
} // namespace content
|