Left: | ||
Right: |
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-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 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 } | 54 } |
55 return result; | 55 return result; |
56 } | 56 } |
57 | 57 |
58 // Testing method | 58 // Testing method |
59 // Get the headers for the request. Return a pair of a bool (found | 59 // Get the headers for the request. Return a pair of a bool (found |
60 // or not) and the actual header names | 60 // or not) and the actual header names |
61 std::pair<bool, std::set<std::string>> headersForRequest(const std::string& url) | 61 std::pair<bool, std::set<std::string>> headersForRequest(const std::string& url) |
62 { | 62 { |
63 std::lock_guard<std::mutex> lock(requestHeaderNamesMutex); | 63 std::lock_guard<std::mutex> lock(requestHeaderNamesMutex); |
64 const auto& iter = requestHeaderNames.find(url); | 64 auto iter = requestHeaderNames.find(url); |
sergei
2017/04/19 13:17:29
const reference keeps the object from destroying h
hub
2017/04/19 14:41:00
indeed, it's better. not sure what I thought here.
| |
65 if (iter != requestHeaderNames.end()) | 65 if (iter != requestHeaderNames.end()) |
66 { | 66 { |
67 auto result = std::make_pair(true, iter->second); | 67 auto result = std::make_pair(true, iter->second); |
68 requestHeaderNames.erase(iter); | 68 requestHeaderNames.erase(iter); |
69 return result; | 69 return result; |
70 } | 70 } |
71 return std::make_pair(false, std::set<std::string>()); | 71 return std::make_pair(false, std::set<std::string>()); |
72 } | 72 } |
73 | 73 |
74 // mutable. Very Ugly. But we are testing and need to change this in GET whi ch is const. | 74 // mutable. Very Ugly. But we are testing and need to change this in GET whi ch is const. |
75 mutable std::mutex requestHeaderNamesMutex; | 75 mutable std::mutex requestHeaderNamesMutex; |
76 mutable std::map<std::string, std::set<std::string>> requestHeaderNames; | 76 mutable std::map<std::string, std::set<std::string>> requestHeaderNames; |
77 }; | 77 }; |
78 | 78 |
79 template<class T> | 79 template<class T> |
80 class WebRequestTest : public BaseJsTest | 80 class WebRequestTest : public BaseJsTest |
81 { | 81 { |
82 protected: | 82 protected: |
83 void SetUp() | 83 void SetUp() |
84 { | 84 { |
85 BaseJsTest::SetUp(); | 85 BaseJsTest::SetUp(); |
86 webRequest = AdblockPlus::WebRequestPtr(new T()); | 86 webRequest = std::make_shared<T>(); |
87 jsEngine->SetWebRequest(webRequest); | 87 jsEngine->SetWebRequest(webRequest); |
88 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); | 88 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); |
89 } | 89 } |
90 | 90 |
91 AdblockPlus::WebRequestPtr webRequest; | 91 std::shared_ptr<T> webRequest; |
sergei
2017/04/19 13:17:29
What about storing of std::shared_ptr<MockWebReque
hub
2017/04/19 14:41:00
more like std::shared_ptr<T>. Sure I can do that.
| |
92 }; | 92 }; |
93 | 93 |
94 typedef WebRequestTest<MockWebRequest> MockWebRequestTest; | 94 typedef WebRequestTest<MockWebRequest> MockWebRequestTest; |
95 typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest; | 95 typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest; |
96 typedef WebRequestTest<MockWebRequest> XMLHttpRequestTest; | 96 typedef WebRequestTest<MockWebRequest> XMLHttpRequestTest; |
97 | 97 |
98 // we return the url of the XHR. | 98 // we return the url of the XHR. |
99 std::string ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine, const std:: string& defaultUrl = "") | 99 std::string ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine, const std:: string& defaultUrl = "") |
100 { | 100 { |
101 std::string url = defaultUrl; | 101 std::string url = defaultUrl; |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 | 243 |
244 typedef std::shared_ptr<CatchLogSystem> CatchLogSystemPtr; | 244 typedef std::shared_ptr<CatchLogSystem> CatchLogSystemPtr; |
245 } | 245 } |
246 | 246 |
247 TEST_F(XMLHttpRequestTest, RequestHeaderValidation) | 247 TEST_F(XMLHttpRequestTest, RequestHeaderValidation) |
248 { | 248 { |
249 auto catchLogSystem = CatchLogSystemPtr(new CatchLogSystem()); | 249 auto catchLogSystem = CatchLogSystemPtr(new CatchLogSystem()); |
250 jsEngine->SetLogSystem(catchLogSystem); | 250 jsEngine->SetLogSystem(catchLogSystem); |
251 | 251 |
252 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); | 252 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); |
253 auto mockWebRequest = | |
254 std::static_pointer_cast<MockWebRequest>(webRequest); | |
255 | |
256 ASSERT_TRUE(mockWebRequest); | |
257 | 253 |
258 const std::string msg = "Attempt to set a forbidden header was denied: "; | 254 const std::string msg = "Attempt to set a forbidden header was denied: "; |
259 | 255 |
260 // The test will check that console.warn has been called when the | 256 // The test will check that console.warn has been called when the |
261 // header is rejected. While this is an implementation detail, we | 257 // header is rejected. While this is an implementation detail, we |
262 // have no other way to check this | 258 // have no other way to check this |
263 | 259 |
264 // test 'Accept-Encoding' is rejected | 260 // test 'Accept-Encoding' is rejected |
265 catchLogSystem->clear(); | 261 catchLogSystem->clear(); |
266 std::string url = ResetTestXHR(jsEngine); | 262 std::string url = ResetTestXHR(jsEngine); |
267 jsEngine->Evaluate("\ | 263 jsEngine->Evaluate("\ |
268 request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();"); | 264 request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();"); |
269 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); | 265 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); |
270 EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage); | 266 EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage); |
271 WaitForVariable("result", jsEngine); | 267 WaitForVariable("result", jsEngine); |
272 { | 268 { |
273 auto headersRequest = mockWebRequest->headersForRequest(url); | 269 auto headersRequest = webRequest->headersForRequest(url); |
274 EXPECT_TRUE(headersRequest.first); | 270 EXPECT_TRUE(headersRequest.first); |
275 const auto& headers = headersRequest.second; | 271 const auto& headers = headersRequest.second; |
276 EXPECT_TRUE(headers.cend() == headers.find("Accept-Encoding")); | 272 EXPECT_TRUE(headers.cend() == headers.find("Accept-Encoding")); |
277 } | 273 } |
278 | 274 |
279 // test 'DNT' is rejected | 275 // test 'DNT' is rejected |
280 catchLogSystem->clear(); | 276 catchLogSystem->clear(); |
281 url = ResetTestXHR(jsEngine); | 277 url = ResetTestXHR(jsEngine); |
282 jsEngine->Evaluate("\ | 278 jsEngine->Evaluate("\ |
283 request.setRequestHeader('DNT', '1');\nrequest.send();"); | 279 request.setRequestHeader('DNT', '1');\nrequest.send();"); |
284 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); | 280 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); |
285 EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage); | 281 EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage); |
286 WaitForVariable("result", jsEngine); | 282 WaitForVariable("result", jsEngine); |
287 { | 283 { |
288 auto headersRequest = mockWebRequest->headersForRequest(url); | 284 auto headersRequest = webRequest->headersForRequest(url); |
289 EXPECT_TRUE(headersRequest.first); | 285 EXPECT_TRUE(headersRequest.first); |
290 const auto& headers = headersRequest.second; | 286 const auto& headers = headersRequest.second; |
291 EXPECT_TRUE(headers.cend() == headers.find("DNT")); | 287 EXPECT_TRUE(headers.cend() == headers.find("DNT")); |
292 } | 288 } |
293 | 289 |
294 // test random 'X' header is accepted | 290 // test random 'X' header is accepted |
295 catchLogSystem->clear(); | 291 catchLogSystem->clear(); |
296 url = ResetTestXHR(jsEngine); | 292 url = ResetTestXHR(jsEngine); |
297 jsEngine->Evaluate("\ | 293 jsEngine->Evaluate("\ |
298 request.setRequestHeader('X', 'y');\nrequest.send();"); | 294 request.setRequestHeader('X', 'y');\nrequest.send();"); |
299 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); | 295 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); |
300 EXPECT_EQ("", catchLogSystem->lastMessage); | 296 EXPECT_EQ("", catchLogSystem->lastMessage); |
301 WaitForVariable("result", jsEngine); | 297 WaitForVariable("result", jsEngine); |
302 { | 298 { |
303 auto headersRequest = mockWebRequest->headersForRequest(url); | 299 auto headersRequest = webRequest->headersForRequest(url); |
304 EXPECT_TRUE(headersRequest.first); | 300 EXPECT_TRUE(headersRequest.first); |
305 const auto& headers = headersRequest.second; | 301 const auto& headers = headersRequest.second; |
306 EXPECT_FALSE(headers.cend() == headers.find("X")); | 302 EXPECT_FALSE(headers.cend() == headers.find("X")); |
307 } | 303 } |
308 | 304 |
309 // test /^Proxy-/ is rejected. | 305 // test /^Proxy-/ is rejected. |
310 catchLogSystem->clear(); | 306 catchLogSystem->clear(); |
311 url = ResetTestXHR(jsEngine); | 307 url = ResetTestXHR(jsEngine); |
312 jsEngine->Evaluate("\ | 308 jsEngine->Evaluate("\ |
313 request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();"); | 309 request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();"); |
314 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); | 310 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); |
315 EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage); | 311 EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage); |
316 WaitForVariable("result", jsEngine); | 312 WaitForVariable("result", jsEngine); |
317 { | 313 { |
318 auto headersRequest = mockWebRequest->headersForRequest(url); | 314 auto headersRequest = webRequest->headersForRequest(url); |
319 EXPECT_TRUE(headersRequest.first); | 315 EXPECT_TRUE(headersRequest.first); |
320 const auto& headers = headersRequest.second; | 316 const auto& headers = headersRequest.second; |
321 EXPECT_TRUE(headers.cend() == headers.find("Proxy-foo")); | 317 EXPECT_TRUE(headers.cend() == headers.find("Proxy-foo")); |
322 } | 318 } |
323 | 319 |
324 // test /^Sec-/ is rejected. | 320 // test /^Sec-/ is rejected. |
325 catchLogSystem->clear(); | 321 catchLogSystem->clear(); |
326 url = ResetTestXHR(jsEngine); | 322 url = ResetTestXHR(jsEngine); |
327 jsEngine->Evaluate("\ | 323 jsEngine->Evaluate("\ |
328 request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();"); | 324 request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();"); |
329 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); | 325 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); |
330 EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage); | 326 EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage); |
331 WaitForVariable("result", jsEngine); | 327 WaitForVariable("result", jsEngine); |
332 { | 328 { |
333 auto headersRequest = mockWebRequest->headersForRequest(url); | 329 auto headersRequest = webRequest->headersForRequest(url); |
334 EXPECT_TRUE(headersRequest.first); | 330 EXPECT_TRUE(headersRequest.first); |
335 const auto& headers = headersRequest.second; | 331 const auto& headers = headersRequest.second; |
336 EXPECT_TRUE(headers.cend() == headers.find("Sec-foo")); | 332 EXPECT_TRUE(headers.cend() == headers.find("Sec-foo")); |
337 } | 333 } |
338 | 334 |
339 // test 'Security' is accepted. | 335 // test 'Security' is accepted. |
340 catchLogSystem->clear(); | 336 catchLogSystem->clear(); |
341 url = ResetTestXHR(jsEngine); | 337 url = ResetTestXHR(jsEngine); |
342 jsEngine->Evaluate("\ | 338 jsEngine->Evaluate("\ |
343 request.setRequestHeader('Security', 'theater');\nrequest.send();"); | 339 request.setRequestHeader('Security', 'theater');\nrequest.send();"); |
344 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); | 340 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); |
345 EXPECT_EQ("", catchLogSystem->lastMessage); | 341 EXPECT_EQ("", catchLogSystem->lastMessage); |
346 WaitForVariable("result", jsEngine); | 342 WaitForVariable("result", jsEngine); |
347 { | 343 { |
348 auto headersRequest = mockWebRequest->headersForRequest(url); | 344 auto headersRequest = webRequest->headersForRequest(url); |
349 EXPECT_TRUE(headersRequest.first); | 345 EXPECT_TRUE(headersRequest.first); |
350 const auto& headers = headersRequest.second; | 346 const auto& headers = headersRequest.second; |
351 EXPECT_FALSE(headers.cend() == headers.find("Security")); | 347 EXPECT_FALSE(headers.cend() == headers.find("Security")); |
352 } | 348 } |
353 } | 349 } |
LEFT | RIGHT |