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