| 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 25 matching lines...) Expand all Loading... |
| 59 } | 65 } |
| 60 | 66 |
| 61 // Make sure it goes away in the end. | 67 // Make sure it goes away in the end. |
| 62 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); | 68 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); |
| 63 blob_data_handle = context.GetBlobDataFromUUID(kId); | 69 blob_data_handle = context.GetBlobDataFromUUID(kId); |
| 64 EXPECT_FALSE(blob_data_handle); | 70 EXPECT_FALSE(blob_data_handle); |
| 65 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); | 71 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); |
| 66 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); | 72 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); |
| 67 } | 73 } |
| 68 | 74 |
| 75 TEST(BlobStorageContextTest, CancelBuildingBlob) { |
| 76 BlobStorageContext context; |
| 77 BlobStorageHost host(&context); |
| 78 base::MessageLoop fake_io_message_loop; |
| 79 |
| 80 // Build up a basic blob. |
| 81 const std::string kId("id"); |
| 82 EXPECT_TRUE(host.StartBuildingBlob(kId)); |
| 83 DataElement item; |
| 84 item.SetToBytes("1", 1); |
| 85 EXPECT_TRUE(host.AppendBlobDataItem(kId, item)); |
| 86 EXPECT_TRUE(host.CancelBuildingBlob(kId)); |
| 87 EXPECT_FALSE(host.FinishBuildingBlob(kId, "text/plain")); |
| 88 EXPECT_TRUE(host.StartBuildingBlob(kId)); |
| 89 } |
| 90 |
| 69 TEST(BlobStorageContextTest, BlobDataHandle) { | 91 TEST(BlobStorageContextTest, BlobDataHandle) { |
| 70 BlobStorageContext context; | 92 BlobStorageContext context; |
| 71 BlobStorageHost host(&context); | 93 BlobStorageHost host(&context); |
| 72 base::MessageLoop fake_io_message_loop; | 94 base::MessageLoop fake_io_message_loop; |
| 73 | 95 |
| 74 // Build up a basic blob. | 96 // Build up a basic blob. |
| 75 const std::string kId("id"); | 97 const std::string kId("id"); |
| 76 SetupBasicBlob(&host, kId); | 98 SetupBasicBlob(&host, kId); |
| 77 | 99 |
| 78 // Get a handle to it. | 100 // Get a handle to it. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 104 const std::string kId2("id2"); | 126 const std::string kId2("id2"); |
| 105 const std::string kId2Prime("id2.prime"); | 127 const std::string kId2Prime("id2.prime"); |
| 106 | 128 |
| 107 base::MessageLoop fake_io_message_loop; | 129 base::MessageLoop fake_io_message_loop; |
| 108 | 130 |
| 109 // Setup a set of blob data for testing. | 131 // Setup a set of blob data for testing. |
| 110 base::Time time1, time2; | 132 base::Time time1, time2; |
| 111 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); | 133 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); | 134 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2); |
| 113 | 135 |
| 114 scoped_refptr<BlobData> blob_data1(new BlobData(kId1)); | 136 scoped_ptr<BlobDataBuilder> blob_data1(new BlobDataBuilder(kId1)); |
| 115 blob_data1->AppendData("Data1"); | 137 blob_data1->AppendData("Data1"); |
| 116 blob_data1->AppendData("Data2"); | 138 blob_data1->AppendData("Data2"); |
| 117 blob_data1->AppendFile(base::FilePath(FILE_PATH_LITERAL("File1.txt")), | 139 blob_data1->AppendFile(base::FilePath(FILE_PATH_LITERAL("File1.txt")), |
| 118 10, 1024, time1); | 140 10, 1024, time1); |
| 119 | 141 |
| 120 scoped_refptr<BlobData> blob_data2(new BlobData(kId2)); | 142 scoped_ptr<BlobDataBuilder> blob_data2(new BlobDataBuilder(kId2)); |
| 121 blob_data2->AppendData("Data3"); | 143 blob_data2->AppendData("Data3"); |
| 122 blob_data2->AppendBlob(kId1, 8, 100); | 144 blob_data2->AppendBlob(kId1, 8, 100); |
| 123 blob_data2->AppendFile(base::FilePath(FILE_PATH_LITERAL("File2.txt")), | 145 blob_data2->AppendFile(base::FilePath(FILE_PATH_LITERAL("File2.txt")), |
| 124 0, 20, time2); | 146 0, 20, time2); |
| 125 | 147 |
| 126 scoped_refptr<BlobData> canonicalized_blob_data2(new BlobData(kId2Prime)); | 148 scoped_ptr<BlobDataBuilder> canonicalized_blob_data2( |
| 149 new BlobDataBuilder(kId2Prime)); |
| 127 canonicalized_blob_data2->AppendData("Data3"); | 150 canonicalized_blob_data2->AppendData("Data3"); |
| 128 canonicalized_blob_data2->AppendData("a2___", 2); | 151 canonicalized_blob_data2->AppendData("a2___", 2); |
| 129 canonicalized_blob_data2->AppendFile( | 152 canonicalized_blob_data2->AppendFile( |
| 130 base::FilePath(FILE_PATH_LITERAL("File1.txt")), | 153 base::FilePath(FILE_PATH_LITERAL("File1.txt")), |
| 131 10, 98, time1); | 154 10, 98, time1); |
| 132 canonicalized_blob_data2->AppendFile( | 155 canonicalized_blob_data2->AppendFile( |
| 133 base::FilePath(FILE_PATH_LITERAL("File2.txt")), 0, 20, time2); | 156 base::FilePath(FILE_PATH_LITERAL("File2.txt")), 0, 20, time2); |
| 134 | 157 |
| 135 BlobStorageContext context; | 158 BlobStorageContext context; |
| 136 scoped_ptr<BlobDataHandle> blob_data_handle; | 159 scoped_ptr<BlobDataHandle> blob_data_handle; |
| 137 | 160 |
| 138 // Test a blob referring to only data and a file. | 161 // Test a blob referring to only data and a file. |
| 139 blob_data_handle = context.AddFinishedBlob(blob_data1.get()); | 162 blob_data_handle = context.AddFinishedBlob(*blob_data1.get()); |
| 140 ASSERT_TRUE(blob_data_handle.get()); | 163 ASSERT_TRUE(blob_data_handle); |
| 141 EXPECT_TRUE(*(blob_data_handle->data()) == *blob_data1.get()); | 164 scoped_ptr<BlobDataSnapshot> data = blob_data_handle->CreateSnapshot(); |
| 165 ASSERT_TRUE(blob_data_handle); |
| 166 EXPECT_EQ(*data, *blob_data1); |
| 142 | 167 |
| 143 // Test a blob composed in part with another blob. | 168 // Test a blob composed in part with another blob. |
| 144 blob_data_handle = context.AddFinishedBlob(blob_data2.get()); | 169 blob_data_handle = context.AddFinishedBlob(*blob_data2.get()); |
| 145 ASSERT_TRUE(blob_data_handle.get()); | 170 data = blob_data_handle->CreateSnapshot(); |
| 146 EXPECT_TRUE(*(blob_data_handle->data()) == *canonicalized_blob_data2.get()); | 171 ASSERT_TRUE(blob_data_handle); |
| 172 ASSERT_TRUE(data); |
| 173 EXPECT_EQ(*data, *canonicalized_blob_data2); |
| 147 | 174 |
| 148 blob_data_handle.reset(); | 175 blob_data_handle.reset(); |
| 149 { // Clean up for ASAN | 176 { // Clean up for ASAN |
| 150 base::RunLoop run_loop; | 177 base::RunLoop run_loop; |
| 151 run_loop.RunUntilIdle(); | 178 run_loop.RunUntilIdle(); |
| 152 } | 179 } |
| 153 } | 180 } |
| 154 | 181 |
| 155 TEST(BlobStorageContextTest, PublicBlobUrls) { | 182 TEST(BlobStorageContextTest, PublicBlobUrls) { |
| 156 BlobStorageContext context; | 183 BlobStorageContext context; |
| 157 BlobStorageHost host(&context); | 184 BlobStorageHost host(&context); |
| 158 base::MessageLoop fake_io_message_loop; | 185 base::MessageLoop fake_io_message_loop; |
| 159 | 186 |
| 160 // Build up a basic blob. | 187 // Build up a basic blob. |
| 161 const std::string kId("id"); | 188 const std::string kId("id"); |
| 162 SetupBasicBlob(&host, kId); | 189 SetupBasicBlob(&host, kId); |
| 163 | 190 |
| 164 // Now register a url for that blob. | 191 // Now register a url for that blob. |
| 165 GURL kUrl("blob:id"); | 192 GURL kUrl("blob:id"); |
| 166 EXPECT_TRUE(host.RegisterPublicBlobURL(kUrl, kId)); | 193 EXPECT_TRUE(host.RegisterPublicBlobURL(kUrl, kId)); |
| 167 scoped_ptr<BlobDataHandle> blob_data_handle = | 194 scoped_ptr<BlobDataHandle> blob_data_handle = |
| 168 context.GetBlobDataFromPublicURL(kUrl); | 195 context.GetBlobDataFromPublicURL(kUrl); |
| 169 ASSERT_TRUE(blob_data_handle.get()); | 196 ASSERT_TRUE(blob_data_handle.get()); |
| 170 EXPECT_EQ(kId, blob_data_handle->data()->uuid()); | 197 EXPECT_EQ(kId, blob_data_handle->uuid()); |
| 198 scoped_ptr<BlobDataSnapshot> data = blob_data_handle->CreateSnapshot(); |
| 171 blob_data_handle.reset(); | 199 blob_data_handle.reset(); |
| 172 { // Clean up for ASAN | 200 { // Clean up for ASAN |
| 173 base::RunLoop run_loop; | 201 base::RunLoop run_loop; |
| 174 run_loop.RunUntilIdle(); | 202 run_loop.RunUntilIdle(); |
| 175 } | 203 } |
| 176 | 204 |
| 177 // The url registration should keep the blob alive even after | 205 // The url registration should keep the blob alive even after |
| 178 // explicit references are dropped. | 206 // explicit references are dropped. |
| 179 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); | 207 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); |
| 180 blob_data_handle = context.GetBlobDataFromPublicURL(kUrl); | 208 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); | 243 scoped_ptr<BlobStorageContext> context(new BlobStorageContext); |
| 216 BlobStorageHost host(context.get()); | 244 BlobStorageHost host(context.get()); |
| 217 base::MessageLoop fake_io_message_loop; | 245 base::MessageLoop fake_io_message_loop; |
| 218 | 246 |
| 219 // Deleting the context should not induce crashes. | 247 // Deleting the context should not induce crashes. |
| 220 context.reset(); | 248 context.reset(); |
| 221 | 249 |
| 222 const std::string kId("id"); | 250 const std::string kId("id"); |
| 223 GURL kUrl("blob:id"); | 251 GURL kUrl("blob:id"); |
| 224 EXPECT_FALSE(host.StartBuildingBlob(kId)); | 252 EXPECT_FALSE(host.StartBuildingBlob(kId)); |
| 225 BlobData::Item item; | 253 DataElement item; |
| 226 item.SetToBytes("1", 1); | 254 item.SetToBytes("1", 1); |
| 227 EXPECT_FALSE(host.AppendBlobDataItem(kId, item)); | 255 EXPECT_FALSE(host.AppendBlobDataItem(kId, item)); |
| 228 EXPECT_FALSE(host.FinishBuildingBlob(kId, "text/plain")); | 256 EXPECT_FALSE(host.FinishBuildingBlob(kId, "text/plain")); |
| 229 EXPECT_FALSE(host.RegisterPublicBlobURL(kUrl, kId)); | 257 EXPECT_FALSE(host.RegisterPublicBlobURL(kUrl, kId)); |
| 230 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); | 258 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); |
| 231 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); | 259 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); |
| 232 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl)); | 260 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl)); |
| 233 } | 261 } |
| 234 | 262 |
| 235 // TODO(michaeln): tests for the depcrecated url stuff | 263 // TODO(michaeln): tests for the depcrecated url stuff |
| 236 | 264 |
| 237 } // namespace content | 265 } // namespace content |
| OLD | NEW |