blob: 17e34beb5215383cb8d3bcb4d3af9b7400c912b8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Neal Nguyen1a44d5d2010-01-13 10:42:43 -080017package android.os;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Jeff Sharkey09734df2017-03-07 20:50:27 -070019import static android.os.FileUtils.roundStorageSize;
Jeff Sharkeyd9526902013-03-14 14:11:57 -070020import static android.text.format.DateUtils.DAY_IN_MILLIS;
21import static android.text.format.DateUtils.HOUR_IN_MILLIS;
22import static android.text.format.DateUtils.WEEK_IN_MILLIS;
23
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070024import static org.junit.Assert.assertArrayEquals;
Jeff Sharkey20356142017-12-06 15:22:05 -070025import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertFalse;
27import static org.junit.Assert.assertTrue;
28
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Context;
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070030import android.os.FileUtils.MemoryPipe;
Ben Kwa62539a22015-04-22 15:43:17 -070031import android.provider.DocumentsContract.Document;
Jeff Sharkey20356142017-12-06 15:22:05 -070032import android.support.test.InstrumentationRegistry;
33import android.support.test.runner.AndroidJUnit4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070035import libcore.io.Streams;
Jeff Sharkey4ca728c2014-01-10 16:27:19 -080036
Jeff Sharkey09734df2017-03-07 20:50:27 -070037import com.google.android.collect.Sets;
38
Jeff Sharkey20356142017-12-06 15:22:05 -070039import org.junit.After;
40import org.junit.Before;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import java.io.ByteArrayInputStream;
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070045import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import java.io.File;
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070047import java.io.FileInputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.io.FileOutputStream;
Jeff Sharkeyd9526902013-03-14 14:11:57 -070049import java.util.Arrays;
50import java.util.HashSet;
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070051import java.util.Random;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
Jeff Sharkey20356142017-12-06 15:22:05 -070053@RunWith(AndroidJUnit4.class)
54public class FileUtilsTest {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 private static final String TEST_DATA =
56 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
57
Jeff Sharkeyd9526902013-03-14 14:11:57 -070058 private File mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 private File mTestFile;
60 private File mCopyFile;
Ben Kwa62539a22015-04-22 15:43:17 -070061 private File mTarget;
62
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070063 private final int[] DATA_SIZES = { 32, 32_000, 32_000_000 };
64
Jeff Sharkey20356142017-12-06 15:22:05 -070065 private Context getContext() {
66 return InstrumentationRegistry.getContext();
67 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
Jeff Sharkey20356142017-12-06 15:22:05 -070069 @Before
70 public void setUp() throws Exception {
Jeff Sharkeyd9526902013-03-14 14:11:57 -070071 mDir = getContext().getDir("testing", Context.MODE_PRIVATE);
72 mTestFile = new File(mDir, "test.file");
73 mCopyFile = new File(mDir, "copy.file");
Ben Kwa62539a22015-04-22 15:43:17 -070074
75 mTarget = getContext().getFilesDir();
76 FileUtils.deleteContents(mTarget);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 }
78
Jeff Sharkey20356142017-12-06 15:22:05 -070079 @After
80 public void tearDown() throws Exception {
Neil Fuller72b61e62018-10-10 14:49:24 +010081 FileUtils.deleteContents(mDir);
Ben Kwa62539a22015-04-22 15:43:17 -070082 FileUtils.deleteContents(mTarget);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 }
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 // TODO: test setPermissions(), getPermissions()
86
Jeff Sharkey20356142017-12-06 15:22:05 -070087 @Test
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 public void testCopyFile() throws Exception {
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070089 writeFile(mTestFile, TEST_DATA);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 assertFalse(mCopyFile.exists());
91 FileUtils.copyFile(mTestFile, mCopyFile);
92 assertTrue(mCopyFile.exists());
93 assertEquals(TEST_DATA, FileUtils.readTextFile(mCopyFile, 0, null));
94 }
95
Jeff Sharkey20356142017-12-06 15:22:05 -070096 @Test
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 public void testCopyToFile() throws Exception {
98 final String s = "Foo Bar";
99 assertFalse(mCopyFile.exists());
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700100 FileUtils.copyToFile(new ByteArrayInputStream(s.getBytes()), mCopyFile);
101 assertTrue(mCopyFile.exists());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 assertEquals(s, FileUtils.readTextFile(mCopyFile, 0, null));
103 }
104
Jeff Sharkey20356142017-12-06 15:22:05 -0700105 @Test
Jeff Sharkeyb18f8992018-01-31 21:47:09 -0700106 public void testCopy_FileToFile() throws Exception {
107 for (int size : DATA_SIZES) {
108 final File src = new File(mTarget, "src");
109 final File dest = new File(mTarget, "dest");
110
111 byte[] expected = new byte[size];
112 byte[] actual = new byte[size];
113 new Random().nextBytes(expected);
114 writeFile(src, expected);
115
116 try (FileInputStream in = new FileInputStream(src);
117 FileOutputStream out = new FileOutputStream(dest)) {
118 FileUtils.copy(in, out);
119 }
120
121 actual = readFile(dest);
122 assertArrayEquals(expected, actual);
123 }
124 }
125
126 @Test
127 public void testCopy_FileToPipe() throws Exception {
128 for (int size : DATA_SIZES) {
129 final File src = new File(mTarget, "src");
130
131 byte[] expected = new byte[size];
132 byte[] actual = new byte[size];
133 new Random().nextBytes(expected);
134 writeFile(src, expected);
135
136 try (FileInputStream in = new FileInputStream(src);
137 MemoryPipe out = MemoryPipe.createSink(actual)) {
138 FileUtils.copy(in.getFD(), out.getFD());
139 out.join();
140 }
141
142 assertArrayEquals(expected, actual);
143 }
144 }
145
146 @Test
147 public void testCopy_PipeToFile() throws Exception {
148 for (int size : DATA_SIZES) {
149 final File dest = new File(mTarget, "dest");
150
151 byte[] expected = new byte[size];
152 byte[] actual = new byte[size];
153 new Random().nextBytes(expected);
154
155 try (MemoryPipe in = MemoryPipe.createSource(expected);
156 FileOutputStream out = new FileOutputStream(dest)) {
157 FileUtils.copy(in.getFD(), out.getFD());
158 }
159
160 actual = readFile(dest);
161 assertArrayEquals(expected, actual);
162 }
163 }
164
165 @Test
166 public void testCopy_PipeToPipe() throws Exception {
167 for (int size : DATA_SIZES) {
168 byte[] expected = new byte[size];
169 byte[] actual = new byte[size];
170 new Random().nextBytes(expected);
171
172 try (MemoryPipe in = MemoryPipe.createSource(expected);
173 MemoryPipe out = MemoryPipe.createSink(actual)) {
174 FileUtils.copy(in.getFD(), out.getFD());
175 out.join();
176 }
177
178 assertArrayEquals(expected, actual);
179 }
180 }
181
182 @Test
Jeff Sharkey45c97df2018-02-01 16:01:52 -0700183 public void testCopy_ShortPipeToFile() throws Exception {
184 byte[] source = new byte[33_000_000];
185 new Random().nextBytes(source);
186
187 for (int size : DATA_SIZES) {
188 final File dest = new File(mTarget, "dest");
189
190 byte[] expected = Arrays.copyOf(source, size);
191 byte[] actual = new byte[size];
192
193 try (MemoryPipe in = MemoryPipe.createSource(source);
194 FileOutputStream out = new FileOutputStream(dest)) {
195 FileUtils.copy(in.getFD(), out.getFD(), null, null, size);
196 }
197
198 actual = readFile(dest);
199 assertArrayEquals(expected, actual);
200 }
201 }
202
203 @Test
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 public void testIsFilenameSafe() throws Exception {
205 assertTrue(FileUtils.isFilenameSafe(new File("foobar")));
206 assertTrue(FileUtils.isFilenameSafe(new File("a_b-c=d.e/0,1+23")));
207 assertFalse(FileUtils.isFilenameSafe(new File("foo*bar")));
208 assertFalse(FileUtils.isFilenameSafe(new File("foo\nbar")));
209 }
210
Jeff Sharkey20356142017-12-06 15:22:05 -0700211 @Test
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 public void testReadTextFile() throws Exception {
Jeff Sharkeyb18f8992018-01-31 21:47:09 -0700213 writeFile(mTestFile, TEST_DATA);
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 0, null));
216
217 assertEquals("ABCDE", FileUtils.readTextFile(mTestFile, 5, null));
218 assertEquals("ABCDE<>", FileUtils.readTextFile(mTestFile, 5, "<>"));
219 assertEquals(TEST_DATA.substring(0, 51) + "<>",
220 FileUtils.readTextFile(mTestFile, 51, "<>"));
221 assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 52, "<>"));
222 assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 100, "<>"));
223
224 assertEquals("vwxyz", FileUtils.readTextFile(mTestFile, -5, null));
225 assertEquals("<>vwxyz", FileUtils.readTextFile(mTestFile, -5, "<>"));
226 assertEquals("<>" + TEST_DATA.substring(1, 52),
227 FileUtils.readTextFile(mTestFile, -51, "<>"));
228 assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, -52, "<>"));
229 assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, -100, "<>"));
230 }
231
Jeff Sharkey20356142017-12-06 15:22:05 -0700232 @Test
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 public void testReadTextFileWithZeroLengthFile() throws Exception {
Jeff Sharkeyb18f8992018-01-31 21:47:09 -0700234 writeFile(mTestFile, TEST_DATA);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 new FileOutputStream(mTestFile).close(); // Zero out the file
236 assertEquals("", FileUtils.readTextFile(mTestFile, 0, null));
237 assertEquals("", FileUtils.readTextFile(mTestFile, 1, "<>"));
238 assertEquals("", FileUtils.readTextFile(mTestFile, 10, "<>"));
239 assertEquals("", FileUtils.readTextFile(mTestFile, -1, "<>"));
240 assertEquals("", FileUtils.readTextFile(mTestFile, -10, "<>"));
241 }
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700242
Jeff Sharkey20356142017-12-06 15:22:05 -0700243 @Test
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800244 public void testContains() throws Exception {
245 assertTrue(FileUtils.contains(new File("/"), new File("/moo.txt")));
246 assertTrue(FileUtils.contains(new File("/"), new File("/")));
247
248 assertTrue(FileUtils.contains(new File("/sdcard"), new File("/sdcard")));
249 assertTrue(FileUtils.contains(new File("/sdcard/"), new File("/sdcard/")));
250
251 assertTrue(FileUtils.contains(new File("/sdcard"), new File("/sdcard/moo.txt")));
252 assertTrue(FileUtils.contains(new File("/sdcard/"), new File("/sdcard/moo.txt")));
253
254 assertFalse(FileUtils.contains(new File("/sdcard"), new File("/moo.txt")));
255 assertFalse(FileUtils.contains(new File("/sdcard/"), new File("/moo.txt")));
256
257 assertFalse(FileUtils.contains(new File("/sdcard"), new File("/sdcard.txt")));
258 assertFalse(FileUtils.contains(new File("/sdcard/"), new File("/sdcard.txt")));
259 }
260
Jeff Sharkey20356142017-12-06 15:22:05 -0700261 @Test
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700262 public void testDeleteOlderEmptyDir() throws Exception {
263 FileUtils.deleteOlderFiles(mDir, 10, WEEK_IN_MILLIS);
264 assertDirContents();
265 }
266
Jeff Sharkey20356142017-12-06 15:22:05 -0700267 @Test
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700268 public void testDeleteOlderTypical() throws Exception {
269 touch("file1", HOUR_IN_MILLIS);
270 touch("file2", 1 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
271 touch("file3", 2 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
272 touch("file4", 3 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
273 touch("file5", 4 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -0800274 assertTrue(FileUtils.deleteOlderFiles(mDir, 3, DAY_IN_MILLIS));
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700275 assertDirContents("file1", "file2", "file3");
276 }
277
Jeff Sharkey20356142017-12-06 15:22:05 -0700278 @Test
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700279 public void testDeleteOlderInFuture() throws Exception {
280 touch("file1", -HOUR_IN_MILLIS);
281 touch("file2", HOUR_IN_MILLIS);
282 touch("file3", WEEK_IN_MILLIS);
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -0800283 assertTrue(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS));
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700284 assertDirContents("file1", "file2");
285
286 touch("file1", -HOUR_IN_MILLIS);
287 touch("file2", HOUR_IN_MILLIS);
288 touch("file3", WEEK_IN_MILLIS);
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -0800289 assertTrue(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS));
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700290 assertDirContents("file1", "file2");
291 }
292
Jeff Sharkey20356142017-12-06 15:22:05 -0700293 @Test
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700294 public void testDeleteOlderOnlyAge() throws Exception {
295 touch("file1", HOUR_IN_MILLIS);
296 touch("file2", 1 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
297 touch("file3", 2 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
298 touch("file4", 3 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
299 touch("file5", 4 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -0800300 assertTrue(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS));
301 assertFalse(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS));
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700302 assertDirContents("file1");
303 }
304
Jeff Sharkey20356142017-12-06 15:22:05 -0700305 @Test
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700306 public void testDeleteOlderOnlyCount() throws Exception {
307 touch("file1", HOUR_IN_MILLIS);
308 touch("file2", 1 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
309 touch("file3", 2 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
310 touch("file4", 3 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
311 touch("file5", 4 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -0800312 assertTrue(FileUtils.deleteOlderFiles(mDir, 2, 0));
313 assertFalse(FileUtils.deleteOlderFiles(mDir, 2, 0));
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700314 assertDirContents("file1", "file2");
315 }
316
Jeff Sharkey20356142017-12-06 15:22:05 -0700317 @Test
Jeff Sharkey0cce5352014-11-26 13:38:26 -0800318 public void testValidExtFilename() throws Exception {
319 assertTrue(FileUtils.isValidExtFilename("a"));
320 assertTrue(FileUtils.isValidExtFilename("foo.bar"));
321 assertTrue(FileUtils.isValidExtFilename("foo bar.baz"));
322 assertTrue(FileUtils.isValidExtFilename("foo.bar.baz"));
323 assertTrue(FileUtils.isValidExtFilename(".bar"));
324 assertTrue(FileUtils.isValidExtFilename("foo~!@#$%^&*()_[]{}+bar"));
325
326 assertFalse(FileUtils.isValidExtFilename(null));
327 assertFalse(FileUtils.isValidExtFilename("."));
328 assertFalse(FileUtils.isValidExtFilename("../foo"));
329 assertFalse(FileUtils.isValidExtFilename("/foo"));
330
331 assertEquals(".._foo", FileUtils.buildValidExtFilename("../foo"));
332 assertEquals("_foo", FileUtils.buildValidExtFilename("/foo"));
333 assertEquals("foo_bar", FileUtils.buildValidExtFilename("foo\0bar"));
334 assertEquals(".foo", FileUtils.buildValidExtFilename(".foo"));
335 assertEquals("foo.bar", FileUtils.buildValidExtFilename("foo.bar"));
336 }
337
Jeff Sharkey20356142017-12-06 15:22:05 -0700338 @Test
Jeff Sharkey0cce5352014-11-26 13:38:26 -0800339 public void testValidFatFilename() throws Exception {
340 assertTrue(FileUtils.isValidFatFilename("a"));
341 assertTrue(FileUtils.isValidFatFilename("foo bar.baz"));
342 assertTrue(FileUtils.isValidFatFilename("foo.bar.baz"));
343 assertTrue(FileUtils.isValidFatFilename(".bar"));
344 assertTrue(FileUtils.isValidFatFilename("foo.bar"));
345 assertTrue(FileUtils.isValidFatFilename("foo bar"));
346 assertTrue(FileUtils.isValidFatFilename("foo+bar"));
347 assertTrue(FileUtils.isValidFatFilename("foo,bar"));
348
349 assertFalse(FileUtils.isValidFatFilename("foo*bar"));
350 assertFalse(FileUtils.isValidFatFilename("foo?bar"));
351 assertFalse(FileUtils.isValidFatFilename("foo<bar"));
352 assertFalse(FileUtils.isValidFatFilename(null));
353 assertFalse(FileUtils.isValidFatFilename("."));
354 assertFalse(FileUtils.isValidFatFilename("../foo"));
355 assertFalse(FileUtils.isValidFatFilename("/foo"));
356
357 assertEquals(".._foo", FileUtils.buildValidFatFilename("../foo"));
358 assertEquals("_foo", FileUtils.buildValidFatFilename("/foo"));
359 assertEquals(".foo", FileUtils.buildValidFatFilename(".foo"));
360 assertEquals("foo.bar", FileUtils.buildValidFatFilename("foo.bar"));
361 assertEquals("foo_bar__baz", FileUtils.buildValidFatFilename("foo?bar**baz"));
362 }
363
Jeff Sharkey20356142017-12-06 15:22:05 -0700364 @Test
Jeff Sharkey4f5e8b32015-06-11 19:13:37 -0700365 public void testTrimFilename() throws Exception {
366 assertEquals("short.txt", FileUtils.trimFilename("short.txt", 16));
367 assertEquals("extrem...eme.txt", FileUtils.trimFilename("extremelylongfilename.txt", 16));
368
369 final String unicode = "a\u03C0\u03C0\u03C0\u03C0z";
370 assertEquals("a\u03C0\u03C0\u03C0\u03C0z", FileUtils.trimFilename(unicode, 10));
371 assertEquals("a\u03C0...\u03C0z", FileUtils.trimFilename(unicode, 9));
372 assertEquals("a...\u03C0z", FileUtils.trimFilename(unicode, 8));
373 assertEquals("a...\u03C0z", FileUtils.trimFilename(unicode, 7));
374 assertEquals("a...z", FileUtils.trimFilename(unicode, 6));
375 }
376
Jeff Sharkey20356142017-12-06 15:22:05 -0700377 @Test
Ben Kwa62539a22015-04-22 15:43:17 -0700378 public void testBuildUniqueFile_normal() throws Exception {
379 assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test"));
380 assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
381 assertNameEquals("test.jpeg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpeg"));
382 assertNameEquals("TEst.JPeg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "TEst.JPeg"));
383 assertNameEquals("test.png.jpg",
384 FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.png.jpg"));
385 assertNameEquals("test.png.jpg",
386 FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.png"));
387
388 assertNameEquals("test.flac", FileUtils.buildUniqueFile(mTarget, "audio/flac", "test"));
389 assertNameEquals("test.flac", FileUtils.buildUniqueFile(mTarget, "audio/flac", "test.flac"));
390 assertNameEquals("test.flac",
391 FileUtils.buildUniqueFile(mTarget, "application/x-flac", "test"));
392 assertNameEquals("test.flac",
393 FileUtils.buildUniqueFile(mTarget, "application/x-flac", "test.flac"));
394 }
395
Jeff Sharkey20356142017-12-06 15:22:05 -0700396 @Test
Ben Kwa62539a22015-04-22 15:43:17 -0700397 public void testBuildUniqueFile_unknown() throws Exception {
398 assertNameEquals("test",
399 FileUtils.buildUniqueFile(mTarget, "application/octet-stream", "test"));
400 assertNameEquals("test.jpg",
401 FileUtils.buildUniqueFile(mTarget, "application/octet-stream", "test.jpg"));
402 assertNameEquals(".test",
403 FileUtils.buildUniqueFile(mTarget, "application/octet-stream", ".test"));
404
405 assertNameEquals("test", FileUtils.buildUniqueFile(mTarget, "lolz/lolz", "test"));
406 assertNameEquals("test.lolz", FileUtils.buildUniqueFile(mTarget, "lolz/lolz", "test.lolz"));
407 }
408
Jeff Sharkey20356142017-12-06 15:22:05 -0700409 @Test
Ben Kwa62539a22015-04-22 15:43:17 -0700410 public void testBuildUniqueFile_dir() throws Exception {
411 assertNameEquals("test", FileUtils.buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test"));
412 new File(mTarget, "test").mkdir();
413 assertNameEquals("test (1)",
414 FileUtils.buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test"));
415
416 assertNameEquals("test.jpg",
417 FileUtils.buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test.jpg"));
418 new File(mTarget, "test.jpg").mkdir();
419 assertNameEquals("test.jpg (1)",
420 FileUtils.buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test.jpg"));
421 }
422
Jeff Sharkey20356142017-12-06 15:22:05 -0700423 @Test
Ben Kwa62539a22015-04-22 15:43:17 -0700424 public void testBuildUniqueFile_increment() throws Exception {
425 assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
426 new File(mTarget, "test.jpg").createNewFile();
427 assertNameEquals("test (1).jpg",
428 FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
429 new File(mTarget, "test (1).jpg").createNewFile();
430 assertNameEquals("test (2).jpg",
431 FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
432 }
433
Jeff Sharkey20356142017-12-06 15:22:05 -0700434 @Test
Jeff Sharkeyaa444762016-09-20 18:54:46 -0600435 public void testBuildUniqueFile_mimeless() throws Exception {
436 assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "test.jpg"));
437 new File(mTarget, "test.jpg").createNewFile();
438 assertNameEquals("test (1).jpg", FileUtils.buildUniqueFile(mTarget, "test.jpg"));
439
440 assertNameEquals("test", FileUtils.buildUniqueFile(mTarget, "test"));
441 new File(mTarget, "test").createNewFile();
442 assertNameEquals("test (1)", FileUtils.buildUniqueFile(mTarget, "test"));
443
444 assertNameEquals("test.foo.bar", FileUtils.buildUniqueFile(mTarget, "test.foo.bar"));
445 new File(mTarget, "test.foo.bar").createNewFile();
446 assertNameEquals("test.foo (1).bar", FileUtils.buildUniqueFile(mTarget, "test.foo.bar"));
447 }
448
Jeff Sharkey20356142017-12-06 15:22:05 -0700449 @Test
Jeff Sharkey373d0172017-02-22 15:47:27 -0700450 public void testRoundStorageSize() throws Exception {
Jeff Sharkey09734df2017-03-07 20:50:27 -0700451 final long M128 = 128000000L;
452 final long M256 = 256000000L;
453 final long M512 = 512000000L;
454 final long G1 = 1000000000L;
455 final long G2 = 2000000000L;
456 final long G16 = 16000000000L;
457 final long G32 = 32000000000L;
458 final long G64 = 64000000000L;
Jeff Sharkey373d0172017-02-22 15:47:27 -0700459
Jeff Sharkey09734df2017-03-07 20:50:27 -0700460 assertEquals(M128, roundStorageSize(M128));
461 assertEquals(M256, roundStorageSize(M128 + 1));
462 assertEquals(M256, roundStorageSize(M256 - 1));
463 assertEquals(M256, roundStorageSize(M256));
464 assertEquals(M512, roundStorageSize(M256 + 1));
465 assertEquals(M512, roundStorageSize(M512 - 1));
466 assertEquals(M512, roundStorageSize(M512));
467 assertEquals(G1, roundStorageSize(M512 + 1));
468 assertEquals(G1, roundStorageSize(G1));
469 assertEquals(G2, roundStorageSize(G1 + 1));
Jeff Sharkey373d0172017-02-22 15:47:27 -0700470
Jeff Sharkey09734df2017-03-07 20:50:27 -0700471 assertEquals(G16, roundStorageSize(G16));
472 assertEquals(G32, roundStorageSize(G16 + 1));
473 assertEquals(G32, roundStorageSize(G32 - 1));
474 assertEquals(G32, roundStorageSize(G32));
475 assertEquals(G64, roundStorageSize(G32 + 1));
Jeff Sharkey373d0172017-02-22 15:47:27 -0700476 }
477
Ben Kwa62539a22015-04-22 15:43:17 -0700478 private static void assertNameEquals(String expected, File actual) {
479 assertEquals(expected, actual.getName());
480 }
481
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700482 private void touch(String name, long age) throws Exception {
483 final File file = new File(mDir, name);
484 file.createNewFile();
485 file.setLastModified(System.currentTimeMillis() - age);
486 }
487
Jeff Sharkeyb18f8992018-01-31 21:47:09 -0700488 private void writeFile(File file, String data) throws Exception {
489 writeFile(file, data.getBytes());
490 }
491
492 private void writeFile(File file, byte[] data) throws Exception {
493 try (FileOutputStream out = new FileOutputStream(file)) {
494 out.write(data);
495 }
496 }
497
498 private byte[] readFile(File file) throws Exception {
499 try (FileInputStream in = new FileInputStream(file);
500 ByteArrayOutputStream out = new ByteArrayOutputStream()) {
501 Streams.copy(in, out);
502 return out.toByteArray();
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700503 }
504 }
505
506 private void assertDirContents(String... expected) {
507 final HashSet<String> expectedSet = Sets.newHashSet(expected);
508 String[] actual = mDir.list();
509 if (actual == null) actual = new String[0];
510
511 assertEquals(
512 "Expected " + Arrays.toString(expected) + " but actual " + Arrays.toString(actual),
513 expected.length, actual.length);
514 for (String actualFile : actual) {
515 assertTrue("Unexpected actual file " + actualFile, expectedSet.contains(actualFile));
516 }
517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518}