| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 /* | 
|  | 2  * This file is part of Adblock Plus <https://adblockplus.org/>, | 
|  | 3  * Copyright (C) 2006-2016 Eyeo GmbH | 
|  | 4  * | 
|  | 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 | 
|  | 7  * published by the Free Software Foundation. | 
|  | 8  * | 
|  | 9  * Adblock Plus is distributed in the hope that it will be useful, | 
|  | 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 12  * GNU General Public License for more details. | 
|  | 13  * | 
|  | 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/>. | 
|  | 16  */ | 
|  | 17 | 
|  | 18 package org.adblockplus.libadblockplus.tests; | 
|  | 19 | 
|  | 20 import org.adblockplus.libadblockplus.AppInfo; | 
|  | 21 import org.adblockplus.libadblockplus.EventCallback; | 
|  | 22 import org.adblockplus.libadblockplus.FilterEngine; | 
|  | 23 import org.adblockplus.libadblockplus.HeaderEntry; | 
|  | 24 import org.adblockplus.libadblockplus.JsEngine; | 
|  | 25 import org.adblockplus.libadblockplus.JsValue; | 
|  | 26 import org.adblockplus.libadblockplus.LazyLogSystem; | 
|  | 27 import org.adblockplus.libadblockplus.LazyWebRequest; | 
|  | 28 import org.adblockplus.libadblockplus.ServerResponse; | 
|  | 29 import org.adblockplus.libadblockplus.UpdateCheckDoneCallback; | 
|  | 30 | 
|  | 31 import org.junit.Test; | 
|  | 32 | 
|  | 33 import java.util.List; | 
|  | 34 | 
|  | 35 public class UpdateCheckTest extends BaseJsTest | 
|  | 36 { | 
|  | 37   protected String previousRequestUrl; | 
|  | 38 | 
|  | 39   public class TestWebRequest extends LazyWebRequest | 
|  | 40   { | 
|  | 41     public ServerResponse response = new ServerResponse(); | 
|  | 42 | 
|  | 43     @Override | 
|  | 44     public ServerResponse httpGET(String url, List<HeaderEntry> headers) | 
|  | 45     { | 
|  | 46       if (url.indexOf("easylist") >= 0) | 
|  | 47       { | 
|  | 48         return super.httpGET(url, headers); | 
|  | 49       } | 
|  | 50 | 
|  | 51       previousRequestUrl = url; | 
|  | 52       return response; | 
|  | 53     } | 
|  | 54   } | 
|  | 55 | 
|  | 56   protected AppInfo appInfo; | 
|  | 57   protected TestWebRequest webRequest; | 
|  | 58   protected JsEngine jsEngine; | 
|  | 59   protected FilterEngine filterEngine; | 
|  | 60 | 
|  | 61   protected boolean eventCallbackCalled; | 
|  | 62   protected List<JsValue> eventCallbackParams; | 
|  | 63   protected boolean updateCallbackCalled; | 
|  | 64   protected String updateError; | 
|  | 65 | 
|  | 66   private EventCallback eventCallback = new EventCallback() | 
|  | 67   { | 
|  | 68     @Override | 
|  | 69     public void eventCallback(List<JsValue> params) | 
|  | 70     { | 
|  | 71       eventCallbackCalled = true; | 
|  | 72       eventCallbackParams = params; | 
|  | 73     } | 
|  | 74   }; | 
|  | 75 | 
|  | 76   private UpdateCheckDoneCallback updateCallback = new UpdateCheckDoneCallback() | 
|  | 77   { | 
|  | 78     @Override | 
|  | 79     public void updateCheckDoneCallback(String error) | 
|  | 80     { | 
|  | 81       updateCallbackCalled = true; | 
|  | 82       updateError = error; | 
|  | 83     } | 
|  | 84   }; | 
|  | 85 | 
|  | 86   public void reset() | 
|  | 87   { | 
|  | 88     jsEngine = new JsEngine(appInfo); | 
|  | 89     jsEngine.setLogSystem(new LazyLogSystem()); | 
|  | 90     jsEngine.setDefaultFileSystem(getContext().getFilesDir().getAbsolutePath()); | 
|  | 91     jsEngine.setWebRequest(webRequest); | 
|  | 92     jsEngine.setEventCallback("updateAvailable", eventCallback); | 
|  | 93 | 
|  | 94     filterEngine = new FilterEngine(jsEngine); | 
|  | 95   } | 
|  | 96 | 
|  | 97   @Override | 
|  | 98   protected void setUp() throws Exception | 
|  | 99   { | 
|  | 100     super.setUp(); | 
|  | 101 | 
|  | 102     appInfo = AppInfo.builder().build(); | 
|  | 103     webRequest = new TestWebRequest(); | 
|  | 104     eventCallbackCalled = false; | 
|  | 105     updateCallbackCalled = false; | 
|  | 106     reset(); | 
|  | 107   } | 
|  | 108 | 
|  | 109   public void forceUpdateCheck() | 
|  | 110   { | 
|  | 111     filterEngine.forceUpdateCheck(updateCallback); | 
|  | 112   } | 
|  | 113 | 
|  | 114   @Test | 
|  | 115   public void testRequestFailure() throws InterruptedException | 
|  | 116   { | 
|  | 117     webRequest.response.setStatus(ServerResponse.NsStatus.ERROR_FAILURE); | 
|  | 118 | 
|  | 119     appInfo = AppInfo | 
|  | 120       .builder() | 
|  | 121       .setName("1") | 
|  | 122       .setVersion("3") | 
|  | 123       .setApplication("4") | 
|  | 124       .setApplicationVersion("2") | 
|  | 125       .setDevelopmentBuild(false) | 
|  | 126       .build(); | 
|  | 127 | 
|  | 128     reset(); | 
|  | 129     forceUpdateCheck(); | 
|  | 130 | 
|  | 131     Thread.sleep(100); | 
|  | 132 | 
|  | 133     assertFalse(eventCallbackCalled); | 
|  | 134     assertTrue(updateCallbackCalled); | 
|  | 135     assertNotNull(updateError); | 
|  | 136 | 
|  | 137     String expectedUrl = filterEngine.getPref("update_url_release").asString(); | 
|  | 138     String platform = "libadblockplus"; | 
|  | 139     String platformVersion = "1.0"; | 
|  | 140 | 
|  | 141     expectedUrl = expectedUrl | 
|  | 142       .replaceAll("%NAME%", appInfo.name) | 
|  | 143       .replaceAll("%TYPE%", "1"); // manual update | 
|  | 144 | 
|  | 145     expectedUrl += | 
|  | 146       "&addonName=" + appInfo.name + | 
|  | 147       "&addonVersion=" + appInfo.version + | 
|  | 148       "&application=" + appInfo.application + | 
|  | 149       "&applicationVersion=" + appInfo.applicationVersion + | 
|  | 150       "&platform=" + platform + | 
|  | 151       "&platformVersion=" + platformVersion + | 
|  | 152       "&lastVersion=0&downloadCount=0"; | 
|  | 153 | 
|  | 154     assertEquals(expectedUrl, previousRequestUrl); | 
|  | 155   } | 
|  | 156 | 
|  | 157   @Test | 
|  | 158   public void testApplicationUpdateAvailable() throws InterruptedException | 
|  | 159   { | 
|  | 160     webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 
|  | 161     webRequest.response.setResponseStatus(200); | 
|  | 162     webRequest.response.setResponse( | 
|  | 163       "{\"1/4\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); | 
|  | 164 | 
|  | 165     appInfo = AppInfo | 
|  | 166       .builder() | 
|  | 167       .setName("1") | 
|  | 168       .setVersion("3") | 
|  | 169       .setApplication("4") | 
|  | 170       .setApplicationVersion("2") | 
|  | 171       .setDevelopmentBuild(true) | 
|  | 172       .build(); | 
|  | 173 | 
|  | 174     reset(); | 
|  | 175     forceUpdateCheck(); | 
|  | 176 | 
|  | 177     Thread.sleep(1000); | 
|  | 178 | 
|  | 179     assertTrue(eventCallbackCalled); | 
|  | 180     assertNotNull(eventCallbackParams); | 
|  | 181     assertEquals(1l, eventCallbackParams.size()); | 
|  | 182     assertEquals("https://foo.bar/", eventCallbackParams.get(0).asString()); | 
|  | 183     assertTrue(updateCallbackCalled); | 
|  | 184     assertEquals("", updateError); | 
|  | 185   } | 
|  | 186 | 
|  | 187   @Test | 
|  | 188   public void testWrongApplication() throws InterruptedException | 
|  | 189   { | 
|  | 190     webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 
|  | 191     webRequest.response.setResponseStatus(200); | 
|  | 192     webRequest.response.setResponse( | 
|  | 193       "{\"1/3\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); | 
|  | 194 | 
|  | 195     appInfo = AppInfo | 
|  | 196       .builder() | 
|  | 197       .setName("1") | 
|  | 198       .setVersion("3") | 
|  | 199       .setApplication("4") | 
|  | 200       .setApplicationVersion("2") | 
|  | 201       .setDevelopmentBuild(true) | 
|  | 202       .build(); | 
|  | 203 | 
|  | 204     reset(); | 
|  | 205     forceUpdateCheck(); | 
|  | 206 | 
|  | 207     Thread.sleep(1000); | 
|  | 208 | 
|  | 209     assertFalse(eventCallbackCalled); | 
|  | 210     assertTrue(updateCallbackCalled); | 
|  | 211     assertEquals("", updateError); | 
|  | 212   } | 
|  | 213 | 
|  | 214   @Test | 
|  | 215   public void testWrongVersion() throws InterruptedException | 
|  | 216   { | 
|  | 217     webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 
|  | 218     webRequest.response.setResponseStatus(200); | 
|  | 219     webRequest.response.setResponse( | 
|  | 220       "{\"1\": {\"version\":\"3\",\"url\":\"https://foo.bar/\"}}"); | 
|  | 221 | 
|  | 222     appInfo = AppInfo | 
|  | 223       .builder() | 
|  | 224       .setName("1") | 
|  | 225       .setVersion("3") | 
|  | 226       .setApplication("4") | 
|  | 227       .setApplicationVersion("2") | 
|  | 228       .setDevelopmentBuild(true) | 
|  | 229       .build(); | 
|  | 230 | 
|  | 231     reset(); | 
|  | 232     forceUpdateCheck(); | 
|  | 233 | 
|  | 234     Thread.sleep(1000); | 
|  | 235 | 
|  | 236     assertFalse(eventCallbackCalled); | 
|  | 237     assertTrue(updateCallbackCalled); | 
|  | 238     assertEquals("", updateError); | 
|  | 239   } | 
|  | 240 | 
|  | 241   @Test | 
|  | 242   public void testWrongURL() throws InterruptedException | 
|  | 243   { | 
|  | 244     webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 
|  | 245     webRequest.response.setResponseStatus(200); | 
|  | 246     webRequest.response.setResponse( | 
|  | 247       "{\"1\": {\"version\":\"3.1\",\"url\":\"http://insecure/\"}}"); | 
|  | 248 | 
|  | 249     appInfo = AppInfo | 
|  | 250       .builder() | 
|  | 251       .setName("1") | 
|  | 252       .setVersion("3") | 
|  | 253       .setApplication("4") | 
|  | 254       .setApplicationVersion("2") | 
|  | 255       .setDevelopmentBuild(true) | 
|  | 256       .build(); | 
|  | 257 | 
|  | 258     reset(); | 
|  | 259     forceUpdateCheck(); | 
|  | 260 | 
|  | 261     Thread.sleep(1000); | 
|  | 262 | 
|  | 263     assertFalse(eventCallbackCalled); | 
|  | 264     assertTrue(updateCallbackCalled); | 
|  | 265     assertTrue(updateError.length() > 0); | 
|  | 266   } | 
|  | 267 } | 
| OLD | NEW | 
|---|