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

Side by Side Diff: test/WebRequest.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 | « test/FilterEngine.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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 { 106 {
107 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )"); 107 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
108 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); 108 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
109 AdblockPlus::Sleep(200); 109 AdblockPlus::Sleep(200);
110 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int()); 110 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int());
111 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt()); 111 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt());
112 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString()); 112 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString());
113 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString()); 113 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString());
114 } 114 }
115 115
116 TEST_F(MockWebRequestTest, ConnectionIsAllowedOnJsEngine)
117 {
118 std::atomic<int> isConnectionAllowedCalledTimes(0);
119 jsEngine->SetIsConnectionAllowedCallback([&isConnectionAllowedCalledTimes]()-> bool
120 {
121 ++isConnectionAllowedCalledTimes;
122 return true;
123 });
124 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
125 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
126 AdblockPlus::Sleep(200);
127 EXPECT_EQ(1, isConnectionAllowedCalledTimes);
128 EXPECT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int());
129 EXPECT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt());
130 EXPECT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString());
131 EXPECT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString());
132 }
133
134 TEST_F(MockWebRequestTest, ConnectionIsNotAllowedOnJsEngine)
135 {
136 std::atomic<int> isConnectionAllowedCalledTimes(0);
137 jsEngine->SetIsConnectionAllowedCallback([&isConnectionAllowedCalledTimes]()-> bool
138 {
139 ++isConnectionAllowedCalledTimes;
140 return false;
141 });
142 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
143 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
144 AdblockPlus::Sleep(200);
145 EXPECT_EQ(1, isConnectionAllowedCalledTimes);
146 EXPECT_EQ(AdblockPlus::WebRequest::NS_ERROR_CONNECTION_REFUSED, jsEngine->Eval uate("foo.status")->AsInt());
147 EXPECT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt());
148 EXPECT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString());
149 EXPECT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS tring());
150 }
151
152 namespace
153 {
154 class SyncStrings
155 {
156 public:
157 void Add(const std::string* value)
158 {
159 std::lock_guard<std::mutex> lock(mutex);
160 strings.emplace_back(!!value, value ? *value : "");
161 }
162 std::vector<std::pair<bool, std::string>> GetStrings() const
163 {
164 std::lock_guard<std::mutex> lock(mutex);
165 return strings;
166 }
167 void Clear()
168 {
169 std::lock_guard<std::mutex> lock(mutex);
170 strings.clear();
171 }
172 private:
173 mutable std::mutex mutex;
174 std::vector<std::pair<bool, std::string>> strings;
175 };
176 }
177
178 TEST_F(MockWebRequestTest, ConnectionIsAllowedOnFilterEngine1)
179 {
180 FilterEngine::CreationParameters createParams;
181 std::string predefinedAllowedConnectionType = "non-metered";
182 createParams.preconfiguredPrefs.emplace("allowed_connection_type", jsEngine->N ewValue(predefinedAllowedConnectionType));
183 auto receivedConnectionTypes = std::make_shared<SyncStrings>();
184 createParams.isConnectionAllowedCallback = [receivedConnectionTypes](const std ::string* allowedConnectionType)->bool {
185 receivedConnectionTypes->Add(allowedConnectionType);
186 return true;
187 };
188 auto filterEngine = FilterEngine::Create(jsEngine, createParams);
189 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
190 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
191 AdblockPlus::Sleep(200);
192 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings();
193 EXPECT_FALSE(receivedConnectionTypesStrings.empty());
194 for (const auto& connectionType : receivedConnectionTypesStrings)
195 {
196 EXPECT_TRUE(connectionType.first);
197 EXPECT_EQ(predefinedAllowedConnectionType, connectionType.second);
198 }
199 EXPECT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int());
200 EXPECT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt());
201 EXPECT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString());
202 EXPECT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString());
203 }
204
205 TEST_F(MockWebRequestTest, ConnectionIsAllowedOnFilterEngine2)
206 {
207 FilterEngine::CreationParameters createParams;
208 auto receivedConnectionTypes = std::make_shared<SyncStrings>();
209 createParams.isConnectionAllowedCallback = [receivedConnectionTypes](const std ::string* allowedConnectionType)->bool {
210 receivedConnectionTypes->Add(allowedConnectionType);
211 return true;
212 };
213 auto filterEngine = FilterEngine::Create(jsEngine, createParams);
214 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
215 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
216 AdblockPlus::Sleep(200);
217 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings();
218 EXPECT_FALSE(receivedConnectionTypesStrings.empty());
219 for (const auto& connectionType : receivedConnectionTypesStrings)
220 {
221 EXPECT_FALSE(connectionType.first);
222 }
223 EXPECT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int());
224 EXPECT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt());
225 EXPECT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString());
226 EXPECT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString());
227 }
228
229 TEST_F(MockWebRequestTest, ConnectionIsAllowedOnFilterEngine3)
230 {
231 // initially allowed connection type is not defined
232 FilterEngine::CreationParameters createParams;
233 auto receivedConnectionTypes = std::make_shared<SyncStrings>();
234 createParams.isConnectionAllowedCallback = [receivedConnectionTypes](const std ::string* allowedConnectionType)->bool {
235 receivedConnectionTypes->Add(allowedConnectionType);
236 return true;
237 };
238 auto filterEngine = FilterEngine::Create(jsEngine, createParams);
239
240 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
241 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
242 AdblockPlus::Sleep(200);
243 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings();
244 EXPECT_FALSE(receivedConnectionTypesStrings.empty());
245 for (const auto& connectionType : receivedConnectionTypesStrings)
246 {
247 EXPECT_FALSE(connectionType.first);
248 }
249 EXPECT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int());
250 EXPECT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt());
251 EXPECT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString());
252 EXPECT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString());
253
254 // set allowed connection type
255 std::string allowedConnectionType = "test-connection";
256 filterEngine->SetAllowedConnectionType(&allowedConnectionType);
257 receivedConnectionTypes->Clear();
258 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
259 AdblockPlus::Sleep(200);
260 receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings();
261 EXPECT_FALSE(receivedConnectionTypesStrings.empty());
262 for (const auto& connectionType : receivedConnectionTypesStrings)
263 {
264 EXPECT_TRUE(connectionType.first);
265 EXPECT_EQ(allowedConnectionType, connectionType.second);
266 }
267
268 // remove allowed connection type
269 filterEngine->SetAllowedConnectionType(nullptr);
270 receivedConnectionTypes->Clear();
271 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
272 AdblockPlus::Sleep(200);
273 receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings();
274 EXPECT_FALSE(receivedConnectionTypesStrings.empty());
275 for (const auto& connectionType : receivedConnectionTypesStrings)
276 {
277 EXPECT_FALSE(connectionType.first);
278 }
279 }
280
281 TEST_F(MockWebRequestTest, ConnectionIsNotAllowedOnFilterEngine)
282 {
283 FilterEngine::CreationParameters createParams;
284 std::string predefinedAllowedConnectionType = "non-metered";
285 createParams.preconfiguredPrefs.emplace("allowed_connection_type", jsEngine->N ewValue(predefinedAllowedConnectionType));
286 auto receivedConnectionTypes = std::make_shared<SyncStrings>();
287 createParams.isConnectionAllowedCallback = [receivedConnectionTypes](const std ::string* allowedConnectionType)->bool {
288 receivedConnectionTypes->Add(allowedConnectionType);
289 return false;
290 };
291 auto filterEngine = FilterEngine::Create(jsEngine, createParams);
292 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
293 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
294 AdblockPlus::Sleep(200);
295 auto receivedConnectionTypesStrings = receivedConnectionTypes->GetStrings();
296 EXPECT_FALSE(receivedConnectionTypesStrings.empty());
297 for (const auto& connectionType : receivedConnectionTypesStrings)
298 {
299 EXPECT_TRUE(connectionType.first);
300 EXPECT_EQ(predefinedAllowedConnectionType, connectionType.second);
301 }
302 EXPECT_EQ(AdblockPlus::WebRequest::NS_ERROR_CONNECTION_REFUSED, jsEngine->Eval uate("foo.status")->AsInt());
303 EXPECT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt());
304 EXPECT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString());
305 EXPECT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS tring());
306 }
307
308 #if defined(HAVE_CURL) || defined(_WIN32) 116 #if defined(HAVE_CURL) || defined(_WIN32)
309 TEST_F(DefaultWebRequestTest, RealWebRequest) 117 TEST_F(DefaultWebRequestTest, RealWebRequest)
310 { 118 {
311 // This URL should redirect to easylist-downloads.adblockplus.org and we 119 // This URL should redirect to easylist-downloads.adblockplus.org and we
312 // should get the actual filter list back. 120 // should get the actual filter list back.
313 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result;} )"); 121 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result;} )");
314 WaitForVariable("this.foo", jsEngine); 122 WaitForVariable("this.foo", jsEngine);
315 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)")->AsString()); 123 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)")->AsString());
316 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int()); 124 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int());
317 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt()); 125 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt());
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 catchLogSystem->clear(); 285 catchLogSystem->clear();
478 ResetTestXHR(jsEngine); 286 ResetTestXHR(jsEngine);
479 jsEngine->Evaluate("\ 287 jsEngine->Evaluate("\
480 request.setRequestHeader('Security', 'theater');\nrequest.send();"); 288 request.setRequestHeader('Security', 'theater');\nrequest.send();");
481 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); 289 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l);
482 EXPECT_EQ("", catchLogSystem->lastMessage); 290 EXPECT_EQ("", catchLogSystem->lastMessage);
483 WaitForVariable("result", jsEngine); 291 WaitForVariable("result", jsEngine);
484 EXPECT_FALSE(webRequest->lastRequestHeaders.cend() == 292 EXPECT_FALSE(webRequest->lastRequestHeaders.cend() ==
485 webRequest->lastRequestHeaders.find("Security")); 293 webRequest->lastRequestHeaders.find("Security"));
486 } 294 }
OLDNEW
« no previous file with comments | « test/FilterEngine.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld