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::BlobDataHandle; | 16 using storage::BlobDataBuilder; |
| 17 using storage::BlobDataSnapshotHandle; |
| 18 using storage::BlobStorageContext; |
| 19 using storage::DataElement; |
17 | 20 |
18 namespace content { | 21 namespace content { |
19 | 22 |
20 namespace { | 23 namespace { |
21 void SetupBasicBlob(BlobStorageHost* host, const std::string& id) { | 24 void SetupBasicBlob(BlobStorageHost* host, const std::string& id) { |
22 EXPECT_TRUE(host->StartBuildingBlob(id)); | 25 EXPECT_TRUE(host->StartBuildingBlob(id)); |
23 BlobData::Item item; | 26 DataElement item; |
24 item.SetToBytes("1", 1); | 27 item.SetToBytes("1", 1); |
25 EXPECT_TRUE(host->AppendBlobDataItem(id, item)); | 28 EXPECT_TRUE(host->AppendBlobDataItem(id, item)); |
26 EXPECT_TRUE(host->FinishBuildingBlob(id, "text/plain")); | 29 EXPECT_TRUE(host->FinishBuildingBlob(id, "text/plain")); |
27 EXPECT_FALSE(host->StartBuildingBlob(id)); | 30 EXPECT_FALSE(host->StartBuildingBlob(id)); |
28 } | 31 } |
29 } // namespace | 32 } // namespace |
30 | 33 |
31 TEST(BlobStorageContextTest, IncrementDecrementRef) { | 34 TEST(BlobStorageContextTest, IncrementDecrementRef) { |
32 BlobStorageContext context; | 35 BlobStorageContext context; |
33 BlobStorageHost host(&context); | 36 BlobStorageHost host(&context); |
34 base::MessageLoop fake_io_message_loop; | 37 base::MessageLoop fake_io_message_loop; |
35 | 38 |
36 // Build up a basic blob. | 39 // Build up a basic blob. |
37 const std::string kId("id"); | 40 const std::string kId("id"); |
38 SetupBasicBlob(&host, kId); | 41 SetupBasicBlob(&host, kId); |
39 | 42 |
40 // Make sure it's there, finish building implies a ref of one. | 43 // Make sure it's there, finish building implies a ref of one. |
41 scoped_ptr<BlobDataHandle> blob_data_handle; | 44 scoped_ptr<BlobDataSnapshotHandle> blob_data_handle; |
42 blob_data_handle = context.GetBlobDataFromUUID(kId); | 45 blob_data_handle = context.GetBlobDataFromUUID(kId); |
43 EXPECT_TRUE(blob_data_handle); | 46 EXPECT_TRUE(blob_data_handle); |
44 blob_data_handle.reset(); | 47 blob_data_handle.reset(); |
45 { // Clean up for ASAN | 48 { // Clean up for ASAN |
46 base::RunLoop run_loop; | 49 base::RunLoop run_loop; |
47 run_loop.RunUntilIdle(); | 50 run_loop.RunUntilIdle(); |
48 } | 51 } |
49 | 52 |
50 // Make sure its still there after inc/dec. | 53 // Make sure its still there after inc/dec. |
51 EXPECT_TRUE(host.IncrementBlobRefCount(kId)); | 54 EXPECT_TRUE(host.IncrementBlobRefCount(kId)); |
52 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); | 55 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); |
53 blob_data_handle = context.GetBlobDataFromUUID(kId); | 56 blob_data_handle = context.GetBlobDataFromUUID(kId); |
54 EXPECT_TRUE(blob_data_handle); | 57 EXPECT_TRUE(blob_data_handle); |
55 blob_data_handle.reset(); | 58 blob_data_handle.reset(); |
56 { // Clean up for ASAN | 59 { // Clean up for ASAN |
57 base::RunLoop run_loop; | 60 base::RunLoop run_loop; |
58 run_loop.RunUntilIdle(); | 61 run_loop.RunUntilIdle(); |
59 } | 62 } |
60 | 63 |
61 // Make sure it goes away in the end. | 64 // Make sure it goes away in the end. |
62 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); | 65 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); |
63 blob_data_handle = context.GetBlobDataFromUUID(kId); | 66 blob_data_handle = context.GetBlobDataFromUUID(kId); |
64 EXPECT_FALSE(blob_data_handle); | 67 EXPECT_FALSE(blob_data_handle); |
65 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); | 68 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); |
66 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); | 69 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); |
67 } | 70 } |
68 | 71 |
69 TEST(BlobStorageContextTest, BlobDataHandle) { | 72 TEST(BlobStorageContextTest, BlobDataSnapshotHandle) { |
70 BlobStorageContext context; | 73 BlobStorageContext context; |
71 BlobStorageHost host(&context); | 74 BlobStorageHost host(&context); |
72 base::MessageLoop fake_io_message_loop; | 75 base::MessageLoop fake_io_message_loop; |
73 | 76 |
74 // Build up a basic blob. | 77 // Build up a basic blob. |
75 const std::string kId("id"); | 78 const std::string kId("id"); |
76 SetupBasicBlob(&host, kId); | 79 SetupBasicBlob(&host, kId); |
77 | 80 |
78 // Get a handle to it. | 81 // Get a handle to it. |
79 scoped_ptr<BlobDataHandle> blob_data_handle = | 82 scoped_ptr<BlobDataSnapshotHandle> blob_data_handle = |
80 context.GetBlobDataFromUUID(kId); | 83 context.GetBlobDataFromUUID(kId); |
81 EXPECT_TRUE(blob_data_handle); | 84 EXPECT_TRUE(blob_data_handle); |
82 | 85 |
83 // Drop the host's ref to it. | 86 // Drop the host's ref to it. |
84 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); | 87 EXPECT_TRUE(host.DecrementBlobRefCount(kId)); |
85 | 88 |
86 // Should still be there due to the handle. | 89 // Should still be there due to the handle. |
87 scoped_ptr<BlobDataHandle> another_handle = | 90 scoped_ptr<BlobDataSnapshotHandle> another_handle = |
88 context.GetBlobDataFromUUID(kId); | 91 context.GetBlobDataFromUUID(kId); |
89 EXPECT_TRUE(another_handle); | 92 EXPECT_TRUE(another_handle); |
90 | 93 |
91 // Should disappear after dropping both handles. | 94 // Should disappear after dropping both handles. |
92 blob_data_handle.reset(); | 95 blob_data_handle.reset(); |
93 another_handle.reset(); | 96 another_handle.reset(); |
94 { // Clean up for ASAN | 97 { // Clean up for ASAN |
95 base::RunLoop run_loop; | 98 base::RunLoop run_loop; |
96 run_loop.RunUntilIdle(); | 99 run_loop.RunUntilIdle(); |
97 } | 100 } |
98 blob_data_handle = context.GetBlobDataFromUUID(kId); | 101 blob_data_handle = context.GetBlobDataFromUUID(kId); |
99 EXPECT_FALSE(blob_data_handle); | 102 EXPECT_FALSE(blob_data_handle); |
100 } | 103 } |
101 | 104 |
102 TEST(BlobStorageContextTest, CompoundBlobs) { | 105 TEST(BlobStorageContextTest, CompoundBlobs) { |
103 const std::string kId1("id1"); | 106 const std::string kId1("id1"); |
104 const std::string kId2("id2"); | 107 const std::string kId2("id2"); |
105 const std::string kId2Prime("id2.prime"); | 108 const std::string kId2Prime("id2.prime"); |
106 | 109 |
107 base::MessageLoop fake_io_message_loop; | 110 base::MessageLoop fake_io_message_loop; |
108 | 111 |
109 // Setup a set of blob data for testing. | 112 // Setup a set of blob data for testing. |
110 base::Time time1, time2; | 113 base::Time time1, time2; |
111 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); | 114 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); | 115 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2); |
113 | 116 |
114 scoped_refptr<BlobData> blob_data1(new BlobData(kId1)); | 117 scoped_ptr<BlobDataBuilder> blob_data1(new BlobDataBuilder(kId1)); |
115 blob_data1->AppendData("Data1"); | 118 blob_data1->AppendData("Data1"); |
116 blob_data1->AppendData("Data2"); | 119 blob_data1->AppendData("Data2"); |
117 blob_data1->AppendFile(base::FilePath(FILE_PATH_LITERAL("File1.txt")), | 120 blob_data1->AppendFile(base::FilePath(FILE_PATH_LITERAL("File1.txt")), |
118 10, 1024, time1); | 121 10, 1024, time1); |
119 | 122 |
120 scoped_refptr<BlobData> blob_data2(new BlobData(kId2)); | 123 scoped_ptr<BlobDataBuilder> blob_data2(new BlobDataBuilder(kId2)); |
121 blob_data2->AppendData("Data3"); | 124 blob_data2->AppendData("Data3"); |
122 blob_data2->AppendBlob(kId1, 8, 100); | 125 blob_data2->AppendBlob(kId1, 8, 100); |
123 blob_data2->AppendFile(base::FilePath(FILE_PATH_LITERAL("File2.txt")), | 126 blob_data2->AppendFile(base::FilePath(FILE_PATH_LITERAL("File2.txt")), |
124 0, 20, time2); | 127 0, 20, time2); |
125 | 128 |
126 scoped_refptr<BlobData> canonicalized_blob_data2(new BlobData(kId2Prime)); | 129 scoped_ptr<BlobDataBuilder> canonicalized_blob_data2( |
| 130 new BlobDataBuilder(kId2Prime)); |
127 canonicalized_blob_data2->AppendData("Data3"); | 131 canonicalized_blob_data2->AppendData("Data3"); |
128 canonicalized_blob_data2->AppendData("a2___", 2); | 132 canonicalized_blob_data2->AppendData("a2___", 2); |
129 canonicalized_blob_data2->AppendFile( | 133 canonicalized_blob_data2->AppendFile( |
130 base::FilePath(FILE_PATH_LITERAL("File1.txt")), | 134 base::FilePath(FILE_PATH_LITERAL("File1.txt")), |
131 10, 98, time1); | 135 10, 98, time1); |
132 canonicalized_blob_data2->AppendFile( | 136 canonicalized_blob_data2->AppendFile( |
133 base::FilePath(FILE_PATH_LITERAL("File2.txt")), 0, 20, time2); | 137 base::FilePath(FILE_PATH_LITERAL("File2.txt")), 0, 20, time2); |
134 | 138 |
135 BlobStorageContext context; | 139 BlobStorageContext context; |
136 scoped_ptr<BlobDataHandle> blob_data_handle; | 140 scoped_ptr<BlobDataSnapshotHandle> blob_data_handle; |
137 | 141 |
138 // Test a blob referring to only data and a file. | 142 // Test a blob referring to only data and a file. |
139 blob_data_handle = context.AddFinishedBlob(blob_data1.get()); | 143 blob_data_handle = context.AddFinishedBlob(*blob_data1.get()); |
140 ASSERT_TRUE(blob_data_handle.get()); | 144 ASSERT_TRUE(blob_data_handle); |
141 EXPECT_TRUE(*(blob_data_handle->data()) == *blob_data1.get()); | 145 ASSERT_TRUE(blob_data_handle->data()); |
| 146 EXPECT_EQ(*(blob_data_handle->data()), *blob_data1); |
142 | 147 |
143 // Test a blob composed in part with another blob. | 148 // Test a blob composed in part with another blob. |
144 blob_data_handle = context.AddFinishedBlob(blob_data2.get()); | 149 blob_data_handle = context.AddFinishedBlob(*blob_data2.get()); |
145 ASSERT_TRUE(blob_data_handle.get()); | 150 ASSERT_TRUE(blob_data_handle); |
146 EXPECT_TRUE(*(blob_data_handle->data()) == *canonicalized_blob_data2.get()); | 151 ASSERT_TRUE(blob_data_handle->data()); |
| 152 EXPECT_EQ(*(blob_data_handle->data()), *canonicalized_blob_data2); |
147 | 153 |
148 blob_data_handle.reset(); | 154 blob_data_handle.reset(); |
149 { // Clean up for ASAN | 155 { // Clean up for ASAN |
150 base::RunLoop run_loop; | 156 base::RunLoop run_loop; |
151 run_loop.RunUntilIdle(); | 157 run_loop.RunUntilIdle(); |
152 } | 158 } |
153 } | 159 } |
154 | 160 |
155 TEST(BlobStorageContextTest, PublicBlobUrls) { | 161 TEST(BlobStorageContextTest, PublicBlobUrls) { |
156 BlobStorageContext context; | 162 BlobStorageContext context; |
157 BlobStorageHost host(&context); | 163 BlobStorageHost host(&context); |
158 base::MessageLoop fake_io_message_loop; | 164 base::MessageLoop fake_io_message_loop; |
159 | 165 |
160 // Build up a basic blob. | 166 // Build up a basic blob. |
161 const std::string kId("id"); | 167 const std::string kId("id"); |
162 SetupBasicBlob(&host, kId); | 168 SetupBasicBlob(&host, kId); |
163 | 169 |
164 // Now register a url for that blob. | 170 // Now register a url for that blob. |
165 GURL kUrl("blob:id"); | 171 GURL kUrl("blob:id"); |
166 EXPECT_TRUE(host.RegisterPublicBlobURL(kUrl, kId)); | 172 EXPECT_TRUE(host.RegisterPublicBlobURL(kUrl, kId)); |
167 scoped_ptr<BlobDataHandle> blob_data_handle = | 173 scoped_ptr<BlobDataSnapshotHandle> blob_data_handle = |
168 context.GetBlobDataFromPublicURL(kUrl); | 174 context.GetBlobDataFromPublicURL(kUrl); |
169 ASSERT_TRUE(blob_data_handle.get()); | 175 ASSERT_TRUE(blob_data_handle.get()); |
170 EXPECT_EQ(kId, blob_data_handle->data()->uuid()); | 176 EXPECT_EQ(kId, blob_data_handle->data()->uuid()); |
171 blob_data_handle.reset(); | 177 blob_data_handle.reset(); |
172 { // Clean up for ASAN | 178 { // Clean up for ASAN |
173 base::RunLoop run_loop; | 179 base::RunLoop run_loop; |
174 run_loop.RunUntilIdle(); | 180 run_loop.RunUntilIdle(); |
175 } | 181 } |
176 | 182 |
177 // The url registration should keep the blob alive even after | 183 // The url registration should keep the blob alive even after |
(...skipping 20 matching lines...) Expand all Loading... |
198 base::MessageLoop fake_io_message_loop; | 204 base::MessageLoop fake_io_message_loop; |
199 | 205 |
200 // Build up a basic blob and register a url | 206 // Build up a basic blob and register a url |
201 const std::string kId("id"); | 207 const std::string kId("id"); |
202 GURL kUrl("blob:id"); | 208 GURL kUrl("blob:id"); |
203 SetupBasicBlob(host.get(), kId); | 209 SetupBasicBlob(host.get(), kId); |
204 EXPECT_TRUE(host->RegisterPublicBlobURL(kUrl, kId)); | 210 EXPECT_TRUE(host->RegisterPublicBlobURL(kUrl, kId)); |
205 | 211 |
206 // All should disappear upon host deletion. | 212 // All should disappear upon host deletion. |
207 host.reset(); | 213 host.reset(); |
208 scoped_ptr<BlobDataHandle> handle = context.GetBlobDataFromPublicURL(kUrl); | 214 scoped_ptr<BlobDataSnapshotHandle> handle = |
| 215 context.GetBlobDataFromPublicURL(kUrl); |
209 EXPECT_TRUE(!handle.get()); | 216 EXPECT_TRUE(!handle.get()); |
210 handle = context.GetBlobDataFromUUID(kId); | 217 handle = context.GetBlobDataFromUUID(kId); |
211 EXPECT_TRUE(!handle.get()); | 218 EXPECT_TRUE(!handle.get()); |
212 } | 219 } |
213 | 220 |
214 TEST(BlobStorageContextTest, EarlyContextDeletion) { | 221 TEST(BlobStorageContextTest, EarlyContextDeletion) { |
215 scoped_ptr<BlobStorageContext> context(new BlobStorageContext); | 222 scoped_ptr<BlobStorageContext> context(new BlobStorageContext); |
216 BlobStorageHost host(context.get()); | 223 BlobStorageHost host(context.get()); |
217 base::MessageLoop fake_io_message_loop; | 224 base::MessageLoop fake_io_message_loop; |
218 | 225 |
219 // Deleting the context should not induce crashes. | 226 // Deleting the context should not induce crashes. |
220 context.reset(); | 227 context.reset(); |
221 | 228 |
222 const std::string kId("id"); | 229 const std::string kId("id"); |
223 GURL kUrl("blob:id"); | 230 GURL kUrl("blob:id"); |
224 EXPECT_FALSE(host.StartBuildingBlob(kId)); | 231 EXPECT_FALSE(host.StartBuildingBlob(kId)); |
225 BlobData::Item item; | 232 DataElement item; |
226 item.SetToBytes("1", 1); | 233 item.SetToBytes("1", 1); |
227 EXPECT_FALSE(host.AppendBlobDataItem(kId, item)); | 234 EXPECT_FALSE(host.AppendBlobDataItem(kId, item)); |
228 EXPECT_FALSE(host.FinishBuildingBlob(kId, "text/plain")); | 235 EXPECT_FALSE(host.FinishBuildingBlob(kId, "text/plain")); |
229 EXPECT_FALSE(host.RegisterPublicBlobURL(kUrl, kId)); | 236 EXPECT_FALSE(host.RegisterPublicBlobURL(kUrl, kId)); |
230 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); | 237 EXPECT_FALSE(host.IncrementBlobRefCount(kId)); |
231 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); | 238 EXPECT_FALSE(host.DecrementBlobRefCount(kId)); |
232 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl)); | 239 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl)); |
233 } | 240 } |
234 | 241 |
235 // TODO(michaeln): tests for the depcrecated url stuff | 242 // TODO(michaeln): tests for the depcrecated url stuff |
236 | 243 |
237 } // namespace content | 244 } // namespace content |
OLD | NEW |