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