Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(924)

Unified Diff: content/browser/notifications/notification_database_unittest.cc

Issue 1004493002: Store the next notification id in the notification database. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@n-db-NotificationDatabase
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/notifications/notification_database.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/notifications/notification_database_unittest.cc
diff --git a/content/browser/notifications/notification_database_unittest.cc b/content/browser/notifications/notification_database_unittest.cc
index 69853a0334fd921a87d7f8e9433540b22ab7be69..35ea1a38ee19ae6251687d89fa8849b14696d33b 100644
--- a/content/browser/notifications/notification_database_unittest.cc
+++ b/content/browser/notifications/notification_database_unittest.cc
@@ -6,6 +6,8 @@
#include "base/files/scoped_temp_dir.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/leveldatabase/src/include/leveldb/db.h"
+#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
namespace content {
@@ -31,6 +33,35 @@ class NotificationDatabaseTest : public ::testing::Test {
bool IsInMemoryDatabase(NotificationDatabase* database) {
return database->IsInMemoryDatabase();
}
+
+ // Writes a LevelDB key-value pair directly to the LevelDB backing the
+ // notification database in |database|.
+ void WriteLevelDBKeyValuePair(NotificationDatabase* database,
+ const std::string& key,
+ const std::string& value) {
+ leveldb::Status status =
+ database->GetDBForTesting()->Put(leveldb::WriteOptions(), key, value);
+ ASSERT_TRUE(status.ok());
+ }
+
+ // Increments the next notification id value in the database. Normally this
+ // is managed by the NotificationDatabase when writing notification data.
+ //
+ // TODO(peter): Stop doing this manually when writing notification data will
+ // do this for us, except for tests verifying corruption behavior.
+ void IncrementNextNotificationId(NotificationDatabase* database) {
+ int64_t next_notification_id;
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->GetNextNotificationId(&next_notification_id));
+
+ leveldb::WriteBatch batch;
+ database->WriteNextNotificationId(&batch, next_notification_id + 1);
+
+ leveldb::Status status =
+ database->GetDBForTesting()->Write(leveldb::WriteOptions(), &batch);
+
+ ASSERT_TRUE(status.ok());
+ }
};
TEST_F(NotificationDatabaseTest, OpenCloseMemory) {
@@ -101,4 +132,63 @@ TEST_F(NotificationDatabaseTest, DestroyDatabase) {
database->Open(false /* create_if_missing */));
}
+TEST_F(NotificationDatabaseTest, GetNextNotificationIdIncrements) {
+ base::ScopedTempDir database_dir;
+ ASSERT_TRUE(database_dir.CreateUniqueTempDir());
+
+ scoped_ptr<NotificationDatabase> database(
+ CreateDatabaseOnFileSystem(database_dir.path()));
+
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->Open(true /* create_if_missing */));
+
+ int64_t notification_id = 0;
+
+ // Verify that getting two ids on the same database instance results in
+ // incrementing values. Notification ids will start at 1.
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->GetNextNotificationId(&notification_id));
+ EXPECT_EQ(1, notification_id);
+
+ ASSERT_NO_FATAL_FAILURE(IncrementNextNotificationId(database.get()));
+
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->GetNextNotificationId(&notification_id));
+ EXPECT_EQ(2, notification_id);
+
+ ASSERT_NO_FATAL_FAILURE(IncrementNextNotificationId(database.get()));
+
+ database.reset(CreateDatabaseOnFileSystem(database_dir.path()));
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->Open(false /* create_if_missing */));
+
+ // Verify that the next notification id was stored in the database, and
+ // continues where we expect it to be, even after closing and opening it.
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->GetNextNotificationId(&notification_id));
+ EXPECT_EQ(3, notification_id);
+}
+
+TEST_F(NotificationDatabaseTest, GetNextNotificationIdCorruption) {
+ scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->Open(true /* create_if_missing */));
+
+ int64_t notification_id = 0;
+
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->GetNextNotificationId(&notification_id));
+ EXPECT_EQ(1, notification_id);
+
+ // Deliberately write an invalid value as the next notification id to the
+ // database, which should cause GetNextNotificationId to realize that
+ // something is wrong with the data it's reading.
+ ASSERT_NO_FATAL_FAILURE(WriteLevelDBKeyValuePair(database.get(),
+ "NEXT_NOTIFICATION_ID",
+ "-42"));
+
+ EXPECT_EQ(NotificationDatabase::STATUS_ERROR_CORRUPTED,
+ database->GetNextNotificationId(&notification_id));
+}
+
} // namespace content
« no previous file with comments | « content/browser/notifications/notification_database.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698