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