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

Side by Side Diff: test/FilterEngine.cpp

Issue 29424786: Issue 5182 - fix IsConnectionAllowed (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created April 28, 2017, 2:42 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 | « src/WebRequestJsObject.cpp ('k') | no next file » | 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
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 strings.clear(); 189 strings.clear();
190 } 190 }
191 private: 191 private:
192 mutable std::mutex mutex; 192 mutable std::mutex mutex;
193 std::vector<std::pair<bool, std::string>> strings; 193 std::vector<std::pair<bool, std::string>> strings;
194 }; 194 };
195 protected: 195 protected:
196 MockWebRequest* webRequest; 196 MockWebRequest* webRequest;
197 std::string subscriptionUrlPrefix; 197 std::string subscriptionUrlPrefix;
198 FilterEngine::CreationParameters createParams; 198 FilterEngine::CreationParameters createParams;
199 SyncStrings capturedConnectionTypes; 199 // HACK: it's a shared pointer to keep it available in
200 bool isConnectionAllowed; 200 // isConnectionAllowedCallback after destroying of the test.
201 struct SharedData
202 {
203 SyncStrings capturedConnectionTypes;
204 bool isConnectionAllowed;
205 struct
206 {
207 std::string url;
208 std::mutex mutex;
209 std::condition_variable cv;
210 } downloadStatusChanged;
211 };
212 std::shared_ptr<SharedData> data;
201 FilterEnginePtr filterEngine; 213 FilterEnginePtr filterEngine;
202 214
203 struct
204 {
205 std::string url;
206 std::mutex mutex;
207 std::condition_variable cv;
208 } downloadStatusChanged;
209
210 void SetUp() 215 void SetUp()
211 { 216 {
217 data = std::make_shared<SharedData>();
212 BaseJsTest::SetUp(); 218 BaseJsTest::SetUp();
213 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); 219 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem()));
214 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(webRequest = new MockWe bRequest())); 220 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(webRequest = new MockWe bRequest()));
215 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem())); 221 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem()));
216 222
217 subscriptionUrlPrefix = "http://example"; 223 subscriptionUrlPrefix = "http://example";
218 ServerResponse exampleSubscriptionResponse; 224 ServerResponse exampleSubscriptionResponse;
219 exampleSubscriptionResponse.responseStatus = 200; 225 exampleSubscriptionResponse.responseStatus = 200;
220 exampleSubscriptionResponse.status = WebRequest::NS_OK; 226 exampleSubscriptionResponse.status = WebRequest::NS_OK;
221 exampleSubscriptionResponse.responseText = "[Adblock Plus 2.0]\n||example. com"; 227 exampleSubscriptionResponse.responseText = "[Adblock Plus 2.0]\n||example. com";
222 webRequest->responses.emplace(subscriptionUrlPrefix, exampleSubscriptionRe sponse); 228 webRequest->responses.emplace(subscriptionUrlPrefix, exampleSubscriptionRe sponse);
223 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec t", jsEngine->NewValue(false)); 229 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec t", jsEngine->NewValue(false));
224 isConnectionAllowed = true; 230 data->isConnectionAllowed = true;
225 createParams.isConnectionAllowedCallback = [this](const std::string* allow edConnectionType)->bool{ 231 auto closure = data;
226 capturedConnectionTypes.Add(allowedConnectionType); 232 createParams.isConnectionAllowedCallback = [closure](const std::string* al lowedConnectionType)->bool{
227 return isConnectionAllowed; 233 closure->capturedConnectionTypes.Add(allowedConnectionType);
234 return closure->isConnectionAllowed;
228 }; 235 };
229 jsEngine->SetEventCallback("filterChange", [this](JsValueList&& params/*ac tion, item*/)
230 {
231 ASSERT_EQ(2u, params.size());
232 if (params[0].AsString() == "subscription.downloadStatus")
233 {
234 {
235 std::lock_guard<std::mutex> lock(downloadStatusChanged.mutex);
236 downloadStatusChanged.url = params[1].GetProperty("url").AsString();
237 }
238 downloadStatusChanged.cv.notify_one();
239 }
240 });
241 } 236 }
242 237
243 Subscription EnsureExampleSubscriptionAndForceUpdate(const std::string& appp endToUrl = std::string()) 238 Subscription EnsureExampleSubscriptionAndForceUpdate(const std::string& appp endToUrl = std::string())
244 { 239 {
245 if (!filterEngine) 240 if (!filterEngine)
241 {
246 filterEngine = FilterEngine::Create(jsEngine, createParams); 242 filterEngine = FilterEngine::Create(jsEngine, createParams);
243 auto closure = data;
244 filterEngine->SetFilterChangeCallback([closure](const std::string& actio n, JsValue&& item)
245 {
246 if (action == "subscription.downloadStatus")
247 {
248 {
249 std::lock_guard<std::mutex> lock(closure->downloadStatusChanged.mu tex);
250 closure->downloadStatusChanged.url = item.GetProperty("url").AsStr ing();
251 }
252 closure->downloadStatusChanged.cv.notify_one();
253 }
254 });
255 }
247 auto subscriptionUrl = subscriptionUrlPrefix + apppendToUrl; 256 auto subscriptionUrl = subscriptionUrlPrefix + apppendToUrl;
248 auto subscription = filterEngine->GetSubscription(subscriptionUrl); 257 auto subscription = filterEngine->GetSubscription(subscriptionUrl);
249 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()) << subs criptionUrl; 258 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()) << subs criptionUrl;
250 EXPECT_TRUE(subscription.GetProperty("downloadStatus").IsNull()) << subscr iptionUrl; 259 EXPECT_TRUE(subscription.GetProperty("downloadStatus").IsNull()) << subscr iptionUrl;
251 subscription.UpdateFilters(); 260 subscription.UpdateFilters();
252 { 261 {
253 std::unique_lock<std::mutex> lock(downloadStatusChanged.mutex); 262 std::unique_lock<std::mutex> lock(data->downloadStatusChanged.mutex);
254 downloadStatusChanged.cv.wait_for(lock, 263 data->downloadStatusChanged.cv.wait_for(lock,
255 /*don't block tests forever*/std::chrono::seconds(5), 264 /*don't block tests forever*/std::chrono::seconds(300),
256 [this, subscriptionUrl]()->bool 265 [this, subscriptionUrl]()->bool
257 { 266 {
258 return subscriptionUrl == downloadStatusChanged.url; 267 return subscriptionUrl == data->downloadStatusChanged.url;
259 }); 268 });
260 // Basically it's enough to wait only for downloadStatus although there 269 // Basically it's enough to wait only for downloadStatus although there
261 // is still some JS code being executed. Any following attempt to work 270 // is still some JS code being executed. Any following attempt to work
262 // with subscription object will result in execution of JS, which will 271 // with subscription object will result in execution of JS, which will
263 // be blocked until finishing of currently running code. 272 // be blocked until finishing of currently running code.
264 } 273 }
265 return subscription; 274 return subscription;
266 } 275 }
267 }; 276 };
268 } 277 }
(...skipping 780 matching lines...) Expand 10 before | Expand all | Expand 10 after
1049 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin g()); 1058 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin g());
1050 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1059 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1051 } 1060 }
1052 1061
1053 TEST_F(FilterEngineIsAllowedConnectionTest, AllowingCallbackAllowsUpdating) 1062 TEST_F(FilterEngineIsAllowedConnectionTest, AllowingCallbackAllowsUpdating)
1054 { 1063 {
1055 // no stored allowed_connection_type preference 1064 // no stored allowed_connection_type preference
1056 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); 1065 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
1057 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin g()); 1066 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin g());
1058 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1067 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1059 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings(); 1068 auto capturedConnectionTypes = data->capturedConnectionTypes.GetStrings();
1060 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1069 ASSERT_EQ(1u, capturedConnectionTypes.size());
1061 EXPECT_FALSE(capturedConnectionTypes[0].first); 1070 EXPECT_FALSE(capturedConnectionTypes[0].first);
1062 } 1071 }
1063 1072
1064 TEST_F(FilterEngineIsAllowedConnectionTest, NotAllowingCallbackDoesNotAllowUpdat ing) 1073 TEST_F(FilterEngineIsAllowedConnectionTest, NotAllowingCallbackDoesNotAllowUpdat ing)
1065 { 1074 {
1066 isConnectionAllowed = false; 1075 data->isConnectionAllowed = false;
1067 // no stored allowed_connection_type preference 1076 // no stored allowed_connection_type preference
1068 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); 1077 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
1069 EXPECT_EQ("synchronize_connection_error", subscription.GetProperty("downloadSt atus").AsString()); 1078 EXPECT_EQ("synchronize_connection_error", subscription.GetProperty("downloadSt atus").AsString());
1070 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()); 1079 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size());
1071 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings(); 1080 auto capturedConnectionTypes = data->capturedConnectionTypes.GetStrings();
1072 EXPECT_EQ(1u, capturedConnectionTypes.size()); 1081 EXPECT_EQ(1u, capturedConnectionTypes.size());
1073 } 1082 }
1074 1083
1075 TEST_F(FilterEngineIsAllowedConnectionTest, PredefinedAllowedConnectionTypeIsPas sedToCallback) 1084 TEST_F(FilterEngineIsAllowedConnectionTest, PredefinedAllowedConnectionTypeIsPas sedToCallback)
1076 { 1085 {
1077 std::string predefinedAllowedConnectionType = "non-metered"; 1086 std::string predefinedAllowedConnectionType = "non-metered";
1078 createParams.preconfiguredPrefs.insert(std::make_pair("allowed_connection_type ", 1087 createParams.preconfiguredPrefs.insert(std::make_pair("allowed_connection_type ",
1079 jsEngine->NewValue(predefinedAllowedConnectionType))); 1088 jsEngine->NewValue(predefinedAllowedConnectionType)));
1080 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); 1089 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
1081 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin g()); 1090 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin g());
1082 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1091 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1083 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings(); 1092 auto capturedConnectionTypes = data->capturedConnectionTypes.GetStrings();
1084 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1093 ASSERT_EQ(1u, capturedConnectionTypes.size());
1085 EXPECT_TRUE(capturedConnectionTypes[0].first); 1094 EXPECT_TRUE(capturedConnectionTypes[0].first);
1086 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second); 1095 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second);
1087 } 1096 }
1088 1097
1089 TEST_F(FilterEngineIsAllowedConnectionTest, ConfiguredConnectionTypeIsPassedToCa llback) 1098 TEST_F(FilterEngineIsAllowedConnectionTest, ConfiguredConnectionTypeIsPassedToCa llback)
1090 { 1099 {
1091 // FilterEngine->RemoveSubscription is not usable here because subscriptions 1100 // FilterEngine->RemoveSubscription is not usable here because subscriptions
1092 // are cached internally by URL. So, different URLs are used in diffirent 1101 // are cached internally by URL. So, different URLs are used in diffirent
1093 // checks. 1102 // checks.
1094 { 1103 {
1095 std::string predefinedAllowedConnectionType = "non-metered"; 1104 std::string predefinedAllowedConnectionType = "non-metered";
1096 createParams.preconfiguredPrefs.insert(std::make_pair( 1105 createParams.preconfiguredPrefs.insert(std::make_pair(
1097 "allowed_connection_type", jsEngine->NewValue(predefinedAllowedConnectionT ype))); 1106 "allowed_connection_type", jsEngine->NewValue(predefinedAllowedConnectionT ype)));
1098 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); 1107 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
1099 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); 1108 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing());
1100 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1109 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1101 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings(); 1110 auto capturedConnectionTypes = data->capturedConnectionTypes.GetStrings();
1102 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1111 ASSERT_EQ(1u, capturedConnectionTypes.size());
1103 EXPECT_TRUE(capturedConnectionTypes[0].first); 1112 EXPECT_TRUE(capturedConnectionTypes[0].first);
1104 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second ); 1113 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second );
1105 } 1114 }
1106 capturedConnectionTypes.Clear(); 1115 data->capturedConnectionTypes.Clear();
1107 { 1116 {
1108 // set no value 1117 // set no value
1109 filterEngine->SetAllowedConnectionType(nullptr); 1118 filterEngine->SetAllowedConnectionType(nullptr);
1110 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subA"); 1119 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subA");
1111 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); 1120 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing());
1112 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1121 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1113 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings(); 1122 auto capturedConnectionTypes = data->capturedConnectionTypes.GetStrings();
1114 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1123 ASSERT_EQ(1u, capturedConnectionTypes.size());
1115 EXPECT_FALSE(capturedConnectionTypes[0].first); 1124 EXPECT_FALSE(capturedConnectionTypes[0].first);
1116 subscription.RemoveFromList(); 1125 subscription.RemoveFromList();
1117 this->capturedConnectionTypes.Clear(); 1126 data->capturedConnectionTypes.Clear();
1118 } 1127 }
1119 capturedConnectionTypes.Clear(); 1128 data->capturedConnectionTypes.Clear();
1120 { 1129 {
1121 // set some value 1130 // set some value
1122 std::string testConnection = "test connection"; 1131 std::string testConnection = "test connection";
1123 filterEngine->SetAllowedConnectionType(&testConnection); 1132 filterEngine->SetAllowedConnectionType(&testConnection);
1124 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); 1133 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB");
1125 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); 1134 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing());
1126 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1135 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1127 auto capturedConnectionTypes = this->capturedConnectionTypes.GetStrings(); 1136 auto capturedConnectionTypes = data->capturedConnectionTypes.GetStrings();
1128 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1137 ASSERT_EQ(1u, capturedConnectionTypes.size());
1129 EXPECT_TRUE(capturedConnectionTypes[0].first); 1138 EXPECT_TRUE(capturedConnectionTypes[0].first);
1130 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); 1139 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second);
1131 } 1140 }
1132 } 1141 }
OLDNEW
« no previous file with comments | « src/WebRequestJsObject.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld