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
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_database.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "content/browser/service_worker/service_worker_database.pb.h"
14 #include "content/common/service_worker/service_worker_types.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
22 typedef ServiceWorkerDatabase::RegistrationData RegistrationData
;
23 typedef ServiceWorkerDatabase::ResourceRecord Resource
;
30 AvailableIds() : reg_id(-1), res_id(-1), ver_id(-1) {}
34 GURL
URL(const GURL
& origin
, const std::string
& path
) {
35 EXPECT_TRUE(origin
.is_valid());
36 EXPECT_EQ(origin
, origin
.GetOrigin());
37 GURL
out(origin
.spec() + path
);
38 EXPECT_TRUE(out
.is_valid());
42 Resource
CreateResource(int64 resource_id
, const GURL
& url
, uint64 size_bytes
) {
43 EXPECT_TRUE(url
.is_valid());
44 return Resource(resource_id
, url
, size_bytes
);
47 ServiceWorkerDatabase
* CreateDatabase(const base::FilePath
& path
) {
48 return new ServiceWorkerDatabase(path
);
51 ServiceWorkerDatabase
* CreateDatabaseInMemory() {
52 return new ServiceWorkerDatabase(base::FilePath());
55 void VerifyRegistrationData(const RegistrationData
& expected
,
56 const RegistrationData
& actual
) {
57 EXPECT_EQ(expected
.registration_id
, actual
.registration_id
);
58 EXPECT_EQ(expected
.scope
, actual
.scope
);
59 EXPECT_EQ(expected
.script
, actual
.script
);
60 EXPECT_EQ(expected
.version_id
, actual
.version_id
);
61 EXPECT_EQ(expected
.is_active
, actual
.is_active
);
62 EXPECT_EQ(expected
.has_fetch_handler
, actual
.has_fetch_handler
);
63 EXPECT_EQ(expected
.last_update_check
, actual
.last_update_check
);
64 EXPECT_EQ(expected
.resources_total_size_bytes
,
65 actual
.resources_total_size_bytes
);
68 void VerifyResourceRecords(const std::vector
<Resource
>& expected
,
69 const std::vector
<Resource
>& actual
) {
70 ASSERT_EQ(expected
.size(), actual
.size());
71 for (size_t i
= 0; i
< expected
.size(); ++i
) {
72 EXPECT_EQ(expected
[i
].resource_id
, actual
[i
].resource_id
);
73 EXPECT_EQ(expected
[i
].url
, actual
[i
].url
);
74 EXPECT_EQ(expected
[i
].size_bytes
, actual
[i
].size_bytes
);
80 TEST(ServiceWorkerDatabaseTest
, OpenDatabase
) {
81 base::ScopedTempDir database_dir
;
82 ASSERT_TRUE(database_dir
.CreateUniqueTempDir());
83 scoped_ptr
<ServiceWorkerDatabase
> database(
84 CreateDatabase(database_dir
.path()));
86 // Should be false because the database does not exist at the path.
87 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
88 database
->LazyOpen(false));
90 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->LazyOpen(true));
92 database
.reset(CreateDatabase(database_dir
.path()));
93 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->LazyOpen(false));
96 TEST(ServiceWorkerDatabaseTest
, OpenDatabase_InMemory
) {
97 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
99 // Should be false because the database does not exist in memory.
100 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
101 database
->LazyOpen(false));
103 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->LazyOpen(true));
104 database
.reset(CreateDatabaseInMemory());
106 // Should be false because the database is not persistent.
107 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
108 database
->LazyOpen(false));
111 TEST(ServiceWorkerDatabaseTest
, DatabaseVersion
) {
112 GURL
origin("http://example.com");
113 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
114 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->LazyOpen(true));
116 // Opening a new database does not write anything, so its schema version
118 int64 db_version
= -1;
119 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
120 database
->ReadDatabaseVersion(&db_version
));
121 EXPECT_EQ(0u, db_version
);
123 // First writing triggers database initialization and bumps the schema
125 std::vector
<ServiceWorkerDatabase::ResourceRecord
> resources
;
126 resources
.push_back(CreateResource(1, URL(origin
, "/resource"), 10));
127 ServiceWorkerDatabase::RegistrationData deleted_version
;
128 std::vector
<int64
> newly_purgeable_resources
;
129 ServiceWorkerDatabase::RegistrationData data
;
130 data
.resources_total_size_bytes
= 10;
131 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
132 database
->WriteRegistration(
133 data
, resources
, &deleted_version
, &newly_purgeable_resources
));
135 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
136 database
->ReadDatabaseVersion(&db_version
));
137 EXPECT_LT(0, db_version
);
140 TEST(ServiceWorkerDatabaseTest
, DiskCacheMigrationState
) {
141 GURL
origin("http://example.com");
142 base::ScopedTempDir database_dir
;
143 ASSERT_TRUE(database_dir
.CreateUniqueTempDir());
144 scoped_ptr
<ServiceWorkerDatabase
> database(
145 CreateDatabase(database_dir
.path()));
147 // An empty database should return false.
148 bool migration_needed
= false;
149 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
150 database
->IsDiskCacheMigrationNeeded(&migration_needed
));
151 EXPECT_FALSE(migration_needed
);
152 bool deletion_needed
= false;
153 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
154 database
->IsOldDiskCacheDeletionNeeded(&deletion_needed
));
155 EXPECT_FALSE(deletion_needed
);
157 // Simulate an existing database created before diskcache migration.
158 database
->set_skip_writing_diskcache_migration_state_on_init_for_testing();
159 std::vector
<ServiceWorkerDatabase::ResourceRecord
> resources
;
160 ServiceWorkerDatabase::RegistrationData deleted_version
;
161 std::vector
<int64
> newly_purgeable_resources
;
162 ServiceWorkerDatabase::RegistrationData data
;
163 data
.registration_id
= 100;
164 data
.scope
= URL(origin
, "/foo");
165 data
.script
= URL(origin
, "/script.js");
166 data
.version_id
= 200;
167 data
.resources_total_size_bytes
= 300;
168 resources
.push_back(CreateResource(1, data
.script
, 300));
169 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
170 database
->WriteRegistration(data
, resources
, &deleted_version
,
171 &newly_purgeable_resources
));
172 migration_needed
= false;
173 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
174 database
->IsDiskCacheMigrationNeeded(&migration_needed
));
175 ASSERT_TRUE(migration_needed
);
176 deletion_needed
= false;
177 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
178 database
->IsOldDiskCacheDeletionNeeded(&deletion_needed
));
179 ASSERT_TRUE(deletion_needed
);
181 // Opening the existing database should not update the migration states.
182 database
.reset(CreateDatabase(database_dir
.path()));
183 migration_needed
= false;
184 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
185 database
->IsDiskCacheMigrationNeeded(&migration_needed
));
186 EXPECT_TRUE(migration_needed
);
187 deletion_needed
= false;
188 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
189 database
->IsOldDiskCacheDeletionNeeded(&deletion_needed
));
190 EXPECT_TRUE(deletion_needed
);
192 // Test SetDiskCacheMigrationNotNeeded().
193 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
194 database
->SetDiskCacheMigrationNotNeeded());
195 migration_needed
= false;
196 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
197 database
->IsDiskCacheMigrationNeeded(&migration_needed
));
198 EXPECT_FALSE(migration_needed
);
200 // Test SetOldDiskCacheDeletionNotNeeded().
201 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
202 database
->SetOldDiskCacheDeletionNotNeeded());
203 deletion_needed
= false;
204 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
205 database
->IsOldDiskCacheDeletionNeeded(&deletion_needed
));
206 EXPECT_FALSE(deletion_needed
);
209 TEST(ServiceWorkerDatabaseTest
, UpgradeSchemaToVersion2
) {
210 base::ScopedTempDir database_dir
;
211 ASSERT_TRUE(database_dir
.CreateUniqueTempDir());
212 scoped_ptr
<ServiceWorkerDatabase
> database(
213 CreateDatabase(database_dir
.path()));
215 GURL
origin("http://example.com");
217 // Add a registration to the database.
218 std::vector
<ServiceWorkerDatabase::ResourceRecord
> resources
;
219 ServiceWorkerDatabase::RegistrationData deleted_version
;
220 std::vector
<int64
> newly_purgeable_resources
;
221 ServiceWorkerDatabase::RegistrationData data
;
222 data
.registration_id
= 100;
223 data
.scope
= URL(origin
, "/foo");
224 data
.script
= URL(origin
, "/script1.js");
225 data
.version_id
= 200;
226 data
.resources_total_size_bytes
= 300;
227 resources
.push_back(CreateResource(1, data
.script
, 300));
228 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
229 database
->WriteRegistration(data
, resources
, &deleted_version
,
230 &newly_purgeable_resources
));
232 // Sanity check on current version.
233 int64 db_version
= -1;
234 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
235 database
->ReadDatabaseVersion(&db_version
));
236 EXPECT_LE(2, db_version
);
238 // Now delete the data that will be created in an upgrade to schema version 2,
239 // and reset the schema version to 1.
240 leveldb::WriteBatch batch
;
241 batch
.Delete("REGID_TO_ORIGIN:" + base::Int64ToString(data
.registration_id
));
242 batch
.Put("INITDATA_DB_VERSION", base::Int64ToString(1));
243 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->WriteBatch(&batch
));
245 // Make sure correct data got deleted.
248 ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
249 database
->ReadRegistrationOrigin(data
.registration_id
, &origin_out
));
251 // Close and reopen the database to verify the schema got updated.
252 database
.reset(CreateDatabase(database_dir
.path()));
253 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->LazyOpen(true));
255 // Verify version number.
256 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
257 database
->ReadDatabaseVersion(&db_version
));
258 EXPECT_LE(2, db_version
);
260 // And check that looking up origin for registration works.
262 ServiceWorkerDatabase::STATUS_OK
,
263 database
->ReadRegistrationOrigin(data
.registration_id
, &origin_out
));
264 EXPECT_EQ(origin
, origin_out
);
267 TEST(ServiceWorkerDatabaseTest
, GetNextAvailableIds
) {
268 base::ScopedTempDir database_dir
;
269 ASSERT_TRUE(database_dir
.CreateUniqueTempDir());
270 scoped_ptr
<ServiceWorkerDatabase
> database(
271 CreateDatabase(database_dir
.path()));
273 GURL
origin("http://example.com");
275 // The database has never been used, so returns initial values.
277 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->GetNextAvailableIds(
278 &ids
.reg_id
, &ids
.ver_id
, &ids
.res_id
));
279 EXPECT_EQ(0, ids
.reg_id
);
280 EXPECT_EQ(0, ids
.ver_id
);
281 EXPECT_EQ(0, ids
.res_id
);
283 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->LazyOpen(true));
284 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->GetNextAvailableIds(
285 &ids
.reg_id
, &ids
.ver_id
, &ids
.res_id
));
286 EXPECT_EQ(0, ids
.reg_id
);
287 EXPECT_EQ(0, ids
.ver_id
);
288 EXPECT_EQ(0, ids
.res_id
);
290 // Writing a registration bumps the next available ids.
291 std::vector
<Resource
> resources1
;
292 RegistrationData data1
;
293 ServiceWorkerDatabase::RegistrationData deleted_version
;
294 std::vector
<int64
> newly_purgeable_resources
;
295 data1
.registration_id
= 100;
296 data1
.scope
= URL(origin
, "/foo");
297 data1
.script
= URL(origin
, "/script1.js");
298 data1
.version_id
= 200;
299 data1
.resources_total_size_bytes
= 300;
300 resources1
.push_back(CreateResource(1, data1
.script
, 300));
301 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
302 database
->WriteRegistration(data1
, resources1
, &deleted_version
,
303 &newly_purgeable_resources
));
305 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->GetNextAvailableIds(
306 &ids
.reg_id
, &ids
.ver_id
, &ids
.res_id
));
307 EXPECT_EQ(101, ids
.reg_id
);
308 EXPECT_EQ(201, ids
.ver_id
);
309 EXPECT_EQ(0, ids
.res_id
);
311 // Writing uncommitted resources bumps the next available id.
312 const int64 kUncommittedIds
[] = {0, 1, 3, 5, 6, 10};
314 ServiceWorkerDatabase::STATUS_OK
,
315 database
->WriteUncommittedResourceIds(std::set
<int64
>(
316 kUncommittedIds
, kUncommittedIds
+ arraysize(kUncommittedIds
))));
318 ServiceWorkerDatabase::STATUS_OK
,
319 database
->GetNextAvailableIds(&ids
.reg_id
, &ids
.ver_id
, &ids
.res_id
));
320 EXPECT_EQ(101, ids
.reg_id
);
321 EXPECT_EQ(201, ids
.ver_id
);
322 EXPECT_EQ(11, ids
.res_id
);
324 // Writing purgeable resources bumps the next available id.
325 const int64 kPurgeableIds
[] = {4, 12, 16, 17, 20};
326 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
327 database
->WriteUncommittedResourceIds(std::set
<int64
>(
328 kPurgeableIds
, kPurgeableIds
+ arraysize(kPurgeableIds
))));
330 ServiceWorkerDatabase::STATUS_OK
,
331 database
->GetNextAvailableIds(&ids
.reg_id
, &ids
.ver_id
, &ids
.res_id
));
332 EXPECT_EQ(101, ids
.reg_id
);
333 EXPECT_EQ(201, ids
.ver_id
);
334 EXPECT_EQ(21, ids
.res_id
);
336 // Writing a registration whose ids are lower than the stored ones should not
337 // bump the next available ids.
338 RegistrationData data2
;
339 data2
.registration_id
= 10;
340 data2
.scope
= URL(origin
, "/bar");
341 data2
.script
= URL(origin
, "/script2.js");
342 data2
.version_id
= 20;
343 data2
.resources_total_size_bytes
= 400;
344 std::vector
<Resource
> resources2
;
345 resources2
.push_back(CreateResource(2, data2
.script
, 400));
346 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
347 database
->WriteRegistration(data2
, resources2
, &deleted_version
,
348 &newly_purgeable_resources
));
350 // Same with resources.
351 int64 kLowResourceId
= 15;
352 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
353 database
->WriteUncommittedResourceIds(
354 std::set
<int64
>(&kLowResourceId
, &kLowResourceId
+ 1)));
356 // Close and reopen the database to verify the stored values.
357 database
.reset(CreateDatabase(database_dir
.path()));
359 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->GetNextAvailableIds(
360 &ids
.reg_id
, &ids
.ver_id
, &ids
.res_id
));
361 EXPECT_EQ(101, ids
.reg_id
);
362 EXPECT_EQ(201, ids
.ver_id
);
363 EXPECT_EQ(21, ids
.res_id
);
366 TEST(ServiceWorkerDatabaseTest
, GetOriginsWithRegistrations
) {
367 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
369 std::set
<GURL
> origins
;
370 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
371 database
->GetOriginsWithRegistrations(&origins
));
372 EXPECT_TRUE(origins
.empty());
374 ServiceWorkerDatabase::RegistrationData deleted_version
;
375 std::vector
<int64
> newly_purgeable_resources
;
377 GURL
origin1("http://example.com");
378 RegistrationData data1
;
379 data1
.registration_id
= 123;
380 data1
.scope
= URL(origin1
, "/foo");
381 data1
.script
= URL(origin1
, "/script1.js");
382 data1
.version_id
= 456;
383 data1
.resources_total_size_bytes
= 100;
384 std::vector
<Resource
> resources1
;
385 resources1
.push_back(CreateResource(1, data1
.script
, 100));
386 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
387 database
->WriteRegistration(data1
, resources1
, &deleted_version
,
388 &newly_purgeable_resources
));
390 GURL
origin2("https://www.example.com");
391 RegistrationData data2
;
392 data2
.registration_id
= 234;
393 data2
.scope
= URL(origin2
, "/bar");
394 data2
.script
= URL(origin2
, "/script2.js");
395 data2
.version_id
= 567;
396 data2
.resources_total_size_bytes
= 200;
397 std::vector
<Resource
> resources2
;
398 resources2
.push_back(CreateResource(2, data2
.script
, 200));
399 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
400 database
->WriteRegistration(data2
, resources2
, &deleted_version
,
401 &newly_purgeable_resources
));
403 GURL
origin3("https://example.org");
404 RegistrationData data3
;
405 data3
.registration_id
= 345;
406 data3
.scope
= URL(origin3
, "/hoge");
407 data3
.script
= URL(origin3
, "/script3.js");
408 data3
.version_id
= 678;
409 data3
.resources_total_size_bytes
= 300;
410 std::vector
<Resource
> resources3
;
411 resources3
.push_back(CreateResource(3, data3
.script
, 300));
412 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
413 database
->WriteRegistration(data3
, resources3
, &deleted_version
,
414 &newly_purgeable_resources
));
416 // |origin3| has two registrations.
417 RegistrationData data4
;
418 data4
.registration_id
= 456;
419 data4
.scope
= URL(origin3
, "/fuga");
420 data4
.script
= URL(origin3
, "/script4.js");
421 data4
.version_id
= 789;
422 data4
.resources_total_size_bytes
= 400;
423 std::vector
<Resource
> resources4
;
424 resources4
.push_back(CreateResource(4, data4
.script
, 400));
425 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
426 database
->WriteRegistration(data4
, resources4
, &deleted_version
,
427 &newly_purgeable_resources
));
430 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
431 database
->GetOriginsWithRegistrations(&origins
));
432 EXPECT_EQ(3U, origins
.size());
433 EXPECT_TRUE(ContainsKey(origins
, origin1
));
434 EXPECT_TRUE(ContainsKey(origins
, origin2
));
435 EXPECT_TRUE(ContainsKey(origins
, origin3
));
437 // |origin3| has another registration, so should not remove it from the
438 // unique origin list.
439 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
440 database
->DeleteRegistration(data4
.registration_id
,
443 &newly_purgeable_resources
));
444 EXPECT_EQ(data4
.registration_id
, deleted_version
.registration_id
);
447 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
448 database
->GetOriginsWithRegistrations(&origins
));
449 EXPECT_EQ(3U, origins
.size());
450 EXPECT_TRUE(ContainsKey(origins
, origin1
));
451 EXPECT_TRUE(ContainsKey(origins
, origin2
));
452 EXPECT_TRUE(ContainsKey(origins
, origin3
));
454 // |origin3| should be removed from the unique origin list.
455 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
456 database
->DeleteRegistration(data3
.registration_id
,
459 &newly_purgeable_resources
));
460 EXPECT_EQ(data3
.registration_id
, deleted_version
.registration_id
);
463 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
464 database
->GetOriginsWithRegistrations(&origins
));
465 EXPECT_EQ(2U, origins
.size());
466 EXPECT_TRUE(ContainsKey(origins
, origin1
));
467 EXPECT_TRUE(ContainsKey(origins
, origin2
));
470 TEST(ServiceWorkerDatabaseTest
, GetRegistrationsForOrigin
) {
471 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
473 GURL
origin1("http://example.com");
474 GURL
origin2("https://www.example.com");
475 GURL
origin3("https://example.org");
477 std::vector
<RegistrationData
> registrations
;
478 std::vector
<std::vector
<Resource
>> resources_list
;
479 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
480 database
->GetRegistrationsForOrigin(origin1
, ®istrations
,
482 EXPECT_TRUE(registrations
.empty());
483 EXPECT_TRUE(resources_list
.empty());
485 ServiceWorkerDatabase::RegistrationData deleted_version
;
486 std::vector
<int64
> newly_purgeable_resources
;
488 RegistrationData data1
;
489 data1
.registration_id
= 100;
490 data1
.scope
= URL(origin1
, "/foo");
491 data1
.script
= URL(origin1
, "/script1.js");
492 data1
.version_id
= 1000;
493 data1
.resources_total_size_bytes
= 100;
494 std::vector
<Resource
> resources1
;
495 resources1
.push_back(CreateResource(1, data1
.script
, 100));
496 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
497 database
->WriteRegistration(data1
, resources1
, &deleted_version
,
498 &newly_purgeable_resources
));
500 registrations
.clear();
501 resources_list
.clear();
502 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
503 database
->GetRegistrationsForOrigin(origin1
, ®istrations
,
505 EXPECT_EQ(1U, registrations
.size());
506 VerifyRegistrationData(data1
, registrations
[0]);
507 EXPECT_EQ(1U, resources_list
.size());
508 VerifyResourceRecords(resources1
, resources_list
[0]);
510 RegistrationData data2
;
511 data2
.registration_id
= 200;
512 data2
.scope
= URL(origin2
, "/bar");
513 data2
.script
= URL(origin2
, "/script2.js");
514 data2
.version_id
= 2000;
515 data2
.resources_total_size_bytes
= 200;
516 std::vector
<Resource
> resources2
;
517 resources2
.push_back(CreateResource(2, data2
.script
, 200));
518 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
519 database
->WriteRegistration(data2
, resources2
, &deleted_version
,
520 &newly_purgeable_resources
));
522 registrations
.clear();
523 resources_list
.clear();
524 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
525 database
->GetRegistrationsForOrigin(origin2
, ®istrations
,
527 EXPECT_EQ(1U, registrations
.size());
528 VerifyRegistrationData(data2
, registrations
[0]);
529 EXPECT_EQ(1U, resources_list
.size());
530 VerifyResourceRecords(resources2
, resources_list
[0]);
532 RegistrationData data3
;
533 data3
.registration_id
= 300;
534 data3
.scope
= URL(origin3
, "/hoge");
535 data3
.script
= URL(origin3
, "/script3.js");
536 data3
.version_id
= 3000;
537 data3
.resources_total_size_bytes
= 300;
538 std::vector
<Resource
> resources3
;
539 resources3
.push_back(CreateResource(3, data3
.script
, 300));
540 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
541 database
->WriteRegistration(data3
, resources3
, &deleted_version
,
542 &newly_purgeable_resources
));
544 // |origin3| has two registrations.
545 RegistrationData data4
;
546 data4
.registration_id
= 400;
547 data4
.scope
= URL(origin3
, "/fuga");
548 data4
.script
= URL(origin3
, "/script4.js");
549 data4
.version_id
= 4000;
550 data4
.resources_total_size_bytes
= 400;
551 std::vector
<Resource
> resources4
;
552 resources4
.push_back(CreateResource(4, data4
.script
, 400));
553 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
554 database
->WriteRegistration(data4
, resources4
, &deleted_version
,
555 &newly_purgeable_resources
));
557 registrations
.clear();
558 resources_list
.clear();
559 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
560 database
->GetRegistrationsForOrigin(origin3
, ®istrations
,
562 EXPECT_EQ(2U, registrations
.size());
563 VerifyRegistrationData(data3
, registrations
[0]);
564 VerifyRegistrationData(data4
, registrations
[1]);
565 EXPECT_EQ(2U, resources_list
.size());
566 VerifyResourceRecords(resources3
, resources_list
[0]);
567 VerifyResourceRecords(resources4
, resources_list
[1]);
569 // The third parameter |opt_resources_list| to GetRegistrationsForOrigin()
570 // is optional. So, nullptr should be acceptable.
571 registrations
.clear();
573 ServiceWorkerDatabase::STATUS_OK
,
574 database
->GetRegistrationsForOrigin(origin1
, ®istrations
, nullptr));
575 EXPECT_EQ(1U, registrations
.size());
576 VerifyRegistrationData(data1
, registrations
[0]);
579 TEST(ServiceWorkerDatabaseTest
, GetAllRegistrations
) {
580 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
582 std::vector
<RegistrationData
> registrations
;
583 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
584 database
->GetAllRegistrations(®istrations
));
585 EXPECT_TRUE(registrations
.empty());
587 ServiceWorkerDatabase::RegistrationData deleted_version
;
588 std::vector
<int64
> newly_purgeable_resources
;
590 GURL
origin1("http://www1.example.com");
591 RegistrationData data1
;
592 data1
.registration_id
= 100;
593 data1
.scope
= URL(origin1
, "/foo");
594 data1
.script
= URL(origin1
, "/script1.js");
595 data1
.version_id
= 1000;
596 data1
.resources_total_size_bytes
= 100;
597 std::vector
<Resource
> resources1
;
598 resources1
.push_back(CreateResource(1, data1
.script
, 100));
599 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
600 database
->WriteRegistration(data1
, resources1
, &deleted_version
,
601 &newly_purgeable_resources
));
603 GURL
origin2("http://www2.example.com");
604 RegistrationData data2
;
605 data2
.registration_id
= 200;
606 data2
.scope
= URL(origin2
, "/bar");
607 data2
.script
= URL(origin2
, "/script2.js");
608 data2
.version_id
= 2000;
609 data2
.resources_total_size_bytes
= 200;
610 std::vector
<Resource
> resources2
;
611 resources2
.push_back(CreateResource(2, data2
.script
, 200));
612 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
613 database
->WriteRegistration(data2
, resources2
, &deleted_version
,
614 &newly_purgeable_resources
));
616 GURL
origin3("http://www3.example.com");
617 RegistrationData data3
;
618 data3
.registration_id
= 300;
619 data3
.scope
= URL(origin3
, "/hoge");
620 data3
.script
= URL(origin3
, "/script3.js");
621 data3
.version_id
= 3000;
622 data3
.resources_total_size_bytes
= 300;
623 std::vector
<Resource
> resources3
;
624 resources3
.push_back(CreateResource(3, data3
.script
, 300));
625 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
626 database
->WriteRegistration(data3
, resources3
, &deleted_version
,
627 &newly_purgeable_resources
));
629 // |origin3| has two registrations.
630 RegistrationData data4
;
631 data4
.registration_id
= 400;
632 data4
.scope
= URL(origin3
, "/fuga");
633 data4
.script
= URL(origin3
, "/script4.js");
634 data4
.version_id
= 4000;
635 data4
.resources_total_size_bytes
= 400;
636 std::vector
<Resource
> resources4
;
637 resources4
.push_back(CreateResource(4, data4
.script
, 400));
638 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
639 database
->WriteRegistration(data4
, resources4
, &deleted_version
,
640 &newly_purgeable_resources
));
642 registrations
.clear();
643 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
644 database
->GetAllRegistrations(®istrations
));
645 EXPECT_EQ(4U, registrations
.size());
646 VerifyRegistrationData(data1
, registrations
[0]);
647 VerifyRegistrationData(data2
, registrations
[1]);
648 VerifyRegistrationData(data3
, registrations
[2]);
649 VerifyRegistrationData(data4
, registrations
[3]);
652 TEST(ServiceWorkerDatabaseTest
, Registration_Basic
) {
653 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
655 GURL
origin("http://example.com");
656 RegistrationData data
;
657 data
.registration_id
= 100;
658 data
.scope
= URL(origin
, "/foo");
659 data
.script
= URL(origin
, "/script.js");
660 data
.version_id
= 200;
661 data
.resources_total_size_bytes
= 10939 + 200;
663 std::vector
<Resource
> resources
;
664 resources
.push_back(CreateResource(1, URL(origin
, "/resource1"), 10939));
665 resources
.push_back(CreateResource(2, URL(origin
, "/resource2"), 200));
667 // Write a resource to the uncommitted list to make sure that writing
668 // registration removes resource ids associated with the registration from
669 // the uncommitted list.
670 std::set
<int64
> uncommitted_ids
;
671 uncommitted_ids
.insert(resources
[0].resource_id
);
672 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
673 database
->WriteUncommittedResourceIds(uncommitted_ids
));
674 std::set
<int64
> uncommitted_ids_out
;
675 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
676 database
->GetUncommittedResourceIds(&uncommitted_ids_out
));
677 EXPECT_EQ(uncommitted_ids
, uncommitted_ids_out
);
679 ServiceWorkerDatabase::RegistrationData deleted_version
;
680 deleted_version
.version_id
= 222; // Dummy initial value
681 std::vector
<int64
> newly_purgeable_resources
;
683 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
684 database
->WriteRegistration(
685 data
, resources
, &deleted_version
, &newly_purgeable_resources
));
686 EXPECT_EQ(kInvalidServiceWorkerVersionId
, deleted_version
.version_id
);
687 EXPECT_TRUE(newly_purgeable_resources
.empty());
689 // Make sure that the registration and resource records are stored.
690 RegistrationData data_out
;
691 std::vector
<Resource
> resources_out
;
692 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
693 database
->ReadRegistration(
694 data
.registration_id
, origin
, &data_out
, &resources_out
));
695 VerifyRegistrationData(data
, data_out
);
696 VerifyResourceRecords(resources
, resources_out
);
699 ServiceWorkerDatabase::STATUS_OK
,
700 database
->ReadRegistrationOrigin(data
.registration_id
, &origin_out
));
701 EXPECT_EQ(origin
, origin_out
);
703 // Make sure that the resource is removed from the uncommitted list.
704 uncommitted_ids_out
.clear();
705 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
706 database
->GetUncommittedResourceIds(&uncommitted_ids_out
));
707 EXPECT_TRUE(uncommitted_ids_out
.empty());
709 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
710 database
->DeleteRegistration(data
.registration_id
,
713 &newly_purgeable_resources
));
714 EXPECT_EQ(data
.version_id
, deleted_version
.version_id
);
715 ASSERT_EQ(resources
.size(), newly_purgeable_resources
.size());
716 for (size_t i
= 0; i
< resources
.size(); ++i
)
717 EXPECT_EQ(newly_purgeable_resources
[i
], resources
[i
].resource_id
);
719 // Make sure that the registration and resource records are gone.
720 resources_out
.clear();
721 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
722 database
->ReadRegistration(
723 data
.registration_id
, origin
, &data_out
, &resources_out
));
724 EXPECT_TRUE(resources_out
.empty());
726 ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
727 database
->ReadRegistrationOrigin(data
.registration_id
, &origin_out
));
729 // Resources should be purgeable because these are no longer referred.
730 std::set
<int64
> purgeable_ids_out
;
731 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
732 database
->GetPurgeableResourceIds(&purgeable_ids_out
));
733 EXPECT_EQ(2u, purgeable_ids_out
.size());
734 EXPECT_TRUE(ContainsKey(purgeable_ids_out
, resources
[0].resource_id
));
735 EXPECT_TRUE(ContainsKey(purgeable_ids_out
, resources
[1].resource_id
));
738 TEST(ServiceWorkerDatabaseTest
, DeleteNonExistentRegistration
) {
739 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
741 GURL
origin("http://example.com");
742 RegistrationData data
;
743 data
.registration_id
= 100;
744 data
.scope
= URL(origin
, "/foo");
745 data
.script
= URL(origin
, "/script.js");
746 data
.version_id
= 200;
747 data
.resources_total_size_bytes
= 19 + 29129;
749 std::vector
<Resource
> resources
;
750 resources
.push_back(CreateResource(1, URL(origin
, "/resource1"), 19));
751 resources
.push_back(CreateResource(2, URL(origin
, "/resource2"), 29129));
753 const int64 kNonExistentRegistrationId
= 999;
754 const int64 kArbitraryVersionId
= 222; // Used as a dummy initial value
756 ServiceWorkerDatabase::RegistrationData deleted_version
;
757 deleted_version
.version_id
= kArbitraryVersionId
;
758 std::vector
<int64
> newly_purgeable_resources
;
759 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
760 database
->WriteRegistration(
761 data
, resources
, &deleted_version
, &newly_purgeable_resources
));
762 EXPECT_EQ(kInvalidServiceWorkerVersionId
, deleted_version
.version_id
);
763 EXPECT_TRUE(newly_purgeable_resources
.empty());
765 // Delete from an origin that has a registration.
766 deleted_version
.version_id
= kArbitraryVersionId
;
767 newly_purgeable_resources
.clear();
768 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
769 database
->DeleteRegistration(kNonExistentRegistrationId
,
772 &newly_purgeable_resources
));
773 EXPECT_EQ(kInvalidServiceWorkerVersionId
, deleted_version
.version_id
);
774 EXPECT_TRUE(newly_purgeable_resources
.empty());
776 // Delete from an origin that has no registration.
777 deleted_version
.version_id
= kArbitraryVersionId
;
778 newly_purgeable_resources
.clear();
779 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
780 database
->DeleteRegistration(kNonExistentRegistrationId
,
781 GURL("http://example.net"),
783 &newly_purgeable_resources
));
784 EXPECT_EQ(kInvalidServiceWorkerVersionId
, deleted_version
.version_id
);
785 EXPECT_TRUE(newly_purgeable_resources
.empty());
788 TEST(ServiceWorkerDatabaseTest
, Registration_Overwrite
) {
789 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
791 GURL
origin("http://example.com");
792 RegistrationData data
;
793 data
.registration_id
= 100;
794 data
.scope
= URL(origin
, "/foo");
795 data
.script
= URL(origin
, "/script.js");
796 data
.version_id
= 200;
797 data
.resources_total_size_bytes
= 10 + 11;
799 std::vector
<Resource
> resources1
;
800 resources1
.push_back(CreateResource(1, URL(origin
, "/resource1"), 10));
801 resources1
.push_back(CreateResource(2, URL(origin
, "/resource2"), 11));
803 ServiceWorkerDatabase::RegistrationData deleted_version
;
804 deleted_version
.version_id
= 222; // Dummy inital value
805 std::vector
<int64
> newly_purgeable_resources
;
808 ServiceWorkerDatabase::STATUS_OK
,
809 database
->WriteRegistration(
810 data
, resources1
, &deleted_version
, &newly_purgeable_resources
));
811 EXPECT_EQ(kInvalidServiceWorkerVersionId
, deleted_version
.version_id
);
812 EXPECT_TRUE(newly_purgeable_resources
.empty());
814 // Make sure that the registration and resource records are stored.
815 RegistrationData data_out
;
816 std::vector
<Resource
> resources_out
;
817 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->ReadRegistration(
818 data
.registration_id
, origin
, &data_out
, &resources_out
));
819 VerifyRegistrationData(data
, data_out
);
820 VerifyResourceRecords(resources1
, resources_out
);
822 // Update the registration.
823 RegistrationData updated_data
= data
;
824 updated_data
.version_id
= data
.version_id
+ 1;
825 updated_data
.resources_total_size_bytes
= 12 + 13;
826 std::vector
<Resource
> resources2
;
827 resources2
.push_back(CreateResource(3, URL(origin
, "/resource3"), 12));
828 resources2
.push_back(CreateResource(4, URL(origin
, "/resource4"), 13));
830 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
831 database
->WriteRegistration(updated_data
,
834 &newly_purgeable_resources
));
835 EXPECT_EQ(data
.version_id
, deleted_version
.version_id
);
836 ASSERT_EQ(resources1
.size(), newly_purgeable_resources
.size());
837 for (size_t i
= 0; i
< resources1
.size(); ++i
)
838 EXPECT_EQ(newly_purgeable_resources
[i
], resources1
[i
].resource_id
);
840 // Make sure that |updated_data| is stored and resources referred from |data|
841 // is moved to the purgeable list.
842 resources_out
.clear();
843 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->ReadRegistration(
844 updated_data
.registration_id
, origin
, &data_out
, &resources_out
));
845 VerifyRegistrationData(updated_data
, data_out
);
846 VerifyResourceRecords(resources2
, resources_out
);
848 std::set
<int64
> purgeable_ids_out
;
849 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
850 database
->GetPurgeableResourceIds(&purgeable_ids_out
));
851 EXPECT_EQ(2u, purgeable_ids_out
.size());
852 EXPECT_TRUE(ContainsKey(purgeable_ids_out
, resources1
[0].resource_id
));
853 EXPECT_TRUE(ContainsKey(purgeable_ids_out
, resources1
[1].resource_id
));
856 TEST(ServiceWorkerDatabaseTest
, Registration_Multiple
) {
857 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
858 GURL
origin("http://example.com");
860 ServiceWorkerDatabase::RegistrationData deleted_version
;
861 std::vector
<int64
> newly_purgeable_resources
;
863 // Add registration1.
864 RegistrationData data1
;
865 data1
.registration_id
= 100;
866 data1
.scope
= URL(origin
, "/foo");
867 data1
.script
= URL(origin
, "/script1.js");
868 data1
.version_id
= 200;
869 data1
.resources_total_size_bytes
= 1451 + 15234;
871 std::vector
<Resource
> resources1
;
872 resources1
.push_back(CreateResource(1, URL(origin
, "/resource1"), 1451));
873 resources1
.push_back(CreateResource(2, URL(origin
, "/resource2"), 15234));
875 ServiceWorkerDatabase::STATUS_OK
,
876 database
->WriteRegistration(
877 data1
, resources1
, &deleted_version
, &newly_purgeable_resources
));
879 // Add registration2.
880 RegistrationData data2
;
881 data2
.registration_id
= 101;
882 data2
.scope
= URL(origin
, "/bar");
883 data2
.script
= URL(origin
, "/script2.js");
884 data2
.version_id
= 201;
885 data2
.resources_total_size_bytes
= 5 + 6;
887 std::vector
<Resource
> resources2
;
888 resources2
.push_back(CreateResource(3, URL(origin
, "/resource3"), 5));
889 resources2
.push_back(CreateResource(4, URL(origin
, "/resource4"), 6));
891 ServiceWorkerDatabase::STATUS_OK
,
892 database
->WriteRegistration(
893 data2
, resources2
, &deleted_version
, &newly_purgeable_resources
));
895 // Make sure that registration1 is stored.
896 RegistrationData data_out
;
897 std::vector
<Resource
> resources_out
;
898 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->ReadRegistration(
899 data1
.registration_id
, origin
, &data_out
, &resources_out
));
900 VerifyRegistrationData(data1
, data_out
);
901 VerifyResourceRecords(resources1
, resources_out
);
904 ServiceWorkerDatabase::STATUS_OK
,
905 database
->ReadRegistrationOrigin(data1
.registration_id
, &origin_out
));
906 EXPECT_EQ(origin
, origin_out
);
908 // Make sure that registration2 is also stored.
909 resources_out
.clear();
910 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->ReadRegistration(
911 data2
.registration_id
, origin
, &data_out
, &resources_out
));
912 VerifyRegistrationData(data2
, data_out
);
913 VerifyResourceRecords(resources2
, resources_out
);
915 ServiceWorkerDatabase::STATUS_OK
,
916 database
->ReadRegistrationOrigin(data2
.registration_id
, &origin_out
));
917 EXPECT_EQ(origin
, origin_out
);
919 std::set
<int64
> purgeable_ids_out
;
920 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
921 database
->GetPurgeableResourceIds(&purgeable_ids_out
));
922 EXPECT_TRUE(purgeable_ids_out
.empty());
924 // Delete registration1.
925 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
926 database
->DeleteRegistration(data1
.registration_id
,
929 &newly_purgeable_resources
));
930 EXPECT_EQ(data1
.registration_id
, deleted_version
.registration_id
);
932 // Make sure that registration1 is gone.
933 resources_out
.clear();
934 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
935 database
->ReadRegistration(
936 data1
.registration_id
, origin
, &data_out
, &resources_out
));
937 EXPECT_TRUE(resources_out
.empty());
939 ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
940 database
->ReadRegistrationOrigin(data1
.registration_id
, &origin_out
));
942 purgeable_ids_out
.clear();
943 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
944 database
->GetPurgeableResourceIds(&purgeable_ids_out
));
945 EXPECT_EQ(2u, purgeable_ids_out
.size());
946 EXPECT_TRUE(ContainsKey(purgeable_ids_out
, resources1
[0].resource_id
));
947 EXPECT_TRUE(ContainsKey(purgeable_ids_out
, resources1
[1].resource_id
));
949 // Make sure that registration2 is still alive.
950 resources_out
.clear();
951 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->ReadRegistration(
952 data2
.registration_id
, origin
, &data_out
, &resources_out
));
953 VerifyRegistrationData(data2
, data_out
);
954 VerifyResourceRecords(resources2
, resources_out
);
956 ServiceWorkerDatabase::STATUS_OK
,
957 database
->ReadRegistrationOrigin(data2
.registration_id
, &origin_out
));
958 EXPECT_EQ(origin
, origin_out
);
961 TEST(ServiceWorkerDatabaseTest
, Registration_UninitializedDatabase
) {
962 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
963 const GURL
origin("http://example.com");
965 // Should be failed because the database does not exist.
966 RegistrationData data_out
;
967 std::vector
<Resource
> resources_out
;
968 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
969 database
->ReadRegistration(
970 100, origin
, &data_out
, &resources_out
));
971 EXPECT_EQ(kInvalidServiceWorkerRegistrationId
, data_out
.registration_id
);
972 EXPECT_TRUE(resources_out
.empty());
974 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
975 database
->ReadRegistrationOrigin(100, &origin_out
));
977 // Deleting non-existent registration should succeed.
978 RegistrationData deleted_version
;
979 std::vector
<int64
> newly_purgeable_resources
;
980 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
981 database
->DeleteRegistration(
982 100, origin
, &deleted_version
, &newly_purgeable_resources
));
983 EXPECT_EQ(kInvalidServiceWorkerVersionId
, deleted_version
.version_id
);
984 EXPECT_TRUE(newly_purgeable_resources
.empty());
986 // Actually create a new database, but not initialized yet.
987 database
->LazyOpen(true);
989 // Should be failed because the database is not initialized.
990 ASSERT_EQ(ServiceWorkerDatabase::UNINITIALIZED
, database
->state_
);
991 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
992 database
->ReadRegistration(
993 100, origin
, &data_out
, &resources_out
));
994 EXPECT_EQ(kInvalidServiceWorkerRegistrationId
, data_out
.registration_id
);
995 EXPECT_TRUE(resources_out
.empty());
996 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
997 database
->ReadRegistrationOrigin(100, &origin_out
));
999 // Deleting non-existent registration should succeed.
1000 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1001 database
->DeleteRegistration(
1002 100, origin
, &deleted_version
, &newly_purgeable_resources
));
1003 EXPECT_EQ(kInvalidServiceWorkerVersionId
, deleted_version
.version_id
);
1004 EXPECT_TRUE(newly_purgeable_resources
.empty());
1007 TEST(ServiceWorkerDatabaseTest
, UserData_Basic
) {
1008 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
1009 const GURL
kOrigin("http://example.com");
1011 // Add a registration.
1012 RegistrationData data
;
1013 data
.registration_id
= 100;
1014 data
.scope
= URL(kOrigin
, "/foo");
1015 data
.script
= URL(kOrigin
, "/script.js");
1016 data
.version_id
= 200;
1017 data
.resources_total_size_bytes
= 100;
1018 std::vector
<Resource
> resources
;
1019 resources
.push_back(CreateResource(1, data
.script
, 100));
1020 ServiceWorkerDatabase::RegistrationData deleted_version
;
1021 std::vector
<int64
> newly_purgeable_resources
;
1022 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1023 database
->WriteRegistration(
1024 data
, resources
, &deleted_version
, &newly_purgeable_resources
));
1026 // Write user data associated with the stored registration.
1027 std::string user_data_out
;
1028 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1029 database
->WriteUserData(
1030 data
.registration_id
, kOrigin
, "key1", "data"));
1031 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1032 database
->ReadUserData(
1033 data
.registration_id
, "key1", &user_data_out
));
1034 EXPECT_EQ("data", user_data_out
);
1036 // Writing user data not associated with the stored registration should be
1038 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1039 database
->WriteUserData(300, kOrigin
, "key1", "data"));
1041 // Write empty user data for a different key.
1042 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1043 database
->WriteUserData(
1044 data
.registration_id
, kOrigin
, "key2", std::string()));
1045 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1046 database
->ReadUserData(
1047 data
.registration_id
, "key2", &user_data_out
));
1048 EXPECT_EQ(std::string(), user_data_out
);
1049 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1050 database
->ReadUserData(
1051 data
.registration_id
, "key1", &user_data_out
));
1052 EXPECT_EQ("data", user_data_out
);
1054 // Overwrite the existing user data.
1055 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1056 database
->WriteUserData(
1057 data
.registration_id
, kOrigin
, "key1", "overwrite"));
1058 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1059 database
->ReadUserData(
1060 data
.registration_id
, "key1", &user_data_out
));
1061 EXPECT_EQ("overwrite", user_data_out
);
1063 // Delete the user data.
1064 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1065 database
->DeleteUserData(data
.registration_id
, "key1"));
1066 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1067 database
->ReadUserData(
1068 data
.registration_id
, "key1", &user_data_out
));
1069 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1070 database
->ReadUserData(
1071 data
.registration_id
, "key2", &user_data_out
));
1072 EXPECT_EQ(std::string(), user_data_out
);
1075 TEST(ServiceWorkerDatabaseTest
, UserData_DataIsolation
) {
1076 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
1077 const GURL
kOrigin("http://example.com");
1079 // Add registration 1.
1080 RegistrationData data1
;
1081 data1
.registration_id
= 100;
1082 data1
.scope
= URL(kOrigin
, "/foo");
1083 data1
.script
= URL(kOrigin
, "/script1.js");
1084 data1
.version_id
= 200;
1085 data1
.resources_total_size_bytes
= 100;
1086 std::vector
<Resource
> resources1
;
1087 resources1
.push_back(CreateResource(1, data1
.script
, 100));
1089 // Add registration 2.
1090 RegistrationData data2
;
1091 data2
.registration_id
= 101;
1092 data2
.scope
= URL(kOrigin
, "/bar");
1093 data2
.script
= URL(kOrigin
, "/script2.js");
1094 data2
.version_id
= 201;
1095 data2
.resources_total_size_bytes
= 200;
1096 std::vector
<Resource
> resources2
;
1097 resources2
.push_back(CreateResource(2, data2
.script
, 200));
1099 ServiceWorkerDatabase::RegistrationData deleted_version
;
1100 std::vector
<int64
> newly_purgeable_resources
;
1101 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1102 database
->WriteRegistration(data1
, resources1
, &deleted_version
,
1103 &newly_purgeable_resources
));
1104 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1105 database
->WriteRegistration(data2
, resources2
, &deleted_version
,
1106 &newly_purgeable_resources
));
1108 // Write user data associated with the registration1.
1109 std::string user_data_out
;
1110 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1111 database
->WriteUserData(
1112 data1
.registration_id
, kOrigin
, "key", "data1"));
1113 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1114 database
->ReadUserData(
1115 data1
.registration_id
, "key", &user_data_out
));
1116 EXPECT_EQ("data1", user_data_out
);
1117 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1118 database
->ReadUserData(
1119 data2
.registration_id
, "key", &user_data_out
));
1121 // Write user data associated with the registration2. This shouldn't overwrite
1122 // the data associated with registration1.
1123 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1124 database
->WriteUserData(
1125 data2
.registration_id
, kOrigin
, "key", "data2"));
1126 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1127 database
->ReadUserData(
1128 data1
.registration_id
, "key", &user_data_out
));
1129 EXPECT_EQ("data1", user_data_out
);
1130 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1131 database
->ReadUserData(
1132 data2
.registration_id
, "key", &user_data_out
));
1133 EXPECT_EQ("data2", user_data_out
);
1135 // Get all registrations with user data.
1136 std::vector
<std::pair
<int64
, std::string
>> user_data_list
;
1137 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1138 database
->ReadUserDataForAllRegistrations("key", &user_data_list
));
1139 EXPECT_EQ(2u, user_data_list
.size());
1140 EXPECT_EQ(data1
.registration_id
, user_data_list
[0].first
);
1141 EXPECT_EQ("data1", user_data_list
[0].second
);
1142 EXPECT_EQ(data2
.registration_id
, user_data_list
[1].first
);
1143 EXPECT_EQ("data2", user_data_list
[1].second
);
1145 // Delete the data associated with the registration2. This shouldn't delete
1146 // the data associated with registration1.
1147 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1148 database
->DeleteUserData(data2
.registration_id
, "key"));
1149 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1150 database
->ReadUserData(
1151 data1
.registration_id
, "key", &user_data_out
));
1152 EXPECT_EQ("data1", user_data_out
);
1153 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1154 database
->ReadUserData(
1155 data2
.registration_id
, "key", &user_data_out
));
1157 // And again get all registrations with user data.
1158 user_data_list
.clear();
1159 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1160 database
->ReadUserDataForAllRegistrations("key", &user_data_list
));
1161 EXPECT_EQ(1u, user_data_list
.size());
1162 EXPECT_EQ(data1
.registration_id
, user_data_list
[0].first
);
1163 EXPECT_EQ("data1", user_data_list
[0].second
);
1166 TEST(ServiceWorkerDatabaseTest
, UserData_DeleteRegistration
) {
1167 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
1168 const GURL
kOrigin("http://example.com");
1170 // Add registration 1.
1171 RegistrationData data1
;
1172 data1
.registration_id
= 100;
1173 data1
.scope
= URL(kOrigin
, "/foo");
1174 data1
.script
= URL(kOrigin
, "/script1.js");
1175 data1
.version_id
= 200;
1176 data1
.resources_total_size_bytes
= 100;
1177 std::vector
<Resource
> resources1
;
1178 resources1
.push_back(CreateResource(1, data1
.script
, 100));
1180 // Add registration 2.
1181 RegistrationData data2
;
1182 data2
.registration_id
= 101;
1183 data2
.scope
= URL(kOrigin
, "/bar");
1184 data2
.script
= URL(kOrigin
, "/script2.js");
1185 data2
.version_id
= 201;
1186 data2
.resources_total_size_bytes
= 200;
1187 std::vector
<Resource
> resources2
;
1188 resources2
.push_back(CreateResource(2, data2
.script
, 200));
1190 ServiceWorkerDatabase::RegistrationData deleted_version
;
1191 std::vector
<int64
> newly_purgeable_resources
;
1192 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1193 database
->WriteRegistration(data1
, resources1
, &deleted_version
,
1194 &newly_purgeable_resources
));
1195 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1196 database
->WriteRegistration(data2
, resources2
, &deleted_version
,
1197 &newly_purgeable_resources
));
1199 // Write user data associated with the registration1.
1200 std::string user_data_out
;
1201 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1202 database
->WriteUserData(
1203 data1
.registration_id
, kOrigin
, "key1", "data1"));
1204 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1205 database
->WriteUserData(
1206 data1
.registration_id
, kOrigin
, "key2", "data2"));
1207 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1208 database
->ReadUserData(
1209 data1
.registration_id
, "key1", &user_data_out
));
1210 ASSERT_EQ("data1", user_data_out
);
1211 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1212 database
->ReadUserData(
1213 data1
.registration_id
, "key2", &user_data_out
));
1214 ASSERT_EQ("data2", user_data_out
);
1216 // Write user data associated with the registration2.
1217 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1218 database
->WriteUserData(
1219 data2
.registration_id
, kOrigin
, "key3", "data3"));
1220 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1221 database
->ReadUserData(
1222 data2
.registration_id
, "key3", &user_data_out
));
1223 ASSERT_EQ("data3", user_data_out
);
1225 // Delete all data associated with the registration1. This shouldn't delete
1226 // the data associated with registration2.
1227 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1228 database
->DeleteRegistration(
1229 data1
.registration_id
, kOrigin
,
1230 &deleted_version
, &newly_purgeable_resources
));
1231 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1232 database
->ReadUserData(
1233 data1
.registration_id
, "key1", &user_data_out
));
1234 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1235 database
->ReadUserData(
1236 data1
.registration_id
, "key2", &user_data_out
));
1237 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1238 database
->ReadUserData(
1239 data2
.registration_id
, "key3", &user_data_out
));
1240 EXPECT_EQ("data3", user_data_out
);
1243 TEST(ServiceWorkerDatabaseTest
, UserData_UninitializedDatabase
) {
1244 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
1245 const GURL
kOrigin("http://example.com");
1247 // Should be failed because the database does not exist.
1248 std::string user_data_out
;
1249 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1250 database
->ReadUserData(100, "key", &user_data_out
));
1252 // Should be failed because the associated registration does not exist.
1253 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1254 database
->WriteUserData(100, kOrigin
, "key", "data"));
1256 // Deleting non-existent entry should succeed.
1257 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1258 database
->DeleteUserData(100, "key"));
1260 // Actually create a new database, but not initialized yet.
1261 database
->LazyOpen(true);
1263 // Should be failed because the database is not initialized.
1264 ASSERT_EQ(ServiceWorkerDatabase::UNINITIALIZED
, database
->state_
);
1265 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1266 database
->ReadUserData(100, "key", &user_data_out
));
1267 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1268 database
->WriteUserData(100, kOrigin
, "key", "data"));
1270 // Deleting non-existent entry should succeed.
1271 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1272 database
->DeleteUserData(100, "key"));
1275 TEST(ServiceWorkerDatabaseTest
, UpdateVersionToActive
) {
1276 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
1277 GURL
origin("http://example.com");
1279 ServiceWorkerDatabase::RegistrationData deleted_version
;
1280 std::vector
<int64
> newly_purgeable_resources
;
1282 // Should be false because a registration does not exist.
1283 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1284 database
->UpdateVersionToActive(0, origin
));
1286 // Add a registration.
1287 RegistrationData data
;
1288 data
.registration_id
= 100;
1289 data
.scope
= URL(origin
, "/foo");
1290 data
.script
= URL(origin
, "/script.js");
1291 data
.version_id
= 200;
1292 data
.is_active
= false;
1293 data
.resources_total_size_bytes
= 100;
1294 std::vector
<Resource
> resources
;
1295 resources
.push_back(CreateResource(1, data
.script
, 100));
1296 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1297 database
->WriteRegistration(data
, resources
, &deleted_version
,
1298 &newly_purgeable_resources
));
1300 // Make sure that the registration is stored.
1301 RegistrationData data_out
;
1302 std::vector
<Resource
> resources_out
;
1303 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1304 database
->ReadRegistration(
1305 data
.registration_id
, origin
, &data_out
, &resources_out
));
1306 VerifyRegistrationData(data
, data_out
);
1307 EXPECT_EQ(1u, resources_out
.size());
1309 // Activate the registration.
1310 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1311 database
->UpdateVersionToActive(data
.registration_id
, origin
));
1313 // Make sure that the registration is activated.
1314 resources_out
.clear();
1315 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1316 database
->ReadRegistration(
1317 data
.registration_id
, origin
, &data_out
, &resources_out
));
1318 RegistrationData expected_data
= data
;
1319 expected_data
.is_active
= true;
1320 VerifyRegistrationData(expected_data
, data_out
);
1321 EXPECT_EQ(1u, resources_out
.size());
1323 // Delete the registration.
1324 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1325 database
->DeleteRegistration(data
.registration_id
,
1328 &newly_purgeable_resources
));
1329 EXPECT_EQ(data
.registration_id
, deleted_version
.registration_id
);
1331 // Should be false because the registration is gone.
1332 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1333 database
->UpdateVersionToActive(data
.registration_id
, origin
));
1336 TEST(ServiceWorkerDatabaseTest
, UpdateLastCheckTime
) {
1337 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
1338 GURL
origin("http://example.com");
1339 ServiceWorkerDatabase::RegistrationData deleted_version
;
1340 std::vector
<int64
> newly_purgeable_resources
;
1342 // Should be false because a registration does not exist.
1343 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1344 database
->UpdateLastCheckTime(0, origin
, base::Time::Now()));
1346 // Add a registration.
1347 RegistrationData data
;
1348 data
.registration_id
= 100;
1349 data
.scope
= URL(origin
, "/foo");
1350 data
.script
= URL(origin
, "/script.js");
1351 data
.version_id
= 200;
1352 data
.last_update_check
= base::Time::Now();
1353 data
.resources_total_size_bytes
= 100;
1354 std::vector
<Resource
> resources
;
1355 resources
.push_back(CreateResource(1, data
.script
, 100));
1356 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1357 database
->WriteRegistration(data
, resources
, &deleted_version
,
1358 &newly_purgeable_resources
));
1360 // Make sure that the registration is stored.
1361 RegistrationData data_out
;
1362 std::vector
<Resource
> resources_out
;
1363 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1364 database
->ReadRegistration(
1365 data
.registration_id
, origin
, &data_out
, &resources_out
));
1366 VerifyRegistrationData(data
, data_out
);
1367 EXPECT_EQ(1u, resources_out
.size());
1369 // Update the last check time.
1370 base::Time updated_time
= base::Time::Now();
1371 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1372 database
->UpdateLastCheckTime(
1373 data
.registration_id
, origin
, updated_time
));
1375 // Make sure that the registration is updated.
1376 resources_out
.clear();
1377 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1378 database
->ReadRegistration(
1379 data
.registration_id
, origin
, &data_out
, &resources_out
));
1380 RegistrationData expected_data
= data
;
1381 expected_data
.last_update_check
= updated_time
;
1382 VerifyRegistrationData(expected_data
, data_out
);
1383 EXPECT_EQ(1u, resources_out
.size());
1385 // Delete the registration.
1386 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1387 database
->DeleteRegistration(data
.registration_id
,
1390 &newly_purgeable_resources
));
1391 EXPECT_EQ(data
.registration_id
, deleted_version
.registration_id
);
1393 // Should be false because the registration is gone.
1394 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1395 database
->UpdateLastCheckTime(
1396 data
.registration_id
, origin
, base::Time::Now()));
1399 TEST(ServiceWorkerDatabaseTest
, UncommittedResourceIds
) {
1400 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
1403 std::set
<int64
> ids1
;
1407 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1408 database
->WriteUncommittedResourceIds(ids1
));
1410 std::set
<int64
> ids_out
;
1411 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1412 database
->GetUncommittedResourceIds(&ids_out
));
1413 EXPECT_EQ(ids1
, ids_out
);
1416 std::set
<int64
> ids2
;
1419 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1420 database
->WriteUncommittedResourceIds(ids2
));
1423 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1424 database
->GetUncommittedResourceIds(&ids_out
));
1425 std::set
<int64
> expected
= base::STLSetUnion
<std::set
<int64
> >(ids1
, ids2
);
1426 EXPECT_EQ(expected
, ids_out
);
1429 std::set
<int64
> ids3
;
1432 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1433 database
->ClearUncommittedResourceIds(ids3
));
1436 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1437 database
->GetUncommittedResourceIds(&ids_out
));
1438 expected
= base::STLSetDifference
<std::set
<int64
> >(expected
, ids3
);
1439 EXPECT_EQ(expected
, ids_out
);
1442 TEST(ServiceWorkerDatabaseTest
, PurgeableResourceIds
) {
1443 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
1446 std::set
<int64
> ids1
;
1450 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1451 database
->WritePurgeableResourceIds(ids1
));
1453 std::set
<int64
> ids_out
;
1454 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1455 database
->GetPurgeableResourceIds(&ids_out
));
1456 EXPECT_EQ(ids1
, ids_out
);
1459 std::set
<int64
> ids2
;
1462 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1463 database
->WritePurgeableResourceIds(ids2
));
1466 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1467 database
->GetPurgeableResourceIds(&ids_out
));
1468 std::set
<int64
> expected
= base::STLSetUnion
<std::set
<int64
> >(ids1
, ids2
);
1469 EXPECT_EQ(expected
, ids_out
);
1472 std::set
<int64
> ids3
;
1475 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1476 database
->ClearPurgeableResourceIds(ids3
));
1479 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1480 database
->GetPurgeableResourceIds(&ids_out
));
1481 expected
= base::STLSetDifference
<std::set
<int64
> >(expected
, ids3
);
1482 EXPECT_EQ(expected
, ids_out
);
1485 TEST(ServiceWorkerDatabaseTest
, DeleteAllDataForOrigin
) {
1486 scoped_ptr
<ServiceWorkerDatabase
> database(CreateDatabaseInMemory());
1487 ServiceWorkerDatabase::RegistrationData deleted_version
;
1488 std::vector
<int64
> newly_purgeable_resources
;
1490 // Data associated with |origin1| will be removed.
1491 GURL
origin1("http://example.com");
1492 GURL
origin2("http://example.org");
1494 // |origin1| has two registrations (registration1 and registration2).
1495 RegistrationData data1
;
1496 data1
.registration_id
= 10;
1497 data1
.scope
= URL(origin1
, "/foo");
1498 data1
.script
= URL(origin1
, "/script1.js");
1499 data1
.version_id
= 100;
1500 data1
.resources_total_size_bytes
= 2013 + 512;
1502 std::vector
<Resource
> resources1
;
1503 resources1
.push_back(CreateResource(1, URL(origin1
, "/resource1"), 2013));
1504 resources1
.push_back(CreateResource(2, URL(origin1
, "/resource2"), 512));
1506 ServiceWorkerDatabase::STATUS_OK
,
1507 database
->WriteRegistration(
1508 data1
, resources1
, &deleted_version
, &newly_purgeable_resources
));
1510 ServiceWorkerDatabase::STATUS_OK
,
1511 database
->WriteUserData(
1512 data1
.registration_id
, origin1
, "key1", "data1"));
1514 ServiceWorkerDatabase::STATUS_OK
,
1515 database
->WriteUserData(
1516 data1
.registration_id
, origin1
, "key2", "data2"));
1518 RegistrationData data2
;
1519 data2
.registration_id
= 11;
1520 data2
.scope
= URL(origin1
, "/bar");
1521 data2
.script
= URL(origin1
, "/script2.js");
1522 data2
.version_id
= 101;
1523 data2
.resources_total_size_bytes
= 4 + 5;
1525 std::vector
<Resource
> resources2
;
1526 resources2
.push_back(CreateResource(3, URL(origin1
, "/resource3"), 4));
1527 resources2
.push_back(CreateResource(4, URL(origin1
, "/resource4"), 5));
1529 ServiceWorkerDatabase::STATUS_OK
,
1530 database
->WriteRegistration(
1531 data2
, resources2
, &deleted_version
, &newly_purgeable_resources
));
1533 ServiceWorkerDatabase::STATUS_OK
,
1534 database
->WriteUserData(
1535 data2
.registration_id
, origin1
, "key3", "data3"));
1537 ServiceWorkerDatabase::STATUS_OK
,
1538 database
->WriteUserData(
1539 data2
.registration_id
, origin1
, "key4", "data4"));
1541 // |origin2| has one registration (registration3).
1542 RegistrationData data3
;
1543 data3
.registration_id
= 12;
1544 data3
.scope
= URL(origin2
, "/hoge");
1545 data3
.script
= URL(origin2
, "/script3.js");
1546 data3
.version_id
= 102;
1547 data3
.resources_total_size_bytes
= 6 + 7;
1549 std::vector
<Resource
> resources3
;
1550 resources3
.push_back(CreateResource(5, URL(origin2
, "/resource5"), 6));
1551 resources3
.push_back(CreateResource(6, URL(origin2
, "/resource6"), 7));
1553 ServiceWorkerDatabase::STATUS_OK
,
1554 database
->WriteRegistration(
1555 data3
, resources3
, &deleted_version
, &newly_purgeable_resources
));
1557 ServiceWorkerDatabase::STATUS_OK
,
1558 database
->WriteUserData(
1559 data3
.registration_id
, origin2
, "key5", "data5"));
1561 ServiceWorkerDatabase::STATUS_OK
,
1562 database
->WriteUserData(
1563 data3
.registration_id
, origin2
, "key6", "data6"));
1565 std::set
<GURL
> origins_to_delete
;
1566 origins_to_delete
.insert(origin1
);
1567 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1568 database
->DeleteAllDataForOrigins(origins_to_delete
,
1569 &newly_purgeable_resources
));
1571 // |origin1| should be removed from the unique origin list.
1572 std::set
<GURL
> unique_origins
;
1573 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1574 database
->GetOriginsWithRegistrations(&unique_origins
));
1575 EXPECT_EQ(1u, unique_origins
.size());
1576 EXPECT_TRUE(ContainsKey(unique_origins
, origin2
));
1578 // The registrations for |origin1| should be removed.
1579 std::vector
<RegistrationData
> registrations
;
1581 ServiceWorkerDatabase::STATUS_OK
,
1582 database
->GetRegistrationsForOrigin(origin1
, ®istrations
, nullptr));
1583 EXPECT_TRUE(registrations
.empty());
1586 ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1587 database
->ReadRegistrationOrigin(data1
.registration_id
, &origin_out
));
1589 // The registration for |origin2| should not be removed.
1590 RegistrationData data_out
;
1591 std::vector
<Resource
> resources_out
;
1592 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->ReadRegistration(
1593 data3
.registration_id
, origin2
, &data_out
, &resources_out
));
1594 VerifyRegistrationData(data3
, data_out
);
1595 VerifyResourceRecords(resources3
, resources_out
);
1597 ServiceWorkerDatabase::STATUS_OK
,
1598 database
->ReadRegistrationOrigin(data3
.registration_id
, &origin_out
));
1599 EXPECT_EQ(origin2
, origin_out
);
1601 // The resources associated with |origin1| should be purgeable.
1602 std::set
<int64
> purgeable_ids_out
;
1603 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1604 database
->GetPurgeableResourceIds(&purgeable_ids_out
));
1605 EXPECT_EQ(4u, purgeable_ids_out
.size());
1606 EXPECT_TRUE(ContainsKey(purgeable_ids_out
, 1));
1607 EXPECT_TRUE(ContainsKey(purgeable_ids_out
, 2));
1608 EXPECT_TRUE(ContainsKey(purgeable_ids_out
, 3));
1609 EXPECT_TRUE(ContainsKey(purgeable_ids_out
, 4));
1611 // The user data associated with |origin1| should be removed.
1612 std::string user_data_out
;
1613 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1614 database
->ReadUserData(
1615 data1
.registration_id
, "key1", &user_data_out
));
1616 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1617 database
->ReadUserData(
1618 data1
.registration_id
, "key2", &user_data_out
));
1619 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1620 database
->ReadUserData(
1621 data2
.registration_id
, "key3", &user_data_out
));
1622 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND
,
1623 database
->ReadUserData(
1624 data2
.registration_id
, "key4", &user_data_out
));
1626 // The user data associated with |origin2| should not be removed.
1627 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1628 database
->ReadUserData(
1629 data3
.registration_id
, "key5", &user_data_out
));
1630 EXPECT_EQ("data5", user_data_out
);
1631 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
,
1632 database
->ReadUserData(
1633 data3
.registration_id
, "key6", &user_data_out
));
1634 EXPECT_EQ("data6", user_data_out
);
1637 TEST(ServiceWorkerDatabaseTest
, DestroyDatabase
) {
1638 base::ScopedTempDir database_dir
;
1639 ASSERT_TRUE(database_dir
.CreateUniqueTempDir());
1640 scoped_ptr
<ServiceWorkerDatabase
> database(
1641 CreateDatabase(database_dir
.path()));
1643 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->LazyOpen(true));
1644 ASSERT_TRUE(base::DirectoryExists(database_dir
.path()));
1646 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK
, database
->DestroyDatabase());
1647 ASSERT_FALSE(base::DirectoryExists(database_dir
.path()));
1650 } // namespace content