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/memory/scoped_ptr.h"
9 #include "base/strings/stringprintf.h"
10 #include "content/public/test/test_browser_context.h"
11 #include "extensions/browser/api/extensions_api_client.h"
12 #include "extensions/browser/api/storage/leveldb_settings_storage_factory.h"
13 #include "extensions/browser/api/storage/settings_storage_quota_enforcer.h"
14 #include "extensions/browser/api/storage/settings_test_util.h"
15 #include "extensions/browser/api/storage/storage_api.h"
16 #include "extensions/browser/api/storage/storage_frontend.h"
17 #include "extensions/browser/api_unittest.h"
18 #include "extensions/browser/event_router.h"
19 #include "extensions/browser/event_router_factory.h"
20 #include "extensions/browser/test_extensions_browser_client.h"
21 #include "extensions/browser/value_store/leveldb_value_store.h"
22 #include "extensions/browser/value_store/value_store.h"
23 #include "extensions/common/manifest.h"
24 #include "extensions/common/test_util.h"
25 #include "third_party/leveldatabase/src/include/leveldb/db.h"
26 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
28 namespace extensions
{
32 // Caller owns the returned object.
33 scoped_ptr
<KeyedService
> CreateStorageFrontendForTesting(
34 content::BrowserContext
* context
) {
35 return StorageFrontend::CreateForTesting(new LeveldbSettingsStorageFactory(),
39 scoped_ptr
<KeyedService
> BuildEventRouter(content::BrowserContext
* context
) {
40 return make_scoped_ptr(new extensions::EventRouter(context
, nullptr));
45 class StorageApiUnittest
: public ApiUnitTest
{
47 StorageApiUnittest() {}
48 ~StorageApiUnittest() override
{}
51 // Runs the storage.set() API function with local storage.
52 void RunSetFunction(const std::string
& key
, const std::string
& value
) {
54 new StorageStorageAreaSetFunction(),
56 "[\"local\", {\"%s\": \"%s\"}]", key
.c_str(), value
.c_str()));
59 // Runs the storage.get() API function with the local storage, and populates
60 // |value| with the string result.
61 testing::AssertionResult
RunGetFunction(const std::string
& key
,
63 scoped_ptr
<base::Value
> result
= RunFunctionAndReturnValue(
64 new StorageStorageAreaGetFunction(),
65 base::StringPrintf("[\"local\", \"%s\"]", key
.c_str()));
67 return testing::AssertionFailure() << "No result";
68 base::DictionaryValue
* dict
= NULL
;
69 if (!result
->GetAsDictionary(&dict
))
70 return testing::AssertionFailure() << result
<< " was not a dictionary.";
71 if (!dict
->GetString(key
, value
)) {
72 return testing::AssertionFailure() << " could not retrieve a string from"
73 << dict
<< " at " << key
;
75 return testing::AssertionSuccess();
78 ExtensionsAPIClient extensions_api_client_
;
81 TEST_F(StorageApiUnittest
, RestoreCorruptedStorage
) {
82 EventRouterFactory::GetInstance()->SetTestingFactory(browser_context(),
85 // Ensure a StorageFrontend can be created on demand. The StorageFrontend
86 // will be owned by the KeyedService system.
87 StorageFrontend::GetFactoryInstance()->SetTestingFactory(
88 browser_context(), &CreateStorageFrontendForTesting
);
90 const char kKey
[] = "key";
91 const char kValue
[] = "value";
94 // Do a simple set/get combo to make sure the API works.
95 RunSetFunction(kKey
, kValue
);
96 EXPECT_TRUE(RunGetFunction(kKey
, &result
));
97 EXPECT_EQ(kValue
, result
);
99 // Corrupt the store. This is not as pretty as ideal, because we use knowledge
100 // of the underlying structure, but there's no real good way to corrupt a
101 // store other than directly modifying the files.
103 settings_test_util::GetStorage(extension_ref(),
104 settings_namespace::LOCAL
,
105 StorageFrontend::Get(browser_context()));
107 SettingsStorageQuotaEnforcer
* quota_store
=
108 static_cast<SettingsStorageQuotaEnforcer
*>(store
);
109 LeveldbValueStore
* leveldb_store
=
110 static_cast<LeveldbValueStore
*>(quota_store
->get_delegate_for_test());
111 leveldb::WriteBatch batch
;
112 batch
.Put(kKey
, "[{(.*+\"\'\\");
113 EXPECT_TRUE(leveldb_store
->WriteToDbForTest(&batch
));
114 EXPECT_TRUE(leveldb_store
->Get(kKey
)->IsCorrupted());
116 // Running another set should end up working (even though it will restore the
117 // store behind the scenes).
118 RunSetFunction(kKey
, kValue
);
119 EXPECT_TRUE(RunGetFunction(kKey
, &result
));
120 EXPECT_EQ(kValue
, result
);
123 } // namespace extensions