| 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-2015 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 | 
|   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  * | 
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   78     }; |   78     }; | 
|   79  |   79  | 
|   80     MockWebRequest* mockWebRequest; |   80     MockWebRequest* mockWebRequest; | 
|   81     FilterEnginePtr filterEngine; |   81     FilterEnginePtr filterEngine; | 
|   82  |   82  | 
|   83     void SetUp() |   83     void SetUp() | 
|   84     { |   84     { | 
|   85       AdblockPlus::AppInfo appInfo; |   85       AdblockPlus::AppInfo appInfo; | 
|   86       appInfo.name = "test"; |   86       appInfo.name = "test"; | 
|   87       appInfo.version = "1.0.1"; |   87       appInfo.version = "1.0.1"; | 
|   88       AdblockPlus::JsEnginePtr jsEngine = createJsEngine(appInfo); |   88       AdblockPlus::JsEnginePtr jsEngine = CreateJsEngine(appInfo); | 
|   89       jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); |   89       jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); | 
|   90       mockWebRequest = new MockWebRequest; |   90       mockWebRequest = new MockWebRequest; | 
|   91       jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(mockWebRequest)); |   91       jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(mockWebRequest)); | 
|   92       filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); |   92       filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); | 
|   93     } |   93     } | 
|   94   }; |   94   }; | 
|   95  |   95  | 
|   96   struct MockUpdateAvailableCallback |   96   struct MockUpdateAvailableCallback | 
|   97   { |   97   { | 
|   98     MockUpdateAvailableCallback(int& timesCalled) : timesCalled(timesCalled) {} |   98     MockUpdateAvailableCallback(int& timesCalled) : timesCalled(timesCalled) {} | 
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  273   ASSERT_TRUE(match1); |  273   ASSERT_TRUE(match1); | 
|  274   ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, match1->GetType()); |  274   ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, match1->GetType()); | 
|  275  |  275  | 
|  276   AdblockPlus::FilterPtr match2 = |  276   AdblockPlus::FilterPtr match2 = | 
|  277     filterEngine->Matches("http://ads.com/adbanner.gif", AdblockPlus::FilterEngi
     ne::CONTENT_TYPE_IMAGE, |  277     filterEngine->Matches("http://ads.com/adbanner.gif", AdblockPlus::FilterEngi
     ne::CONTENT_TYPE_IMAGE, | 
|  278                           "http://example.org/"); |  278                           "http://example.org/"); | 
|  279   ASSERT_TRUE(match2); |  279   ASSERT_TRUE(match2); | 
|  280   ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match2->GetType()); |  280   ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match2->GetType()); | 
|  281 } |  281 } | 
|  282  |  282  | 
 |  283 TEST_F(FilterEngineTest, MatchesWithContentTypeMask) | 
 |  284 { | 
 |  285   filterEngine->GetFilter("adbanner.gif.js$script,image")->AddToList(); | 
 |  286   filterEngine->GetFilter("@@notbanner.gif")->AddToList(); | 
 |  287   filterEngine->GetFilter("blockme")->AddToList(); | 
 |  288   filterEngine->GetFilter("@@||example.doc^$document")->AddToList(); | 
 |  289  | 
 |  290   EXPECT_FALSE(filterEngine->Matches("http://example.org/foobar.gif", | 
 |  291     AdblockPlus::FilterEngine::CONTENT_TYPE_IMAGE, "")) | 
 |  292     << "another url should not match"; | 
 |  293  | 
 |  294   EXPECT_FALSE(filterEngine->Matches("http://example.org/adbanner.gif.js", | 
 |  295     /*mask*/ 0, "")) << "zero mask should not match (filter with some options)"; | 
 |  296  | 
 |  297   EXPECT_FALSE(filterEngine->Matches("http://example.xxx/blockme", | 
 |  298     /*mask*/ 0, "")) << "zero mask should not match (filter without any option)"
     ; | 
 |  299  | 
 |  300   EXPECT_FALSE(filterEngine->Matches("http://example.org/adbanner.gif.js", | 
 |  301     AdblockPlus::FilterEngine::CONTENT_TYPE_OBJECT, "")) | 
 |  302     << "one arbitrary flag in mask should not match"; | 
 |  303  | 
 |  304   EXPECT_TRUE(filterEngine->Matches("http://example.org/adbanner.gif.js", | 
 |  305     AdblockPlus::FilterEngine::CONTENT_TYPE_IMAGE | | 
 |  306     AdblockPlus::FilterEngine::CONTENT_TYPE_OBJECT, "")) | 
 |  307     << "one of flags in mask should match"; | 
 |  308  | 
 |  309   EXPECT_TRUE(filterEngine->Matches("http://example.org/adbanner.gif.js", | 
 |  310     AdblockPlus::FilterEngine::CONTENT_TYPE_IMAGE | | 
 |  311     AdblockPlus::FilterEngine::CONTENT_TYPE_SCRIPT, "")) | 
 |  312     << "both flags in mask should match"; | 
 |  313  | 
 |  314   EXPECT_TRUE(filterEngine->Matches("http://example.org/adbanner.gif.js", | 
 |  315     AdblockPlus::FilterEngine::CONTENT_TYPE_IMAGE | | 
 |  316     AdblockPlus::FilterEngine::CONTENT_TYPE_SCRIPT | | 
 |  317     AdblockPlus::FilterEngine::CONTENT_TYPE_OBJECT, "")) | 
 |  318     << "both flags with another flag in mask should match"; | 
 |  319  | 
 |  320   EXPECT_TRUE(filterEngine->Matches("http://example.org/adbanner.gif.js", | 
 |  321     AdblockPlus::FilterEngine::CONTENT_TYPE_SCRIPT | | 
 |  322     AdblockPlus::FilterEngine::CONTENT_TYPE_OBJECT, "")) | 
 |  323     << "one of flags in mask should match"; | 
 |  324  | 
 |  325   { | 
 |  326     AdblockPlus::FilterPtr filter; | 
 |  327     ASSERT_TRUE(filter = filterEngine->Matches("http://child.any/blockme", | 
 |  328       AdblockPlus::FilterEngine::CONTENT_TYPE_SCRIPT | | 
 |  329       AdblockPlus::FilterEngine::CONTENT_TYPE_OBJECT, "http://example.doc")) | 
 |  330       << "non-zero mask should match on whitelisted document"; | 
 |  331  | 
 |  332     EXPECT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter->GetType()); | 
 |  333   } | 
 |  334  | 
 |  335   { | 
 |  336     AdblockPlus::FilterPtr filter; | 
 |  337     ASSERT_TRUE(filter = filterEngine->Matches("http://example.doc/blockme", | 
 |  338       /*mask*/0, "http://example.doc")) | 
 |  339       << "zero mask should match when document is whitelisted"; | 
 |  340  | 
 |  341     EXPECT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter->GetType()); | 
 |  342   } | 
 |  343 } | 
 |  344  | 
|  283 TEST_F(FilterEngineTest, MatchesNestedFrameRequest) |  345 TEST_F(FilterEngineTest, MatchesNestedFrameRequest) | 
|  284 { |  346 { | 
|  285   filterEngine->GetFilter("adbanner.gif")->AddToList(); |  347   filterEngine->GetFilter("adbanner.gif")->AddToList(); | 
|  286   filterEngine->GetFilter("@@adbanner.gif$domain=example.org")->AddToList(); |  348   filterEngine->GetFilter("@@adbanner.gif$domain=example.org")->AddToList(); | 
|  287  |  349  | 
|  288   std::vector<std::string> documentUrls1; |  350   std::vector<std::string> documentUrls1; | 
|  289   documentUrls1.push_back("http://ads.com/frame/"); |  351   documentUrls1.push_back("http://ads.com/frame/"); | 
|  290   documentUrls1.push_back("http://example.com/"); |  352   documentUrls1.push_back("http://example.com/"); | 
|  291   AdblockPlus::FilterPtr match1 = |  353   AdblockPlus::FilterPtr match1 = | 
|  292     filterEngine->Matches("http://ads.com/adbanner.gif", AdblockPlus::FilterEngi
     ne::CONTENT_TYPE_IMAGE, |  354     filterEngine->Matches("http://ads.com/adbanner.gif", AdblockPlus::FilterEngi
     ne::CONTENT_TYPE_IMAGE, | 
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  374   ASSERT_TRUE(filterEngine->IsFirstRun()); |  436   ASSERT_TRUE(filterEngine->IsFirstRun()); | 
|  375 } |  437 } | 
|  376  |  438  | 
|  377 TEST_F(FilterEngineTest, SetRemoveFilterChangeCallback) |  439 TEST_F(FilterEngineTest, SetRemoveFilterChangeCallback) | 
|  378 { |  440 { | 
|  379   int timesCalled = 0; |  441   int timesCalled = 0; | 
|  380   MockFilterChangeCallback mockFilterChangeCallback(timesCalled); |  442   MockFilterChangeCallback mockFilterChangeCallback(timesCalled); | 
|  381  |  443  | 
|  382   filterEngine->SetFilterChangeCallback(mockFilterChangeCallback); |  444   filterEngine->SetFilterChangeCallback(mockFilterChangeCallback); | 
|  383   filterEngine->GetFilter("foo")->AddToList(); |  445   filterEngine->GetFilter("foo")->AddToList(); | 
|  384   EXPECT_EQ(2, timesCalled); |  446   EXPECT_EQ(1, timesCalled); | 
|  385  |  447  | 
|  386   filterEngine->RemoveFilterChangeCallback(); |  448   filterEngine->RemoveFilterChangeCallback(); | 
|  387   filterEngine->GetFilter("foo")->RemoveFromList(); |  449   filterEngine->GetFilter("foo")->RemoveFromList(); | 
|  388   EXPECT_EQ(2, timesCalled); |  450   EXPECT_EQ(1, timesCalled); | 
|  389 } |  451 } | 
|  390  |  452  | 
|  391 TEST_F(UpdaterTest, SetRemoveUpdateAvailableCallback) |  453 TEST_F(UpdaterTest, SetRemoveUpdateAvailableCallback) | 
|  392 { |  454 { | 
|  393   mockWebRequest->response.status = 0; |  455   mockWebRequest->response.status = 0; | 
|  394   mockWebRequest->response.responseStatus = 200; |  456   mockWebRequest->response.responseStatus = 200; | 
|  395   mockWebRequest->response.responseText = "\ |  457   mockWebRequest->response.responseText = "\ | 
|  396 {\ |  458 {\ | 
|  397   \"test\": {\ |  459   \"test\": {\ | 
|  398     \"version\": \"1.0.2\",\ |  460     \"version\": \"1.0.2\",\ | 
|  399     \"url\": \"https://downloads.adblockplus.org/test-1.0.2.tar.gz?update\"\ |  461     \"url\": \"https://downloads.adblockplus.org/test-1.0.2.tar.gz?update\"\ | 
|  400   }\ |  462   }\ | 
|  401 }"; |  463 }"; | 
|  402  |  464  | 
|  403   int timesCalled = 0; |  465   int timesCalled = 0; | 
|  404   MockUpdateAvailableCallback mockUpdateAvailableCallback(timesCalled); |  466   MockUpdateAvailableCallback mockUpdateAvailableCallback(timesCalled); | 
|  405  |  467  | 
|  406   filterEngine->SetUpdateAvailableCallback(mockUpdateAvailableCallback); |  468   filterEngine->SetUpdateAvailableCallback(mockUpdateAvailableCallback); | 
|  407   filterEngine->ForceUpdateCheck(&NoOpUpdaterCallback); |  469   filterEngine->ForceUpdateCheck(&NoOpUpdaterCallback); | 
|  408   AdblockPlus::Sleep(100); |  470   AdblockPlus::Sleep(100); | 
|  409   ASSERT_EQ(1, timesCalled); |  471   ASSERT_EQ(1, timesCalled); | 
|  410  |  472  | 
|  411   filterEngine->RemoveUpdateAvailableCallback(); |  473   filterEngine->RemoveUpdateAvailableCallback(); | 
|  412   filterEngine->ForceUpdateCheck(&NoOpUpdaterCallback); |  474   filterEngine->ForceUpdateCheck(&NoOpUpdaterCallback); | 
|  413   AdblockPlus::Sleep(100); |  475   AdblockPlus::Sleep(100); | 
|  414   ASSERT_EQ(1, timesCalled); |  476   ASSERT_EQ(1, timesCalled); | 
|  415 } |  477 } | 
 |  478  | 
 |  479 TEST_F(FilterEngineTest, DocumentWhitelisting) | 
 |  480 { | 
 |  481   filterEngine->GetFilter("@@||example.org^$document")->AddToList(); | 
 |  482   filterEngine->GetFilter("@@||example.com^$document,domain=example.de")->AddToL
     ist(); | 
 |  483  | 
 |  484   ASSERT_TRUE(filterEngine->IsDocumentWhitelisted( | 
 |  485       "http://example.org", | 
 |  486       std::vector<std::string>())); | 
 |  487  | 
 |  488   ASSERT_FALSE(filterEngine->IsDocumentWhitelisted( | 
 |  489       "http://example.co.uk", | 
 |  490       std::vector<std::string>())); | 
 |  491  | 
 |  492   ASSERT_FALSE(filterEngine->IsDocumentWhitelisted( | 
 |  493       "http://example.com", | 
 |  494       std::vector<std::string>())); | 
 |  495  | 
 |  496   std::vector<std::string> documentUrls1; | 
 |  497   documentUrls1.push_back("http://example.de"); | 
 |  498  | 
 |  499   ASSERT_TRUE(filterEngine->IsDocumentWhitelisted( | 
 |  500       "http://example.com", | 
 |  501       documentUrls1)); | 
 |  502  | 
 |  503   ASSERT_FALSE(filterEngine->IsDocumentWhitelisted( | 
 |  504       "http://example.co.uk", | 
 |  505       documentUrls1)); | 
 |  506 } | 
 |  507  | 
 |  508 TEST_F(FilterEngineTest, ElemhideWhitelisting) | 
 |  509 { | 
 |  510   filterEngine->GetFilter("@@||example.org^$elemhide")->AddToList(); | 
 |  511   filterEngine->GetFilter("@@||example.com^$elemhide,domain=example.de")->AddToL
     ist(); | 
 |  512  | 
 |  513   ASSERT_TRUE(filterEngine->IsElemhideWhitelisted( | 
 |  514       "http://example.org", | 
 |  515       std::vector<std::string>())); | 
 |  516  | 
 |  517   ASSERT_FALSE(filterEngine->IsElemhideWhitelisted( | 
 |  518       "http://example.co.uk", | 
 |  519       std::vector<std::string>())); | 
 |  520  | 
 |  521   ASSERT_FALSE(filterEngine->IsElemhideWhitelisted( | 
 |  522       "http://example.com", | 
 |  523       std::vector<std::string>())); | 
 |  524  | 
 |  525   std::vector<std::string> documentUrls1; | 
 |  526   documentUrls1.push_back("http://example.de"); | 
 |  527  | 
 |  528   ASSERT_TRUE(filterEngine->IsElemhideWhitelisted( | 
 |  529       "http://example.com", | 
 |  530       documentUrls1)); | 
 |  531  | 
 |  532   ASSERT_FALSE(filterEngine->IsElemhideWhitelisted( | 
 |  533       "http://example.co.uk", | 
 |  534       documentUrls1)); | 
 |  535 } | 
| LEFT | RIGHT |