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

Side by Side Diff: test/FilterEngine.cpp

Issue 29402569: Issue 5074 - fix tests for "is allowed connection" (Closed)
Patch Set: Created April 4, 2017, 10:51 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/Utils.h ('k') | test/WebRequest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include "BaseJsTest.h" 18 #include "BaseJsTest.h"
19 #include <thread> 19 #include <thread>
20 #include "../src/Utils.h"
21 #include <condition_variable>
20 22
21 using namespace AdblockPlus; 23 using namespace AdblockPlus;
22 24
23 namespace 25 namespace
24 { 26 {
25 class VeryLazyFileSystem : public LazyFileSystem 27 class VeryLazyFileSystem : public LazyFileSystem
26 { 28 {
27 public: 29 public:
28 StatResult Stat(const std::string& path) const 30 StatResult Stat(const std::string& path) const
29 { 31 {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 catch (...) 140 catch (...)
139 { 141 {
140 return false; 142 return false;
141 } 143 }
142 }; 144 };
143 int i = 5; 145 int i = 5;
144 while ((i-- > 0 && weakJsEngine.lock()) || !safeRemove()) 146 while ((i-- > 0 && weakJsEngine.lock()) || !safeRemove())
145 std::this_thread::sleep_for(std::chrono::seconds(2)); 147 std::this_thread::sleep_for(std::chrono::seconds(2));
146 } 148 }
147 }; 149 };
150
151 class FilterEngineIsAllowedConnectionTest : public BaseJsTest
152 {
153 class MockWebRequest : public LazyWebRequest
154 {
155 public:
156 std::map</*beginning of url*/std::string, AdblockPlus::ServerResponse> res ponses;
157
158 AdblockPlus::ServerResponse GET(const std::string& url,
159 const AdblockPlus::HeaderList& requestHeaders) const
160 {
161 for (const auto& response : responses)
162 {
163 if (Utils::BeginsWith(url, response.first))
164 {
165 return response.second;
166 }
167 }
168 return LazyWebRequest::GET(url, requestHeaders);
169 }
170 };
171 class SyncStrings
172 {
173 public:
174 void Add(const std::string* value)
175 {
176 std::lock_guard<std::mutex> lock(mutex);
177 strings.emplace_back(!!value, value ? *value : "");
178 }
179 std::vector<std::pair<bool, std::string>> GetStrings() const
180 {
181 std::lock_guard<std::mutex> lock(mutex);
182 return strings;
183 }
184 void Clear()
185 {
186 std::lock_guard<std::mutex> lock(mutex);
187 strings.clear();
188 }
189 private:
190 mutable std::mutex mutex;
191 std::vector<std::pair<bool, std::string>> strings;
192 };
193 protected:
194 MockWebRequest* webRequest;
195 std::string subscriptionUrlPrefix;
196 FilterEngine::CreationParameters createParams;
197 SyncStrings capturedConnectionTypes;
198 bool isConnectionAllowed;
199 FilterEnginePtr filterEngine;
200
201 struct
202 {
203 std::string url;
204 std::mutex mutex;
205 std::condition_variable cv;
206 } downloadStatusChanged;
207
208 void SetUp()
209 {
210 BaseJsTest::SetUp();
211 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem()));
212 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(webRequest = new MockWe bRequest()));
213 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem()));
214
215 subscriptionUrlPrefix = "http://example";
216 ServerResponse exampleSubscriptionResponse;
217 exampleSubscriptionResponse.responseStatus = 200;
218 exampleSubscriptionResponse.status = WebRequest::NS_OK;
219 exampleSubscriptionResponse.responseText = "[Adblock Plus 2.0]\n||example. com";
220 webRequest->responses.emplace(subscriptionUrlPrefix, exampleSubscriptionRe sponse);
221 createParams.preconfiguredPrefs["first_run_subscription_auto_select"] = js Engine->NewValue(false);
222 isConnectionAllowed = true;
223 createParams.isConnectionAllowedCallback = [this](const std::string* allow edConnectionType)->bool{
224 capturedConnectionTypes.Add(allowedConnectionType);
225 return isConnectionAllowed;
226 };
227 jsEngine->SetEventCallback("filterChange", [this](const JsValueList& param s/*action, item*/)
228 {
229 ASSERT_EQ(2u, params.size());
230 if (params[0]->AsString() == "subscription.downloadStatus")
231 {
232 {
233 std::lock_guard<std::mutex> lock(downloadStatusChanged.mutex);
234 downloadStatusChanged.url = params[1]->GetProperty("url")->AsString( );
235 }
236 downloadStatusChanged.cv.notify_one();
237 }
238 });
239 }
240
241 SubscriptionPtr EnsureExampleSubscriptionAndForceUpdate(const std::string& a pppendToUrl = std::string())
242 {
243 if (!filterEngine)
244 filterEngine = FilterEngine::Create(jsEngine, createParams);
245 auto subscriptionUrl = subscriptionUrlPrefix + apppendToUrl;
246 auto subscription = filterEngine->GetSubscription(subscriptionUrl);
247 EXPECT_EQ(0u, subscription->GetProperty("filters")->AsList().size()) << su bscriptionUrl;
248 EXPECT_TRUE(subscription->GetProperty("downloadStatus")->IsNull()) << subs criptionUrl;
249 subscription->UpdateFilters();
250 {
251 std::unique_lock<std::mutex> lock(downloadStatusChanged.mutex);
252 downloadStatusChanged.cv.wait_for(lock,
253 /*don't block tests forever*/std::chrono::seconds(5),
254 [this, subscriptionUrl]()->bool
255 {
256 return subscriptionUrl == downloadStatusChanged.url;
257 });
258 // Basically it's enough to wait only for downloadStatus although there
259 // is still some JS code being executed. Any following attempt to work
260 // with subscription object will result in execution of JS, which will
261 // be blocked until finishing of currently running code.
262 }
263 return subscription;
264 }
265 };
148 } 266 }
149 267
150 TEST_F(FilterEngineTest, FilterCreation) 268 TEST_F(FilterEngineTest, FilterCreation)
151 { 269 {
152 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo"); 270 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo");
153 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType()); 271 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType());
154 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo"); 272 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo");
155 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetType()); 273 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetType());
156 AdblockPlus::FilterPtr filter3 = filterEngine->GetFilter("example.com##foo"); 274 AdblockPlus::FilterPtr filter3 = filterEngine->GetFilter("example.com##foo");
157 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetType()); 275 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetType());
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 739
622 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) 740 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun)
623 { 741 {
624 auto jsEngine = createJsEngine(); 742 auto jsEngine = createJsEngine();
625 FilterEngine::CreationParameters createParams; 743 FilterEngine::CreationParameters createParams;
626 createParams.preconfiguredPrefs["first_run_subscription_auto_select"] = jsEngi ne->NewValue(false); 744 createParams.preconfiguredPrefs["first_run_subscription_auto_select"] = jsEngi ne->NewValue(false);
627 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine, createParams); 745 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine, createParams);
628 const auto subscriptions = filterEngine->GetListedSubscriptions(); 746 const auto subscriptions = filterEngine->GetListedSubscriptions();
629 EXPECT_EQ(0u, subscriptions.size()); 747 EXPECT_EQ(0u, subscriptions.size());
630 } 748 }
749
750 TEST_F(FilterEngineIsAllowedConnectionTest, AbsentCallbackAllowsUpdating)
751 {
752 createParams.isConnectionAllowedCallback = FilterEngine::IsConnectionAllowedCa llback();
753 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
754 EXPECT_EQ("synchronize_ok", subscription->GetProperty("downloadStatus")->AsStr ing());
755 EXPECT_EQ(1u, subscription->GetProperty("filters")->AsList().size());
756 }
757
758 TEST_F(FilterEngineIsAllowedConnectionTest, AllowingCallbackAllowsUpdating)
759 {
760 // no stored allowed_connection_type preference
761 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
762 EXPECT_EQ("synchronize_ok", subscription->GetProperty("downloadStatus")->AsStr ing());
763 EXPECT_EQ(1u, subscription->GetProperty("filters")->AsList().size());
764 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings();
765 ASSERT_EQ(1u, capturedConnectionTypes.size());
766 EXPECT_FALSE(capturedConnectionTypes[0].first);
767 }
768
769 TEST_F(FilterEngineIsAllowedConnectionTest, NotAllowingCallbackDoesNotAllowUpdat ing)
770 {
771 isConnectionAllowed = false;
772 // no stored allowed_connection_type preference
773 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
774 EXPECT_EQ("synchronize_connection_error", subscription->GetProperty("downloadS tatus")->AsString());
775 EXPECT_EQ(0u, subscription->GetProperty("filters")->AsList().size());
776 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings();
777 EXPECT_EQ(1u, capturedConnectionTypes.size());
778 }
779
780 TEST_F(FilterEngineIsAllowedConnectionTest, PredefinedAllowedConnectionTypeIsPas sedToCallback)
781 {
782 std::string predefinedAllowedConnectionType = "non-metered";
783 createParams.preconfiguredPrefs["allowed_connection_type"] = jsEngine->NewValu e(predefinedAllowedConnectionType);
784 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
785 EXPECT_EQ("synchronize_ok", subscription->GetProperty("downloadStatus")->AsStr ing());
786 EXPECT_EQ(1u, subscription->GetProperty("filters")->AsList().size());
787 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings();
788 ASSERT_EQ(1u, capturedConnectionTypes.size());
789 EXPECT_TRUE(capturedConnectionTypes[0].first);
790 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second);
791 }
792
793 TEST_F(FilterEngineIsAllowedConnectionTest, ConfiguredConnectionTypeIsPassedToCa llback)
794 {
795 // FilterEngine->RemoveSubscription is not usable here because subscriptions
796 // are cached internally by URL. So, different URLs are used in diffirent
797 // checks.
798 {
799 std::string predefinedAllowedConnectionType = "non-metered";
800 createParams.preconfiguredPrefs["allowed_connection_type"] = jsEngine->NewVa lue(predefinedAllowedConnectionType);
801 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
802 EXPECT_EQ("synchronize_ok", subscription->GetProperty("downloadStatus")->AsS tring());
803 EXPECT_EQ(1u, subscription->GetProperty("filters")->AsList().size());
804 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings();
805 ASSERT_EQ(1u, capturedConnectionTypes.size());
806 EXPECT_TRUE(capturedConnectionTypes[0].first);
807 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second );
808 }
809 capturedConnectionTypes.Clear();
810 {
811 // set no value
812 filterEngine->SetAllowedConnectionType(nullptr);
813 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subA");
814 EXPECT_EQ("synchronize_ok", subscription->GetProperty("downloadStatus")->AsS tring());
815 EXPECT_EQ(1u, subscription->GetProperty("filters")->AsList().size());
816 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings();
817 ASSERT_EQ(1u, capturedConnectionTypes.size());
818 EXPECT_FALSE(capturedConnectionTypes[0].first);
819 subscription->RemoveFromList();
820 this->capturedConnectionTypes.Clear();
821 }
822 capturedConnectionTypes.Clear();
823 {
824 // set some value
825 std::string testConnection = "test connection";
826 filterEngine->SetAllowedConnectionType(&testConnection);
827 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB");
828 EXPECT_EQ("synchronize_ok", subscription->GetProperty("downloadStatus")->AsS tring());
829 EXPECT_EQ(1u, subscription->GetProperty("filters")->AsList().size());
830 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings();
831 ASSERT_EQ(1u, capturedConnectionTypes.size());
832 EXPECT_TRUE(capturedConnectionTypes[0].first);
833 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second);
834 }
835 }
OLDNEW
« no previous file with comments | « src/Utils.h ('k') | test/WebRequest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld