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