| 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 "storage/browser/blob/blob_storage_context.h" | 5 #include "storage/browser/blob/blob_storage_context.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 return nullptr; | 65 return nullptr; |
| 66 disk_cache::ScopedEntryPtr entry(temp_entry); | 66 disk_cache::ScopedEntryPtr entry(temp_entry); |
| 67 | 67 |
| 68 scoped_refptr<net::StringIOBuffer> iobuffer = new net::StringIOBuffer(data); | 68 scoped_refptr<net::StringIOBuffer> iobuffer = new net::StringIOBuffer(data); |
| 69 rv = entry->WriteData(kTestDiskCacheStreamIndex, 0, iobuffer.get(), | 69 rv = entry->WriteData(kTestDiskCacheStreamIndex, 0, iobuffer.get(), |
| 70 iobuffer->size(), callback.callback(), false); | 70 iobuffer->size(), callback.callback(), false); |
| 71 EXPECT_EQ(static_cast<int>(data.size()), callback.GetResult(rv)); | 71 EXPECT_EQ(static_cast<int>(data.size()), callback.GetResult(rv)); |
| 72 return entry.Pass(); | 72 return entry.Pass(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 void SetupBasicBlob(BlobStorageHost* host, const std::string& id) { | |
| 76 EXPECT_TRUE(host->StartBuildingBlob(id)); | |
| 77 DataElement item; | |
| 78 item.SetToBytes("1", 1); | |
| 79 EXPECT_TRUE(host->AppendBlobDataItem(id, item)); | |
| 80 EXPECT_TRUE(host->FinishBuildingBlob(id, "text/plain")); | |
| 81 EXPECT_FALSE(host->StartBuildingBlob(id)); | |
| 82 } | |
| 83 | 75 |
| 84 } // namespace | 76 } // namespace |
| 85 | 77 |
| 86 TEST(BlobStorageContextTest, IncrementDecrementRef) { | 78 class BlobStorageContextTest : public testing::Test { |
| 79 protected: |
| 80 BlobStorageContextTest() |
| 81 : done_called_(false), |
| 82 cancel_called_(false), |
| 83 cancel_code_(storage::IPCBlobCreationCancelCode::UNKNOWN), |
| 84 request_called_(false) {} |
| 85 ~BlobStorageContextTest() override {} |
| 86 |
| 87 // Callback Methods and Getters |
| 88 base::Callback<void(storage::IPCBlobCreationCancelCode)> GetCancelCallback() { |
| 89 cancel_called_ = false; |
| 90 return base::Bind(&BlobStorageContextTest::CancelCallback, |
| 91 base::Unretained(this)); |
| 92 } |
| 93 |
| 94 void CancelCallback(storage::IPCBlobCreationCancelCode code) { |
| 95 cancel_called_ = true; |
| 96 cancel_code_ = code; |
| 97 } |
| 98 |
| 99 base::Closure GetDoneCallback() { |
| 100 done_called_ = false; |
| 101 return base::Bind(&BlobStorageContextTest::DoneCallback, |
| 102 base::Unretained(this)); |
| 103 } |
| 104 |
| 105 void DoneCallback() { done_called_ = true; } |
| 106 |
| 107 base::Callback<void(const std::vector<storage::BlobItemBytesRequest>&, |
| 108 const std::vector<base::SharedMemoryHandle>&, |
| 109 const std::vector<IPC::PlatformFileForTransit>&)> |
| 110 GetRequestCallback() { |
| 111 request_called_ = false; |
| 112 return base::Bind(&BlobStorageContextTest::RequestMemoryCallback, |
| 113 base::Unretained(this)); |
| 114 } |
| 115 |
| 116 void RequestMemoryCallback(const std::vector<storage::BlobItemBytesRequest>&, |
| 117 const std::vector<base::SharedMemoryHandle>&, |
| 118 const std::vector<IPC::PlatformFileForTransit>&) { |
| 119 request_called_ = true; |
| 120 } |
| 121 |
| 122 // Helper call to create basic blob. |
| 123 void SetupBasicBlob(BlobStorageHost* host, |
| 124 BlobStorageContext* context, |
| 125 const std::string& id) { |
| 126 EXPECT_FALSE(context->MaybeStartAsyncBlobTransfer(id)); |
| 127 EXPECT_TRUE(host->RegisterBlobUUID(id)); |
| 128 EXPECT_TRUE(context->MaybeStartAsyncBlobTransfer(id)); |
| 129 BlobDataBuilder builder(id); |
| 130 builder.AppendData("1", 1); |
| 131 builder.set_content_type("text/plain"); |
| 132 context->FinishAsyncBlobTransfer(GetDoneCallback(), GetCancelCallback(), |
| 133 builder); |
| 134 EXPECT_TRUE(done_called_); |
| 135 EXPECT_FALSE(cancel_called_); |
| 136 EXPECT_FALSE(host->RegisterBlobUUID(id)); |
| 137 } |
| 138 |
| 139 // Delegating methods |
| 140 bool MaybeStartAsyncBlobTransfer(BlobStorageContext* context, |
| 141 const std::string& uuid) { |
| 142 return context->MaybeStartAsyncBlobTransfer(uuid); |
| 143 } |
| 144 |
| 145 void FinishAsyncBlobTransfer(BlobStorageContext* context, |
| 146 const storage::BlobDataBuilder& builder) { |
| 147 context->FinishAsyncBlobTransfer(GetDoneCallback(), GetCancelCallback(), |
| 148 builder); |
| 149 } |
| 150 |
| 151 void AsyncBlobTransfer(BlobStorageHost* host, const std::string& uuid) { |
| 152 DataElement e; |
| 153 e.SetToBytes("1", 1); |
| 154 std::vector<storage::DataElement> elements = {e}; |
| 155 host->StartBuildingBlob(uuid, "text/plain", elements, GetRequestCallback(), |
| 156 GetDoneCallback(), GetCancelCallback()); |
| 157 } |
| 158 |
| 159 bool done_called_; |
| 160 bool cancel_called_; |
| 161 storage::IPCBlobCreationCancelCode cancel_code_; |
| 162 bool request_called_; |
| 163 }; |
| 164 |
| 165 TEST_F(BlobStorageContextTest, IncrementDecrementRef) { |
| 87 BlobStorageContext context; | 166 BlobStorageContext context; |
| 88 BlobStorageHost host(&context); | 167 BlobStorageHost host(&context); |
| 89 base::MessageLoop fake_io_message_loop; | 168 base::MessageLoop fake_io_message_loop; |
| 90 | 169 |
| 91 // Build up a basic blob. | 170 // Build up a basic blob. |
| 92 const std::string kId("id"); | 171 const std::string kId("id"); |
| 93 SetupBasicBlob(&host, kId); | 172 SetupBasicBlob(&host, &context, kId); |
| 94 | 173 |
| 95 // Make sure it's there, finish building implies a ref of one. | 174 // Make sure it's there, finish building implies a ref of one. |
| 96 scoped_ptr<BlobDataHandle> blob_data_handle; | 175 scoped_ptr<BlobDataHandle> blob_data_handle; |
| 97 blob_data_handle = context.GetBlobDataFromUUID(kId); | 176 blob_data_handle = context.GetBlobDataFromUUID(kId); |
| 98 EXPECT_TRUE(blob_data_handle); | 177 EXPECT_TRUE(blob_data_handle); |
| 99 blob_data_handle.reset(); | 178 blob_data_handle.reset(); |
| 100 base::RunLoop().RunUntilIdle(); | 179 base::RunLoop().RunUntilIdle(); |
| 101 | 180 |
| 102 // Make sure its still there after inc/dec. | 181 // Make sure its still there after inc/dec. |
| 103 EXPECT_TRUE(host.IncrementBlobRefCount(kId)); | 182 EXPECT_TRUE(host.IncrementBlobRefCount(kId)); |
| 104 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); | 183 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); |
| 105 blob_data_handle = context.GetBlobDataFromUUID(kId); | 184 blob_data_handle = context.GetBlobDataFromUUID(kId); |
| 106 EXPECT_TRUE(blob_data_handle); | 185 EXPECT_TRUE(blob_data_handle); |
| 107 blob_data_handle.reset(); | 186 blob_data_handle.reset(); |
| 108 base::RunLoop().RunUntilIdle(); | 187 base::RunLoop().RunUntilIdle(); |
| 109 | 188 |
| 110 // Make sure it goes away in the end. | 189 // Make sure it goes away in the end. |
| 111 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); | 190 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); |
| 112 blob_data_handle = context.GetBlobDataFromUUID(kId); | 191 blob_data_handle = context.GetBlobDataFromUUID(kId); |
| 113 EXPECT_FALSE(blob_data_handle); | 192 EXPECT_FALSE(blob_data_handle); |
| 114 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); | 193 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); |
| 115 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); | 194 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); |
| 116 } | 195 } |
| 117 | 196 |
| 118 TEST(BlobStorageContextTest, CancelBuildingBlob) { | 197 TEST_F(BlobStorageContextTest, CancelBuildingBlob) { |
| 119 BlobStorageContext context; | 198 BlobStorageContext context; |
| 120 BlobStorageHost host(&context); | 199 BlobStorageHost host(&context); |
| 121 base::MessageLoop fake_io_message_loop; | 200 base::MessageLoop fake_io_message_loop; |
| 122 | 201 |
| 123 // Build up a basic blob. | 202 // Build up a basic blob. |
| 124 const std::string kId("id"); | 203 const std::string kId("id"); |
| 125 EXPECT_TRUE(host.StartBuildingBlob(kId)); | 204 EXPECT_FALSE(host.CancelBuildingBlob(kId)); |
| 126 DataElement item; | 205 EXPECT_TRUE(host.RegisterBlobUUID(kId)); |
| 127 item.SetToBytes("1", 1); | 206 EXPECT_TRUE(MaybeStartAsyncBlobTransfer(&context, kId)); |
| 128 EXPECT_TRUE(host.AppendBlobDataItem(kId, item)); | |
| 129 EXPECT_TRUE(host.CancelBuildingBlob(kId)); | 207 EXPECT_TRUE(host.CancelBuildingBlob(kId)); |
| 130 EXPECT_FALSE(host.FinishBuildingBlob(kId, "text/plain")); | 208 |
| 131 EXPECT_TRUE(host.StartBuildingBlob(kId)); | 209 BlobDataBuilder builder(kId); |
| 210 builder.AppendData("1", 1); |
| 211 builder.set_content_type("text/plain"); |
| 212 FinishAsyncBlobTransfer(&context, builder); |
| 213 EXPECT_FALSE(done_called_); |
| 214 EXPECT_TRUE(cancel_called_); |
| 215 |
| 216 EXPECT_TRUE(host.RegisterBlobUUID(kId)); |
| 132 } | 217 } |
| 133 | 218 |
| 134 TEST(BlobStorageContextTest, BlobDataHandle) { | 219 TEST_F(BlobStorageContextTest, BlobDataHandle) { |
| 135 BlobStorageContext context; | 220 BlobStorageContext context; |
| 136 BlobStorageHost host(&context); | 221 BlobStorageHost host(&context); |
| 137 base::MessageLoop fake_io_message_loop; | 222 base::MessageLoop fake_io_message_loop; |
| 138 | 223 |
| 139 // Build up a basic blob. | 224 // Build up a basic blob. |
| 140 const std::string kId("id"); | 225 const std::string kId("id"); |
| 141 SetupBasicBlob(&host, kId); | 226 SetupBasicBlob(&host, &context, kId); |
| 142 | 227 |
| 143 // Get a handle to it. | 228 // Get a handle to it. |
| 144 scoped_ptr<BlobDataHandle> blob_data_handle = | 229 scoped_ptr<BlobDataHandle> blob_data_handle = |
| 145 context.GetBlobDataFromUUID(kId); | 230 context.GetBlobDataFromUUID(kId); |
| 146 EXPECT_TRUE(blob_data_handle); | 231 EXPECT_TRUE(blob_data_handle); |
| 147 | 232 |
| 148 // Drop the host's ref to it. | 233 // Drop the host's ref to it. |
| 149 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); | 234 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); |
| 150 | 235 |
| 151 // Should still be there due to the handle. | 236 // Should still be there due to the handle. |
| 152 scoped_ptr<BlobDataHandle> another_handle = | 237 scoped_ptr<BlobDataHandle> another_handle = |
| 153 context.GetBlobDataFromUUID(kId); | 238 context.GetBlobDataFromUUID(kId); |
| 154 EXPECT_TRUE(another_handle); | 239 EXPECT_TRUE(another_handle); |
| 155 | 240 |
| 156 // Should disappear after dropping both handles. | 241 // Should disappear after dropping both handles. |
| 157 blob_data_handle.reset(); | 242 blob_data_handle.reset(); |
| 158 another_handle.reset(); | 243 another_handle.reset(); |
| 159 base::RunLoop().RunUntilIdle(); | 244 base::RunLoop().RunUntilIdle(); |
| 160 | 245 |
| 161 blob_data_handle = context.GetBlobDataFromUUID(kId); | 246 blob_data_handle = context.GetBlobDataFromUUID(kId); |
| 162 EXPECT_FALSE(blob_data_handle); | 247 EXPECT_FALSE(blob_data_handle); |
| 163 } | 248 } |
| 164 | 249 |
| 165 TEST(BlobStorageContextTest, MemoryUsage) { | 250 TEST_F(BlobStorageContextTest, MemoryUsage) { |
| 166 const std::string kId1("id1"); | 251 const std::string kId1("id1"); |
| 167 const std::string kId2("id2"); | 252 const std::string kId2("id2"); |
| 168 | 253 |
| 169 base::MessageLoop fake_io_message_loop; | 254 base::MessageLoop fake_io_message_loop; |
| 170 | 255 |
| 171 BlobDataBuilder builder1(kId1); | 256 BlobDataBuilder builder1(kId1); |
| 172 BlobDataBuilder builder2(kId2); | 257 BlobDataBuilder builder2(kId2); |
| 173 builder1.AppendData("Data1Data2"); | 258 builder1.AppendData("Data1Data2"); |
| 174 builder2.AppendBlob(kId1); | 259 builder2.AppendBlob(kId1); |
| 175 builder2.AppendBlob(kId1); | 260 builder2.AppendBlob(kId1); |
| 176 builder2.AppendBlob(kId1); | 261 builder2.AppendBlob(kId1); |
| 177 builder2.AppendBlob(kId1); | 262 builder2.AppendBlob(kId1); |
| 178 builder2.AppendBlob(kId1); | 263 builder2.AppendBlob(kId1); |
| 179 builder2.AppendBlob(kId1); | 264 builder2.AppendBlob(kId1); |
| 180 builder2.AppendBlob(kId1); | 265 builder2.AppendBlob(kId1); |
| 181 | 266 |
| 182 BlobStorageContext context; | 267 BlobStorageContext context; |
| 183 EXPECT_EQ(0lu, context.memory_usage()); | 268 EXPECT_EQ(0lu, context.memory_usage()); |
| 184 | 269 |
| 185 scoped_ptr<BlobDataHandle> blob_data_handle = | 270 scoped_ptr<BlobDataHandle> blob_data_handle = |
| 186 context.AddFinishedBlob(&builder1); | 271 context.AddFinishedBlob(&builder1); |
| 187 EXPECT_EQ(10lu, context.memory_usage()); | 272 EXPECT_EQ(10lu, context.memory_usage()); |
| 188 scoped_ptr<BlobDataHandle> blob_data_handle2 = | 273 scoped_ptr<BlobDataHandle> blob_data_handle2 = |
| 189 context.AddFinishedBlob(&builder2); | 274 context.AddFinishedBlob(&builder2); |
| 190 EXPECT_EQ(10lu, context.memory_usage()); | 275 EXPECT_EQ(10lu, context.memory_usage()); |
| 191 | 276 |
| 277 EXPECT_EQ(2u, context.registry().blob_count()); |
| 278 |
| 192 blob_data_handle.reset(); | 279 blob_data_handle.reset(); |
| 193 base::RunLoop().RunUntilIdle(); | 280 base::RunLoop().RunUntilIdle(); |
| 194 | 281 |
| 195 EXPECT_EQ(10lu, context.memory_usage()); | 282 EXPECT_EQ(10lu, context.memory_usage()); |
| 283 EXPECT_EQ(1u, context.registry().blob_count()); |
| 196 blob_data_handle2.reset(); | 284 blob_data_handle2.reset(); |
| 197 base::RunLoop().RunUntilIdle(); | 285 base::RunLoop().RunUntilIdle(); |
| 198 | 286 |
| 199 EXPECT_EQ(0lu, context.memory_usage()); | 287 EXPECT_EQ(0lu, context.memory_usage()); |
| 288 EXPECT_EQ(0u, context.registry().blob_count()); |
| 200 } | 289 } |
| 201 | 290 |
| 202 TEST(BlobStorageContextTest, AddFinishedBlob) { | 291 TEST_F(BlobStorageContextTest, AsyncPipeline) { |
| 292 BlobStorageContext context; |
| 293 BlobStorageHost host(&context); |
| 294 |
| 295 // Build up a basic blob. |
| 296 const std::string kId("id"); |
| 297 EXPECT_FALSE(MaybeStartAsyncBlobTransfer(&context, kId)); |
| 298 AsyncBlobTransfer(&host, kId); |
| 299 EXPECT_TRUE(cancel_called_); |
| 300 EXPECT_FALSE(done_called_); |
| 301 |
| 302 EXPECT_TRUE(host.RegisterBlobUUID(kId)); |
| 303 AsyncBlobTransfer(&host, kId); |
| 304 EXPECT_TRUE(done_called_); |
| 305 EXPECT_FALSE(cancel_called_); |
| 306 // We assume that it uses the shortcut method, and immediately returns. |
| 307 EXPECT_FALSE(request_called_); |
| 308 |
| 309 EXPECT_EQ(1u, context.registry().blob_count()); |
| 310 } |
| 311 |
| 312 TEST_F(BlobStorageContextTest, AddFinishedBlob) { |
| 203 const std::string kId1("id1"); | 313 const std::string kId1("id1"); |
| 204 const std::string kId2("id12"); | 314 const std::string kId2("id12"); |
| 205 const std::string kId2Prime("id2.prime"); | 315 const std::string kId2Prime("id2.prime"); |
| 206 const std::string kId3("id3"); | 316 const std::string kId3("id3"); |
| 207 const std::string kId3Prime("id3.prime"); | 317 const std::string kId3Prime("id3.prime"); |
| 208 | 318 |
| 209 base::MessageLoop fake_io_message_loop; | 319 base::MessageLoop fake_io_message_loop; |
| 210 | 320 |
| 211 BlobDataBuilder builder1(kId1); | 321 BlobDataBuilder builder1(kId1); |
| 212 BlobDataBuilder builder2(kId2); | 322 BlobDataBuilder builder2(kId2); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 canonicalized_blob_data3.AppendData("Data2"); | 371 canonicalized_blob_data3.AppendData("Data2"); |
| 262 canonicalized_blob_data3.AppendData(" is the best"); | 372 canonicalized_blob_data3.AppendData(" is the best"); |
| 263 EXPECT_EQ(*data3, canonicalized_blob_data3); | 373 EXPECT_EQ(*data3, canonicalized_blob_data3); |
| 264 | 374 |
| 265 blob_data_handle.reset(); | 375 blob_data_handle.reset(); |
| 266 blob_data_handle2.reset(); | 376 blob_data_handle2.reset(); |
| 267 blob_data_handle3.reset(); | 377 blob_data_handle3.reset(); |
| 268 base::RunLoop().RunUntilIdle(); | 378 base::RunLoop().RunUntilIdle(); |
| 269 } | 379 } |
| 270 | 380 |
| 271 TEST(BlobStorageContextTest, AddFinishedBlob_LargeOffset) { | 381 TEST_F(BlobStorageContextTest, AddFinishedBlob_LargeOffset) { |
| 272 // A value which does not fit in a 4-byte data type. Used to confirm that | 382 // A value which does not fit in a 4-byte data type. Used to confirm that |
| 273 // large values are supported on 32-bit Chromium builds. Regression test for: | 383 // large values are supported on 32-bit Chromium builds. Regression test for: |
| 274 // crbug.com/458122. | 384 // crbug.com/458122. |
| 275 const uint64_t kLargeSize = std::numeric_limits<uint64_t>::max(); | 385 const uint64_t kLargeSize = std::numeric_limits<uint64_t>::max(); |
| 276 | 386 |
| 277 const uint64_t kBlobLength = 5; | 387 const uint64_t kBlobLength = 5; |
| 278 const std::string kId1("id1"); | 388 const std::string kId1("id1"); |
| 279 const std::string kId2("id2"); | 389 const std::string kId2("id2"); |
| 280 base::MessageLoop fake_io_message_loop; | 390 base::MessageLoop fake_io_message_loop; |
| 281 | 391 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 297 ASSERT_EQ(1u, data->items().size()); | 407 ASSERT_EQ(1u, data->items().size()); |
| 298 const scoped_refptr<BlobDataItem> item = data->items()[0]; | 408 const scoped_refptr<BlobDataItem> item = data->items()[0]; |
| 299 EXPECT_EQ(kLargeSize - kBlobLength, item->offset()); | 409 EXPECT_EQ(kLargeSize - kBlobLength, item->offset()); |
| 300 EXPECT_EQ(kBlobLength, item->length()); | 410 EXPECT_EQ(kBlobLength, item->length()); |
| 301 | 411 |
| 302 blob_data_handle1.reset(); | 412 blob_data_handle1.reset(); |
| 303 blob_data_handle2.reset(); | 413 blob_data_handle2.reset(); |
| 304 base::RunLoop().RunUntilIdle(); | 414 base::RunLoop().RunUntilIdle(); |
| 305 } | 415 } |
| 306 | 416 |
| 307 TEST(BlobStorageContextTest, BuildDiskCacheBlob) { | 417 TEST_F(BlobStorageContextTest, BuildDiskCacheBlob) { |
| 308 base::MessageLoop fake_io_message_loop; | 418 base::MessageLoop fake_io_message_loop; |
| 309 scoped_refptr<BlobDataBuilder::DataHandle> | 419 scoped_refptr<BlobDataBuilder::DataHandle> |
| 310 data_handle = new EmptyDataHandle(); | 420 data_handle = new EmptyDataHandle(); |
| 311 | 421 |
| 312 { | 422 { |
| 313 scoped_ptr<BlobStorageContext> context(new BlobStorageContext); | 423 scoped_ptr<BlobStorageContext> context(new BlobStorageContext); |
| 314 BlobStorageHost host(context.get()); | 424 BlobStorageHost host(context.get()); |
| 315 | 425 |
| 316 scoped_ptr<disk_cache::Backend> cache = CreateInMemoryDiskCache(); | 426 scoped_ptr<disk_cache::Backend> cache = CreateInMemoryDiskCache(); |
| 317 ASSERT_TRUE(cache); | 427 ASSERT_TRUE(cache); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 335 scoped_ptr<BlobDataSnapshot> data = blob_data_handle->CreateSnapshot(); | 445 scoped_ptr<BlobDataSnapshot> data = blob_data_handle->CreateSnapshot(); |
| 336 EXPECT_EQ(*data, builder); | 446 EXPECT_EQ(*data, builder); |
| 337 EXPECT_FALSE(data_handle->HasOneRef()) | 447 EXPECT_FALSE(data_handle->HasOneRef()) |
| 338 << "Data handle was destructed while context and builder still exist."; | 448 << "Data handle was destructed while context and builder still exist."; |
| 339 } | 449 } |
| 340 EXPECT_TRUE(data_handle->HasOneRef()) | 450 EXPECT_TRUE(data_handle->HasOneRef()) |
| 341 << "Data handle was not destructed along with blob storage context."; | 451 << "Data handle was not destructed along with blob storage context."; |
| 342 base::RunLoop().RunUntilIdle(); | 452 base::RunLoop().RunUntilIdle(); |
| 343 } | 453 } |
| 344 | 454 |
| 345 TEST(BlobStorageContextTest, CompoundBlobs) { | 455 TEST_F(BlobStorageContextTest, CompoundBlobs) { |
| 346 const std::string kId1("id1"); | 456 const std::string kId1("id1"); |
| 347 const std::string kId2("id2"); | 457 const std::string kId2("id2"); |
| 348 const std::string kId3("id3"); | 458 const std::string kId3("id3"); |
| 349 const std::string kId2Prime("id2.prime"); | 459 const std::string kId2Prime("id2.prime"); |
| 350 | 460 |
| 351 base::MessageLoop fake_io_message_loop; | 461 base::MessageLoop fake_io_message_loop; |
| 352 | 462 |
| 353 // Setup a set of blob data for testing. | 463 // Setup a set of blob data for testing. |
| 354 base::Time time1, time2; | 464 base::Time time1, time2; |
| 355 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); | 465 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 // Test a blob referring to only data and a disk cache entry. | 515 // Test a blob referring to only data and a disk cache entry. |
| 406 blob_data_handle = context.AddFinishedBlob(&blob_data3); | 516 blob_data_handle = context.AddFinishedBlob(&blob_data3); |
| 407 data = blob_data_handle->CreateSnapshot(); | 517 data = blob_data_handle->CreateSnapshot(); |
| 408 ASSERT_TRUE(blob_data_handle); | 518 ASSERT_TRUE(blob_data_handle); |
| 409 EXPECT_EQ(*data, blob_data3); | 519 EXPECT_EQ(*data, blob_data3); |
| 410 | 520 |
| 411 blob_data_handle.reset(); | 521 blob_data_handle.reset(); |
| 412 base::RunLoop().RunUntilIdle(); | 522 base::RunLoop().RunUntilIdle(); |
| 413 } | 523 } |
| 414 | 524 |
| 415 TEST(BlobStorageContextTest, PublicBlobUrls) { | 525 TEST_F(BlobStorageContextTest, PublicBlobUrls) { |
| 416 BlobStorageContext context; | 526 BlobStorageContext context; |
| 417 BlobStorageHost host(&context); | 527 BlobStorageHost host(&context); |
| 418 base::MessageLoop fake_io_message_loop; | 528 base::MessageLoop fake_io_message_loop; |
| 419 | 529 |
| 420 // Build up a basic blob. | 530 // Build up a basic blob. |
| 421 const std::string kId("id"); | 531 const std::string kId("id"); |
| 422 SetupBasicBlob(&host, kId); | 532 SetupBasicBlob(&host, &context, kId); |
| 423 | 533 |
| 424 // Now register a url for that blob. | 534 // Now register a url for that blob. |
| 425 GURL kUrl("blob:id"); | 535 GURL kUrl("blob:id"); |
| 426 EXPECT_TRUE(host.RegisterPublicBlobURL(kUrl, kId)); | 536 EXPECT_TRUE(host.RegisterPublicBlobURL(kUrl, kId)); |
| 427 scoped_ptr<BlobDataHandle> blob_data_handle = | 537 scoped_ptr<BlobDataHandle> blob_data_handle = |
| 428 context.GetBlobDataFromPublicURL(kUrl); | 538 context.GetBlobDataFromPublicURL(kUrl); |
| 429 ASSERT_TRUE(blob_data_handle.get()); | 539 ASSERT_TRUE(blob_data_handle.get()); |
| 430 EXPECT_EQ(kId, blob_data_handle->uuid()); | 540 EXPECT_EQ(kId, blob_data_handle->uuid()); |
| 431 scoped_ptr<BlobDataSnapshot> data = blob_data_handle->CreateSnapshot(); | 541 scoped_ptr<BlobDataSnapshot> data = blob_data_handle->CreateSnapshot(); |
| 432 blob_data_handle.reset(); | 542 blob_data_handle.reset(); |
| 433 base::RunLoop().RunUntilIdle(); | 543 base::RunLoop().RunUntilIdle(); |
| 434 | 544 |
| 435 // The url registration should keep the blob alive even after | 545 // The url registration should keep the blob alive even after |
| 436 // explicit references are dropped. | 546 // explicit references are dropped. |
| 437 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); | 547 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); |
| 438 blob_data_handle = context.GetBlobDataFromPublicURL(kUrl); | 548 blob_data_handle = context.GetBlobDataFromPublicURL(kUrl); |
| 439 EXPECT_TRUE(blob_data_handle); | 549 EXPECT_TRUE(blob_data_handle); |
| 440 blob_data_handle.reset(); | 550 blob_data_handle.reset(); |
| 441 base::RunLoop().RunUntilIdle(); | 551 base::RunLoop().RunUntilIdle(); |
| 442 | 552 |
| 443 // Finally get rid of the url registration and the blob. | 553 // Finally get rid of the url registration and the blob. |
| 444 EXPECT_TRUE(host.RevokePublicBlobURL(kUrl)); | 554 EXPECT_TRUE(host.RevokePublicBlobURL(kUrl)); |
| 445 blob_data_handle = context.GetBlobDataFromPublicURL(kUrl); | 555 blob_data_handle = context.GetBlobDataFromPublicURL(kUrl); |
| 446 EXPECT_TRUE(!blob_data_handle.get()); | 556 EXPECT_TRUE(!blob_data_handle.get()); |
| 447 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl)); | 557 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl)); |
| 448 } | 558 } |
| 449 | 559 |
| 450 TEST(BlobStorageContextTest, HostCleanup) { | 560 TEST_F(BlobStorageContextTest, HostCleanup) { |
| 451 BlobStorageContext context; | 561 BlobStorageContext context; |
| 452 scoped_ptr<BlobStorageHost> host(new BlobStorageHost(&context)); | 562 scoped_ptr<BlobStorageHost> host(new BlobStorageHost(&context)); |
| 453 base::MessageLoop fake_io_message_loop; | 563 base::MessageLoop fake_io_message_loop; |
| 454 | 564 |
| 455 // Build up a basic blob and register a url | 565 // Build up a basic blob and register a url |
| 456 const std::string kId("id"); | 566 const std::string kId("id"); |
| 457 GURL kUrl("blob:id"); | 567 GURL kUrl("blob:id"); |
| 458 SetupBasicBlob(host.get(), kId); | 568 SetupBasicBlob(host.get(), &context, kId); |
| 459 EXPECT_TRUE(host->RegisterPublicBlobURL(kUrl, kId)); | 569 EXPECT_TRUE(host->RegisterPublicBlobURL(kUrl, kId)); |
| 460 | 570 |
| 461 // All should disappear upon host deletion. | 571 // All should disappear upon host deletion. |
| 462 host.reset(); | 572 host.reset(); |
| 463 scoped_ptr<BlobDataHandle> handle = context.GetBlobDataFromPublicURL(kUrl); | 573 scoped_ptr<BlobDataHandle> handle = context.GetBlobDataFromPublicURL(kUrl); |
| 464 EXPECT_TRUE(!handle.get()); | 574 EXPECT_TRUE(!handle.get()); |
| 465 handle = context.GetBlobDataFromUUID(kId); | 575 handle = context.GetBlobDataFromUUID(kId); |
| 466 EXPECT_TRUE(!handle.get()); | 576 EXPECT_TRUE(!handle.get()); |
| 467 } | 577 } |
| 468 | 578 |
| 469 TEST(BlobStorageContextTest, EarlyContextDeletion) { | 579 TEST_F(BlobStorageContextTest, EarlyContextDeletion) { |
| 470 scoped_ptr<BlobStorageContext> context(new BlobStorageContext); | 580 scoped_ptr<BlobStorageContext> context(new BlobStorageContext); |
| 471 BlobStorageHost host(context.get()); | 581 BlobStorageHost host(context.get()); |
| 472 base::MessageLoop fake_io_message_loop; | 582 base::MessageLoop fake_io_message_loop; |
| 473 | 583 |
| 474 // Deleting the context should not induce crashes. | 584 // Deleting the context should not induce crashes. |
| 475 context.reset(); | 585 context.reset(); |
| 476 | 586 |
| 477 const std::string kId("id"); | 587 const std::string kId("id"); |
| 478 GURL kUrl("blob:id"); | 588 GURL kUrl("blob:id"); |
| 479 EXPECT_FALSE(host.StartBuildingBlob(kId)); | 589 EXPECT_FALSE(host.RegisterBlobUUID(kId)); |
| 480 DataElement item; | 590 DataElement item; |
| 481 item.SetToBytes("1", 1); | 591 item.SetToBytes("1", 1); |
| 482 EXPECT_FALSE(host.AppendBlobDataItem(kId, item)); | 592 EXPECT_FALSE(host.CancelBuildingBlob(kId)); |
| 483 EXPECT_FALSE(host.FinishBuildingBlob(kId, "text/plain")); | 593 AsyncBlobTransfer(&host, kId); |
| 594 EXPECT_FALSE(request_called_); |
| 595 EXPECT_FALSE(done_called_); |
| 596 EXPECT_TRUE(cancel_called_); |
| 484 EXPECT_FALSE(host.RegisterPublicBlobURL(kUrl, kId)); | 597 EXPECT_FALSE(host.RegisterPublicBlobURL(kUrl, kId)); |
| 485 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); | 598 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); |
| 486 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); | 599 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); |
| 487 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl)); | 600 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl)); |
| 488 } | 601 } |
| 489 | 602 |
| 490 // TODO(michaeln): tests for the depcrecated url stuff | 603 // TODO(michaeln): tests for the depcrecated url stuff |
| 491 | 604 |
| 492 } // namespace content | 605 } // namespace content |
| OLD | NEW |