| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/browser/notifications/notification_database.h" | 5 #include "content/browser/notifications/notification_database.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 | 121 |
| 122 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, | 122 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 123 database->Open(false /* create_if_missing */)); | 123 database->Open(false /* create_if_missing */)); |
| 124 } | 124 } |
| 125 | 125 |
| 126 TEST_F(NotificationDatabaseTest, OpenCloseFileSystem) { | 126 TEST_F(NotificationDatabaseTest, OpenCloseFileSystem) { |
| 127 base::ScopedTempDir database_dir; | 127 base::ScopedTempDir database_dir; |
| 128 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); | 128 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 129 | 129 |
| 130 std::unique_ptr<NotificationDatabase> database( | 130 std::unique_ptr<NotificationDatabase> database( |
| 131 CreateDatabaseOnFileSystem(database_dir.path())); | 131 CreateDatabaseOnFileSystem(database_dir.GetPath())); |
| 132 | 132 |
| 133 // Should return false because the database does not exist on the file system. | 133 // Should return false because the database does not exist on the file system. |
| 134 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, | 134 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 135 database->Open(false /* create_if_missing */)); | 135 database->Open(false /* create_if_missing */)); |
| 136 | 136 |
| 137 // Should return true, indicating that the database could be created. | 137 // Should return true, indicating that the database could be created. |
| 138 EXPECT_EQ(NotificationDatabase::STATUS_OK, | 138 EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 139 database->Open(true /* create_if_missing */)); | 139 database->Open(true /* create_if_missing */)); |
| 140 | 140 |
| 141 EXPECT_TRUE(IsDatabaseOpen(database.get())); | 141 EXPECT_TRUE(IsDatabaseOpen(database.get())); |
| 142 EXPECT_FALSE(IsInMemoryDatabase(database.get())); | 142 EXPECT_FALSE(IsInMemoryDatabase(database.get())); |
| 143 | 143 |
| 144 // Close the database, and re-open it without attempting to create it because | 144 // Close the database, and re-open it without attempting to create it because |
| 145 // the files on the file system should still exist as expected. | 145 // the files on the file system should still exist as expected. |
| 146 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); | 146 database.reset(CreateDatabaseOnFileSystem(database_dir.GetPath())); |
| 147 EXPECT_EQ(NotificationDatabase::STATUS_OK, | 147 EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 148 database->Open(false /* create_if_missing */)); | 148 database->Open(false /* create_if_missing */)); |
| 149 } | 149 } |
| 150 | 150 |
| 151 TEST_F(NotificationDatabaseTest, DestroyDatabase) { | 151 TEST_F(NotificationDatabaseTest, DestroyDatabase) { |
| 152 base::ScopedTempDir database_dir; | 152 base::ScopedTempDir database_dir; |
| 153 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); | 153 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 154 | 154 |
| 155 std::unique_ptr<NotificationDatabase> database( | 155 std::unique_ptr<NotificationDatabase> database( |
| 156 CreateDatabaseOnFileSystem(database_dir.path())); | 156 CreateDatabaseOnFileSystem(database_dir.GetPath())); |
| 157 | 157 |
| 158 EXPECT_EQ(NotificationDatabase::STATUS_OK, | 158 EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 159 database->Open(true /* create_if_missing */)); | 159 database->Open(true /* create_if_missing */)); |
| 160 EXPECT_TRUE(IsDatabaseOpen(database.get())); | 160 EXPECT_TRUE(IsDatabaseOpen(database.get())); |
| 161 | 161 |
| 162 // Destroy the database. This will immediately close it as well. | 162 // Destroy the database. This will immediately close it as well. |
| 163 ASSERT_EQ(NotificationDatabase::STATUS_OK, database->Destroy()); | 163 ASSERT_EQ(NotificationDatabase::STATUS_OK, database->Destroy()); |
| 164 EXPECT_FALSE(IsDatabaseOpen(database.get())); | 164 EXPECT_FALSE(IsDatabaseOpen(database.get())); |
| 165 | 165 |
| 166 // Try to re-open the database (but not re-create it). This should fail as | 166 // Try to re-open the database (but not re-create it). This should fail as |
| 167 // the files associated with the database should have been blown away. | 167 // the files associated with the database should have been blown away. |
| 168 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); | 168 database.reset(CreateDatabaseOnFileSystem(database_dir.GetPath())); |
| 169 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, | 169 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 170 database->Open(false /* create_if_missing */)); | 170 database->Open(false /* create_if_missing */)); |
| 171 } | 171 } |
| 172 | 172 |
| 173 TEST_F(NotificationDatabaseTest, NotificationIdIncrements) { | 173 TEST_F(NotificationDatabaseTest, NotificationIdIncrements) { |
| 174 base::ScopedTempDir database_dir; | 174 base::ScopedTempDir database_dir; |
| 175 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); | 175 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 176 | 176 |
| 177 std::unique_ptr<NotificationDatabase> database( | 177 std::unique_ptr<NotificationDatabase> database( |
| 178 CreateDatabaseOnFileSystem(database_dir.path())); | 178 CreateDatabaseOnFileSystem(database_dir.GetPath())); |
| 179 | 179 |
| 180 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 180 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 181 database->Open(true /* create_if_missing */)); | 181 database->Open(true /* create_if_missing */)); |
| 182 | 182 |
| 183 GURL origin("https://example.com"); | 183 GURL origin("https://example.com"); |
| 184 | 184 |
| 185 int64_t notification_id = 0; | 185 int64_t notification_id = 0; |
| 186 | 186 |
| 187 // Verify that getting two ids on the same database instance results in | 187 // Verify that getting two ids on the same database instance results in |
| 188 // incrementing values. Notification ids will start at 1. | 188 // incrementing values. Notification ids will start at 1. |
| 189 ASSERT_NO_FATAL_FAILURE( | 189 ASSERT_NO_FATAL_FAILURE( |
| 190 CreateAndWriteNotification(database.get(), origin, "" /* tag */, | 190 CreateAndWriteNotification(database.get(), origin, "" /* tag */, |
| 191 0 /* sw_registration_id */, ¬ification_id)); | 191 0 /* sw_registration_id */, ¬ification_id)); |
| 192 EXPECT_EQ(notification_id, 1); | 192 EXPECT_EQ(notification_id, 1); |
| 193 | 193 |
| 194 ASSERT_NO_FATAL_FAILURE( | 194 ASSERT_NO_FATAL_FAILURE( |
| 195 CreateAndWriteNotification(database.get(), origin, "" /* tag */, | 195 CreateAndWriteNotification(database.get(), origin, "" /* tag */, |
| 196 0 /* sw_registration_id */, ¬ification_id)); | 196 0 /* sw_registration_id */, ¬ification_id)); |
| 197 EXPECT_EQ(notification_id, 2); | 197 EXPECT_EQ(notification_id, 2); |
| 198 | 198 |
| 199 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); | 199 database.reset(CreateDatabaseOnFileSystem(database_dir.GetPath())); |
| 200 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 200 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 201 database->Open(false /* create_if_missing */)); | 201 database->Open(false /* create_if_missing */)); |
| 202 | 202 |
| 203 // Verify that the next notification id was stored in the database, and | 203 // Verify that the next notification id was stored in the database, and |
| 204 // continues where we expect it to be, even after closing and opening it. | 204 // continues where we expect it to be, even after closing and opening it. |
| 205 ASSERT_NO_FATAL_FAILURE( | 205 ASSERT_NO_FATAL_FAILURE( |
| 206 CreateAndWriteNotification(database.get(), origin, "" /* tag */, | 206 CreateAndWriteNotification(database.get(), origin, "" /* tag */, |
| 207 0 /* sw_registration_id */, ¬ification_id)); | 207 0 /* sw_registration_id */, ¬ification_id)); |
| 208 EXPECT_EQ(notification_id, 3); | 208 EXPECT_EQ(notification_id, 3); |
| 209 } | 209 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 228 database->ReadNotificationData(notification_id, origin, &database_data)); | 228 database->ReadNotificationData(notification_id, origin, &database_data)); |
| 229 | 229 |
| 230 EXPECT_EQ(notification_id, database_data.notification_id); | 230 EXPECT_EQ(notification_id, database_data.notification_id); |
| 231 } | 231 } |
| 232 | 232 |
| 233 TEST_F(NotificationDatabaseTest, NotificationIdCorruption) { | 233 TEST_F(NotificationDatabaseTest, NotificationIdCorruption) { |
| 234 base::ScopedTempDir database_dir; | 234 base::ScopedTempDir database_dir; |
| 235 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); | 235 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 236 | 236 |
| 237 std::unique_ptr<NotificationDatabase> database( | 237 std::unique_ptr<NotificationDatabase> database( |
| 238 CreateDatabaseOnFileSystem(database_dir.path())); | 238 CreateDatabaseOnFileSystem(database_dir.GetPath())); |
| 239 | 239 |
| 240 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 240 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 241 database->Open(true /* create_if_missing */)); | 241 database->Open(true /* create_if_missing */)); |
| 242 | 242 |
| 243 GURL origin("https://example.com"); | 243 GURL origin("https://example.com"); |
| 244 | 244 |
| 245 NotificationDatabaseData database_data; | 245 NotificationDatabaseData database_data; |
| 246 int64_t notification_id = 0; | 246 int64_t notification_id = 0; |
| 247 | 247 |
| 248 ASSERT_EQ( | 248 ASSERT_EQ( |
| 249 NotificationDatabase::STATUS_OK, | 249 NotificationDatabase::STATUS_OK, |
| 250 database->WriteNotificationData(origin, database_data, ¬ification_id)); | 250 database->WriteNotificationData(origin, database_data, ¬ification_id)); |
| 251 EXPECT_EQ(notification_id, 1); | 251 EXPECT_EQ(notification_id, 1); |
| 252 | 252 |
| 253 // Deliberately write an invalid value as the next notification id. When | 253 // Deliberately write an invalid value as the next notification id. When |
| 254 // re-opening the database, the Open() method should realize that an invalid | 254 // re-opening the database, the Open() method should realize that an invalid |
| 255 // value is being read, and mark the database as corrupted. | 255 // value is being read, and mark the database as corrupted. |
| 256 ASSERT_NO_FATAL_FAILURE( | 256 ASSERT_NO_FATAL_FAILURE( |
| 257 WriteLevelDBKeyValuePair(database.get(), "NEXT_NOTIFICATION_ID", "-42")); | 257 WriteLevelDBKeyValuePair(database.get(), "NEXT_NOTIFICATION_ID", "-42")); |
| 258 | 258 |
| 259 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); | 259 database.reset(CreateDatabaseOnFileSystem(database_dir.GetPath())); |
| 260 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_CORRUPTED, | 260 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_CORRUPTED, |
| 261 database->Open(false /* create_if_missing */)); | 261 database->Open(false /* create_if_missing */)); |
| 262 } | 262 } |
| 263 | 263 |
| 264 TEST_F(NotificationDatabaseTest, ReadInvalidNotificationData) { | 264 TEST_F(NotificationDatabaseTest, ReadInvalidNotificationData) { |
| 265 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); | 265 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 266 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 266 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 267 database->Open(true /* create_if_missing */)); | 267 database->Open(true /* create_if_missing */)); |
| 268 | 268 |
| 269 NotificationDatabaseData database_data; | 269 NotificationDatabaseData database_data; |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 | 632 |
| 633 std::vector<NotificationDatabaseData> notifications; | 633 std::vector<NotificationDatabaseData> notifications; |
| 634 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 634 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 635 database->ReadAllNotificationDataForServiceWorkerRegistration( | 635 database->ReadAllNotificationDataForServiceWorkerRegistration( |
| 636 origin, kExampleServiceWorkerRegistrationId, ¬ifications)); | 636 origin, kExampleServiceWorkerRegistrationId, ¬ifications)); |
| 637 | 637 |
| 638 EXPECT_EQ(0u, notifications.size()); | 638 EXPECT_EQ(0u, notifications.size()); |
| 639 } | 639 } |
| 640 | 640 |
| 641 } // namespace content | 641 } // namespace content |
| OLD | NEW |