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

Powered by Google App Engine
This is Rietveld