OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "content/browser/fileapi/blob_storage_host.h" | 11 #include "content/browser/fileapi/blob_storage_host.h" |
12 #include "storage/browser/blob/blob_data_handle.h" | 12 #include "storage/browser/blob/blob_data_handle.h" |
13 #include "storage/browser/blob/blob_storage_context.h" | 13 #include "storage/browser/blob/blob_storage_context.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
| 16 using storage::BlobDataBuilder; |
16 using storage::BlobDataHandle; | 17 using storage::BlobDataHandle; |
| 18 using storage::BlobDataSnapshot; |
| 19 using storage::BlobStorageContext; |
| 20 using storage::DataElement; |
17 | 21 |
18 namespace content { | 22 namespace content { |
19 | 23 |
20 namespace { | 24 namespace { |
21 void SetupBasicBlob(BlobStorageHost* host, const std::string& id) { | 25 void SetupBasicBlob(BlobStorageHost* host, const std::string& id) { |
22 EXPECT_TRUE(host->StartBuildingBlob(id)); | 26 EXPECT_TRUE(host->StartBuildingBlob(id)); |
23 BlobData::Item item; | 27 DataElement item; |
24 item.SetToBytes("1", 1); | 28 item.SetToBytes("1", 1); |
25 EXPECT_TRUE(host->AppendBlobDataItem(id, item)); | 29 EXPECT_TRUE(host->AppendBlobDataItem(id, item)); |
26 EXPECT_TRUE(host->FinishBuildingBlob(id, "text/plain")); | 30 EXPECT_TRUE(host->FinishBuildingBlob(id, "text/plain")); |
27 EXPECT_FALSE(host->StartBuildingBlob(id)); | 31 EXPECT_FALSE(host->StartBuildingBlob(id)); |
28 } | 32 } |
29 } // namespace | 33 } // namespace |
30 | 34 |
31 TEST(BlobStorageContextTest, IncrementDecrementRef) { | 35 TEST(BlobStorageContextTest, IncrementDecrementRef) { |
32 BlobStorageContext context; | 36 BlobStorageContext context; |
33 BlobStorageHost host(&context); | 37 BlobStorageHost host(&context); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 const std::string kId2("id2"); | 108 const std::string kId2("id2"); |
105 const std::string kId2Prime("id2.prime"); | 109 const std::string kId2Prime("id2.prime"); |
106 | 110 |
107 base::MessageLoop fake_io_message_loop; | 111 base::MessageLoop fake_io_message_loop; |
108 | 112 |
109 // Setup a set of blob data for testing. | 113 // Setup a set of blob data for testing. |
110 base::Time time1, time2; | 114 base::Time time1, time2; |
111 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); | 115 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); |
112 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2); | 116 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2); |
113 | 117 |
114 scoped_refptr<BlobData> blob_data1(new BlobData(kId1)); | 118 scoped_ptr<BlobDataBuilder> blob_data1(new BlobDataBuilder(kId1)); |
115 blob_data1->AppendData("Data1"); | 119 blob_data1->AppendData("Data1"); |
116 blob_data1->AppendData("Data2"); | 120 blob_data1->AppendData("Data2"); |
117 blob_data1->AppendFile(base::FilePath(FILE_PATH_LITERAL("File1.txt")), | 121 blob_data1->AppendFile(base::FilePath(FILE_PATH_LITERAL("File1.txt")), |
118 10, 1024, time1); | 122 10, 1024, time1); |
119 | 123 |
120 scoped_refptr<BlobData> blob_data2(new BlobData(kId2)); | 124 scoped_ptr<BlobDataBuilder> blob_data2(new BlobDataBuilder(kId2)); |
121 blob_data2->AppendData("Data3"); | 125 blob_data2->AppendData("Data3"); |
122 blob_data2->AppendBlob(kId1, 8, 100); | 126 blob_data2->AppendBlob(kId1, 8, 100); |
123 blob_data2->AppendFile(base::FilePath(FILE_PATH_LITERAL("File2.txt")), | 127 blob_data2->AppendFile(base::FilePath(FILE_PATH_LITERAL("File2.txt")), |
124 0, 20, time2); | 128 0, 20, time2); |
125 | 129 |
126 scoped_refptr<BlobData> canonicalized_blob_data2(new BlobData(kId2Prime)); | 130 scoped_ptr<BlobDataBuilder> canonicalized_blob_data2( |
| 131 new BlobDataBuilder(kId2Prime)); |
127 canonicalized_blob_data2->AppendData("Data3"); | 132 canonicalized_blob_data2->AppendData("Data3"); |
128 canonicalized_blob_data2->AppendData("a2___", 2); | 133 canonicalized_blob_data2->AppendData("a2___", 2); |
129 canonicalized_blob_data2->AppendFile( | 134 canonicalized_blob_data2->AppendFile( |
130 base::FilePath(FILE_PATH_LITERAL("File1.txt")), | 135 base::FilePath(FILE_PATH_LITERAL("File1.txt")), |
131 10, 98, time1); | 136 10, 98, time1); |
132 canonicalized_blob_data2->AppendFile( | 137 canonicalized_blob_data2->AppendFile( |
133 base::FilePath(FILE_PATH_LITERAL("File2.txt")), 0, 20, time2); | 138 base::FilePath(FILE_PATH_LITERAL("File2.txt")), 0, 20, time2); |
134 | 139 |
135 BlobStorageContext context; | 140 BlobStorageContext context; |
136 scoped_ptr<BlobDataHandle> blob_data_handle; | 141 scoped_ptr<BlobDataHandle> blob_data_handle; |
137 | 142 |
138 // Test a blob referring to only data and a file. | 143 // Test a blob referring to only data and a file. |
139 blob_data_handle = context.AddFinishedBlob(blob_data1.get()); | 144 blob_data_handle = context.AddFinishedBlob(*blob_data1.get()); |
140 ASSERT_TRUE(blob_data_handle.get()); | 145 ASSERT_TRUE(blob_data_handle); |
141 EXPECT_TRUE(*(blob_data_handle->data()) == *blob_data1.get()); | 146 scoped_ptr<BlobDataSnapshot> data = blob_data_handle->CreateSnapshot(); |
| 147 ASSERT_TRUE(blob_data_handle); |
| 148 EXPECT_EQ(*data, *blob_data1); |
142 | 149 |
143 // Test a blob composed in part with another blob. | 150 // Test a blob composed in part with another blob. |
144 blob_data_handle = context.AddFinishedBlob(blob_data2.get()); | 151 blob_data_handle = context.AddFinishedBlob(*blob_data2.get()); |
145 ASSERT_TRUE(blob_data_handle.get()); | 152 data = blob_data_handle->CreateSnapshot(); |
146 EXPECT_TRUE(*(blob_data_handle->data()) == *canonicalized_blob_data2.get()); | 153 ASSERT_TRUE(blob_data_handle); |
| 154 ASSERT_TRUE(data); |
| 155 EXPECT_EQ(*data, *canonicalized_blob_data2); |
147 | 156 |
148 blob_data_handle.reset(); | 157 blob_data_handle.reset(); |
149 { // Clean up for ASAN | 158 { // Clean up for ASAN |
150 base::RunLoop run_loop; | 159 base::RunLoop run_loop; |
151 run_loop.RunUntilIdle(); | 160 run_loop.RunUntilIdle(); |
152 } | 161 } |
153 } | 162 } |
154 | 163 |
155 TEST(BlobStorageContextTest, PublicBlobUrls) { | 164 TEST(BlobStorageContextTest, PublicBlobUrls) { |
156 BlobStorageContext context; | 165 BlobStorageContext context; |
157 BlobStorageHost host(&context); | 166 BlobStorageHost host(&context); |
158 base::MessageLoop fake_io_message_loop; | 167 base::MessageLoop fake_io_message_loop; |
159 | 168 |
160 // Build up a basic blob. | 169 // Build up a basic blob. |
161 const std::string kId("id"); | 170 const std::string kId("id"); |
162 SetupBasicBlob(&host, kId); | 171 SetupBasicBlob(&host, kId); |
163 | 172 |
164 // Now register a url for that blob. | 173 // Now register a url for that blob. |
165 GURL kUrl("blob:id"); | 174 GURL kUrl("blob:id"); |
166 EXPECT_TRUE(host.RegisterPublicBlobURL(kUrl, kId)); | 175 EXPECT_TRUE(host.RegisterPublicBlobURL(kUrl, kId)); |
167 scoped_ptr<BlobDataHandle> blob_data_handle = | 176 scoped_ptr<BlobDataHandle> blob_data_handle = |
168 context.GetBlobDataFromPublicURL(kUrl); | 177 context.GetBlobDataFromPublicURL(kUrl); |
169 ASSERT_TRUE(blob_data_handle.get()); | 178 ASSERT_TRUE(blob_data_handle.get()); |
170 EXPECT_EQ(kId, blob_data_handle->data()->uuid()); | 179 EXPECT_EQ(kId, blob_data_handle->uuid()); |
| 180 scoped_ptr<BlobDataSnapshot> data = blob_data_handle->CreateSnapshot(); |
| 181 EXPECT_EQ(kId, data->uuid()); |
171 blob_data_handle.reset(); | 182 blob_data_handle.reset(); |
172 { // Clean up for ASAN | 183 { // Clean up for ASAN |
173 base::RunLoop run_loop; | 184 base::RunLoop run_loop; |
174 run_loop.RunUntilIdle(); | 185 run_loop.RunUntilIdle(); |
175 } | 186 } |
176 | 187 |
177 // The url registration should keep the blob alive even after | 188 // The url registration should keep the blob alive even after |
178 // explicit references are dropped. | 189 // explicit references are dropped. |
179 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); | 190 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); |
180 blob_data_handle = context.GetBlobDataFromPublicURL(kUrl); | 191 blob_data_handle = context.GetBlobDataFromPublicURL(kUrl); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 scoped_ptr<BlobStorageContext> context(new BlobStorageContext); | 226 scoped_ptr<BlobStorageContext> context(new BlobStorageContext); |
216 BlobStorageHost host(context.get()); | 227 BlobStorageHost host(context.get()); |
217 base::MessageLoop fake_io_message_loop; | 228 base::MessageLoop fake_io_message_loop; |
218 | 229 |
219 // Deleting the context should not induce crashes. | 230 // Deleting the context should not induce crashes. |
220 context.reset(); | 231 context.reset(); |
221 | 232 |
222 const std::string kId("id"); | 233 const std::string kId("id"); |
223 GURL kUrl("blob:id"); | 234 GURL kUrl("blob:id"); |
224 EXPECT_FALSE(host.StartBuildingBlob(kId)); | 235 EXPECT_FALSE(host.StartBuildingBlob(kId)); |
225 BlobData::Item item; | 236 DataElement item; |
226 item.SetToBytes("1", 1); | 237 item.SetToBytes("1", 1); |
227 EXPECT_FALSE(host.AppendBlobDataItem(kId, item)); | 238 EXPECT_FALSE(host.AppendBlobDataItem(kId, item)); |
228 EXPECT_FALSE(host.FinishBuildingBlob(kId, "text/plain")); | 239 EXPECT_FALSE(host.FinishBuildingBlob(kId, "text/plain")); |
229 EXPECT_FALSE(host.RegisterPublicBlobURL(kUrl, kId)); | 240 EXPECT_FALSE(host.RegisterPublicBlobURL(kUrl, kId)); |
230 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); | 241 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); |
231 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); | 242 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); |
232 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl)); | 243 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl)); |
233 } | 244 } |
234 | 245 |
235 // TODO(michaeln): tests for the depcrecated url stuff | 246 // TODO(michaeln): tests for the depcrecated url stuff |
236 | 247 |
237 } // namespace content | 248 } // namespace content |
OLD | NEW |