Home | History | Annotate | Download | only in storage
      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.
      4 
      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"
     28 
     29 namespace extensions {
     30 
     31 namespace {
     32 
     33 // Caller owns the returned object.
     34 KeyedService* CreateStorageFrontendForTesting(
     35     content::BrowserContext* context) {
     36   return StorageFrontend::CreateForTesting(new LeveldbSettingsStorageFactory(),
     37                                            context);
     38 }
     39 
     40 }  // namespace
     41 
     42 class StorageApiUnittest : public ApiUnitTest {
     43  public:
     44   StorageApiUnittest() {}
     45 
     46   virtual void SetUp() OVERRIDE {
     47     ApiUnitTest::SetUp();
     48     extensions_browser_client()->set_extension_system_factory(
     49         &extension_system_factory_);
     50   }
     51 
     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()));
     59   }
     60 
     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;
     76     }
     77     return testing::AssertionSuccess();
     78   }
     79 
     80   MockExtensionSystemFactory<
     81       settings_test_util::MockExtensionSystemWithEventRouter>
     82       extension_system_factory_;
     83   ExtensionsAPIClient extensions_api_client_;
     84 };
     85 
     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);
     91 
     92   const char kKey[] = "key";
     93   const char kValue[] = "value";
     94   std::string result;
     95 
     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);
    100 
    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());
    117 
    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);
    123 }
    124 
    125 }  // namespace extensions
    126