Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / extensions / browser / api / storage / storage_api_unittest.cc
bloba0845d9f0c6ac2e43af9a5fde727964aa769c1b7
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() {}
45 ~StorageApiUnittest() override {}
47 protected:
48 // Runs the storage.set() API function with local storage.
49 void RunSetFunction(const std::string& key, const std::string& value) {
50 RunFunction(
51 new StorageStorageAreaSetFunction(),
52 base::StringPrintf(
53 "[\"local\", {\"%s\": \"%s\"}]", key.c_str(), value.c_str()));
56 // Runs the storage.get() API function with the local storage, and populates
57 // |value| with the string result.
58 testing::AssertionResult RunGetFunction(const std::string& key,
59 std::string* value) {
60 scoped_ptr<base::Value> result = RunFunctionAndReturnValue(
61 new StorageStorageAreaGetFunction(),
62 base::StringPrintf("[\"local\", \"%s\"]", key.c_str()));
63 if (!result.get())
64 return testing::AssertionFailure() << "No result";
65 base::DictionaryValue* dict = NULL;
66 if (!result->GetAsDictionary(&dict))
67 return testing::AssertionFailure() << result << " was not a dictionary.";
68 if (!dict->GetString(key, value)) {
69 return testing::AssertionFailure() << " could not retrieve a string from"
70 << dict << " at " << key;
72 return testing::AssertionSuccess();
75 ExtensionsAPIClient extensions_api_client_;
78 TEST_F(StorageApiUnittest, RestoreCorruptedStorage) {
79 EventRouter event_router(browser_context(), nullptr);
80 MockExtensionSystem* system = static_cast<MockExtensionSystem*>(
81 ExtensionSystem::Get(browser_context()));
82 system->set_event_router(&event_router);
84 // Ensure a StorageFrontend can be created on demand. The StorageFrontend
85 // will be owned by the KeyedService system.
86 StorageFrontend::GetFactoryInstance()->SetTestingFactory(
87 browser_context(), &CreateStorageFrontendForTesting);
89 const char kKey[] = "key";
90 const char kValue[] = "value";
91 std::string result;
93 // Do a simple set/get combo to make sure the API works.
94 RunSetFunction(kKey, kValue);
95 EXPECT_TRUE(RunGetFunction(kKey, &result));
96 EXPECT_EQ(kValue, result);
98 // Corrupt the store. This is not as pretty as ideal, because we use knowledge
99 // of the underlying structure, but there's no real good way to corrupt a
100 // store other than directly modifying the files.
101 ValueStore* store =
102 settings_test_util::GetStorage(extension_ref(),
103 settings_namespace::LOCAL,
104 StorageFrontend::Get(browser_context()));
105 ASSERT_TRUE(store);
106 SettingsStorageQuotaEnforcer* quota_store =
107 static_cast<SettingsStorageQuotaEnforcer*>(store);
108 LeveldbValueStore* leveldb_store =
109 static_cast<LeveldbValueStore*>(quota_store->get_delegate_for_test());
110 leveldb::WriteBatch batch;
111 batch.Put(kKey, "[{(.*+\"\'\\");
112 EXPECT_TRUE(leveldb_store->WriteToDbForTest(&batch));
113 EXPECT_TRUE(leveldb_store->Get(kKey)->IsCorrupted());
115 // Running another set should end up working (even though it will restore the
116 // store behind the scenes).
117 RunSetFunction(kKey, kValue);
118 EXPECT_TRUE(RunGetFunction(kKey, &result));
119 EXPECT_EQ(kValue, result);
122 } // namespace extensions