Update path of checkdeps to buildtools checkout
[chromium-blink-merge.git] / extensions / browser / api / storage / storage_api_unittest.cc
bloba108b5d6f2d1ea30dc47378a77f21b42aed5e5a5
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 "base/command_line.h"
6 #include "base/files/file_path.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/extensions/extension_api_unittest.h"
10 #include "chrome/browser/extensions/test_extension_system.h"
11 #include "extensions/browser/api/storage/leveldb_settings_storage_factory.h"
12 #include "extensions/browser/api/storage/settings_storage_quota_enforcer.h"
13 #include "extensions/browser/api/storage/settings_test_util.h"
14 #include "extensions/browser/api/storage/storage_api.h"
15 #include "extensions/browser/api/storage/storage_frontend.h"
16 #include "extensions/browser/event_router.h"
17 #include "extensions/browser/extension_prefs.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/value_store/leveldb_value_store.h"
20 #include "extensions/browser/value_store/value_store.h"
21 #include "extensions/common/id_util.h"
22 #include "extensions/common/manifest.h"
23 #include "extensions/common/test_util.h"
24 #include "third_party/leveldatabase/src/include/leveldb/db.h"
25 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
27 namespace extensions {
29 namespace {
31 // Caller owns the returned object.
32 KeyedService* CreateStorageFrontendForTesting(
33 content::BrowserContext* context) {
34 return StorageFrontend::CreateForTesting(new LeveldbSettingsStorageFactory(),
35 context);
38 } // namespace
40 class StorageApiUnittest : public ExtensionApiUnittest {
41 public:
42 virtual void SetUp() OVERRIDE {
43 ExtensionApiUnittest::SetUp();
44 TestExtensionSystem* extension_system =
45 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()));
46 // StorageFrontend requires an EventRouter.
47 extension_system->SetEventRouter(scoped_ptr<EventRouter>(
48 new EventRouter(profile(), ExtensionPrefs::Get(profile()))));
51 protected:
52 // Runs the storage.set() API function with local storage.
53 void RunSetFunction(const std::string& key, const std::string& value) {
54 RunFunction(
55 new StorageStorageAreaSetFunction(),
56 base::StringPrintf(
57 "[\"local\", {\"%s\": \"%s\"}]", key.c_str(), value.c_str()));
60 // Runs the storage.get() API function with the local storage, and populates
61 // |value| with the string result.
62 testing::AssertionResult RunGetFunction(const std::string& key,
63 std::string* value) {
64 scoped_ptr<base::Value> result = RunFunctionAndReturnValue(
65 new StorageStorageAreaGetFunction(),
66 base::StringPrintf("[\"local\", \"%s\"]", key.c_str()));
67 if (!result.get())
68 return testing::AssertionFailure() << "No result";
69 base::DictionaryValue* dict = NULL;
70 if (!result->GetAsDictionary(&dict))
71 return testing::AssertionFailure() << result << " was not a dictionary.";
72 if (!dict->GetString(key, value)) {
73 return testing::AssertionFailure() << " could not retrieve a string from"
74 << dict << " at " << key;
76 return testing::AssertionSuccess();
80 TEST_F(StorageApiUnittest, RestoreCorruptedStorage) {
81 // Ensure a StorageFrontend can be created on demand. The StorageFrontend
82 // will be owned by the KeyedService system.
83 StorageFrontend::GetFactoryInstance()->SetTestingFactory(
84 profile(), &CreateStorageFrontendForTesting);
86 const char kKey[] = "key";
87 const char kValue[] = "value";
88 std::string result;
90 // Do a simple set/get combo to make sure the API works.
91 RunSetFunction(kKey, kValue);
92 EXPECT_TRUE(RunGetFunction(kKey, &result));
93 EXPECT_EQ(kValue, result);
95 // Corrupt the store. This is not as pretty as ideal, because we use knowledge
96 // of the underlying structure, but there's no real good way to corrupt a
97 // store other than directly modifying the files.
98 ValueStore* store =
99 settings_test_util::GetStorage(extension_ref(),
100 settings_namespace::LOCAL,
101 StorageFrontend::Get(profile()));
102 ASSERT_TRUE(store);
103 SettingsStorageQuotaEnforcer* quota_store =
104 static_cast<SettingsStorageQuotaEnforcer*>(store);
105 LeveldbValueStore* leveldb_store =
106 static_cast<LeveldbValueStore*>(quota_store->get_delegate_for_test());
107 leveldb::WriteBatch batch;
108 batch.Put(kKey, "[{(.*+\"\'\\");
109 EXPECT_TRUE(leveldb_store->WriteToDbForTest(&batch));
110 EXPECT_TRUE(leveldb_store->Get(kKey)->IsCorrupted());
112 // Running another set should end up working (even though it will restore the
113 // store behind the scenes).
114 RunSetFunction(kKey, kValue);
115 EXPECT_TRUE(RunGetFunction(kKey, &result));
116 EXPECT_EQ(kValue, result);
119 } // namespace extensions