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

Delta Between Two Patch Sets: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidFileSystemTest.java

Issue 29424615: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Left Patch Set: now using smart_ptr, changed impl for reading file as string, minor changes Created May 22, 2017, 1:03 p.m.
Right Patch Set: changed impl for reading file as bytes array, modified test. AndroidFileSystem now does not resolve… Created May 29, 2017, 11:26 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.libadblockplus.tests; 18 package org.adblockplus.libadblockplus.tests;
19 19
20 import android.os.SystemClock;
20 import android.test.AndroidTestCase; 21 import android.test.AndroidTestCase;
22 import android.util.Log;
21 23
22 import org.adblockplus.libadblockplus.AdblockPlusException; 24 import org.adblockplus.libadblockplus.AdblockPlusException;
25 import org.adblockplus.libadblockplus.AppInfo;
23 import org.adblockplus.libadblockplus.FileSystem; 26 import org.adblockplus.libadblockplus.FileSystem;
24 import org.adblockplus.libadblockplus.FileSystemUtils; 27 import org.adblockplus.libadblockplus.FileSystemUtils;
28 import org.adblockplus.libadblockplus.FilterEngine;
29 import org.adblockplus.libadblockplus.JsEngine;
25 import org.adblockplus.libadblockplus.android.AndroidFileSystem; 30 import org.adblockplus.libadblockplus.android.AndroidFileSystem;
31 import org.adblockplus.libadblockplus.android.AndroidWebRequest;
32 import org.adblockplus.libadblockplus.android.Utils;
26 import org.junit.Test; 33 import org.junit.Test;
27 34
28 import java.io.File; 35 import java.io.File;
29 import java.io.FileNotFoundException;
30 import java.io.IOException; 36 import java.io.IOException;
37 import java.util.Arrays;
38
39 import static org.adblockplus.libadblockplus.FileSystemUtils.byteFromInt;
31 40
32 public class AndroidFileSystemTest extends AndroidTestCase 41 public class AndroidFileSystemTest extends AndroidTestCase
33 { 42 {
43 private static final String TAG = Utils.getTag(AndroidFileSystemTest.class);
44
45 protected static final String TEXT_DATA = "12345qwerty";
46 protected static final byte[] EMPTY_DATA_BYTES = {};
47 protected static final byte[] TEXT_DATA_BYTES = TEXT_DATA.getBytes();
48 protected static final byte[] BINARY_DATA_BYTES = new byte[]
49 {
50 byteFromInt(1),
51 byteFromInt(2),
52 byteFromInt(3)
53 };
54
34 protected final FileSystemUtils fileSystemUtils = new FileSystemUtils(); 55 protected final FileSystemUtils fileSystemUtils = new FileSystemUtils();
35 protected static final String DATA = "12345qwerty"; 56 protected File basePathFile;
36 protected AndroidFileSystem fileSystem; 57 protected AndroidFileSystem fileSystem;
37 58
38 @Override 59 @Override
39 protected void setUp() throws Exception 60 protected void setUp() throws Exception
40 { 61 {
41 fileSystem = new AndroidFileSystem(getContext().getCacheDir()); 62 String tmpFolder = FileSystemUtils.generateUniqueFileName("tmp", null);
63 basePathFile = new File(getContext().getCacheDir(), tmpFolder);
64 basePathFile.mkdirs();
65
66 fileSystem = new AndroidFileSystem(basePathFile);
42 } 67 }
43 68
44 protected String generateUniqueFilename() 69 protected String generateUniqueFilename()
45 { 70 {
46 return fileSystemUtils.generateUniqueFileName("file", ".tmp"); 71 return fileSystemUtils.generateUniqueFileName("file", ".tmp");
47 } 72 }
48 73
49 protected File getFullPath(String path) 74 protected File getFullPath(String path)
50 { 75 {
51 return new File(fileSystem.getBasePath(), path); 76 return new File(fileSystem.getBasePath(), path);
52 } 77 }
53 78
54 @Test 79 @Test
80 public void testWriteReadBinaryFile() throws IOException
81 {
82 String path = generateUniqueFilename();
83 String resolvedPath = fileSystem.resolve(path);
84 File file = getFullPath(path);
85 assertFalse(file.exists());
86
87 fileSystem.write(resolvedPath, BINARY_DATA_BYTES);
88
89 assertTrue(file.exists());
90 assertTrue(Arrays.equals(BINARY_DATA_BYTES, fileSystemUtils.readFile(file))) ;
91
92 byte[] data = fileSystem.read(resolvedPath);
93 assertNotNull(data);
94 assertTrue(Arrays.equals(BINARY_DATA_BYTES, data));
95 }
96
97 @Test
55 public void testWriteNullFile() throws IOException 98 public void testWriteNullFile() throws IOException
56 { 99 {
57 String path = generateUniqueFilename(); 100 String path = generateUniqueFilename();
58 File file = getFullPath(path); 101 String resolvedPath = fileSystem.resolve(path);
59 assertFalse(file.exists()); 102 File file = getFullPath(path);
60 103 assertFalse(file.exists());
61 fileSystem.write(path, null); 104
62 105 fileSystem.write(resolvedPath, null);
63 assertTrue(file.exists()); 106
64 assertEquals("", fileSystemUtils.readFile(file)); 107 assertTrue(file.exists());
108 assertTrue(Arrays.equals(EMPTY_DATA_BYTES, fileSystemUtils.readFile(file)));
65 } 109 }
66 110
67 @Test 111 @Test
68 public void testWriteEmptyFile() throws IOException 112 public void testWriteEmptyFile() throws IOException
69 { 113 {
70 String path = generateUniqueFilename(); 114 String path = generateUniqueFilename();
71 File file = getFullPath(path); 115 String resolvedPath = fileSystem.resolve(path);
72 assertFalse(file.exists()); 116 File file = getFullPath(path);
73 117 assertFalse(file.exists());
74 fileSystem.write(path, ""); 118
75 119 fileSystem.write(resolvedPath, EMPTY_DATA_BYTES);
76 assertTrue(file.exists()); 120
77 assertEquals("", fileSystemUtils.readFile(file)); 121 assertTrue(file.exists());
122 assertTrue(Arrays.equals(EMPTY_DATA_BYTES, fileSystemUtils.readFile(file)));
78 } 123 }
79 124
80 @Test 125 @Test
81 public void testWriteInvalidPath() 126 public void testWriteInvalidPath()
82 { 127 {
83 String path = "notExistingDirectory/" + generateUniqueFilename(); 128 String path = "notExistingDirectory/" + generateUniqueFilename();
129 String resolvedPath = fileSystem.resolve(path);
84 File file = getFullPath(path); 130 File file = getFullPath(path);
85 assertFalse(file.exists()); 131 assertFalse(file.exists());
86 File notExistingDirectoryFile = file.getParentFile(); 132 File notExistingDirectoryFile = file.getParentFile();
87 assertFalse(notExistingDirectoryFile.exists()); 133 assertFalse(notExistingDirectoryFile.exists());
88 134
89 try 135 try
90 { 136 {
91 fileSystem.write(path, DATA); 137 fileSystem.write(resolvedPath, TEXT_DATA_BYTES);
92 fail("Exception should be thrown"); 138 fail("Exception should be thrown");
93 } 139 }
94 catch (AdblockPlusException e) 140 catch (AdblockPlusException e)
95 { 141 {
96 // ignored 142 // ignored
97 } 143 }
98 } 144 }
99 145
100 @Test 146 @Test
101 public void testWriteSingleLineData() throws IOException 147 public void testWriteSingleLineData() throws IOException
102 { 148 {
103 String path = generateUniqueFilename(); 149 String path = generateUniqueFilename();
104 File file = getFullPath(path); 150 String resolvedPath = fileSystem.resolve(path);
105 assertFalse(file.exists()); 151 File file = getFullPath(path);
106 152 assertFalse(file.exists());
107 final String singleLineData = DATA.replace("\n", ""); 153
108 fileSystem.write(path, singleLineData); 154 final String singleLineData = TEXT_DATA.replace("\n", "");
109 155 fileSystem.write(resolvedPath, singleLineData.getBytes());
110 assertTrue(file.exists()); 156
111 assertEquals(singleLineData, fileSystemUtils.readFile(file)); 157 assertTrue(file.exists());
158 assertTrue(Arrays.equals(singleLineData.getBytes(), fileSystemUtils.readFile (file)));
112 } 159 }
113 160
114 @Test 161 @Test
115 public void testWriteMultiLineData() throws IOException 162 public void testWriteMultiLineData() throws IOException
116 { 163 {
117 String path = generateUniqueFilename(); 164 String path = generateUniqueFilename();
118 File file = getFullPath(path); 165 String resolvedPath = fileSystem.resolve(path);
119 assertFalse(file.exists()); 166 File file = getFullPath(path);
120 167 assertFalse(file.exists());
121 final String multiLineData = DATA + "\n" + DATA; 168
122 fileSystem.write(path, multiLineData); 169 final String multiLineData = TEXT_DATA + "\n" + TEXT_DATA;
123 170 fileSystem.write(resolvedPath, multiLineData.getBytes());
124 assertTrue(file.exists()); 171
125 assertEquals(multiLineData, fileSystemUtils.readFile(file)); 172 assertTrue(file.exists());
173 assertTrue(Arrays.equals(multiLineData.getBytes(), fileSystemUtils.readFile( file)));
126 } 174 }
127 175
128 @Test 176 @Test
129 public void testReadEmptyFile() throws IOException 177 public void testReadEmptyFile() throws IOException
130 { 178 {
131 String path = generateUniqueFilename(); 179 String path = generateUniqueFilename();
132 File file = getFullPath(path); 180 String resolvedPath = fileSystem.resolve(path);
133 assertFalse(file.exists()); 181 File file = getFullPath(path);
134 182 assertFalse(file.exists());
135 fileSystemUtils.writeFile(file, ""); 183
136 assertTrue(file.exists()); 184 fileSystemUtils.writeFile(file, EMPTY_DATA_BYTES);
137 185 assertTrue(file.exists());
138 assertEquals("", fileSystem.read(path)); 186
187 assertTrue(Arrays.equals(EMPTY_DATA_BYTES, fileSystem.read(resolvedPath)));
139 } 188 }
140 189
141 @Test 190 @Test
142 public void testReadSingleLineData() throws IOException 191 public void testReadSingleLineData() throws IOException
143 { 192 {
144 String path = generateUniqueFilename(); 193 String path = generateUniqueFilename();
145 File file = getFullPath(path); 194 String resolvedPath = fileSystem.resolve(path);
146 assertFalse(file.exists()); 195 File file = getFullPath(path);
147 196 assertFalse(file.exists());
148 final String singleLineData = DATA.replace("\n", ""); 197
149 fileSystemUtils.writeFile(file, singleLineData); 198 final String singleLineData = TEXT_DATA.replace("\n", "");
150 assertTrue(file.exists()); 199 fileSystemUtils.writeFile(file, singleLineData.getBytes());
151 200 assertTrue(file.exists());
152 assertEquals(singleLineData, fileSystem.read(path)); 201
202 assertTrue(Arrays.equals(singleLineData.getBytes(), fileSystem.read(resolved Path)));
153 } 203 }
154 204
155 @Test 205 @Test
156 public void testReadMultiLineData() throws IOException 206 public void testReadMultiLineData() throws IOException
157 { 207 {
158 String path = generateUniqueFilename(); 208 String path = generateUniqueFilename();
159 File file = getFullPath(path); 209 String resolvedPath = fileSystem.resolve(path);
160 assertFalse(file.exists()); 210 File file = getFullPath(path);
161 211 assertFalse(file.exists());
162 final String multiLineData = DATA + "\n" + DATA; 212
163 fileSystemUtils.writeFile(file, multiLineData); 213 final String multiLineData = TEXT_DATA + "\n" + TEXT_DATA;
164 assertTrue(file.exists()); 214 fileSystemUtils.writeFile(file, multiLineData.getBytes());
165 215 assertTrue(file.exists());
166 assertEquals(multiLineData, fileSystem.read(path)); 216
217 assertTrue(Arrays.equals(multiLineData.getBytes(), fileSystem.read(resolvedP ath)));
167 } 218 }
168 219
169 @Test 220 @Test
170 public void testMoveExistingFile() throws IOException 221 public void testMoveExistingFile() throws IOException
171 { 222 {
172 String fromPath = generateUniqueFilename(); 223 String fromPath = generateUniqueFilename();
224 String resolvedFromPath= fileSystem.resolve(fromPath);
173 File fromFile = getFullPath(fromPath); 225 File fromFile = getFullPath(fromPath);
174 String toPath = generateUniqueFilename(); 226 String toPath = generateUniqueFilename();
227 String resolvedToPath = fileSystem.resolve(toPath);
175 File toFile = getFullPath(toPath); 228 File toFile = getFullPath(toPath);
176 fileSystemUtils.writeFile(fromFile, DATA); 229 fileSystemUtils.writeFile(fromFile, TEXT_DATA_BYTES);
177 230
178 assertTrue(fromFile.exists()); 231 assertTrue(fromFile.exists());
179 assertFalse(toFile.exists()); 232 assertFalse(toFile.exists());
180 233
181 fileSystem.move(fromPath, toPath); 234 fileSystem.move(resolvedFromPath, resolvedToPath);
182 235
183 assertFalse(fromFile.exists()); 236 assertFalse(fromFile.exists());
184 assertTrue(toFile.exists()); 237 assertTrue(toFile.exists());
185 assertEquals(DATA, fileSystemUtils.readFile(toFile)); 238 assertTrue(Arrays.equals(TEXT_DATA_BYTES, fileSystemUtils.readFile(toFile))) ;
186 } 239 }
187 240
188 @Test 241 @Test
189 public void testMoveNotExistingFile() 242 public void testMoveNotExistingFile()
190 { 243 {
191 String fromPath = generateUniqueFilename(); 244 String fromPath = generateUniqueFilename();
245 String resolvedFromPath = fileSystem.resolve(fromPath);
192 File fromFile = getFullPath(fromPath); 246 File fromFile = getFullPath(fromPath);
193 String toPath = generateUniqueFilename(); 247 String toPath = generateUniqueFilename();
248 String resolvedToPath = fileSystem.resolve(toPath);
194 assertFalse(fromFile.exists()); 249 assertFalse(fromFile.exists());
195 250
196 try 251 try
197 { 252 {
198 fileSystem.move(fromPath, toPath); 253 fileSystem.move(resolvedFromPath, resolvedToPath);
199 fail("Exception should be thrown"); 254 fail("Exception should be thrown");
200 } 255 }
201 catch (AdblockPlusException e) 256 catch (AdblockPlusException e)
202 { 257 {
203 // ignored 258 // ignored
204 } 259 }
205 } 260 }
206 261
207 @Test 262 @Test
208 public void testRemoveExistingFile() throws IOException 263 public void testRemoveExistingFile() throws IOException
209 { 264 {
210 String path = generateUniqueFilename(); 265 String path = generateUniqueFilename();
211 File file = getFullPath(path); 266 String resolvedPath = fileSystem.resolve(path);
212 fileSystemUtils.writeFile(file, DATA); 267 File file = getFullPath(path);
213 268 fileSystemUtils.writeFile(file, TEXT_DATA_BYTES);
214 assertTrue(file.exists()); 269
215 fileSystem.remove(path); 270 assertTrue(file.exists());
271 fileSystem.remove(resolvedPath);
216 assertFalse(file.exists()); 272 assertFalse(file.exists());
217 } 273 }
218 274
219 @Test 275 @Test
220 public void testRemoveNotExistingFile() 276 public void testRemoveNotExistingFile()
221 { 277 {
222 String path = generateUniqueFilename(); 278 String path = generateUniqueFilename();
223 File file = getFullPath(path); 279 String resolvedPath = fileSystem.resolve(path);
224 assertFalse(file.exists()); 280 File file = getFullPath(path);
225 281 assertFalse(file.exists());
226 fileSystem.remove(path); 282
283 fileSystem.remove(resolvedPath);
227 assertFalse(file.exists()); 284 assertFalse(file.exists());
228 } 285 }
229 286
230 @Test 287 @Test
231 public void testStatExistingFile() throws IOException 288 public void testStatExistingFile() throws IOException
232 { 289 {
233 String path = generateUniqueFilename(); 290 String path = generateUniqueFilename();
234 File file = getFullPath(path); 291 String resolvedPath = fileSystem.resolve(path);
235 fileSystemUtils.writeFile(file, DATA); 292 File file = getFullPath(path);
236 assertTrue(file.exists()); 293 fileSystemUtils.writeFile(file, TEXT_DATA_BYTES);
237 294 assertTrue(file.exists());
238 FileSystem.StatResult stat = fileSystem.stat(path); 295
296 FileSystem.StatResult stat = fileSystem.stat(resolvedPath);
239 assertNotNull(stat); 297 assertNotNull(stat);
240 assertTrue(stat.exists()); 298 assertTrue(stat.exists());
241 assertFalse(stat.isDirectory()); 299 assertFalse(stat.isDirectory());
242 assertTrue(stat.isFile()); 300 assertTrue(stat.isFile());
243 assertTrue(stat.getLastModified() > 0); 301 assertTrue(stat.getLastModified() > 0);
244 } 302 }
245 303
246 @Test 304 @Test
247 public void testStatExistingDirectory() 305 public void testStatExistingDirectory()
248 { 306 {
249 String path = generateUniqueFilename(); 307 String path = generateUniqueFilename();
308 String resolvedPath = fileSystem.resolve(path);
250 File file = getFullPath(path); 309 File file = getFullPath(path);
251 file.mkdir(); 310 file.mkdir();
252 311
253 FileSystem.StatResult stat = fileSystem.stat(path); 312 FileSystem.StatResult stat = fileSystem.stat(resolvedPath);
254 assertNotNull(stat); 313 assertNotNull(stat);
255 assertTrue(stat.exists()); 314 assertTrue(stat.exists());
256 assertFalse(stat.isFile()); 315 assertFalse(stat.isFile());
257 assertTrue(stat.isDirectory()); 316 assertTrue(stat.isDirectory());
258 assertTrue(stat.getLastModified() > 0); 317 assertTrue(stat.getLastModified() > 0);
259 } 318 }
260 319
261 @Test 320 @Test
262 public void testStatNotExistingFile() 321 public void testStatNotExistingFile()
263 { 322 {
264 String path = generateUniqueFilename(); 323 String path = generateUniqueFilename();
265 File file = getFullPath(path); 324 String resolvedPath = fileSystem.resolve(path);
266 assertFalse(file.exists()); 325 File file = getFullPath(path);
267 326 assertFalse(file.exists());
268 fileSystem.remove(path); 327
269 assertFalse(file.exists()); 328 fileSystem.remove(resolvedPath);
270 FileSystem.StatResult notExistingStat = fileSystem.stat(path); 329 assertFalse(file.exists());
330 FileSystem.StatResult notExistingStat = fileSystem.stat(resolvedPath);
271 assertFalse(notExistingStat.exists()); 331 assertFalse(notExistingStat.exists());
272 assertEquals(0l, notExistingStat.getLastModified()); 332 assertEquals(0l, notExistingStat.getLastModified());
273 } 333 }
274 334
275 @Test 335 @Test
276 public void testResolveAbsolute() 336 public void testResolveAbsolute()
277 { 337 {
278 String path = generateUniqueFilename(); 338 String path = generateUniqueFilename();
279 String absolutePath = fileSystem.resolve(path); 339 String resolvedPath = fileSystem.resolve(path);
280 String fullPath = getFullPath(path).getAbsolutePath(); 340 String fullPath = getFullPath(path).getAbsolutePath();
281 String fullPathFromBasePath = fullPath 341 assertEquals(fullPath, resolvedPath);
282 .substring(fileSystem.getBasePath()
283 .getAbsolutePath().length());
284 assertEquals(fullPathFromBasePath, absolutePath);
285 } 342 }
286 343
287 @Test 344 @Test
288 public void testResolveRelative() 345 public void testResolveRelative()
289 { 346 {
290 String relativePath = "./file"; 347 String relativePath = "./folder/file2";
291 String absolutePath = fileSystem.resolve(relativePath); 348 String resolvedPath = fileSystem.resolve(relativePath);
292 assertNotNull(absolutePath); 349 assertNotNull(resolvedPath);
293 assertTrue(absolutePath.startsWith("/")); 350 assertEquals(new File(basePathFile, relativePath).getAbsolutePath(), resolve dPath);
351 }
352
353 private boolean readInvoked;
354 private boolean writeInvoked;
355 private boolean statInvoked;
356 private boolean resolveInvoked;
357
358 private final class TestAndroidFileSystem extends AndroidFileSystem
359 {
360 public TestAndroidFileSystem(File basePath)
361 {
362 super(basePath);
363 }
364
365 @Override
366 public byte[] read(String path)
367 {
368 readInvoked = true;
369 try
370 {
371 Log.d(TAG, "Reading from " + path);
372 String content = new String(FileSystemUtils.readFile(new File(path)), "U TF-8");
373 Log.d(TAG, "Read from " + path + ":\n" + content);
374 }
375 catch (IOException e)
376 {
377 throw new RuntimeException(e);
378 }
379
380 return super.read(path);
381 }
382
383 @Override
384 public void write(String path, byte[] data)
385 {
386 writeInvoked = true;
387 Log.d(TAG, "Write " + data.length + " bytes to " + path);
388 super.write(path, data);
389 }
390
391 @Override
392 public StatResult stat(String path)
393 {
394 statInvoked = true;
395 StatResult stat = super.stat(path);
396 Log.d(TAG, "Stat for " + path + " is " + stat);
397 return stat;
398 }
399
400 @Override
401 public String resolve(String path)
402 {
403 resolveInvoked = true;
404
405 String resolvedPath = super.resolve(path);
406 Log.d(TAG, "Resolve " + path + " to " + resolvedPath);
407 return resolvedPath;
408 }
409 }
410
411 @Test
412 public void testInvokeFromNativeCode()
413 {
414 readInvoked = false;
415 writeInvoked = false;
416 statInvoked = false;
417 resolveInvoked = false;
418
419 JsEngine jsEngine = new JsEngine(AppInfo.builder().build());
420 jsEngine.setDefaultLogSystem();
421 jsEngine.setFileSystem(new TestAndroidFileSystem(basePathFile));
422 jsEngine.setWebRequest(new AndroidWebRequest());
423
424 FilterEngine filterEngine = new FilterEngine(jsEngine);
425 SystemClock.sleep(10 * 1000);
426
427 assertTrue(readInvoked);
428 assertTrue(writeInvoked);
429 assertTrue(statInvoked);
430 assertTrue(resolveInvoked);
294 } 431 }
295 } 432 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld