Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: test/WebRequest.cpp

Issue 29408721: Issue 5002 - Fix potential concurrency issues in WebRequest tests. (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Left Patch Set: Created April 10, 2017, 12:41 p.m.
Right Patch Set: Fixed the last nits. Created April 19, 2017, 2:54 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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 std::set<std::string> r; 33 std::set<std::string> headerNames;
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 r.insert(header.first); 36 headerNames.insert(header.first);
37 } 37 }
38 { 38 {
39 std::lock_guard<std::mutex> lock(lastRequestHeadersMutex); 39 std::lock_guard<std::mutex> lock(requestHeaderNamesMutex);
40 // XXX we currently ignore the result. We should check it actually gets inserted. 40 // we currently ignore the result. We should check it actually gets inse rted.
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))); 41 requestHeaderNames.insert(std::make_pair(url, std::move(headerNames)));
42 } 42 }
43 43
44 AdblockPlus::Sleep(50); 44 AdblockPlus::Sleep(50);
45 45
46 AdblockPlus::ServerResponse result; 46 AdblockPlus::ServerResponse result;
47 result.status = NS_OK; 47 result.status = NS_OK;
48 result.responseStatus = 123; 48 result.responseStatus = 123;
49 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"));
50 result.responseText = url + "\n"; 50 result.responseText = url + "\n";
51 if (!requestHeaders.empty()) 51 if (!requestHeaders.empty())
52 { 52 {
53 result.responseText += requestHeaders[0].first + "\n" + requestHeaders[0 ].second; 53 result.responseText += requestHeaders[0].first + "\n" + requestHeaders[0 ].second;
54 } 54 }
55 return result; 55 return result;
56 } 56 }
57 57
58 // Testing method
59 // Get the headers for the request. Return a pair of a bool (found
60 // or not) and the actual header names
61 std::pair<bool, std::set<std::string>> headersForRequest(const std::string& url)
62 {
63 std::lock_guard<std::mutex> lock(requestHeaderNamesMutex);
64 auto iter = requestHeaderNames.find(url);
65 if (iter != requestHeaderNames.end())
66 {
67 auto result = std::make_pair(true, iter->second);
68 requestHeaderNames.erase(iter);
69 return result;
70 }
71 return std::make_pair(false, std::set<std::string>());
72 }
73
58 // 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.
59 mutable std::mutex lastRequestHeadersMutex; 75 mutable std::mutex requestHeaderNamesMutex;
60 mutable std::map<std::string, std::set<std::string>> lastRequestHeaders; 76 mutable std::map<std::string, std::set<std::string>> requestHeaderNames;
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.
61 }; 77 };
62 78
63 template<class T> 79 template<class T>
64 class WebRequestTest : public BaseJsTest 80 class WebRequestTest : public BaseJsTest
65 { 81 {
66 protected: 82 protected:
67 void SetUp() 83 void SetUp()
68 { 84 {
69 BaseJsTest::SetUp(); 85 BaseJsTest::SetUp();
70 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new T())); 86 webRequest = std::make_shared<T>();
87 jsEngine->SetWebRequest(webRequest);
71 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); 88 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem()));
72 } 89 }
90
91 std::shared_ptr<T> webRequest;
73 }; 92 };
74 93
75 typedef WebRequestTest<MockWebRequest> MockWebRequestTest; 94 typedef WebRequestTest<MockWebRequest> MockWebRequestTest;
76 typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest; 95 typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest;
77 typedef WebRequestTest<MockWebRequest> XMLHttpRequestTest; 96 typedef WebRequestTest<MockWebRequest> XMLHttpRequestTest;
78 97
79 // we return the url of the XHR. 98 // we return the url of the XHR.
80 std::string ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine, const std:: string& defaultUrl = "") 99 std::string ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine, const std:: string& defaultUrl = "")
81 { 100 {
82 std::string url = defaultUrl; 101 std::string url = defaultUrl;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 243
225 typedef std::shared_ptr<CatchLogSystem> CatchLogSystemPtr; 244 typedef std::shared_ptr<CatchLogSystem> CatchLogSystemPtr;
226 } 245 }
227 246
228 TEST_F(XMLHttpRequestTest, RequestHeaderValidation) 247 TEST_F(XMLHttpRequestTest, RequestHeaderValidation)
229 { 248 {
230 auto catchLogSystem = CatchLogSystemPtr(new CatchLogSystem()); 249 auto catchLogSystem = CatchLogSystemPtr(new CatchLogSystem());
231 jsEngine->SetLogSystem(catchLogSystem); 250 jsEngine->SetLogSystem(catchLogSystem);
232 251
233 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 252 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
234 auto webRequest =
235 std::static_pointer_cast<MockWebRequest>(jsEngine->GetWebRequest());
236
237 ASSERT_TRUE(webRequest);
238 253
239 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: ";
240 255
241 // 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
242 // header is rejected. While this is an implementation detail, we 257 // header is rejected. While this is an implementation detail, we
243 // have no other way to check this 258 // have no other way to check this
244 259
245 // test 'Accept-Encoding' is rejected 260 // test 'Accept-Encoding' is rejected
246 catchLogSystem->clear(); 261 catchLogSystem->clear();
247 std::string url = ResetTestXHR(jsEngine); 262 std::string url = ResetTestXHR(jsEngine);
248 jsEngine->Evaluate("\ 263 jsEngine->Evaluate("\
249 request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();"); 264 request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();");
250 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 265 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
251 EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage); 266 EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage);
252 WaitForVariable("result", jsEngine); 267 WaitForVariable("result", jsEngine);
253 { 268 {
254 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); 269 auto headersRequest = webRequest->headersForRequest(url);
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); 270 EXPECT_TRUE(headersRequest.first);
256 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); 271 const auto& headers = headersRequest.second;
257 const auto& headers = iter->second;
258 EXPECT_TRUE(headers.cend() == headers.find("Accept-Encoding")); 272 EXPECT_TRUE(headers.cend() == headers.find("Accept-Encoding"));
259 webRequest->lastRequestHeaders.erase(iter);
260 } 273 }
261 274
262 // test 'DNT' is rejected 275 // test 'DNT' is rejected
263 catchLogSystem->clear(); 276 catchLogSystem->clear();
264 url = ResetTestXHR(jsEngine); 277 url = ResetTestXHR(jsEngine);
265 jsEngine->Evaluate("\ 278 jsEngine->Evaluate("\
266 request.setRequestHeader('DNT', '1');\nrequest.send();"); 279 request.setRequestHeader('DNT', '1');\nrequest.send();");
267 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 280 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
268 EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage); 281 EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage);
269 WaitForVariable("result", jsEngine); 282 WaitForVariable("result", jsEngine);
270 { 283 {
271 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); 284 auto headersRequest = webRequest->headersForRequest(url);
272 const auto& iter = webRequest->lastRequestHeaders.find(url); 285 EXPECT_TRUE(headersRequest.first);
273 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); 286 const auto& headers = headersRequest.second;
274 const auto& headers = iter->second;
275 EXPECT_TRUE(headers.cend() == headers.find("DNT")); 287 EXPECT_TRUE(headers.cend() == headers.find("DNT"));
276 webRequest->lastRequestHeaders.erase(iter);
277 } 288 }
278 289
279 // test random 'X' header is accepted 290 // test random 'X' header is accepted
280 catchLogSystem->clear(); 291 catchLogSystem->clear();
281 url = ResetTestXHR(jsEngine); 292 url = ResetTestXHR(jsEngine);
282 jsEngine->Evaluate("\ 293 jsEngine->Evaluate("\
283 request.setRequestHeader('X', 'y');\nrequest.send();"); 294 request.setRequestHeader('X', 'y');\nrequest.send();");
284 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); 295 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l);
285 EXPECT_EQ("", catchLogSystem->lastMessage); 296 EXPECT_EQ("", catchLogSystem->lastMessage);
286 WaitForVariable("result", jsEngine); 297 WaitForVariable("result", jsEngine);
287 { 298 {
288 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); 299 auto headersRequest = webRequest->headersForRequest(url);
289 const auto& iter = webRequest->lastRequestHeaders.find(url); 300 EXPECT_TRUE(headersRequest.first);
290 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); 301 const auto& headers = headersRequest.second;
291 const auto& headers = iter->second;
292 EXPECT_FALSE(headers.cend() == headers.find("X")); 302 EXPECT_FALSE(headers.cend() == headers.find("X"));
293 webRequest->lastRequestHeaders.erase(iter);
294 } 303 }
295 304
296 // test /^Proxy-/ is rejected. 305 // test /^Proxy-/ is rejected.
297 catchLogSystem->clear(); 306 catchLogSystem->clear();
298 url = ResetTestXHR(jsEngine); 307 url = ResetTestXHR(jsEngine);
299 jsEngine->Evaluate("\ 308 jsEngine->Evaluate("\
300 request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();"); 309 request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();");
301 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 310 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
302 EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage); 311 EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage);
303 WaitForVariable("result", jsEngine); 312 WaitForVariable("result", jsEngine);
304 { 313 {
305 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); 314 auto headersRequest = webRequest->headersForRequest(url);
306 const auto& iter = webRequest->lastRequestHeaders.find(url); 315 EXPECT_TRUE(headersRequest.first);
307 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); 316 const auto& headers = headersRequest.second;
308 const auto& headers = iter->second;
309 EXPECT_TRUE(headers.cend() == headers.find("Proxy-foo")); 317 EXPECT_TRUE(headers.cend() == headers.find("Proxy-foo"));
310 webRequest->lastRequestHeaders.erase(iter);
311 } 318 }
312 319
313 // test /^Sec-/ is rejected. 320 // test /^Sec-/ is rejected.
314 catchLogSystem->clear(); 321 catchLogSystem->clear();
315 url = ResetTestXHR(jsEngine); 322 url = ResetTestXHR(jsEngine);
316 jsEngine->Evaluate("\ 323 jsEngine->Evaluate("\
317 request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();"); 324 request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();");
318 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 325 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
319 EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage); 326 EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage);
320 WaitForVariable("result", jsEngine); 327 WaitForVariable("result", jsEngine);
321 { 328 {
322 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); 329 auto headersRequest = webRequest->headersForRequest(url);
323 const auto& iter = webRequest->lastRequestHeaders.find(url); 330 EXPECT_TRUE(headersRequest.first);
324 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); 331 const auto& headers = headersRequest.second;
325 const auto& headers = iter->second;
326 EXPECT_TRUE(headers.cend() == headers.find("Sec-foo")); 332 EXPECT_TRUE(headers.cend() == headers.find("Sec-foo"));
327 webRequest->lastRequestHeaders.erase(iter);
328 } 333 }
329 334
330 // test 'Security' is accepted. 335 // test 'Security' is accepted.
331 catchLogSystem->clear(); 336 catchLogSystem->clear();
332 url = ResetTestXHR(jsEngine); 337 url = ResetTestXHR(jsEngine);
333 jsEngine->Evaluate("\ 338 jsEngine->Evaluate("\
334 request.setRequestHeader('Security', 'theater');\nrequest.send();"); 339 request.setRequestHeader('Security', 'theater');\nrequest.send();");
335 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); 340 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l);
336 EXPECT_EQ("", catchLogSystem->lastMessage); 341 EXPECT_EQ("", catchLogSystem->lastMessage);
337 WaitForVariable("result", jsEngine); 342 WaitForVariable("result", jsEngine);
338 { 343 {
339 std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); 344 auto headersRequest = webRequest->headersForRequest(url);
340 const auto& iter = webRequest->lastRequestHeaders.find(url); 345 EXPECT_TRUE(headersRequest.first);
341 EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); 346 const auto& headers = headersRequest.second;
342 const auto& headers = iter->second;
343 EXPECT_FALSE(headers.cend() == headers.find("Security")); 347 EXPECT_FALSE(headers.cend() == headers.find("Security"));
344 webRequest->lastRequestHeaders.erase(iter); 348 }
345 } 349 }
346 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld