OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #include <sstream> | 18 #include <sstream> |
19 | 19 |
20 #include "../src/Thread.h" | 20 #include "../src/Thread.h" |
21 #include "BaseJsTest.h" | 21 #include "BaseJsTest.h" |
22 | 22 |
23 using namespace AdblockPlus; | 23 using namespace AdblockPlus; |
24 | 24 |
25 namespace | 25 namespace |
26 { | 26 { |
27 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; | |
28 | |
29 class TestFileSystem : public LazyFileSystem | 27 class TestFileSystem : public LazyFileSystem |
30 { | 28 { |
31 public: | 29 public: |
32 IOBuffer prefsContents; | 30 IOBuffer prefsContents; |
33 | 31 |
34 void Read(const std::string& path, const ReadCallback& callback) const overr
ide | 32 void Read(const std::string& path, const ReadCallback& callback) const overr
ide |
35 { | 33 { |
36 scheduler([this, path, callback] | 34 scheduler([this, path, callback] |
37 { | 35 { |
38 if (path == "prefs.json" && !prefsContents.empty()) | 36 if (path == "prefs.json" && !prefsContents.empty()) |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 void ResetPlatform() | 86 void ResetPlatform() |
89 { | 87 { |
90 ThrowingPlatformCreationParameters platformParams; | 88 ThrowingPlatformCreationParameters platformParams; |
91 platformParams.fileSystem = fileSystem; | 89 platformParams.fileSystem = fileSystem; |
92 platformParams.webRequest.reset(new NoopWebRequest()); | 90 platformParams.webRequest.reset(new NoopWebRequest()); |
93 platformParams.logSystem.reset(new LazyLogSystem()); | 91 platformParams.logSystem.reset(new LazyLogSystem()); |
94 platformParams.timer.reset(new NoopTimer()); | 92 platformParams.timer.reset(new NoopTimer()); |
95 platform.reset(new Platform(std::move(platformParams))); | 93 platform.reset(new Platform(std::move(platformParams))); |
96 } | 94 } |
97 | 95 |
98 FilterEnginePtr CreateFilterEngine(const AdblockPlus::FilterEngine::Prefs& p
reconfiguredPrefs = | 96 FilterEngine& CreateFilterEngine(const AdblockPlus::FilterEngine::Prefs& pre
configuredPrefs = |
99 AdblockPlus::FilterEngine::Prefs()) | 97 AdblockPlus::FilterEngine::Prefs()) |
100 { | 98 { |
101 AdblockPlus::FilterEngine::CreationParameters createParams; | 99 AdblockPlus::FilterEngine::CreationParameters createParams; |
102 createParams.preconfiguredPrefs = preconfiguredPrefs; | 100 createParams.preconfiguredPrefs = preconfiguredPrefs; |
103 return ::CreateFilterEngine(*fileSystem, *platform, createParams); | 101 return ::CreateFilterEngine(*fileSystem, *platform, createParams); |
104 } | 102 } |
105 }; | 103 }; |
106 } | 104 } |
107 | 105 |
108 TEST_F(PrefsTest, PrefsGetSet) | 106 TEST_F(PrefsTest, PrefsGetSet) |
109 { | 107 { |
110 auto filterEngine = CreateFilterEngine(); | 108 auto& filterEngine = CreateFilterEngine(); |
111 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile").AsString()); | 109 ASSERT_EQ("patterns.ini", filterEngine.GetPref("patternsfile").AsString()); |
112 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt()); | 110 ASSERT_EQ(24, filterEngine.GetPref("patternsbackupinterval").AsInt()); |
113 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); | 111 ASSERT_TRUE(filterEngine.GetPref("subscriptions_autoupdate").AsBool()); |
114 ASSERT_TRUE(filterEngine->GetPref("foobar").IsUndefined()); | 112 ASSERT_TRUE(filterEngine.GetPref("foobar").IsUndefined()); |
115 | 113 |
116 ASSERT_ANY_THROW(filterEngine->SetPref("patternsfile", GetJsEngine().NewValue(
0))); | 114 ASSERT_ANY_THROW(filterEngine.SetPref("patternsfile", GetJsEngine().NewValue(0
))); |
117 ASSERT_ANY_THROW(filterEngine->SetPref("patternsbackupinterval", GetJsEngine()
.NewValue(true))); | 115 ASSERT_ANY_THROW(filterEngine.SetPref("patternsbackupinterval", GetJsEngine().
NewValue(true))); |
118 ASSERT_ANY_THROW(filterEngine->SetPref("subscriptions_autoupdate", GetJsEngine
().NewValue("foo"))); | 116 ASSERT_ANY_THROW(filterEngine.SetPref("subscriptions_autoupdate", GetJsEngine(
).NewValue("foo"))); |
119 | 117 |
120 filterEngine->SetPref("patternsfile", GetJsEngine().NewValue("filters.ini")); | 118 filterEngine.SetPref("patternsfile", GetJsEngine().NewValue("filters.ini")); |
121 filterEngine->SetPref("patternsbackupinterval", GetJsEngine().NewValue(48)); | 119 filterEngine.SetPref("patternsbackupinterval", GetJsEngine().NewValue(48)); |
122 filterEngine->SetPref("subscriptions_autoupdate", GetJsEngine().NewValue(false
)); | 120 filterEngine.SetPref("subscriptions_autoupdate", GetJsEngine().NewValue(false)
); |
123 | 121 |
124 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile").AsString()); | 122 ASSERT_EQ("filters.ini", filterEngine.GetPref("patternsfile").AsString()); |
125 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval").AsInt()); | 123 ASSERT_EQ(48, filterEngine.GetPref("patternsbackupinterval").AsInt()); |
126 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); | 124 ASSERT_FALSE(filterEngine.GetPref("subscriptions_autoupdate").AsBool()); |
127 } | 125 } |
128 | 126 |
129 TEST_F(PrefsTest, PrefsPersist) | 127 TEST_F(PrefsTest, PrefsPersist) |
130 { | 128 { |
131 { | 129 { |
132 auto filterEngine = CreateFilterEngine(); | 130 auto& filterEngine = CreateFilterEngine(); |
133 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile").AsString()); | 131 ASSERT_EQ("patterns.ini", filterEngine.GetPref("patternsfile").AsString()); |
134 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt()); | 132 ASSERT_EQ(24, filterEngine.GetPref("patternsbackupinterval").AsInt()); |
135 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); | 133 ASSERT_TRUE(filterEngine.GetPref("subscriptions_autoupdate").AsBool()); |
136 | 134 |
137 filterEngine->SetPref("patternsfile", GetJsEngine().NewValue("filters.ini"))
; | 135 filterEngine.SetPref("patternsfile", GetJsEngine().NewValue("filters.ini")); |
138 filterEngine->SetPref("patternsbackupinterval", GetJsEngine().NewValue(48)); | 136 filterEngine.SetPref("patternsbackupinterval", GetJsEngine().NewValue(48)); |
139 filterEngine->SetPref("subscriptions_autoupdate", GetJsEngine().NewValue(fal
se)); | 137 filterEngine.SetPref("subscriptions_autoupdate", GetJsEngine().NewValue(fals
e)); |
140 } | 138 } |
141 ASSERT_FALSE(fileSystem->prefsContents.empty()); | 139 ASSERT_FALSE(fileSystem->prefsContents.empty()); |
142 | 140 |
143 { | 141 { |
144 ResetPlatform(); | 142 ResetPlatform(); |
145 auto filterEngine = CreateFilterEngine(); | 143 auto& filterEngine = CreateFilterEngine(); |
146 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile").AsString()); | 144 ASSERT_EQ("filters.ini", filterEngine.GetPref("patternsfile").AsString()); |
147 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval").AsInt()); | 145 ASSERT_EQ(48, filterEngine.GetPref("patternsbackupinterval").AsInt()); |
148 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); | 146 ASSERT_FALSE(filterEngine.GetPref("subscriptions_autoupdate").AsBool()); |
149 } | 147 } |
150 } | 148 } |
151 | 149 |
152 TEST_F(PrefsTest, UnknownPrefs) | 150 TEST_F(PrefsTest, UnknownPrefs) |
153 { | 151 { |
154 using IOBuffer = AdblockPlus::IFileSystem::IOBuffer; | 152 using IOBuffer = AdblockPlus::IFileSystem::IOBuffer; |
155 std::string content = "{\"foobar\":2, \"patternsbackupinterval\": 12}"; | 153 std::string content = "{\"foobar\":2, \"patternsbackupinterval\": 12}"; |
156 fileSystem->prefsContents = IOBuffer(content.cbegin(), content.cend()); | 154 fileSystem->prefsContents = IOBuffer(content.cbegin(), content.cend()); |
157 auto filterEngine = CreateFilterEngine(); | 155 auto& filterEngine = CreateFilterEngine(); |
158 ASSERT_TRUE(filterEngine->GetPref("foobar").IsUndefined()); | 156 ASSERT_TRUE(filterEngine.GetPref("foobar").IsUndefined()); |
159 ASSERT_EQ(12, filterEngine->GetPref("patternsbackupinterval").AsInt()); | 157 ASSERT_EQ(12, filterEngine.GetPref("patternsbackupinterval").AsInt()); |
160 } | 158 } |
161 | 159 |
162 TEST_F(PrefsTest, SyntaxFailure) | 160 TEST_F(PrefsTest, SyntaxFailure) |
163 { | 161 { |
164 using IOBuffer = AdblockPlus::IFileSystem::IOBuffer; | 162 using IOBuffer = AdblockPlus::IFileSystem::IOBuffer; |
165 std::string content = "{\"patternsbackupinterval\": 6, \"foo\"}"; | 163 std::string content = "{\"patternsbackupinterval\": 6, \"foo\"}"; |
166 fileSystem->prefsContents = IOBuffer(content.cbegin(), content.cend()); | 164 fileSystem->prefsContents = IOBuffer(content.cbegin(), content.cend()); |
167 auto filterEngine = CreateFilterEngine(); | 165 ASSERT_EQ(24, CreateFilterEngine().GetPref("patternsbackupinterval").AsInt()); |
168 | |
169 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt()); | |
170 } | 166 } |
171 | 167 |
172 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured) | 168 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured) |
173 { | 169 { |
174 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 170 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
175 preconfiguredPrefs.emplace("disable_auto_updates", GetJsEngine().NewValue(fals
e)); | 171 preconfiguredPrefs.emplace("disable_auto_updates", GetJsEngine().NewValue(fals
e)); |
176 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue(t
rue)); | 172 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue(t
rue)); |
177 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); | 173 auto& filterEngine = CreateFilterEngine(preconfiguredPrefs); |
178 | 174 |
179 ASSERT_TRUE(filterEngine->GetPref("disable_auto_updates").IsBool()); | 175 ASSERT_TRUE(filterEngine.GetPref("disable_auto_updates").IsBool()); |
180 ASSERT_FALSE(filterEngine->GetPref("disable_auto_updates").AsBool()); | 176 ASSERT_FALSE(filterEngine.GetPref("disable_auto_updates").AsBool()); |
181 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); | 177 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").IsBool()); |
182 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").AsBool()); | 178 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").AsBool()); |
183 } | 179 } |
184 | 180 |
185 TEST_F(PrefsTest, PreconfiguredPrefsUnsupported) | 181 TEST_F(PrefsTest, PreconfiguredPrefsUnsupported) |
186 { | 182 { |
187 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 183 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
188 preconfiguredPrefs.emplace("unsupported_preconfig", GetJsEngine().NewValue(tru
e)); | 184 preconfiguredPrefs.emplace("unsupported_preconfig", GetJsEngine().NewValue(tru
e)); |
189 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); | 185 auto& filterEngine = CreateFilterEngine(preconfiguredPrefs); |
190 | 186 |
191 ASSERT_TRUE(filterEngine->GetPref("unsupported_preconfig").IsUndefined()); | 187 ASSERT_TRUE(filterEngine.GetPref("unsupported_preconfig").IsUndefined()); |
192 } | 188 } |
193 | 189 |
194 TEST_F(PrefsTest, PreconfiguredPrefsOverride) | 190 TEST_F(PrefsTest, PreconfiguredPrefsOverride) |
195 { | 191 { |
196 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 192 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
197 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue(t
rue)); | 193 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue(t
rue)); |
198 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); | 194 auto& filterEngine = CreateFilterEngine(preconfiguredPrefs); |
199 | 195 |
200 filterEngine->SetPref("suppress_first_run_page", GetJsEngine().NewValue(false)
); | 196 filterEngine.SetPref("suppress_first_run_page", GetJsEngine().NewValue(false))
; |
201 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); | 197 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").IsBool()); |
202 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page").AsBool()); | 198 ASSERT_FALSE(filterEngine.GetPref("suppress_first_run_page").AsBool()); |
203 } | 199 } |
204 | 200 |
205 TEST_F(PrefsTest, PrefsPersistWhenPreconfigured) | 201 TEST_F(PrefsTest, PrefsPersistWhenPreconfigured) |
206 { | 202 { |
207 { | 203 { |
208 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 204 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
209 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue
(true)); | 205 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue
(true)); |
210 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); | 206 auto& filterEngine = CreateFilterEngine(preconfiguredPrefs); |
211 | 207 |
212 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); | 208 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").IsBool()); |
213 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").AsBool()); | 209 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").AsBool()); |
214 filterEngine->SetPref("suppress_first_run_page", GetJsEngine().NewValue(fals
e)); | 210 filterEngine.SetPref("suppress_first_run_page", GetJsEngine().NewValue(false
)); |
215 } | 211 } |
216 ASSERT_FALSE(fileSystem->prefsContents.empty()); | 212 ASSERT_FALSE(fileSystem->prefsContents.empty()); |
217 | 213 |
218 { | 214 { |
219 ResetPlatform(); | 215 ResetPlatform(); |
220 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 216 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
221 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue
(true)); | 217 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue
(true)); |
222 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); | 218 auto& filterEngine = CreateFilterEngine(preconfiguredPrefs); |
223 | 219 |
224 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); | 220 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").IsBool()); |
225 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page").AsBool()); | 221 ASSERT_FALSE(filterEngine.GetPref("suppress_first_run_page").AsBool()); |
226 } | 222 } |
227 } | 223 } |
OLD | NEW |