OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/glue/resource_request_body.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/file_util.h" | |
10 #include "base/files/file_path.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/message_loop/message_loop_proxy.h" | |
13 #include "base/time.h" | |
14 #include "googleurl/src/gurl.h" | |
15 #include "net/base/upload_bytes_element_reader.h" | |
16 #include "net/base/upload_data_stream.h" | |
17 #include "net/base/upload_file_element_reader.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 #include "webkit/browser/blob/blob_storage_controller.h" | |
20 | |
21 using webkit_blob::BlobData; | |
22 using webkit_blob::BlobStorageController; | |
23 | |
24 namespace webkit_glue { | |
25 | |
26 namespace { | |
27 | |
28 bool AreElementsEqual(const net::UploadElementReader& reader, | |
29 const ResourceRequestBody::Element& element) { | |
30 switch(element.type()) { | |
31 case ResourceRequestBody::Element::TYPE_BYTES: { | |
32 const net::UploadBytesElementReader* bytes_reader = | |
33 reader.AsBytesReader(); | |
34 return bytes_reader && | |
35 element.length() == bytes_reader->length() && | |
36 std::equal(element.bytes(), element.bytes() + element.length(), | |
37 bytes_reader->bytes()); | |
38 } | |
39 case ResourceRequestBody::Element::TYPE_FILE: { | |
40 const net::UploadFileElementReader* file_reader = reader.AsFileReader(); | |
41 return file_reader && | |
42 file_reader->path() == element.path() && | |
43 file_reader->range_offset() == element.offset() && | |
44 file_reader->range_length() == element.length() && | |
45 file_reader->expected_modification_time() == | |
46 element.expected_modification_time(); | |
47 break; | |
48 } | |
49 default: | |
50 NOTREACHED(); | |
51 } | |
52 return false; | |
53 } | |
54 | |
55 } // namespace | |
56 | |
57 TEST(ResourceRequestBodyTest, CreateUploadDataStreamWithoutBlob) { | |
58 base::MessageLoop message_loop; | |
59 scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody; | |
60 | |
61 const char kData[] = "123"; | |
62 const base::FilePath::StringType kFilePath = FILE_PATH_LITERAL("abc"); | |
63 const uint64 kFileOffset = 10U; | |
64 const uint64 kFileLength = 100U; | |
65 const base::Time kFileTime = base::Time::FromDoubleT(999); | |
66 const int64 kIdentifier = 12345; | |
67 | |
68 request_body->AppendBytes(kData, arraysize(kData) - 1); | |
69 request_body->AppendFileRange(base::FilePath(kFilePath), | |
70 kFileOffset, kFileLength, kFileTime); | |
71 request_body->set_identifier(kIdentifier); | |
72 | |
73 scoped_ptr<net::UploadDataStream> upload( | |
74 request_body->ResolveElementsAndCreateUploadDataStream( | |
75 NULL, NULL, base::MessageLoopProxy::current().get())); | |
76 | |
77 EXPECT_EQ(kIdentifier, upload->identifier()); | |
78 ASSERT_EQ(request_body->elements()->size(), upload->element_readers().size()); | |
79 | |
80 const net::UploadBytesElementReader* r1 = | |
81 upload->element_readers()[0]->AsBytesReader(); | |
82 ASSERT_TRUE(r1); | |
83 EXPECT_EQ(kData, std::string(r1->bytes(), r1->length())); | |
84 | |
85 const net::UploadFileElementReader* r2 = | |
86 upload->element_readers()[1]->AsFileReader(); | |
87 ASSERT_TRUE(r2); | |
88 EXPECT_EQ(kFilePath, r2->path().value()); | |
89 EXPECT_EQ(kFileOffset, r2->range_offset()); | |
90 EXPECT_EQ(kFileLength, r2->range_length()); | |
91 EXPECT_EQ(kFileTime, r2->expected_modification_time()); | |
92 } | |
93 | |
94 TEST(ResourceRequestBodyTest, ResolveBlobAndCreateUploadDataStream) { | |
95 base::MessageLoop message_loop; | |
96 // Setup blob data for testing. | |
97 base::Time time1, time2; | |
98 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); | |
99 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2); | |
100 | |
101 BlobStorageController blob_storage_controller; | |
102 scoped_refptr<BlobData> blob_data(new BlobData()); | |
103 | |
104 GURL blob_url0("blob://url_0"); | |
105 blob_storage_controller.AddFinishedBlob(blob_url0, blob_data.get()); | |
106 | |
107 blob_data->AppendData("BlobData"); | |
108 blob_data->AppendFile( | |
109 base::FilePath(FILE_PATH_LITERAL("BlobFile.txt")), 0, 20, time1); | |
110 | |
111 GURL blob_url1("blob://url_1"); | |
112 blob_storage_controller.AddFinishedBlob(blob_url1, blob_data.get()); | |
113 | |
114 GURL blob_url2("blob://url_2"); | |
115 blob_storage_controller.CloneBlob(blob_url2, blob_url1); | |
116 | |
117 GURL blob_url3("blob://url_3"); | |
118 blob_storage_controller.CloneBlob(blob_url3, blob_url2); | |
119 | |
120 // Setup upload data elements for comparison. | |
121 ResourceRequestBody::Element blob_element1, blob_element2; | |
122 blob_element1.SetToBytes( | |
123 blob_data->items().at(0).bytes() + | |
124 static_cast<int>(blob_data->items().at(0).offset()), | |
125 static_cast<int>(blob_data->items().at(0).length())); | |
126 blob_element2.SetToFilePathRange( | |
127 blob_data->items().at(1).path(), | |
128 blob_data->items().at(1).offset(), | |
129 blob_data->items().at(1).length(), | |
130 blob_data->items().at(1).expected_modification_time()); | |
131 | |
132 ResourceRequestBody::Element upload_element1, upload_element2; | |
133 upload_element1.SetToBytes("Hello", 5); | |
134 upload_element2.SetToFilePathRange( | |
135 base::FilePath(FILE_PATH_LITERAL("foo1.txt")), 0, 20, time2); | |
136 | |
137 // Test no blob reference. | |
138 scoped_refptr<ResourceRequestBody> request_body(new ResourceRequestBody()); | |
139 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); | |
140 request_body->AppendFileRange(upload_element2.path(), | |
141 upload_element2.offset(), | |
142 upload_element2.length(), | |
143 upload_element2.expected_modification_time()); | |
144 | |
145 scoped_ptr<net::UploadDataStream> upload( | |
146 request_body->ResolveElementsAndCreateUploadDataStream( | |
147 &blob_storage_controller, | |
148 NULL, | |
149 base::MessageLoopProxy::current().get())); | |
150 | |
151 ASSERT_EQ(2U, upload->element_readers().size()); | |
152 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], upload_element1)); | |
153 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], upload_element2)); | |
154 | |
155 // Test having only one blob reference that refers to empty blob data. | |
156 request_body = new ResourceRequestBody(); | |
157 request_body->AppendBlob(blob_url0); | |
158 | |
159 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | |
160 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | |
161 ASSERT_EQ(0U, upload->element_readers().size()); | |
162 | |
163 // Test having only one blob reference. | |
164 request_body = new ResourceRequestBody(); | |
165 request_body->AppendBlob(blob_url1); | |
166 | |
167 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | |
168 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | |
169 ASSERT_EQ(2U, upload->element_readers().size()); | |
170 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], blob_element1)); | |
171 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element2)); | |
172 | |
173 // Test having one blob reference at the beginning. | |
174 request_body = new ResourceRequestBody(); | |
175 request_body->AppendBlob(blob_url1); | |
176 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); | |
177 request_body->AppendFileRange(upload_element2.path(), | |
178 upload_element2.offset(), | |
179 upload_element2.length(), | |
180 upload_element2.expected_modification_time()); | |
181 | |
182 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | |
183 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | |
184 ASSERT_EQ(4U, upload->element_readers().size()); | |
185 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], blob_element1)); | |
186 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element2)); | |
187 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], upload_element1)); | |
188 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], upload_element2)); | |
189 | |
190 // Test having one blob reference at the end. | |
191 request_body = new ResourceRequestBody(); | |
192 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); | |
193 request_body->AppendFileRange(upload_element2.path(), | |
194 upload_element2.offset(), | |
195 upload_element2.length(), | |
196 upload_element2.expected_modification_time()); | |
197 request_body->AppendBlob(blob_url1); | |
198 | |
199 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | |
200 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | |
201 ASSERT_EQ(4U, upload->element_readers().size()); | |
202 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], upload_element1)); | |
203 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], upload_element2)); | |
204 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], blob_element1)); | |
205 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], blob_element2)); | |
206 | |
207 // Test having one blob reference in the middle. | |
208 request_body = new ResourceRequestBody(); | |
209 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); | |
210 request_body->AppendBlob(blob_url1); | |
211 request_body->AppendFileRange(upload_element2.path(), | |
212 upload_element2.offset(), | |
213 upload_element2.length(), | |
214 upload_element2.expected_modification_time()); | |
215 | |
216 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | |
217 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | |
218 ASSERT_EQ(4U, upload->element_readers().size()); | |
219 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], upload_element1)); | |
220 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element1)); | |
221 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], blob_element2)); | |
222 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], upload_element2)); | |
223 | |
224 // Test having multiple blob references. | |
225 request_body = new ResourceRequestBody(); | |
226 request_body->AppendBlob(blob_url1); | |
227 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); | |
228 request_body->AppendBlob(blob_url2); | |
229 request_body->AppendBlob(blob_url3); | |
230 request_body->AppendFileRange(upload_element2.path(), | |
231 upload_element2.offset(), | |
232 upload_element2.length(), | |
233 upload_element2.expected_modification_time()); | |
234 | |
235 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | |
236 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | |
237 ASSERT_EQ(8U, upload->element_readers().size()); | |
238 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], blob_element1)); | |
239 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element2)); | |
240 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], upload_element1)); | |
241 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], blob_element1)); | |
242 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[4], blob_element2)); | |
243 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[5], blob_element1)); | |
244 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[6], blob_element2)); | |
245 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[7], upload_element2)); | |
246 } | |
247 | |
248 } // namespace webkit_glue | |
OLD | NEW |