Add testing/scripts/OWNERS
[chromium-blink-merge.git] / extensions / browser / api / storage / storage_api_unittest.cc
blob272396cba3a681d642e42f8768b1b64bdd3321fc
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 "content/public/test/test_browser_context.h"
10 #include "extensions/browser/api/extensions_api_client.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/api_unittest.h"
17 #include "extensions/browser/event_router.h"
18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/browser/mock_extension_system.h"
21 #include "extensions/browser/test_extensions_browser_client.h"
22 #include "extensions/browser/value_store/leveldb_value_store.h"
23 #include "extensions/browser/value_store/value_store.h"
24 #include "extensions/common/manifest.h"
25 #include "extensions/common/test_util.h"
26 #include "third_party/leveldatabase/src/include/leveldb/db.h"
27 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
29 namespace extensions {
31 namespace {
33 // Caller owns the returned object.
34 KeyedService* CreateStorageFrontendForTesting(
35 content::BrowserContext* context) {
36 return StorageFrontend::CreateForTesting(new LeveldbSettingsStorageFactory(),
37 context);
40 } // namespace
42 class StorageApiUnittest : public ApiUnitTest {
43 public:
44 StorageApiUnittest() {}
46 void SetUp() override {
47 ApiUnitTest::SetUp();
48 extensions_browser_client()->set_extension_system_factory(
49 &extension_system_factory_);
52 protected:
53 // Runs the storage.set() API function with local storage.
54 void RunSetFunction(const std::string& key, const std::string& value) {
55 RunFunction(
56 new StorageStorageAreaSetFunction(),
57 base::StringPrintf(
58 "[\"local\", {\"%s\": \"%s\"}]", key.c_str(), value.c_str()));
61 // Runs the storage.get() API function with the local storage, and populates
62 // |value| with the string result.
63 testing::AssertionResult RunGetFunction(const std::string& key,
64 std::string* value) {
65 scoped_ptr<base::Value> result = RunFunctionAndReturnValue(
66 new StorageStorageAreaGetFunction(),
67 base::StringPrintf("[\"local\", \"%s\"]", key.c_str()));
68 if (!result.get())
69 return testing::AssertionFailure() << "No result";
70 base::DictionaryValue* dict = NULL;
71 if (!result->GetAsDictionary(&dict))
72 return testing::AssertionFailure() << result << " was not a dictionary.";
73 if (!dict->GetString(key, value)) {
74 return testing::AssertionFailure() << " could not retrieve a string from"
75 << dict << " at " << key;
77 return testing::AssertionSuccess();
80 MockExtensionSystemFactory<
81 settings_test_util::MockExtensionSystemWithEventRouter>
82 extension_system_factory_;
83 ExtensionsAPIClient extensions_api_client_;
86 TEST_F(StorageApiUnittest, RestoreCorruptedStorage) {
87 // Ensure a StorageFrontend can be created on demand. The StorageFrontend
88 // will be owned by the KeyedService system.
89 StorageFrontend::GetFactoryInstance()->SetTestingFactory(
90 browser_context(), &CreateStorageFrontendForTesting);
92 const char kKey[] = "key";
93 const char kValue[] = "value";
94 std::string result;
96 // Do a simple set/get combo to make sure the API works.
97 RunSetFunction(kKey, kValue);
98 EXPECT_TRUE(RunGetFunction(kKey, &result));
99 EXPECT_EQ(kValue, result);
101 // Corrupt the store. This is not as pretty as ideal, because we use knowledge
102 // of the underlying structure, but there's no real good way to corrupt a
103 // store other than directly modifying the files.
104 ValueStore* store =
105 settings_test_util::GetStorage(extension_ref(),
106 settings_namespace::LOCAL,
107 StorageFrontend::Get(browser_context()));
108 ASSERT_TRUE(store);
109 SettingsStorageQuotaEnforcer* quota_store =
110 static_cast<SettingsStorageQuotaEnforcer*>(store);
111 LeveldbValueStore* leveldb_store =
112 static_cast<LeveldbValueStore*>(quota_store->get_delegate_for_test());
113 leveldb::WriteBatch batch;
114 batch.Put(kKey, "[{(.*+\"\'\\");
115 EXPECT_TRUE(leveldb_store->WriteToDbForTest(&batch));
116 EXPECT_TRUE(leveldb_store->Get(kKey)->IsCorrupted());
118 // Running another set should end up working (even though it will restore the
119 // store behind the scenes).
120 RunSetFunction(kKey, kValue);
121 EXPECT_TRUE(RunGetFunction(kKey, &result));
122 EXPECT_EQ(kValue, result);
125 } // namespace extensions