diff options
author | marja@chromium.org <marja@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-06 10:15:33 +0000 |
---|---|---|
committer | marja@chromium.org <marja@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-06 10:15:33 +0000 |
commit | 438aa693de1f4c67ad104f8221345eb96f29c163 (patch) | |
tree | 18c5951e3e1dab487f3b3a1e101d2f2d10619177 /webkit/dom_storage | |
parent | 97714c845f1f387d07c8463f511ff6def1eda20e (diff) | |
download | chromium_src-438aa693de1f4c67ad104f8221345eb96f29c163.zip chromium_src-438aa693de1f4c67ad104f8221345eb96f29c163.tar.gz chromium_src-438aa693de1f4c67ad104f8221345eb96f29c163.tar.bz2 |
SessionStorageDatabase: use string identifiers, add ReadNamespaceIds.
BUG=104292
TEST=SessionStorageDatabaseTest.*, SessionStorageDatabaseTest.ReadNamespaceIds.
Review URL: https://chromiumcodereview.appspot.com/10537004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@140733 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/dom_storage')
-rw-r--r-- | webkit/dom_storage/session_storage_database.cc | 184 | ||||
-rw-r--r-- | webkit/dom_storage/session_storage_database.h | 66 | ||||
-rw-r--r-- | webkit/dom_storage/session_storage_database_unittest.cc | 470 |
3 files changed, 257 insertions, 463 deletions
diff --git a/webkit/dom_storage/session_storage_database.cc b/webkit/dom_storage/session_storage_database.cc index f4bc6c5..0f01db7 100644 --- a/webkit/dom_storage/session_storage_database.cc +++ b/webkit/dom_storage/session_storage_database.cc @@ -32,7 +32,6 @@ // | namespace-3- | dummy | // | namespace-3-origin1 | 3 (deep copy) | // | namespace-3-origin2 | 2 (shallow copy) | -// | next-namespace-id | 4 | // | next-map-id | 4 | namespace dom_storage { @@ -40,14 +39,13 @@ namespace dom_storage { SessionStorageDatabase::SessionStorageDatabase(const FilePath& file_path) : file_path_(file_path), db_error_(false), - is_inconsistent_(false), - namespace_offset_(0) { + is_inconsistent_(false) { } SessionStorageDatabase::~SessionStorageDatabase() { } -void SessionStorageDatabase::ReadAreaValues(int64 namespace_id, +void SessionStorageDatabase::ReadAreaValues(const std::string& namespace_id, const GURL& origin, ValuesMap* result) { // We don't create a database if it doesn't exist. In that case, there is @@ -56,13 +54,13 @@ void SessionStorageDatabase::ReadAreaValues(int64 namespace_id, return; std::string map_id; bool exists; - if (!GetMapForArea(namespace_id, origin, &exists, &map_id)) + if (!GetMapForArea(namespace_id, origin.spec(), &exists, &map_id)) return; if (exists) ReadMap(map_id, result, false); } -bool SessionStorageDatabase::CommitAreaChanges(int64 namespace_id, +bool SessionStorageDatabase::CommitAreaChanges(const std::string& namespace_id, const GURL& origin, bool clear_all_first, const ValuesMap& changes) { @@ -80,7 +78,7 @@ bool SessionStorageDatabase::CommitAreaChanges(int64 namespace_id, std::string map_id; bool exists; - if (!GetMapForArea(namespace_id, origin, &exists, &map_id)) + if (!GetMapForArea(namespace_id, origin.spec(), &exists, &map_id)) return false; if (exists) { int64 ref_count; @@ -109,8 +107,8 @@ bool SessionStorageDatabase::CommitAreaChanges(int64 namespace_id, return DatabaseErrorCheck(s.ok()); } -bool SessionStorageDatabase::CloneNamespace(int64 namespace_id, - int64 new_namespace_id) { +bool SessionStorageDatabase::CloneNamespace( + const std::string& namespace_id, const std::string& new_namespace_id) { // Go through all origins in the namespace |namespace_id|, create placeholders // for them in |new_namespace_id|, and associate them with the existing maps. @@ -152,20 +150,20 @@ bool SessionStorageDatabase::CloneNamespace(int64 namespace_id, return DatabaseErrorCheck(s.ok()); } -bool SessionStorageDatabase::DeleteArea(int64 namespace_id, +bool SessionStorageDatabase::DeleteArea(const std::string& namespace_id, const GURL& origin) { if (!LazyOpen(false)) { // No need to create the database if it doesn't exist. return true; } leveldb::WriteBatch batch; - if (!DeleteArea(namespace_id, origin.spec(), &batch)) + if (!DeleteAreaHelper(namespace_id, origin.spec(), &batch)) return false; leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); return DatabaseErrorCheck(s.ok()); } -bool SessionStorageDatabase::DeleteNamespace(int64 namespace_id) { +bool SessionStorageDatabase::DeleteNamespace(const std::string& namespace_id) { if (!LazyOpen(false)) { // No need to create the database if it doesn't exist. return true; @@ -178,14 +176,53 @@ bool SessionStorageDatabase::DeleteNamespace(int64 namespace_id) { for (std::map<std::string, std::string>::const_iterator it = areas.begin(); it != areas.end(); ++it) { const std::string& origin = it->first; - if (!DeleteArea(namespace_id, origin, &batch)) + if (!DeleteAreaHelper(namespace_id, origin, &batch)) return false; } - batch.Delete(NamespaceStartKey(namespace_id, namespace_offset_)); + batch.Delete(NamespaceStartKey(namespace_id)); leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); return DatabaseErrorCheck(s.ok()); } +bool SessionStorageDatabase::ReadNamespaceIds( + std::vector<std::string>* namespace_ids) { + if (!LazyOpen(true)) + return false; + + std::string namespace_prefix = NamespacePrefix(); + scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); + it->Seek(namespace_prefix); + if (it->status().IsNotFound()) + return true; + + if (!DatabaseErrorCheck(it->status().ok())) + return false; + + // Skip the dummy entry "namespace-" and iterate the namespaces. + std::string current_namespace_start_key; + for (it->Next(); it->Valid(); it->Next()) { + std::string key = it->key().ToString(); + if (key.find(namespace_prefix) != 0) { + // Iterated past the "namespace-" keys. + break; + } + // For each namespace, the first key is "namespace-<namespaceid>-", and the + // subsequent keys are "namespace-<namespaceid>-<origin>". Read the unique + // "<namespaceid>" parts from the keys. + if (current_namespace_start_key.empty() || + key.substr(0, current_namespace_start_key.length()) != + current_namespace_start_key) { + // The key is of the form "namespace-<namespaceid>-" for a new + // <namespaceid>. + current_namespace_start_key = key; + namespace_ids->push_back( + key.substr(namespace_prefix.length(), + key.length() - namespace_prefix.length() - 1)); + } + } + return true; +} + bool SessionStorageDatabase::LazyOpen(bool create_if_needed) { base::AutoLock auto_lock(db_lock_); if (db_error_ || is_inconsistent_) { @@ -224,8 +261,7 @@ bool SessionStorageDatabase::LazyOpen(bool create_if_needed) { } } db_.reset(db); - - return GetNextNamespaceId(&namespace_offset_); + return true; } leveldb::Status SessionStorageDatabase::TryToOpen(leveldb::DB** db) { @@ -272,7 +308,7 @@ bool SessionStorageDatabase::DatabaseErrorCheck(bool ok) { return false; } -bool SessionStorageDatabase::CreateNamespace(int64 namespace_id, +bool SessionStorageDatabase::CreateNamespace(const std::string& namespace_id, bool ok_if_exists, leveldb::WriteBatch* batch) { leveldb::Slice namespace_prefix = NamespacePrefix(); @@ -284,56 +320,21 @@ bool SessionStorageDatabase::CreateNamespace(int64 namespace_id, if (s.IsNotFound()) batch->Put(namespace_prefix, ""); - std::string namespace_start_key = - NamespaceStartKey(namespace_id, namespace_offset_); + std::string namespace_start_key = NamespaceStartKey(namespace_id); s = db_->Get(leveldb::ReadOptions(), namespace_start_key, &dummy); if (!DatabaseErrorCheck(s.ok() || s.IsNotFound())) return false; if (s.IsNotFound()) { batch->Put(namespace_start_key, ""); - return UpdateNextNamespaceId(namespace_id, batch); - } - return CallerErrorCheck(ok_if_exists); -} - -bool SessionStorageDatabase::GetNextNamespaceId(int64* next_namespace_id) { - std::string next_namespace_id_string; - leveldb::Status s = db_->Get(leveldb::ReadOptions(), NextNamespaceIdKey(), - &next_namespace_id_string); - if (!DatabaseErrorCheck(s.ok() || s.IsNotFound())) - return false; - if (s.IsNotFound()) { - *next_namespace_id = 0; return true; } - bool conversion_ok = - base::StringToInt64(next_namespace_id_string, next_namespace_id); - return ConsistencyCheck(conversion_ok); -} - -bool SessionStorageDatabase::UpdateNextNamespaceId(int64 namespace_id, - leveldb::WriteBatch* batch) { - int64 next_namespace_id; - if (!GetNextNamespaceId(&next_namespace_id)) - return false; - if (next_namespace_id < namespace_id + namespace_offset_ + 1) { - next_namespace_id = namespace_id + namespace_offset_ + 1; - batch->Put(NextNamespaceIdKey(), base::Int64ToString(next_namespace_id)); - } - return true; -} - -bool SessionStorageDatabase::GetAreasInNamespace( - int64 namespace_id, - std::map<std::string, std::string>* areas) { - return GetAreasInNamespace(NamespaceIdStr(namespace_id, namespace_offset_), - areas); + return CallerErrorCheck(ok_if_exists); } bool SessionStorageDatabase::GetAreasInNamespace( - const std::string& namespace_id_str, + const std::string& namespace_id, std::map<std::string, std::string>* areas) { - std::string namespace_start_key = NamespaceStartKey(namespace_id_str); + std::string namespace_start_key = NamespaceStartKey(namespace_id); scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); it->Seek(namespace_start_key); if (it->status().IsNotFound()) { @@ -358,51 +359,35 @@ bool SessionStorageDatabase::GetAreasInNamespace( return true; } -void SessionStorageDatabase::AddAreaToNamespace(int64 namespace_id, +void SessionStorageDatabase::AddAreaToNamespace(const std::string& namespace_id, const std::string& origin, const std::string& map_id, leveldb::WriteBatch* batch) { - std::string namespace_key = NamespaceKey( - NamespaceIdStr(namespace_id, namespace_offset_), origin); + std::string namespace_key = NamespaceKey(namespace_id, origin); batch->Put(namespace_key, map_id); } -bool SessionStorageDatabase::DeleteArea(int64 namespace_id, - const std::string& origin, - leveldb::WriteBatch* batch) { - return DeleteArea(NamespaceIdStr(namespace_id, namespace_offset_), - origin, batch); -} - -bool SessionStorageDatabase::DeleteArea(const std::string& namespace_id_str, - const std::string& origin, - leveldb::WriteBatch* batch) { +bool SessionStorageDatabase::DeleteAreaHelper( + const std::string& namespace_id, + const std::string& origin, + leveldb::WriteBatch* batch) { std::string map_id; bool exists; - if (!GetMapForArea(namespace_id_str, origin, &exists, &map_id)) + if (!GetMapForArea(namespace_id, origin, &exists, &map_id)) return false; if (!exists) return true; // Nothing to delete. if (!DecreaseMapRefCount(map_id, 1, batch)) return false; - std::string namespace_key = NamespaceKey(namespace_id_str, origin); + std::string namespace_key = NamespaceKey(namespace_id, origin); batch->Delete(namespace_key); return true; } -bool SessionStorageDatabase::GetMapForArea(int64 namespace_id, - const GURL& origin, - bool* exists, - std::string* map_id) { - return GetMapForArea( - base::Int64ToString(namespace_id + namespace_offset_), - origin.spec(), exists, map_id); -} - -bool SessionStorageDatabase::GetMapForArea(const std::string& namespace_id_str, +bool SessionStorageDatabase::GetMapForArea(const std::string& namespace_id, const std::string& origin, bool* exists, std::string* map_id) { - std::string namespace_key = NamespaceKey(namespace_id_str, origin); + std::string namespace_key = NamespaceKey(namespace_id, origin); leveldb::Status s = db_->Get(leveldb::ReadOptions(), namespace_key, map_id); if (s.IsNotFound()) { *exists = false; @@ -412,7 +397,7 @@ bool SessionStorageDatabase::GetMapForArea(const std::string& namespace_id_str, return DatabaseErrorCheck(s.ok()); } -bool SessionStorageDatabase::CreateMapForArea(int64 namespace_id, +bool SessionStorageDatabase::CreateMapForArea(const std::string& namespace_id, const GURL& origin, std::string* map_id, leveldb::WriteBatch* batch) { @@ -429,8 +414,7 @@ bool SessionStorageDatabase::CreateMapForArea(int64 namespace_id, return false; } batch->Put(next_map_id_key, base::Int64ToString(++next_map_id)); - std::string namespace_key = - NamespaceKey(namespace_id, namespace_offset_, origin); + std::string namespace_key = NamespaceKey(namespace_id, origin.spec()); batch->Put(namespace_key, *map_id); batch->Put(MapRefCountKey(*map_id), "1"); return true; @@ -542,7 +526,7 @@ bool SessionStorageDatabase::ClearMap(const std::string& map_id, } bool SessionStorageDatabase::DeepCopyArea( - int64 namespace_id, const GURL& origin, bool copy_data, + const std::string& namespace_id, const GURL& origin, bool copy_data, std::string* map_id, leveldb::WriteBatch* batch) { // Example, data before deep copy: // | namespace-1- (1 = namespace id)| dummy | @@ -579,32 +563,16 @@ bool SessionStorageDatabase::DeepCopyArea( } std::string SessionStorageDatabase::NamespaceStartKey( - const std::string& namespace_id_str) { - return base::StringPrintf("namespace-%s-", namespace_id_str.c_str()); -} - -std::string SessionStorageDatabase::NamespaceStartKey(int64 namespace_id, - int64 namespace_offset) { - return NamespaceStartKey(NamespaceIdStr(namespace_id, namespace_offset)); + const std::string& namespace_id) { + return base::StringPrintf("namespace-%s-", namespace_id.c_str()); } std::string SessionStorageDatabase::NamespaceKey( - const std::string& namespace_id_str, const std::string& origin) { - return base::StringPrintf("namespace-%s-%s", namespace_id_str.c_str(), + const std::string& namespace_id, const std::string& origin) { + return base::StringPrintf("namespace-%s-%s", namespace_id.c_str(), origin.c_str()); } -std::string SessionStorageDatabase::NamespaceKey( - int64 namespace_id, int64 namespace_offset, const GURL& origin) { - return NamespaceKey(NamespaceIdStr(namespace_id, namespace_offset), - origin.spec()); -} - -std::string SessionStorageDatabase::NamespaceIdStr(int64 namespace_id, - int64 namespace_offset) { - return base::Int64ToString(namespace_id + namespace_offset); -} - const char* SessionStorageDatabase::NamespacePrefix() { return "namespace-"; } @@ -618,10 +586,6 @@ std::string SessionStorageDatabase::MapKey(const std::string& map_id, return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); } -const char* SessionStorageDatabase::NextNamespaceIdKey() { - return "next-namespace-id"; -} - const char* SessionStorageDatabase::NextMapIdKey() { return "next-map-id"; } diff --git a/webkit/dom_storage/session_storage_database.h b/webkit/dom_storage/session_storage_database.h index 822dbc3..eb03993 100644 --- a/webkit/dom_storage/session_storage_database.h +++ b/webkit/dom_storage/session_storage_database.h @@ -37,7 +37,7 @@ class SessionStorageDatabase : // assumed to be empty and any duplicate keys will be overwritten. If the // database exists on disk then it will be opened. If it does not exist then // it will not be created and |result| will be unmodified. - void ReadAreaValues(int64 namespace_id, + void ReadAreaValues(const std::string& namespace_id, const GURL& origin, ValuesMap* result); @@ -47,20 +47,24 @@ class SessionStorageDatabase : // be removed and all others will be inserted/updated as appropriate. It is // allowed to write data into a shallow copy created by CloneNamespace, and in // that case the copy will be made deep before writing the values. - bool CommitAreaChanges(int64 namespace_id, + bool CommitAreaChanges(const std::string& namespace_id, const GURL& origin, bool clear_all_first, const ValuesMap& changes); // Creates shallow copies of the areas for |namespace_id| and associates them // with |new_namespace_id|. - bool CloneNamespace(int64 namespace_id, int64 new_namespace_id); + bool CloneNamespace(const std::string& namespace_id, + const std::string& new_namespace_id); // Deletes the data for |namespace_id| and |origin|. - bool DeleteArea(int64 namespace_id, const GURL& origin); + bool DeleteArea(const std::string& namespace_id, const GURL& origin); // Deletes the data for |namespace_id|. - bool DeleteNamespace(int64 namespace_id); + bool DeleteNamespace(const std::string& namespace_id); + + // Reads all namespace IDs from the database. + bool ReadNamespaceIds(std::vector<std::string>* namespace_ids); private: friend class base::RefCountedThreadSafe<SessionStorageDatabase>; @@ -97,42 +101,30 @@ class SessionStorageDatabase : // Creates a namespace for |namespace_id| and updates the next namespace id if // needed. If |ok_if_exists| is false, checks that the namespace didn't exist // before. - bool CreateNamespace(int64 namespace_id, + bool CreateNamespace(const std::string& namespace_id, bool ok_if_exists, leveldb::WriteBatch* batch); - // Reads the next namespace id. - bool GetNextNamespaceId(int64* next_namespace_id); - bool UpdateNextNamespaceId(int64 namespace_id, - leveldb::WriteBatch* batch); + // Reads the areas assoiated with |namespace_id| and puts the (origin, map_id) // pairs into |areas|. - bool GetAreasInNamespace(int64 namespace_id, - std::map<std::string, std::string>* areas); - bool GetAreasInNamespace(const std::string& namespace_id_str, + bool GetAreasInNamespace(const std::string& namespace_id, std::map<std::string, std::string>* areas); // Adds an association between |origin| and |map_id| into the namespace // |namespace_id|. - void AddAreaToNamespace(int64 namespace_id, + void AddAreaToNamespace(const std::string& namespace_id, const std::string& origin, const std::string& map_id, leveldb::WriteBatch* batch); // Helpers for deleting data for |namespace_id| and |origin|. - bool DeleteArea(int64 namespace_id, - const std::string& origin, - leveldb::WriteBatch* batch); - bool DeleteArea(const std::string& namespace_id_str, - const std::string& origin, - leveldb::WriteBatch* batch); + bool DeleteAreaHelper(const std::string& namespace_id, + const std::string& origin, + leveldb::WriteBatch* batch); // Retrieves the map id for |namespace_id| and |origin|. It's not an error if // the map doesn't exist. - bool GetMapForArea(int64 namespace_id, - const GURL& origin, - bool* exists, - std::string* map_id); - bool GetMapForArea(const std::string& namespace_id_str, + bool GetMapForArea(const std::string& namespace_id, const std::string& origin, bool* exists, std::string* map_id); @@ -141,7 +133,7 @@ class SessionStorageDatabase : // id of the created map. If there is a map for |namespace_id| and |origin|, // this just overwrites the map id. The caller is responsible for decreasing // the ref count. - bool CreateMapForArea(int64 namespace_id, + bool CreateMapForArea(const std::string& namespace_id, const GURL& origin, std::string* map_id, leveldb::WriteBatch* batch); @@ -171,27 +163,19 @@ class SessionStorageDatabase : // Breaks the association between (|namespace_id|, |origin|) and |map_id| and // creates a new map for (|namespace_id|, |origin|). Copies the data from the // old map if |copy_data| is true. - bool DeepCopyArea(int64 namespace_id, + bool DeepCopyArea(const std::string& namespace_id, const GURL& origin, bool copy_data, std::string* map_id, leveldb::WriteBatch* batch); // Helper functions for creating the keys needed for the schema. - static std::string NamespaceStartKey(const std::string& namespace_id_str); - static std::string NamespaceStartKey(int64 namespace_id, - int64 namespace_offset); - static std::string NamespaceKey(const std::string& namespace_id_str, + static std::string NamespaceStartKey(const std::string& namespace_id); + static std::string NamespaceKey(const std::string& namespace_id, const std::string& origin); - static std::string NamespaceKey(int64 namespace_id, - int64 namespace_offset, - const GURL& origin); - static std::string NamespaceIdStr(int64 namespace_id, int64 namespace_offset); static const char* NamespacePrefix(); static std::string MapRefCountKey(const std::string& map_id); static std::string MapKey(const std::string& map_id, const std::string& key); - static const char* MapPrefix(); - static const char* NextNamespaceIdKey(); static const char* NextMapIdKey(); scoped_ptr<leveldb::DB> db_; @@ -205,14 +189,6 @@ class SessionStorageDatabase : // True if the database is in an inconsistent state. bool is_inconsistent_; - // On startup, we read the next ununsed namespace id from the database. It - // will be the offset for namespace ids. The actual id of a namespace in the - // database will be: id passed to the API function + namespace_offset_. The - // namespace ids which are handled as int64 (named namespace_id) don't contain - // the offset yet. The namespaces ids which are handled as strings (named - // namesapce_id_str) contain the offset. - int64 namespace_offset_; - DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase); }; diff --git a/webkit/dom_storage/session_storage_database_unittest.cc b/webkit/dom_storage/session_storage_database_unittest.cc index 57352df..de71a2f 100644 --- a/webkit/dom_storage/session_storage_database_unittest.cc +++ b/webkit/dom_storage/session_storage_database_unittest.cc @@ -34,9 +34,9 @@ class SessionStorageDatabaseTest : public testing::Test { // Helpers. static bool IsNamespaceKey(const std::string& key, - int64* namespace_id); + std::string* namespace_id); static bool IsNamespaceOriginKey(const std::string& key, - int64* namespace_id); + std::string* namespace_id); static bool IsMapRefCountKey(const std::string& key, int64* map_id); static bool IsMapValueKey(const std::string& key, @@ -46,14 +46,15 @@ class SessionStorageDatabaseTest : public testing::Test { void CheckDatabaseConsistency() const; void CheckEmptyDatabase() const; void DumpData() const; - void CheckAreaData(int64 namespace_id, + void CheckAreaData(const std::string& namespace_id, const GURL& origin, const ValuesMap& reference) const; void CompareValuesMaps(const ValuesMap& map1, const ValuesMap& map2) const; - std::string GetMapForArea(int64 namespace_id, + void CheckNamespaceIds( + const std::set<std::string>& expected_namespace_ids) const; + std::string GetMapForArea(const std::string& namespace_id, const GURL& origin) const; int64 GetMapRefCount(const std::string& map_id) const; - int64 NextNamespaceId() const; ScopedTempDir temp_dir_; scoped_refptr<SessionStorageDatabase> db_; @@ -61,6 +62,9 @@ class SessionStorageDatabaseTest : public testing::Test { // Test data. const GURL kOrigin1; const GURL kOrigin2; + const std::string kNamespace1; + const std::string kNamespace2; + const std::string kNamespaceClone; const string16 kKey1; const string16 kKey2; const string16 kKey3; @@ -76,6 +80,9 @@ class SessionStorageDatabaseTest : public testing::Test { SessionStorageDatabaseTest::SessionStorageDatabaseTest() : kOrigin1("http://www.origin1.com"), kOrigin2("http://www.origin2.com"), + kNamespace1("1"), + kNamespace2("namespace2"), + kNamespaceClone("wascloned"), kKey1(ASCIIToUTF16("key1")), kKey2(ASCIIToUTF16("key2")), kKey3(ASCIIToUTF16("key3")), @@ -99,7 +106,7 @@ void SessionStorageDatabaseTest::ResetDatabase() { // static bool SessionStorageDatabaseTest::IsNamespaceKey(const std::string& key, - int64* namespace_id) { + std::string* namespace_id) { std::string namespace_prefix = SessionStorageDatabase::NamespacePrefix(); if (key.find(namespace_prefix) != 0) return false; @@ -111,17 +118,16 @@ bool SessionStorageDatabaseTest::IsNamespaceKey(const std::string& key, return false; // Key is of the form "namespace-<namespaceid>-". - std::string namespace_id_str = key.substr( + *namespace_id = key.substr( namespace_prefix.length(), second_dash - namespace_prefix.length()); - bool conversion_ok = base::StringToInt64(namespace_id_str, namespace_id); - EXPECT_TRUE(conversion_ok); return true; } // static -bool SessionStorageDatabaseTest::IsNamespaceOriginKey(const std::string& key, - int64* namespace_id) { +bool SessionStorageDatabaseTest::IsNamespaceOriginKey( + const std::string& key, + std::string* namespace_id) { std::string namespace_prefix = SessionStorageDatabase::NamespacePrefix(); if (key.find(namespace_prefix) != 0) return false; @@ -131,11 +137,9 @@ bool SessionStorageDatabaseTest::IsNamespaceOriginKey(const std::string& key, // Key is of the form "namespace-<namespaceid>-<origin>", and the value // is the map id. - std::string namespace_id_str = key.substr( + *namespace_id = key.substr( namespace_prefix.length(), second_dash - namespace_prefix.length()); - bool conversion_ok = base::StringToInt64(namespace_id_str, namespace_id); - EXPECT_TRUE(conversion_ok); return true; } @@ -192,38 +196,32 @@ void SessionStorageDatabaseTest::CheckDatabaseConsistency() const { // For detecting rubbish keys. size_t valid_keys = 0; - std::string next_namespace_id_key = - SessionStorageDatabase::NextNamespaceIdKey(); std::string next_map_id_key = SessionStorageDatabase::NextMapIdKey(); // Check the namespace start key. if (data.find(SessionStorageDatabase::NamespacePrefix()) == data.end()) { // If there is no namespace start key, the database may contain only counter // keys. for (DataMap::const_iterator it = data.begin(); it != data.end(); ++it) { - ASSERT_TRUE(it->first == next_namespace_id_key || - it->first == next_map_id_key); + ASSERT_TRUE(it->first == next_map_id_key); } return; } ++valid_keys; // Iterate the "namespace-" keys. - std::set<int64> found_namespace_ids; - int64 max_namespace_id = -1; + std::set<std::string> found_namespace_ids; std::map<int64, int64> expected_map_refcounts; int64 max_map_id = -1; for (DataMap::const_iterator it = data.begin(); it != data.end(); ++it) { - int64 namespace_id; + std::string namespace_id; std::string origin; if (IsNamespaceKey(it->first, &namespace_id)) { - ASSERT_GT(namespace_id, 0); found_namespace_ids.insert(namespace_id); - max_namespace_id = std::max(namespace_id, max_namespace_id); ++valid_keys; } else if (IsNamespaceOriginKey( it->first, &namespace_id)) { - // Check that the corresponding "namespace-<namespaceid>" key exists. It + // Check that the corresponding "namespace-<namespaceid>-" key exists. It // has been read by now, since the keys are stored in order. ASSERT_TRUE(found_namespace_ids.find(namespace_id) != found_namespace_ids.end()); @@ -236,15 +234,6 @@ void SessionStorageDatabaseTest::CheckDatabaseConsistency() const { ++valid_keys; } } - if (max_namespace_id != -1) { - // The database contains namespaces. - ASSERT_TRUE(data.find(next_namespace_id_key) != data.end()); - int64 next_namespace_id; - bool conversion_ok = - base::StringToInt64(data[next_namespace_id_key], &next_namespace_id); - ASSERT_TRUE(conversion_ok); - ASSERT_GT(next_namespace_id, max_namespace_id); - } if (max_map_id != -1) { // The database contains maps. ASSERT_TRUE(data.find(next_map_id_key) != data.end()); @@ -280,10 +269,6 @@ void SessionStorageDatabaseTest::CheckDatabaseConsistency() const { // Check that all maps referred to exist. ASSERT_TRUE(expected_map_refcounts.empty()); - // Count valid keys. - if (data.find(next_namespace_id_key) != data.end()) - ++valid_keys; - if (data.find(next_map_id_key) != data.end()) ++valid_keys; @@ -296,8 +281,6 @@ void SessionStorageDatabaseTest::CheckEmptyDatabase() const { size_t valid_keys = 0; if (data.find(SessionStorageDatabase::NamespacePrefix()) != data.end()) ++valid_keys; - if (data.find(SessionStorageDatabase::NextNamespaceIdKey()) != data.end()) - ++valid_keys; if (data.find(SessionStorageDatabase::NextMapIdKey()) != data.end()) ++valid_keys; EXPECT_EQ(valid_keys, data.size()); @@ -324,7 +307,8 @@ void SessionStorageDatabaseTest::DumpData() const { } void SessionStorageDatabaseTest::CheckAreaData( - int64 namespace_id, const GURL& origin, const ValuesMap& reference) const { + const std::string& namespace_id, const GURL& origin, + const ValuesMap& reference) const { ValuesMap values; db_->ReadAreaValues(namespace_id, origin, &values); CompareValuesMaps(values, reference); @@ -344,11 +328,24 @@ void SessionStorageDatabaseTest::CompareValuesMaps( } } +void SessionStorageDatabaseTest::CheckNamespaceIds( + const std::set<std::string>& expected_namespace_ids) const { + std::vector<std::string> namespace_ids; + EXPECT_TRUE(db_->ReadNamespaceIds(&namespace_ids)); + EXPECT_EQ(expected_namespace_ids.size(), namespace_ids.size()); + for (std::vector<std::string>::const_iterator it = namespace_ids.begin(); + it != namespace_ids.end(); ++it) { + LOG(WARNING) << *it; + EXPECT_TRUE(expected_namespace_ids.find(*it) != + expected_namespace_ids.end()); + } +} + std::string SessionStorageDatabaseTest::GetMapForArea( - int64 namespace_id, const GURL& origin) const { + const std::string& namespace_id, const GURL& origin) const { bool exists; std::string map_id; - EXPECT_TRUE(db_->GetMapForArea(namespace_id, origin, + EXPECT_TRUE(db_->GetMapForArea(namespace_id, origin.spec(), &exists, &map_id)); EXPECT_TRUE(exists); return map_id; @@ -361,12 +358,6 @@ int64 SessionStorageDatabaseTest::GetMapRefCount( return ref_count; } -int64 SessionStorageDatabaseTest::NextNamespaceId() const { - int64 next_namespace_id; - EXPECT_TRUE(db_->GetNextNamespaceId(&next_namespace_id)); - return next_namespace_id; -} - TEST_F(SessionStorageDatabaseTest, EmptyDatabaseSanityCheck) { // An empty database should be valid. CheckDatabaseConsistency(); @@ -384,10 +375,10 @@ TEST_F(SessionStorageDatabaseTest, WriteDataForOneOrigin) { reference[kKey1] = kValue1; reference[kKey2] = kValue2; reference[kKey3] = kValue3; - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, changes)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, changes)); } CheckDatabaseConsistency(); - CheckAreaData(1, kOrigin1, reference); + CheckAreaData(kNamespace1, kOrigin1, reference); // Overwrite and delete values. { @@ -396,10 +387,10 @@ TEST_F(SessionStorageDatabaseTest, WriteDataForOneOrigin) { changes[kKey3] = kValueNull; reference[kKey1] = kValue4; reference.erase(kKey3); - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, changes)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, changes)); } CheckDatabaseConsistency(); - CheckAreaData(1, kOrigin1, reference); + CheckAreaData(kNamespace1, kOrigin1, reference); // Clear data before writing. { @@ -407,10 +398,10 @@ TEST_F(SessionStorageDatabaseTest, WriteDataForOneOrigin) { changes[kKey2] = kValue2; reference.erase(kKey1); reference[kKey2] = kValue2; - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, true, changes)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, true, changes)); } CheckDatabaseConsistency(); - CheckAreaData(1, kOrigin1, reference); + CheckAreaData(kNamespace1, kOrigin1, reference); } TEST_F(SessionStorageDatabaseTest, WriteDataForTwoOrigins) { @@ -419,17 +410,17 @@ TEST_F(SessionStorageDatabaseTest, WriteDataForTwoOrigins) { data1[kKey1] = kValue1; data1[kKey2] = kValue2; data1[kKey3] = kValue3; - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, data1)); ValuesMap data2; data2[kKey1] = kValue4; data2[kKey2] = kValue1; data2[kKey3] = kValue2; - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin2, false, data2)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin2, false, data2)); CheckDatabaseConsistency(); - CheckAreaData(1, kOrigin1, data1); - CheckAreaData(1, kOrigin2, data2); + CheckAreaData(kNamespace1, kOrigin1, data1); + CheckAreaData(kNamespace1, kOrigin2, data2); } TEST_F(SessionStorageDatabaseTest, WriteDataForTwoNamespaces) { @@ -438,24 +429,24 @@ TEST_F(SessionStorageDatabaseTest, WriteDataForTwoNamespaces) { data11[kKey1] = kValue1; data11[kKey2] = kValue2; data11[kKey3] = kValue3; - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data11)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, data11)); ValuesMap data12; data12[kKey2] = kValue4; data12[kKey3] = kValue3; - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin2, false, data12)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin2, false, data12)); ValuesMap data21; data21[kKey1] = kValue2; data21[kKey2] = kValue4; - EXPECT_TRUE(db_->CommitAreaChanges(2, kOrigin1, false, data21)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespace2, kOrigin1, false, data21)); ValuesMap data22; data22[kKey2] = kValue1; data22[kKey3] = kValue2; - EXPECT_TRUE(db_->CommitAreaChanges(2, kOrigin2, false, data22)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespace2, kOrigin2, false, data22)); CheckDatabaseConsistency(); - CheckAreaData(1, kOrigin1, data11); - CheckAreaData(1, kOrigin2, data12); - CheckAreaData(2, kOrigin1, data21); - CheckAreaData(2, kOrigin2, data22); + CheckAreaData(kNamespace1, kOrigin1, data11); + CheckAreaData(kNamespace1, kOrigin2, data12); + CheckAreaData(kNamespace2, kOrigin1, data21); + CheckAreaData(kNamespace2, kOrigin2, data22); } TEST_F(SessionStorageDatabaseTest, ShallowCopy) { @@ -464,32 +455,34 @@ TEST_F(SessionStorageDatabaseTest, ShallowCopy) { data1[kKey1] = kValue1; data1[kKey2] = kValue2; data1[kKey3] = kValue3; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, data1)); ValuesMap data2; data2[kKey1] = kValue2; data2[kKey3] = kValue1; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin2, false, data2)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin2, false, data2)); // Make a shallow copy. - EXPECT_TRUE(db_->CloneNamespace(1, 5)); + EXPECT_TRUE(db_->CloneNamespace(kNamespace1, kNamespaceClone)); // Now both namespaces should have the same data. CheckDatabaseConsistency(); - CheckAreaData(1, kOrigin1, data1); - CheckAreaData(1, kOrigin2, data2); - CheckAreaData(5, kOrigin1, data1); - CheckAreaData(5, kOrigin2, data2); + CheckAreaData(kNamespace1, kOrigin1, data1); + CheckAreaData(kNamespace1, kOrigin2, data2); + CheckAreaData(kNamespaceClone, kOrigin1, data1); + CheckAreaData(kNamespaceClone, kOrigin2, data2); // Both the namespaces refer to the same maps. - EXPECT_EQ(GetMapForArea(1, kOrigin1), GetMapForArea(5, kOrigin1)); - EXPECT_EQ(GetMapForArea(1, kOrigin2), GetMapForArea(5, kOrigin2)); - EXPECT_EQ(2, GetMapRefCount(GetMapForArea(1, kOrigin1))); - EXPECT_EQ(2, GetMapRefCount(GetMapForArea(1, kOrigin2))); + EXPECT_EQ(GetMapForArea(kNamespace1, kOrigin1), + GetMapForArea(kNamespaceClone, kOrigin1)); + EXPECT_EQ(GetMapForArea(kNamespace1, kOrigin2), + GetMapForArea(kNamespaceClone, kOrigin2)); + EXPECT_EQ(2, GetMapRefCount(GetMapForArea(kNamespace1, kOrigin1))); + EXPECT_EQ(2, GetMapRefCount(GetMapForArea(kNamespace1, kOrigin2))); } TEST_F(SessionStorageDatabaseTest, WriteIntoShallowCopy) { ValuesMap data1; data1[kKey1] = kValue1; data1[kKey2] = kValue2; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); - EXPECT_TRUE(db_->CloneNamespace(1, 5)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, data1)); + EXPECT_TRUE(db_->CloneNamespace(kNamespace1, kNamespaceClone)); // Write data into a shallow copy. ValuesMap changes; @@ -499,17 +492,19 @@ TEST_F(SessionStorageDatabaseTest, WriteIntoShallowCopy) { changes[kKey3] = kValue4; reference[kKey2] = kValue4; reference[kKey3] = kValue4; - EXPECT_TRUE(db_->CommitAreaChanges(5, kOrigin1, false, changes)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespaceClone, kOrigin1, false, + changes)); // Values in the original namespace were not changed. - CheckAreaData(1, kOrigin1, data1); + CheckAreaData(kNamespace1, kOrigin1, data1); // But values in the copy were. - CheckAreaData(5, kOrigin1, reference); + CheckAreaData(kNamespaceClone, kOrigin1, reference); // The namespaces no longer refer to the same map. - EXPECT_NE(GetMapForArea(1, kOrigin1), GetMapForArea(5, kOrigin1)); - EXPECT_EQ(1, GetMapRefCount(GetMapForArea(1, kOrigin1))); - EXPECT_EQ(1, GetMapRefCount(GetMapForArea(5, kOrigin1))); + EXPECT_NE(GetMapForArea(kNamespace1, kOrigin1), + GetMapForArea(kNamespaceClone, kOrigin1)); + EXPECT_EQ(1, GetMapRefCount(GetMapForArea(kNamespace1, kOrigin1))); + EXPECT_EQ(1, GetMapRefCount(GetMapForArea(kNamespaceClone, kOrigin1))); } TEST_F(SessionStorageDatabaseTest, ManyShallowCopies) { @@ -518,52 +513,60 @@ TEST_F(SessionStorageDatabaseTest, ManyShallowCopies) { data1[kKey1] = kValue1; data1[kKey2] = kValue2; data1[kKey3] = kValue3; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, data1)); ValuesMap data2; data2[kKey1] = kValue2; data2[kKey3] = kValue1; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin2, false, data2)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin2, false, data2)); // Make a two shallow copies. - EXPECT_TRUE(db_->CloneNamespace(1, 5)); - EXPECT_TRUE(db_->CloneNamespace(1, 6)); + EXPECT_TRUE(db_->CloneNamespace(kNamespace1, kNamespaceClone)); + std::string another_clone("another_cloned"); + EXPECT_TRUE(db_->CloneNamespace(kNamespace1, another_clone)); // Make a shallow copy of a shallow copy. - EXPECT_TRUE(db_->CloneNamespace(6, 7)); + std::string clone_of_clone("clone_of_clone"); + EXPECT_TRUE(db_->CloneNamespace(another_clone, clone_of_clone)); // Now all namespaces should have the same data. CheckDatabaseConsistency(); - CheckAreaData(1, kOrigin1, data1); - CheckAreaData(5, kOrigin1, data1); - CheckAreaData(6, kOrigin1, data1); - CheckAreaData(7, kOrigin1, data1); - CheckAreaData(1, kOrigin2, data2); - CheckAreaData(5, kOrigin2, data2); - CheckAreaData(6, kOrigin2, data2); - CheckAreaData(7, kOrigin2, data2); + CheckAreaData(kNamespace1, kOrigin1, data1); + CheckAreaData(kNamespaceClone, kOrigin1, data1); + CheckAreaData(another_clone, kOrigin1, data1); + CheckAreaData(clone_of_clone, kOrigin1, data1); + CheckAreaData(kNamespace1, kOrigin2, data2); + CheckAreaData(kNamespaceClone, kOrigin2, data2); + CheckAreaData(another_clone, kOrigin2, data2); + CheckAreaData(clone_of_clone, kOrigin2, data2); // All namespaces refer to the same maps. - EXPECT_EQ(GetMapForArea(1, kOrigin1), GetMapForArea(5, kOrigin1)); - EXPECT_EQ(GetMapForArea(1, kOrigin2), GetMapForArea(5, kOrigin2)); - EXPECT_EQ(GetMapForArea(1, kOrigin1), GetMapForArea(6, kOrigin1)); - EXPECT_EQ(GetMapForArea(1, kOrigin2), GetMapForArea(6, kOrigin2)); - EXPECT_EQ(GetMapForArea(1, kOrigin1), GetMapForArea(7, kOrigin1)); - EXPECT_EQ(GetMapForArea(1, kOrigin2), GetMapForArea(7, kOrigin2)); + EXPECT_EQ(GetMapForArea(kNamespace1, kOrigin1), + GetMapForArea(kNamespaceClone, kOrigin1)); + EXPECT_EQ(GetMapForArea(kNamespace1, kOrigin2), + GetMapForArea(kNamespaceClone, kOrigin2)); + EXPECT_EQ(GetMapForArea(kNamespace1, kOrigin1), + GetMapForArea(another_clone, kOrigin1)); + EXPECT_EQ(GetMapForArea(kNamespace1, kOrigin2), + GetMapForArea(another_clone, kOrigin2)); + EXPECT_EQ(GetMapForArea(kNamespace1, kOrigin1), + GetMapForArea(clone_of_clone, kOrigin1)); + EXPECT_EQ(GetMapForArea(kNamespace1, kOrigin2), + GetMapForArea(clone_of_clone, kOrigin2)); // Check the ref counts. - EXPECT_EQ(4, GetMapRefCount(GetMapForArea(1, kOrigin1))); - EXPECT_EQ(4, GetMapRefCount(GetMapForArea(1, kOrigin2))); + EXPECT_EQ(4, GetMapRefCount(GetMapForArea(kNamespace1, kOrigin1))); + EXPECT_EQ(4, GetMapRefCount(GetMapForArea(kNamespace1, kOrigin2))); } TEST_F(SessionStorageDatabaseTest, DisassociateShallowCopy) { ValuesMap data1; data1[kKey1] = kValue1; data1[kKey2] = kValue2; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); - EXPECT_TRUE(db_->CloneNamespace(1, 5)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, data1)); + EXPECT_TRUE(db_->CloneNamespace(kNamespace1, kNamespaceClone)); // Disassoaciate the shallow copy. - EXPECT_TRUE(db_->DeleteArea(5, kOrigin1)); + EXPECT_TRUE(db_->DeleteArea(kNamespaceClone, kOrigin1)); CheckDatabaseConsistency(); // Now new data can be written to that map. @@ -574,13 +577,14 @@ TEST_F(SessionStorageDatabaseTest, DisassociateShallowCopy) { changes[kKey3] = kValue4; reference[kKey2] = kValue4; reference[kKey3] = kValue4; - EXPECT_TRUE(db_->CommitAreaChanges(5, kOrigin1, false, changes)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespaceClone, kOrigin1, false, + changes)); // Values in the original map were not changed. - CheckAreaData(1, kOrigin1, data1); + CheckAreaData(kNamespace1, kOrigin1, data1); // But values in the disassociated map were. - CheckAreaData(5, kOrigin1, reference); + CheckAreaData(kNamespaceClone, kOrigin1, reference); } TEST_F(SessionStorageDatabaseTest, DeleteNamespace) { @@ -588,12 +592,12 @@ TEST_F(SessionStorageDatabaseTest, DeleteNamespace) { data1[kKey1] = kValue1; data1[kKey2] = kValue2; data1[kKey3] = kValue3; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, data1)); ValuesMap data2; data2[kKey2] = kValue4; data2[kKey3] = kValue3; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin2, false, data2)); - EXPECT_TRUE(db_->DeleteNamespace(1)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin2, false, data2)); + EXPECT_TRUE(db_->DeleteNamespace(kNamespace1)); CheckDatabaseConsistency(); CheckEmptyDatabase(); } @@ -604,23 +608,23 @@ TEST_F(SessionStorageDatabaseTest, DeleteNamespaceWithShallowCopy) { data1[kKey1] = kValue1; data1[kKey2] = kValue2; data1[kKey3] = kValue3; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, data1)); ValuesMap data2; data2[kKey1] = kValue2; data2[kKey3] = kValue1; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin2, false, data2)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin2, false, data2)); // Make a shallow copy and delete the original namespace. - EXPECT_TRUE(db_->CloneNamespace(1, 5));; - EXPECT_TRUE(db_->DeleteNamespace(1)); + EXPECT_TRUE(db_->CloneNamespace(kNamespace1, kNamespaceClone)); + EXPECT_TRUE(db_->DeleteNamespace(kNamespace1)); // The original namespace has no data. CheckDatabaseConsistency(); - CheckAreaData(1, kOrigin1, ValuesMap()); - CheckAreaData(1, kOrigin2, ValuesMap()); + CheckAreaData(kNamespace1, kOrigin1, ValuesMap()); + CheckAreaData(kNamespace1, kOrigin2, ValuesMap()); // But the copy persists. - CheckAreaData(5, kOrigin1, data1); - CheckAreaData(5, kOrigin2, data2); + CheckAreaData(kNamespaceClone, kOrigin1, data1); + CheckAreaData(kNamespaceClone, kOrigin2, data2); } TEST_F(SessionStorageDatabaseTest, DeleteArea) { @@ -629,18 +633,18 @@ TEST_F(SessionStorageDatabaseTest, DeleteArea) { data1[kKey1] = kValue1; data1[kKey2] = kValue2; data1[kKey3] = kValue3; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, data1)); ValuesMap data2; data2[kKey1] = kValue2; data2[kKey3] = kValue1; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin2, false, data2)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin2, false, data2)); - EXPECT_TRUE(db_->DeleteArea(1, kOrigin2)); + EXPECT_TRUE(db_->DeleteArea(kNamespace1, kOrigin2)); CheckDatabaseConsistency(); // The data for the non-deleted origin persists. - CheckAreaData(1, kOrigin1, data1); + CheckAreaData(kNamespace1, kOrigin1, data1); // The data for the deleted origin is gone. - CheckAreaData(1, kOrigin2, ValuesMap()); + CheckAreaData(kNamespace1, kOrigin2, ValuesMap()); } TEST_F(SessionStorageDatabaseTest, DeleteAreaWithShallowCopy) { @@ -649,23 +653,23 @@ TEST_F(SessionStorageDatabaseTest, DeleteAreaWithShallowCopy) { data1[kKey1] = kValue1; data1[kKey2] = kValue2; data1[kKey3] = kValue3; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, data1)); ValuesMap data2; data2[kKey1] = kValue2; data2[kKey3] = kValue1; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin2, false, data2)); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin2, false, data2)); // Make a shallow copy and delete an origin from the original namespace. - EXPECT_TRUE(db_->CloneNamespace(1, 5)); - EXPECT_TRUE(db_->DeleteArea(1, kOrigin1)); + EXPECT_TRUE(db_->CloneNamespace(kNamespace1, kNamespaceClone)); + EXPECT_TRUE(db_->DeleteArea(kNamespace1, kOrigin1)); CheckDatabaseConsistency(); // The original namespace has data for only the non-deleted origin. - CheckAreaData(1, kOrigin1, ValuesMap()); - CheckAreaData(1, kOrigin2, data2); + CheckAreaData(kNamespace1, kOrigin1, ValuesMap()); + CheckAreaData(kNamespace1, kOrigin2, data2); // But the copy persists. - CheckAreaData(5, kOrigin1, data1); - CheckAreaData(5, kOrigin2, data2); + CheckAreaData(kNamespaceClone, kOrigin1, data1); + CheckAreaData(kNamespaceClone, kOrigin2, data2); } TEST_F(SessionStorageDatabaseTest, WriteRawBytes) { @@ -675,199 +679,49 @@ TEST_F(SessionStorageDatabaseTest, WriteRawBytes) { string16 string_with_raw_data; string_with_raw_data.assign(reinterpret_cast<char16*>(raw_data), 5); changes[kKey1] = NullableString16(string_with_raw_data, false); - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, changes)); + EXPECT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, changes)); CheckDatabaseConsistency(); ValuesMap values; - db_->ReadAreaValues(1, kOrigin1, &values); + db_->ReadAreaValues(kNamespace1, kOrigin1, &values); const unsigned char* data = reinterpret_cast<const unsigned char*>(values[kKey1].string().data()); for (int i = 0; i < 10; ++i) EXPECT_EQ(raw_data[i], data[i]); } -TEST_F(SessionStorageDatabaseTest, NextNamespaceId) { - // Create namespaces, check the next namespace id. - ValuesMap data1; - data1[kKey1] = kValue1; - data1[kKey2] = kValue2; - ASSERT_TRUE(db_->CommitAreaChanges(10, kOrigin1, false, data1)); - EXPECT_EQ(10 + 1, NextNamespaceId()); - ASSERT_TRUE(db_->CommitAreaChanges(343, kOrigin1, false, data1)); - EXPECT_EQ(343 + 1, NextNamespaceId()); - ASSERT_TRUE(db_->CommitAreaChanges(99, kOrigin1, false, data1)); - EXPECT_EQ(343 + 1, NextNamespaceId()); - - // Close the database and recreate it. - ResetDatabase(); - - // The next namespace id is persisted. - EXPECT_EQ(344, NextNamespaceId()); - - // Create more namespaces. - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); - EXPECT_EQ(344 + 1 + 1, NextNamespaceId()); - - EXPECT_TRUE(db_->CommitAreaChanges(959, kOrigin1, false, data1)); - EXPECT_EQ(344 + 959 + 1, NextNamespaceId()); -} - -TEST_F(SessionStorageDatabaseTest, NamespaceOffset) { - // Create a namespace with id 1. - ValuesMap data1; - data1[kKey1] = kValue1; - data1[kKey2] = kValue2; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); - - // Close the database and recreate it. - ResetDatabase(); - - // Create another namespace with id 1. - ValuesMap data2; - data2[kKey1] = kValue3; - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data2)); - - // Now the values for namespace 1 are the new ones. - CheckAreaData(1, kOrigin1, data2); - - // The values for the old namespace 1 are still accessible via id -1. - CheckAreaData(-1, kOrigin1, data1); -} - -TEST_F(SessionStorageDatabaseTest, NamespaceOffsetCloneNamespace) { - // Create a namespace with id 1. - ValuesMap data1; - data1[kKey1] = kValue1; - data1[kKey2] = kValue2; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); - - // Close the database and recreate it. - ResetDatabase(); - - // Create another namespace with id 1. - ValuesMap data2; - data2[kKey1] = kValue3; - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data2)); - - // Make a shallow copy of the newly created namespace. - EXPECT_TRUE(db_->CloneNamespace(1, 20)); - - // The clone contains values from the newly created namespace. - CheckAreaData(20, kOrigin1, data2); - CheckAreaData(1, kOrigin1, data2); - - // The values for the old namespace 1 are still accessible via id -1. - CheckAreaData(-1, kOrigin1, data1); - - // Close the database and recreate it. - ResetDatabase(); - - // The namespace and the clone are still accessible. - CheckAreaData(-1, kOrigin1, data2); - CheckAreaData(-20, kOrigin1, data2); - CheckAreaData(-22, kOrigin1, data1); -} - -TEST_F(SessionStorageDatabaseTest, NamespaceOffsetWriteIntoShallowCopy) { - // Create a namespace with id 1. - ValuesMap data1; - data1[kKey1] = kValue1; - data1[kKey2] = kValue2; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); - - // Close the database and recreate it. - ResetDatabase(); - - // Create another namespace with id 1. - ValuesMap data2; - data2[kKey1] = kValue3; - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data2)); - - // Make a shallow copy of the newly created namespace. - EXPECT_TRUE(db_->CloneNamespace(1, 20)); - - // Now the values can be altered and a deep copy will be made. - ValuesMap data3; - data3[kKey1] = kValue2; - EXPECT_TRUE(db_->CommitAreaChanges(20, kOrigin1, false, data3)); - - CheckAreaData(20, kOrigin1, data3); - CheckAreaData(1, kOrigin1, data2); - - // The values for the old namespace 1 are still accessible via id -1. - CheckAreaData(-1, kOrigin1, data1); - - // Close the database and recreate it. - ResetDatabase(); - - // The namespace and the deep copy are still accessible. - CheckAreaData(-1, kOrigin1, data3); - CheckAreaData(-20, kOrigin1, data2); - CheckAreaData(-22, kOrigin1, data1); -} +TEST_F(SessionStorageDatabaseTest, DeleteNamespaceConfusion) { + // Regression test for a bug where a namespace with id 10 prevented deleting + // the namespace with id 1. -TEST_F(SessionStorageDatabaseTest, NamespaceOffsetDeleteArea) { - // Create a namespace with id 1. ValuesMap data1; data1[kKey1] = kValue1; - data1[kKey2] = kValue2; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); + ASSERT_TRUE(db_->CommitAreaChanges("foobar", kOrigin1, false, data1)); + ASSERT_TRUE(db_->CommitAreaChanges("foobarbaz", kOrigin1, false, data1)); - // Close the database and recreate it. - ResetDatabase(); - - // Create another namespace with id 1. - ValuesMap data2; - data2[kKey1] = kValue3; - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data2)); - - // Delete kOrigin1 from the newly created namespace. - EXPECT_TRUE(db_->DeleteArea(1, kOrigin1)); - - // Namespace 1 is empty. - CheckAreaData(1, kOrigin1, ValuesMap()); - - // The values for the old namespace 1 are still accessible via id -1. - CheckAreaData(-1, kOrigin1, data1); + // Delete the namespace with ID 1. + EXPECT_TRUE(db_->DeleteNamespace("foobar")); } -TEST_F(SessionStorageDatabaseTest, NamespaceOffsetDeleteNamespace) { - // Create a namespace with id 1. +TEST_F(SessionStorageDatabaseTest, ReadNamespaceIds) { ValuesMap data1; data1[kKey1] = kValue1; data1[kKey2] = kValue2; - ASSERT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data1)); - - // Close the database and recreate it. - ResetDatabase(); - - // Create another namespace with id 1. - ValuesMap data2; - data2[kKey1] = kValue3; - EXPECT_TRUE(db_->CommitAreaChanges(1, kOrigin1, false, data2)); - - // Delete the newly created namespace. - EXPECT_TRUE(db_->DeleteNamespace(1)); + data1[kKey3] = kValue3; + std::set<std::string> expected_namespace_ids; - // Namespace 1 is empty. - CheckAreaData(1, kOrigin1, ValuesMap()); + ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin1, false, data1)); + expected_namespace_ids.insert(kNamespace1); + CheckNamespaceIds(expected_namespace_ids); - // The values for the old namespace 1 are still accessible via id -1. - CheckAreaData(-1, kOrigin1, data1); -} + ASSERT_TRUE(db_->CloneNamespace(kNamespace1, kNamespaceClone)); + expected_namespace_ids.insert(kNamespaceClone); + CheckNamespaceIds(expected_namespace_ids); -TEST_F(SessionStorageDatabaseTest, DeleteNamespaceConfusion) { - // Regression test for a bug where a namespace with id 10 prevented deleting - // the namespace with id 1. + ASSERT_TRUE(db_->DeleteNamespace(kNamespace1)); + expected_namespace_ids.erase(kNamespace1); + CheckNamespaceIds(expected_namespace_ids); - // Create namespace with IDs 0 to 10. The real IDs in the DB will correspond - // to these IDs. - ValuesMap data1; - data1[kKey1] = kValue1; - for (int i = 0; i <= 10; ++i) - ASSERT_TRUE(db_->CommitAreaChanges(i, kOrigin1, false, data1)); - - // Delete the namespace with ID 1. - EXPECT_TRUE(db_->DeleteNamespace(1)); + CheckDatabaseConsistency(); } } // namespace dom_storage |