LEFT | RIGHT |
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 using namespace AdblockPlus; | 24 using namespace AdblockPlus; |
25 | 25 |
26 namespace | 26 namespace |
27 { | 27 { |
28 class MockWebRequest : public AdblockPlus::WebRequest | 28 class MockWebRequest : public AdblockPlus::WebRequest |
29 { | 29 { |
30 public: | 30 public: |
31 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H
eaderList& requestHeaders) const | 31 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H
eaderList& requestHeaders) const |
32 { | 32 { |
| 33 lastRequestHeaders.clear(); |
| 34 for (auto header : requestHeaders) |
| 35 { |
| 36 lastRequestHeaders.insert(header.first); |
| 37 } |
| 38 |
33 AdblockPlus::Sleep(50); | 39 AdblockPlus::Sleep(50); |
34 | 40 |
35 AdblockPlus::ServerResponse result; | 41 AdblockPlus::ServerResponse result; |
36 result.status = NS_OK; | 42 result.status = NS_OK; |
37 result.responseStatus = 123; | 43 result.responseStatus = 123; |
38 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo"
, "Bar")); | 44 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo"
, "Bar")); |
39 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + reques
tHeaders[0].second; | 45 result.responseText = url + "\n"; |
| 46 if (!requestHeaders.empty()) |
| 47 { |
| 48 result.responseText += requestHeaders[0].first + "\n" + requestHeaders[0
].second; |
| 49 } |
40 return result; | 50 return result; |
41 } | 51 } |
| 52 |
| 53 // mutable. Very Ugly. But we are testing and need to change this in GET whi
ch is const. |
| 54 mutable std::set<std::string> lastRequestHeaders; |
42 }; | 55 }; |
43 | 56 |
44 template<class T> | 57 template<class T> |
45 class WebRequestTest : public BaseJsTest | 58 class WebRequestTest : public BaseJsTest |
46 { | 59 { |
47 protected: | 60 protected: |
48 void SetUp() | 61 void SetUp() |
49 { | 62 { |
50 BaseJsTest::SetUp(); | 63 BaseJsTest::SetUp(); |
51 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new T)); | 64 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new T)); |
52 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); | 65 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); |
53 } | 66 } |
54 }; | 67 }; |
55 | 68 |
56 typedef WebRequestTest<MockWebRequest> MockWebRequestTest; | 69 typedef WebRequestTest<MockWebRequest> MockWebRequestTest; |
57 typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest; | 70 typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest; |
| 71 typedef WebRequestTest<MockWebRequest> XMLHttpRequestTest; |
| 72 |
| 73 void ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine) |
| 74 { |
| 75 jsEngine->Evaluate("\ |
| 76 var result;\ |
| 77 var request = new XMLHttpRequest();\ |
| 78 request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.t
xt');\ |
| 79 request.overrideMimeType('text/plain');\ |
| 80 request.addEventListener('load', function() {result = request.responseText
;}, false);\ |
| 81 request.addEventListener('error', function() {result = 'error';}, false);\ |
| 82 "); |
| 83 } |
| 84 |
| 85 void WaitForVariable(const std::string& variable, const AdblockPlus::JsEngineP
tr& jsEngine) |
| 86 { |
| 87 do |
| 88 { |
| 89 AdblockPlus::Sleep(60); |
| 90 } while (jsEngine->Evaluate(variable)->IsUndefined()); |
| 91 } |
| 92 |
58 } | 93 } |
59 | 94 |
60 TEST_F(MockWebRequestTest, BadCall) | 95 TEST_F(MockWebRequestTest, BadCall) |
61 { | 96 { |
62 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()")); | 97 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()")); |
63 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})")); | 98 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})")); |
64 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu
nction(){})")); | 99 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu
nction(){})")); |
65 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu
ll, function(){})")); | 100 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu
ll, function(){})")); |
66 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, null)")); | 101 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, null)")); |
67 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, function(){}, 0)")); | 102 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, function(){}, 0)")); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 strings.clear(); | 170 strings.clear(); |
136 } | 171 } |
137 private: | 172 private: |
138 mutable std::mutex mutex; | 173 mutable std::mutex mutex; |
139 std::vector<std::pair<bool, std::string>> strings; | 174 std::vector<std::pair<bool, std::string>> strings; |
140 }; | 175 }; |
141 } | 176 } |
142 | 177 |
143 TEST_F(MockWebRequestTest, ConnectionIsAllowedOnFilterEngine1) | 178 TEST_F(MockWebRequestTest, ConnectionIsAllowedOnFilterEngine1) |
144 { | 179 { |
145 FilterEngine::CreateParameters createParams; | 180 FilterEngine::CreationParameters createParams; |
146 std::string predefinedAllowedConnectionType = "non-metered"; | 181 std::string predefinedAllowedConnectionType = "non-metered"; |
147 createParams.preconfiguredPrefs.emplace("allowed_connection_type", jsEngine->N
ewValue(predefinedAllowedConnectionType)); | 182 createParams.preconfiguredPrefs.emplace("allowed_connection_type", jsEngine->N
ewValue(predefinedAllowedConnectionType)); |
148 auto receivedConnectionTypes = std::make_shared<SyncStrings>(); | 183 auto receivedConnectionTypes = std::make_shared<SyncStrings>(); |
149 createParams.isConnectionAllowed = [receivedConnectionTypes](const std::string
* allowedConnectionType)->bool { | 184 createParams.isConnectionAllowedCallback = [receivedConnectionTypes](const std
::string* allowedConnectionType)->bool { |
150 receivedConnectionTypes->Add(allowedConnectionType); | 185 receivedConnectionTypes->Add(allowedConnectionType); |
151 return true; | 186 return true; |
152 }; | 187 }; |
153 auto filterEngine = FilterEngine::Create(jsEngine, createParams); | 188 auto filterEngine = FilterEngine::Create(jsEngine, createParams); |
154 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(
result) {foo = result;} )"); | 189 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(
result) {foo = result;} )"); |
155 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); | 190 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); |
156 AdblockPlus::Sleep(200); | 191 AdblockPlus::Sleep(200); |
157 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings(); | 192 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings(); |
158 EXPECT_FALSE(receivedConnectionTypesStrings.empty()); | 193 EXPECT_FALSE(receivedConnectionTypesStrings.empty()); |
159 for (const auto& connectionType : receivedConnectionTypesStrings) | 194 for (const auto& connectionType : receivedConnectionTypesStrings) |
160 { | 195 { |
161 EXPECT_TRUE(connectionType.first); | 196 EXPECT_TRUE(connectionType.first); |
162 EXPECT_EQ(predefinedAllowedConnectionType, connectionType.second); | 197 EXPECT_EQ(predefinedAllowedConnectionType, connectionType.second); |
163 } | 198 } |
164 EXPECT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As
Int()); | 199 EXPECT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As
Int()); |
165 EXPECT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt()); | 200 EXPECT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt()); |
166 EXPECT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")-
>AsString()); | 201 EXPECT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")-
>AsString()); |
167 EXPECT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response
Headers)")->AsString()); | 202 EXPECT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response
Headers)")->AsString()); |
168 } | 203 } |
169 | 204 |
170 TEST_F(MockWebRequestTest, ConnectionIsAllowedOnFilterEngine2) | 205 TEST_F(MockWebRequestTest, ConnectionIsAllowedOnFilterEngine2) |
171 { | 206 { |
172 FilterEngine::CreateParameters createParams; | 207 FilterEngine::CreationParameters createParams; |
173 auto receivedConnectionTypes = std::make_shared<SyncStrings>(); | 208 auto receivedConnectionTypes = std::make_shared<SyncStrings>(); |
174 createParams.isConnectionAllowed = [receivedConnectionTypes](const std::string
* allowedConnectionType)->bool { | 209 createParams.isConnectionAllowedCallback = [receivedConnectionTypes](const std
::string* allowedConnectionType)->bool { |
175 receivedConnectionTypes->Add(allowedConnectionType); | 210 receivedConnectionTypes->Add(allowedConnectionType); |
176 return true; | 211 return true; |
177 }; | 212 }; |
178 auto filterEngine = FilterEngine::Create(jsEngine, createParams); | 213 auto filterEngine = FilterEngine::Create(jsEngine, createParams); |
179 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(
result) {foo = result;} )"); | 214 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(
result) {foo = result;} )"); |
180 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); | 215 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); |
181 AdblockPlus::Sleep(200); | 216 AdblockPlus::Sleep(200); |
182 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings(); | 217 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings(); |
183 EXPECT_FALSE(receivedConnectionTypesStrings.empty()); | 218 EXPECT_FALSE(receivedConnectionTypesStrings.empty()); |
184 for (const auto& connectionType : receivedConnectionTypesStrings) | 219 for (const auto& connectionType : receivedConnectionTypesStrings) |
185 { | 220 { |
186 EXPECT_FALSE(connectionType.first); | 221 EXPECT_FALSE(connectionType.first); |
187 } | 222 } |
188 EXPECT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As
Int()); | 223 EXPECT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As
Int()); |
189 EXPECT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt()); | 224 EXPECT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt()); |
190 EXPECT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")-
>AsString()); | 225 EXPECT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")-
>AsString()); |
191 EXPECT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response
Headers)")->AsString()); | 226 EXPECT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response
Headers)")->AsString()); |
192 } | 227 } |
193 | 228 |
194 TEST_F(MockWebRequestTest, ConnectionIsAllowedOnFilterEngine3) | 229 TEST_F(MockWebRequestTest, ConnectionIsAllowedOnFilterEngine3) |
195 { | 230 { |
196 // initially allowed connection type is not defined | 231 // initially allowed connection type is not defined |
197 FilterEngine::CreateParameters createParams; | 232 FilterEngine::CreationParameters createParams; |
198 auto receivedConnectionTypes = std::make_shared<SyncStrings>(); | 233 auto receivedConnectionTypes = std::make_shared<SyncStrings>(); |
199 createParams.isConnectionAllowed = [receivedConnectionTypes](const std::string
* allowedConnectionType)->bool { | 234 createParams.isConnectionAllowedCallback = [receivedConnectionTypes](const std
::string* allowedConnectionType)->bool { |
200 receivedConnectionTypes->Add(allowedConnectionType); | 235 receivedConnectionTypes->Add(allowedConnectionType); |
201 return true; | 236 return true; |
202 }; | 237 }; |
203 auto filterEngine = FilterEngine::Create(jsEngine, createParams); | 238 auto filterEngine = FilterEngine::Create(jsEngine, createParams); |
204 | 239 |
205 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(
result) {foo = result;} )"); | 240 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(
result) {foo = result;} )"); |
206 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); | 241 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); |
207 AdblockPlus::Sleep(200); | 242 AdblockPlus::Sleep(200); |
208 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings(); | 243 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings(); |
209 EXPECT_FALSE(receivedConnectionTypesStrings.empty()); | 244 EXPECT_FALSE(receivedConnectionTypesStrings.empty()); |
(...skipping 28 matching lines...) Expand all Loading... |
238 receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings(); | 273 receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings(); |
239 EXPECT_FALSE(receivedConnectionTypesStrings.empty()); | 274 EXPECT_FALSE(receivedConnectionTypesStrings.empty()); |
240 for (const auto& connectionType : receivedConnectionTypesStrings) | 275 for (const auto& connectionType : receivedConnectionTypesStrings) |
241 { | 276 { |
242 EXPECT_FALSE(connectionType.first); | 277 EXPECT_FALSE(connectionType.first); |
243 } | 278 } |
244 } | 279 } |
245 | 280 |
246 TEST_F(MockWebRequestTest, ConnectionIsNotAllowedOnFilterEngine) | 281 TEST_F(MockWebRequestTest, ConnectionIsNotAllowedOnFilterEngine) |
247 { | 282 { |
248 FilterEngine::CreateParameters createParams; | 283 FilterEngine::CreationParameters createParams; |
249 std::string predefinedAllowedConnectionType = "non-metered"; | 284 std::string predefinedAllowedConnectionType = "non-metered"; |
250 createParams.preconfiguredPrefs.emplace("allowed_connection_type", jsEngine->N
ewValue(predefinedAllowedConnectionType)); | 285 createParams.preconfiguredPrefs.emplace("allowed_connection_type", jsEngine->N
ewValue(predefinedAllowedConnectionType)); |
251 auto receivedConnectionTypes = std::make_shared<SyncStrings>(); | 286 auto receivedConnectionTypes = std::make_shared<SyncStrings>(); |
252 createParams.isConnectionAllowed = [receivedConnectionTypes](const std::string
* allowedConnectionType)->bool { | 287 createParams.isConnectionAllowedCallback = [receivedConnectionTypes](const std
::string* allowedConnectionType)->bool { |
253 receivedConnectionTypes->Add(allowedConnectionType); | 288 receivedConnectionTypes->Add(allowedConnectionType); |
254 return false; | 289 return false; |
255 }; | 290 }; |
256 auto filterEngine = FilterEngine::Create(jsEngine, createParams); | 291 auto filterEngine = FilterEngine::Create(jsEngine, createParams); |
257 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(
result) {foo = result;} )"); | 292 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(
result) {foo = result;} )"); |
258 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); | 293 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); |
259 AdblockPlus::Sleep(200); | 294 AdblockPlus::Sleep(200); |
260 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings(); | 295 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings(); |
261 EXPECT_FALSE(receivedConnectionTypesStrings.empty()); | 296 EXPECT_FALSE(receivedConnectionTypesStrings.empty()); |
262 for (const auto& connectionType : receivedConnectionTypesStrings) | 297 for (const auto& connectionType : receivedConnectionTypesStrings) |
263 { | 298 { |
264 EXPECT_TRUE(connectionType.first); | 299 EXPECT_TRUE(connectionType.first); |
265 EXPECT_EQ(predefinedAllowedConnectionType, connectionType.second); | 300 EXPECT_EQ(predefinedAllowedConnectionType, connectionType.second); |
266 } | 301 } |
267 EXPECT_EQ(AdblockPlus::WebRequest::NS_ERROR_CONNECTION_REFUSED, jsEngine->Eval
uate("foo.status")->AsInt()); | 302 EXPECT_EQ(AdblockPlus::WebRequest::NS_ERROR_CONNECTION_REFUSED, jsEngine->Eval
uate("foo.status")->AsInt()); |
268 EXPECT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt()); | 303 EXPECT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt()); |
269 EXPECT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString()); | 304 EXPECT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString()); |
270 EXPECT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS
tring()); | 305 EXPECT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS
tring()); |
271 } | 306 } |
272 | 307 |
273 #if defined(HAVE_CURL) || defined(_WIN32) | 308 #if defined(HAVE_CURL) || defined(_WIN32) |
274 TEST_F(DefaultWebRequestTest, RealWebRequest) | 309 TEST_F(DefaultWebRequestTest, RealWebRequest) |
275 { | 310 { |
276 // This URL should redirect to easylist-downloads.adblockplus.org and we | 311 // This URL should redirect to easylist-downloads.adblockplus.org and we |
277 // should get the actual filter list back. | 312 // should get the actual filter list back. |
278 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or
g/easylist.txt', {}, function(result) {foo = result;} )"); | 313 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or
g/easylist.txt', {}, function(result) {foo = result;} )"); |
279 do | 314 WaitForVariable("this.foo", jsEngine); |
280 { | |
281 AdblockPlus::Sleep(200); | |
282 } while (jsEngine->Evaluate("this.foo")->IsUndefined()); | |
283 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type'
].substr(0, 10)")->AsString()); | 315 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type'
].substr(0, 10)")->AsString()); |
284 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As
Int()); | 316 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As
Int()); |
285 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt()); | 317 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt()); |
286 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("foo.responseText.substr(0, 14)
")->AsString()); | 318 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("foo.responseText.substr(0, 14)
")->AsString()); |
287 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type'
].substr(0, 10)")->AsString()); | 319 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type'
].substr(0, 10)")->AsString()); |
| 320 #if defined(HAVE_CURL) |
| 321 ASSERT_EQ("gzip", jsEngine->Evaluate("foo.responseHeaders['content-encoding'].
substr(0, 4)")->AsString()); |
| 322 #endif |
288 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined
()); | 323 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined
()); |
289 } | 324 } |
290 | 325 |
291 TEST_F(DefaultWebRequestTest, XMLHttpRequest) | 326 TEST_F(DefaultWebRequestTest, XMLHttpRequest) |
292 { | 327 { |
293 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); | 328 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); |
294 | 329 |
295 jsEngine->Evaluate("\ | 330 ResetTestXHR(jsEngine); |
296 var result;\ | 331 jsEngine->Evaluate("\ |
297 var request = new XMLHttpRequest();\ | |
298 request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.txt
');\ | |
299 request.setRequestHeader('X', 'Y');\ | 332 request.setRequestHeader('X', 'Y');\ |
300 request.setRequestHeader('X2', 'Y2');\ | 333 request.setRequestHeader('X2', 'Y2');\ |
301 request.overrideMimeType('text/plain');\ | |
302 request.addEventListener('load', function() {result = request.responseText;}
, false);\ | |
303 request.addEventListener('error', function() {result = 'error';}, false);\ | |
304 request.send(null);"); | 334 request.send(null);"); |
305 do | 335 WaitForVariable("result", jsEngine); |
306 { | |
307 AdblockPlus::Sleep(200); | |
308 } while (jsEngine->Evaluate("result")->IsUndefined()); | |
309 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel.
status")->AsInt()); | 336 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel.
status")->AsInt()); |
310 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt()); | 337 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt()); |
311 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri
ng()); | 338 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri
ng()); |
312 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content
-Type').substr(0, 10)")->AsString()); | 339 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content
-Type').substr(0, 10)")->AsString()); |
| 340 #if defined(HAVE_CURL) |
| 341 ASSERT_EQ("gzip", jsEngine->Evaluate("request.getResponseHeader('Content-Encod
ing').substr(0, 4)")->AsString()); |
| 342 #endif |
313 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')")->IsNul
l()); | 343 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')")->IsNul
l()); |
314 } | 344 } |
315 #else | 345 #else |
316 TEST_F(DefaultWebRequestTest, DummyWebRequest) | 346 TEST_F(DefaultWebRequestTest, DummyWebRequest) |
317 { | 347 { |
318 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or
g/easylist.txt', {}, function(result) {foo = result;} )"); | 348 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or
g/easylist.txt', {}, function(result) {foo = result;} )"); |
319 do | 349 WaitForVariable("this.foo", jsEngine); |
320 { | |
321 AdblockPlus::Sleep(200); | |
322 } while (jsEngine->Evaluate("this.foo")->IsUndefined()); | |
323 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.s
tatus")->AsInt()); | 350 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.s
tatus")->AsInt()); |
324 ASSERT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt()); | 351 ASSERT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt()); |
325 ASSERT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString()); | 352 ASSERT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString()); |
326 ASSERT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS
tring()); | 353 ASSERT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS
tring()); |
327 } | 354 } |
328 | 355 |
329 TEST_F(DefaultWebRequestTest, XMLHttpRequest) | 356 TEST_F(DefaultWebRequestTest, XMLHttpRequest) |
330 { | 357 { |
331 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); | 358 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); |
332 | 359 |
333 jsEngine->Evaluate("\ | 360 ResetTestXHR(jsEngine); |
334 var result;\ | 361 jsEngine->Evaluate("\ |
335 var request = new XMLHttpRequest();\ | |
336 request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.txt
');\ | |
337 request.setRequestHeader('X', 'Y');\ | 362 request.setRequestHeader('X', 'Y');\ |
338 request.overrideMimeType('text/plain');\ | |
339 request.addEventListener('load', function() {result = request.responseText;}
, false);\ | |
340 request.addEventListener('error', function() {result = 'error';}, false);\ | |
341 request.send(null);"); | 363 request.send(null);"); |
342 do | 364 WaitForVariable("result", jsEngine); |
343 { | |
344 AdblockPlus::Sleep(200); | |
345 } while (jsEngine->Evaluate("result")->IsUndefined()); | |
346 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque
st.channel.status")->AsInt()); | 365 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque
st.channel.status")->AsInt()); |
347 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt()); | 366 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt()); |
348 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString()); | 367 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString()); |
349 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I
sNull()); | 368 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I
sNull()); |
350 } | 369 } |
351 | 370 |
352 #endif | 371 #endif |
| 372 |
| 373 namespace |
| 374 { |
| 375 class CatchLogSystem : public AdblockPlus::LogSystem |
| 376 { |
| 377 public: |
| 378 AdblockPlus::LogSystem::LogLevel lastLogLevel; |
| 379 std::string lastMessage; |
| 380 |
| 381 CatchLogSystem() |
| 382 : AdblockPlus::LogSystem(), |
| 383 lastLogLevel(AdblockPlus::LogSystem::LOG_LEVEL_TRACE) |
| 384 { |
| 385 } |
| 386 |
| 387 void operator()(AdblockPlus::LogSystem::LogLevel logLevel, |
| 388 const std::string& message, const std::string&) |
| 389 { |
| 390 lastLogLevel = logLevel; |
| 391 lastMessage = message; |
| 392 } |
| 393 |
| 394 void clear() |
| 395 { |
| 396 lastLogLevel = AdblockPlus::LogSystem::LOG_LEVEL_TRACE; |
| 397 lastMessage.clear(); |
| 398 } |
| 399 }; |
| 400 |
| 401 typedef std::shared_ptr<CatchLogSystem> CatchLogSystemPtr; |
| 402 } |
| 403 |
| 404 TEST_F(XMLHttpRequestTest, RequestHeaderValidation) |
| 405 { |
| 406 auto catchLogSystem = CatchLogSystemPtr(new CatchLogSystem); |
| 407 jsEngine->SetLogSystem(catchLogSystem); |
| 408 |
| 409 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); |
| 410 auto webRequest = |
| 411 std::static_pointer_cast<MockWebRequest>(jsEngine->GetWebRequest()); |
| 412 |
| 413 ASSERT_TRUE(webRequest); |
| 414 |
| 415 const std::string msg = "Attempt to set a forbidden header was denied: "; |
| 416 |
| 417 // The test will check that console.warn has been called when the |
| 418 // header is rejected. While this is an implementation detail, we |
| 419 // have no other way to check this |
| 420 |
| 421 // test 'Accept-Encoding' is rejected |
| 422 catchLogSystem->clear(); |
| 423 ResetTestXHR(jsEngine); |
| 424 jsEngine->Evaluate("\ |
| 425 request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();"); |
| 426 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel
); |
| 427 EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage); |
| 428 WaitForVariable("result", jsEngine); |
| 429 EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == |
| 430 webRequest->lastRequestHeaders.find("Accept-Encoding")); |
| 431 |
| 432 // test 'DNT' is rejected |
| 433 catchLogSystem->clear(); |
| 434 ResetTestXHR(jsEngine); |
| 435 jsEngine->Evaluate("\ |
| 436 request.setRequestHeader('DNT', '1');\nrequest.send();"); |
| 437 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel
); |
| 438 EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage); |
| 439 WaitForVariable("result", jsEngine); |
| 440 EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == |
| 441 webRequest->lastRequestHeaders.find("DNT")); |
| 442 |
| 443 // test random 'X' header is accepted |
| 444 catchLogSystem->clear(); |
| 445 ResetTestXHR(jsEngine); |
| 446 jsEngine->Evaluate("\ |
| 447 request.setRequestHeader('X', 'y');\nrequest.send();"); |
| 448 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve
l); |
| 449 EXPECT_EQ("", catchLogSystem->lastMessage); |
| 450 WaitForVariable("result", jsEngine); |
| 451 EXPECT_FALSE(webRequest->lastRequestHeaders.cend() == |
| 452 webRequest->lastRequestHeaders.find("X")); |
| 453 |
| 454 // test /^Proxy-/ is rejected. |
| 455 catchLogSystem->clear(); |
| 456 ResetTestXHR(jsEngine); |
| 457 jsEngine->Evaluate("\ |
| 458 request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();"); |
| 459 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel
); |
| 460 EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage); |
| 461 WaitForVariable("result", jsEngine); |
| 462 EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == |
| 463 webRequest->lastRequestHeaders.find("Proxy-foo")); |
| 464 |
| 465 // test /^Sec-/ is rejected. |
| 466 catchLogSystem->clear(); |
| 467 ResetTestXHR(jsEngine); |
| 468 jsEngine->Evaluate("\ |
| 469 request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();"); |
| 470 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel
); |
| 471 EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage); |
| 472 WaitForVariable("result", jsEngine); |
| 473 EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == |
| 474 webRequest->lastRequestHeaders.find("Sec-foo")); |
| 475 |
| 476 // test 'Security' is accepted. |
| 477 catchLogSystem->clear(); |
| 478 ResetTestXHR(jsEngine); |
| 479 jsEngine->Evaluate("\ |
| 480 request.setRequestHeader('Security', 'theater');\nrequest.send();"); |
| 481 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve
l); |
| 482 EXPECT_EQ("", catchLogSystem->lastMessage); |
| 483 WaitForVariable("result", jsEngine); |
| 484 EXPECT_FALSE(webRequest->lastRequestHeaders.cend() == |
| 485 webRequest->lastRequestHeaders.find("Security")); |
| 486 } |
LEFT | RIGHT |