Left: | ||
Right: |
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 |
(...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(); | 33 std::set<std::string> r; |
sergei
2017/04/19 10:38:31
what about renaming here?
r -> headerNames
hub
2017/04/19 13:12:04
Done.
| |
34 for (auto header : requestHeaders) | 34 for (auto header : requestHeaders) |
35 { | 35 { |
36 lastRequestHeaders.insert(header.first); | 36 r.insert(header.first); |
37 } | |
38 { | |
39 std::lock_guard<std::mutex> lock(lastRequestHeadersMutex); | |
40 // XXX we currently ignore the result. We should check it actually gets inserted. | |
sergei
2017/04/19 10:38:31
It would be better to remove XXX.
hub
2017/04/19 13:12:04
Acknowledged.
| |
41 lastRequestHeaders.insert(std::make_pair(url, std::move(r))); | |
37 } | 42 } |
38 | 43 |
39 AdblockPlus::Sleep(50); | 44 AdblockPlus::Sleep(50); |
40 | 45 |
41 AdblockPlus::ServerResponse result; | 46 AdblockPlus::ServerResponse result; |
42 result.status = NS_OK; | 47 result.status = NS_OK; |
43 result.responseStatus = 123; | 48 result.responseStatus = 123; |
44 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo" , "Bar")); | 49 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo" , "Bar")); |
45 result.responseText = url + "\n"; | 50 result.responseText = url + "\n"; |
46 if (!requestHeaders.empty()) | 51 if (!requestHeaders.empty()) |
47 { | 52 { |
48 result.responseText += requestHeaders[0].first + "\n" + requestHeaders[0 ].second; | 53 result.responseText += requestHeaders[0].first + "\n" + requestHeaders[0 ].second; |
49 } | 54 } |
50 return result; | 55 return result; |
51 } | 56 } |
52 | 57 |
53 // mutable. Very Ugly. But we are testing and need to change this in GET whi ch is const. | 58 // 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; | 59 mutable std::mutex lastRequestHeadersMutex; |
60 mutable std::map<std::string, std::set<std::string>> lastRequestHeaders; | |
sergei
2017/04/19 10:38:31
because now it's not only for last requests
lastR
hub
2017/04/19 13:12:04
Done.
| |
55 }; | 61 }; |
56 | 62 |
57 template<class T> | 63 template<class T> |
58 class WebRequestTest : public BaseJsTest | 64 class WebRequestTest : public BaseJsTest |
59 { | 65 { |
60 protected: | 66 protected: |
61 void SetUp() | 67 void SetUp() |
62 { | 68 { |
63 BaseJsTest::SetUp(); | 69 BaseJsTest::SetUp(); |
64 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new T())); | 70 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new T())); |
65 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); | 71 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); |
66 } | 72 } |
67 }; | 73 }; |
68 | 74 |
69 typedef WebRequestTest<MockWebRequest> MockWebRequestTest; | 75 typedef WebRequestTest<MockWebRequest> MockWebRequestTest; |
70 typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest; | 76 typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest; |
71 typedef WebRequestTest<MockWebRequest> XMLHttpRequestTest; | 77 typedef WebRequestTest<MockWebRequest> XMLHttpRequestTest; |
72 | 78 |
73 void ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine) | 79 // we return the url of the XHR. |
80 std::string ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine, const std:: string& defaultUrl = "") | |
74 { | 81 { |
75 jsEngine->Evaluate("\ | 82 std::string url = defaultUrl; |
83 // make up a unique URL if we don't have one. | |
84 if (url == "") | |
85 { | |
86 url = "https://tests.adblockplus.org/easylist.txt-"; | |
87 url += std::to_string(std::chrono::system_clock::to_time_t(std::chrono::sy stem_clock::now())); | |
88 } | |
89 | |
90 jsEngine->Evaluate(std::string("\ | |
76 var result;\ | 91 var result;\ |
77 var request = new XMLHttpRequest();\ | 92 var request = new XMLHttpRequest();\ |
78 request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.t xt');\ | 93 request.open('GET', '") + url + "'); \ |
79 request.overrideMimeType('text/plain');\ | 94 request.overrideMimeType('text/plain');\ |
80 request.addEventListener('load', function() {result = request.responseText ;}, false);\ | 95 request.addEventListener('load', function() {result = request.responseText ;}, false);\ |
81 request.addEventListener('error', function() {result = 'error';}, false);\ | 96 request.addEventListener('error', function() {result = 'error';}, false);\ |
82 "); | 97 "); |
98 return url; | |
83 } | 99 } |
84 | 100 |
85 void WaitForVariable(const std::string& variable, const AdblockPlus::JsEngineP tr& jsEngine) | 101 void WaitForVariable(const std::string& variable, const AdblockPlus::JsEngineP tr& jsEngine) |
86 { | 102 { |
87 do | 103 do |
88 { | 104 { |
89 AdblockPlus::Sleep(60); | 105 AdblockPlus::Sleep(60); |
90 } while (jsEngine->Evaluate(variable)->IsUndefined()); | 106 } while (jsEngine->Evaluate(variable)->IsUndefined()); |
91 } | 107 } |
92 | 108 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 #if defined(HAVE_CURL) | 144 #if defined(HAVE_CURL) |
129 ASSERT_EQ("gzip", jsEngine->Evaluate("foo.responseHeaders['content-encoding']. substr(0, 4)")->AsString()); | 145 ASSERT_EQ("gzip", jsEngine->Evaluate("foo.responseHeaders['content-encoding']. substr(0, 4)")->AsString()); |
130 #endif | 146 #endif |
131 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined ()); | 147 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined ()); |
132 } | 148 } |
133 | 149 |
134 TEST_F(DefaultWebRequestTest, XMLHttpRequest) | 150 TEST_F(DefaultWebRequestTest, XMLHttpRequest) |
135 { | 151 { |
136 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); | 152 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); |
137 | 153 |
138 ResetTestXHR(jsEngine); | 154 ResetTestXHR(jsEngine, "https://easylist-downloads.adblockplus.org/easylist.tx t"); |
139 jsEngine->Evaluate("\ | 155 jsEngine->Evaluate("\ |
140 request.setRequestHeader('X', 'Y');\ | 156 request.setRequestHeader('X', 'Y');\ |
141 request.setRequestHeader('X2', 'Y2');\ | 157 request.setRequestHeader('X2', 'Y2');\ |
142 request.send(null);"); | 158 request.send(null);"); |
143 WaitForVariable("result", jsEngine); | 159 WaitForVariable("result", jsEngine); |
144 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel. status")->AsInt()); | 160 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel. status")->AsInt()); |
145 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt()); | 161 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt()); |
146 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri ng()); | 162 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri ng()); |
147 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content -Type').substr(0, 10)")->AsString()); | 163 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content -Type').substr(0, 10)")->AsString()); |
148 #if defined(HAVE_CURL) | 164 #if defined(HAVE_CURL) |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
221 ASSERT_TRUE(webRequest); | 237 ASSERT_TRUE(webRequest); |
222 | 238 |
223 const std::string msg = "Attempt to set a forbidden header was denied: "; | 239 const std::string msg = "Attempt to set a forbidden header was denied: "; |
224 | 240 |
225 // The test will check that console.warn has been called when the | 241 // The test will check that console.warn has been called when the |
226 // header is rejected. While this is an implementation detail, we | 242 // header is rejected. While this is an implementation detail, we |
227 // have no other way to check this | 243 // have no other way to check this |
228 | 244 |
229 // test 'Accept-Encoding' is rejected | 245 // test 'Accept-Encoding' is rejected |
230 catchLogSystem->clear(); | 246 catchLogSystem->clear(); |
231 ResetTestXHR(jsEngine); | 247 std::string url = ResetTestXHR(jsEngine); |
232 jsEngine->Evaluate("\ | 248 jsEngine->Evaluate("\ |
233 request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();"); | 249 request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();"); |
234 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); | 250 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); |
235 EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage); | 251 EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage); |
236 WaitForVariable("result", jsEngine); | 252 WaitForVariable("result", jsEngine); |
237 EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == | 253 { |
238 webRequest->lastRequestHeaders.find("Accept-Encoding")); | 254 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
sergei
2017/04/19 10:38:31
any work with the mutex of webRequest should be mo
hub
2017/04/19 13:12:04
Done.
| |
255 const auto& iter = webRequest->lastRequestHeaders.find(url); | |
256 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); | |
257 const auto& headers = iter->second; | |
258 EXPECT_TRUE(headers.cend() == headers.find("Accept-Encoding")); | |
259 webRequest->lastRequestHeaders.erase(iter); | |
260 } | |
239 | 261 |
240 // test 'DNT' is rejected | 262 // test 'DNT' is rejected |
241 catchLogSystem->clear(); | 263 catchLogSystem->clear(); |
242 ResetTestXHR(jsEngine); | 264 url = ResetTestXHR(jsEngine); |
243 jsEngine->Evaluate("\ | 265 jsEngine->Evaluate("\ |
244 request.setRequestHeader('DNT', '1');\nrequest.send();"); | 266 request.setRequestHeader('DNT', '1');\nrequest.send();"); |
245 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); | 267 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); |
246 EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage); | 268 EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage); |
247 WaitForVariable("result", jsEngine); | 269 WaitForVariable("result", jsEngine); |
248 EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == | 270 { |
249 webRequest->lastRequestHeaders.find("DNT")); | 271 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
272 const auto& iter = webRequest->lastRequestHeaders.find(url); | |
273 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); | |
274 const auto& headers = iter->second; | |
275 EXPECT_TRUE(headers.cend() == headers.find("DNT")); | |
276 webRequest->lastRequestHeaders.erase(iter); | |
277 } | |
250 | 278 |
251 // test random 'X' header is accepted | 279 // test random 'X' header is accepted |
252 catchLogSystem->clear(); | 280 catchLogSystem->clear(); |
253 ResetTestXHR(jsEngine); | 281 url = ResetTestXHR(jsEngine); |
254 jsEngine->Evaluate("\ | 282 jsEngine->Evaluate("\ |
255 request.setRequestHeader('X', 'y');\nrequest.send();"); | 283 request.setRequestHeader('X', 'y');\nrequest.send();"); |
256 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); | 284 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); |
257 EXPECT_EQ("", catchLogSystem->lastMessage); | 285 EXPECT_EQ("", catchLogSystem->lastMessage); |
258 WaitForVariable("result", jsEngine); | 286 WaitForVariable("result", jsEngine); |
259 EXPECT_FALSE(webRequest->lastRequestHeaders.cend() == | 287 { |
260 webRequest->lastRequestHeaders.find("X")); | 288 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
289 const auto& iter = webRequest->lastRequestHeaders.find(url); | |
290 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); | |
291 const auto& headers = iter->second; | |
292 EXPECT_FALSE(headers.cend() == headers.find("X")); | |
293 webRequest->lastRequestHeaders.erase(iter); | |
294 } | |
261 | 295 |
262 // test /^Proxy-/ is rejected. | 296 // test /^Proxy-/ is rejected. |
263 catchLogSystem->clear(); | 297 catchLogSystem->clear(); |
264 ResetTestXHR(jsEngine); | 298 url = ResetTestXHR(jsEngine); |
265 jsEngine->Evaluate("\ | 299 jsEngine->Evaluate("\ |
266 request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();"); | 300 request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();"); |
267 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); | 301 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); |
268 EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage); | 302 EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage); |
269 WaitForVariable("result", jsEngine); | 303 WaitForVariable("result", jsEngine); |
270 EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == | 304 { |
271 webRequest->lastRequestHeaders.find("Proxy-foo")); | 305 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
306 const auto& iter = webRequest->lastRequestHeaders.find(url); | |
307 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); | |
308 const auto& headers = iter->second; | |
309 EXPECT_TRUE(headers.cend() == headers.find("Proxy-foo")); | |
310 webRequest->lastRequestHeaders.erase(iter); | |
311 } | |
272 | 312 |
273 // test /^Sec-/ is rejected. | 313 // test /^Sec-/ is rejected. |
274 catchLogSystem->clear(); | 314 catchLogSystem->clear(); |
275 ResetTestXHR(jsEngine); | 315 url = ResetTestXHR(jsEngine); |
276 jsEngine->Evaluate("\ | 316 jsEngine->Evaluate("\ |
277 request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();"); | 317 request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();"); |
278 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); | 318 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); |
279 EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage); | 319 EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage); |
280 WaitForVariable("result", jsEngine); | 320 WaitForVariable("result", jsEngine); |
281 EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == | 321 { |
282 webRequest->lastRequestHeaders.find("Sec-foo")); | 322 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
323 const auto& iter = webRequest->lastRequestHeaders.find(url); | |
324 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); | |
325 const auto& headers = iter->second; | |
326 EXPECT_TRUE(headers.cend() == headers.find("Sec-foo")); | |
327 webRequest->lastRequestHeaders.erase(iter); | |
328 } | |
283 | 329 |
284 // test 'Security' is accepted. | 330 // test 'Security' is accepted. |
285 catchLogSystem->clear(); | 331 catchLogSystem->clear(); |
286 ResetTestXHR(jsEngine); | 332 url = ResetTestXHR(jsEngine); |
287 jsEngine->Evaluate("\ | 333 jsEngine->Evaluate("\ |
288 request.setRequestHeader('Security', 'theater');\nrequest.send();"); | 334 request.setRequestHeader('Security', 'theater');\nrequest.send();"); |
289 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); | 335 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); |
290 EXPECT_EQ("", catchLogSystem->lastMessage); | 336 EXPECT_EQ("", catchLogSystem->lastMessage); |
291 WaitForVariable("result", jsEngine); | 337 WaitForVariable("result", jsEngine); |
292 EXPECT_FALSE(webRequest->lastRequestHeaders.cend() == | 338 { |
293 webRequest->lastRequestHeaders.find("Security")); | 339 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
340 const auto& iter = webRequest->lastRequestHeaders.find(url); | |
341 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); | |
342 const auto& headers = iter->second; | |
343 EXPECT_FALSE(headers.cend() == headers.find("Security")); | |
344 webRequest->lastRequestHeaders.erase(iter); | |
345 } | |
294 } | 346 } |
OLD | NEW |