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