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