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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
// Copyright (c) 2011 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 "base/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/time.h"
#include "net/base/upload_data.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/blob/blob_data.h"
#include "webkit/blob/blob_storage_controller.h"
using net::UploadData;
namespace webkit_blob {
TEST(BlobStorageControllerTest, RegisterBlobUrl) {
// Setup a set of blob data for testing.
base::Time time1, time2;
base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1);
base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2);
scoped_refptr<BlobData> blob_data1(new BlobData());
blob_data1->AppendData("Data1");
blob_data1->AppendData("Data2");
blob_data1->AppendFile(FilePath(FILE_PATH_LITERAL("File1.txt")),
10, 1024, time1);
scoped_refptr<BlobData> blob_data2(new BlobData());
blob_data2->AppendData("Data3");
blob_data2->AppendBlob(GURL("blob://url_1"), 8, 100);
blob_data2->AppendFile(FilePath(FILE_PATH_LITERAL("File2.txt")),
0, 20, time2);
scoped_refptr<BlobData> canonicalized_blob_data2(new BlobData());
canonicalized_blob_data2->AppendData("Data3");
canonicalized_blob_data2->AppendData("a2___", 2);
canonicalized_blob_data2->AppendFile(FilePath(FILE_PATH_LITERAL("File1.txt")),
10, 98, time1);
canonicalized_blob_data2->AppendFile(FilePath(FILE_PATH_LITERAL("File2.txt")),
0, 20, time2);
BlobStorageController blob_storage_controller;
// Test registering a blob URL referring to the blob data containing only
// data and file.
GURL blob_url1("blob://url_1");
blob_storage_controller.AddFinishedBlob(blob_url1, blob_data1);
BlobData* blob_data_found =
blob_storage_controller.GetBlobDataFromUrl(blob_url1);
ASSERT_TRUE(blob_data_found != NULL);
EXPECT_TRUE(*blob_data_found == *blob_data1);
// Test registering a blob URL referring to the blob data containing data,
// file and blob.
GURL blob_url2("blob://url_2");
blob_storage_controller.AddFinishedBlob(blob_url2, blob_data2);
blob_data_found = blob_storage_controller.GetBlobDataFromUrl(blob_url2);
ASSERT_TRUE(blob_data_found != NULL);
EXPECT_TRUE(*blob_data_found == *canonicalized_blob_data2);
// Test registering a blob URL referring to existent blob URL.
GURL blob_url3("blob://url_3");
blob_storage_controller.CloneBlob(blob_url3, blob_url1);
blob_data_found = blob_storage_controller.GetBlobDataFromUrl(blob_url3);
ASSERT_TRUE(blob_data_found != NULL);
EXPECT_TRUE(*blob_data_found == *blob_data1);
// Test unregistering a blob URL.
blob_storage_controller.RemoveBlob(blob_url3);
blob_data_found = blob_storage_controller.GetBlobDataFromUrl(blob_url3);
EXPECT_TRUE(!blob_data_found);
}
TEST(BlobStorageControllerTest, ResolveBlobReferencesInUploadData) {
// Setup blob data for testing.
base::Time time1, time2;
base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1);
base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2);
BlobStorageController blob_storage_controller;
scoped_refptr<BlobData> blob_data(new BlobData());
GURL blob_url0("blob://url_0");
blob_storage_controller.AddFinishedBlob(blob_url0, blob_data);
blob_data->AppendData("BlobData");
blob_data->AppendFile(
FilePath(FILE_PATH_LITERAL("BlobFile.txt")), 0, 20, time1);
GURL blob_url1("blob://url_1");
blob_storage_controller.AddFinishedBlob(blob_url1, blob_data);
GURL blob_url2("blob://url_2");
blob_storage_controller.CloneBlob(blob_url2, blob_url1);
GURL blob_url3("blob://url_3");
blob_storage_controller.CloneBlob(blob_url3, blob_url2);
// Setup upload data elements for comparison.
UploadData::Element blob_element1, blob_element2;
blob_element1.SetToBytes(
blob_data->items().at(0).data.c_str() +
static_cast<int>(blob_data->items().at(0).offset),
static_cast<int>(blob_data->items().at(0).length));
blob_element2.SetToFilePathRange(
blob_data->items().at(1).file_path,
blob_data->items().at(1).offset,
blob_data->items().at(1).length,
blob_data->items().at(1).expected_modification_time);
UploadData::Element upload_element1, upload_element2;
upload_element1.SetToBytes("Hello", 5);
upload_element2.SetToFilePathRange(
FilePath(FILE_PATH_LITERAL("foo1.txt")), 0, 20, time2);
// Test no blob reference.
scoped_refptr<UploadData> upload_data(new UploadData());
upload_data->AppendBytes(
&upload_element1.bytes().at(0),
upload_element1.bytes().size());
upload_data->AppendFileRange(
upload_element2.file_path(),
upload_element2.file_range_offset(),
upload_element2.file_range_length(),
upload_element2.expected_file_modification_time());
blob_storage_controller.ResolveBlobReferencesInUploadData(upload_data.get());
ASSERT_EQ(upload_data->elements()->size(), 2U);
EXPECT_TRUE(upload_data->elements()->at(0) == upload_element1);
EXPECT_TRUE(upload_data->elements()->at(1) == upload_element2);
// Test having only one blob reference that refers to empty blob data.
upload_data = new UploadData();
upload_data->AppendBlob(blob_url0);
blob_storage_controller.ResolveBlobReferencesInUploadData(upload_data.get());
ASSERT_EQ(upload_data->elements()->size(), 0U);
// Test having only one blob reference.
upload_data = new UploadData();
upload_data->AppendBlob(blob_url1);
blob_storage_controller.ResolveBlobReferencesInUploadData(upload_data.get());
ASSERT_EQ(upload_data->elements()->size(), 2U);
EXPECT_TRUE(upload_data->elements()->at(0) == blob_element1);
EXPECT_TRUE(upload_data->elements()->at(1) == blob_element2);
// Test having one blob reference at the beginning.
upload_data = new UploadData();
upload_data->AppendBlob(blob_url1);
upload_data->AppendBytes(
&upload_element1.bytes().at(0),
upload_element1.bytes().size());
upload_data->AppendFileRange(
upload_element2.file_path(),
upload_element2.file_range_offset(),
upload_element2.file_range_length(),
upload_element2.expected_file_modification_time());
blob_storage_controller.ResolveBlobReferencesInUploadData(upload_data.get());
ASSERT_EQ(upload_data->elements()->size(), 4U);
EXPECT_TRUE(upload_data->elements()->at(0) == blob_element1);
EXPECT_TRUE(upload_data->elements()->at(1) == blob_element2);
EXPECT_TRUE(upload_data->elements()->at(2) == upload_element1);
EXPECT_TRUE(upload_data->elements()->at(3) == upload_element2);
// Test having one blob reference at the end.
upload_data = new UploadData();
upload_data->AppendBytes(
&upload_element1.bytes().at(0),
upload_element1.bytes().size());
upload_data->AppendFileRange(
upload_element2.file_path(),
upload_element2.file_range_offset(),
upload_element2.file_range_length(),
upload_element2.expected_file_modification_time());
upload_data->AppendBlob(blob_url1);
blob_storage_controller.ResolveBlobReferencesInUploadData(upload_data.get());
ASSERT_EQ(upload_data->elements()->size(), 4U);
EXPECT_TRUE(upload_data->elements()->at(0) == upload_element1);
EXPECT_TRUE(upload_data->elements()->at(1) == upload_element2);
EXPECT_TRUE(upload_data->elements()->at(2) == blob_element1);
EXPECT_TRUE(upload_data->elements()->at(3) == blob_element2);
// Test having one blob reference in the middle.
upload_data = new UploadData();
upload_data->AppendBytes(
&upload_element1.bytes().at(0),
upload_element1.bytes().size());
upload_data->AppendBlob(blob_url1);
upload_data->AppendFileRange(
upload_element2.file_path(),
upload_element2.file_range_offset(),
upload_element2.file_range_length(),
upload_element2.expected_file_modification_time());
blob_storage_controller.ResolveBlobReferencesInUploadData(upload_data.get());
ASSERT_EQ(upload_data->elements()->size(), 4U);
EXPECT_TRUE(upload_data->elements()->at(0) == upload_element1);
EXPECT_TRUE(upload_data->elements()->at(1) == blob_element1);
EXPECT_TRUE(upload_data->elements()->at(2) == blob_element2);
EXPECT_TRUE(upload_data->elements()->at(3) == upload_element2);
// Test having multiple blob references.
upload_data = new UploadData();
upload_data->AppendBlob(blob_url1);
upload_data->AppendBytes(
&upload_element1.bytes().at(0),
upload_element1.bytes().size());
upload_data->AppendBlob(blob_url2);
upload_data->AppendBlob(blob_url3);
upload_data->AppendFileRange(
upload_element2.file_path(),
upload_element2.file_range_offset(),
upload_element2.file_range_length(),
upload_element2.expected_file_modification_time());
blob_storage_controller.ResolveBlobReferencesInUploadData(upload_data.get());
ASSERT_EQ(upload_data->elements()->size(), 8U);
EXPECT_TRUE(upload_data->elements()->at(0) == blob_element1);
EXPECT_TRUE(upload_data->elements()->at(1) == blob_element2);
EXPECT_TRUE(upload_data->elements()->at(2) == upload_element1);
EXPECT_TRUE(upload_data->elements()->at(3) == blob_element1);
EXPECT_TRUE(upload_data->elements()->at(4) == blob_element2);
EXPECT_TRUE(upload_data->elements()->at(5) == blob_element1);
EXPECT_TRUE(upload_data->elements()->at(6) == blob_element2);
EXPECT_TRUE(upload_data->elements()->at(7) == upload_element2);
}
} // namespace webkit_blob
|