blob: 0bc3a2d879ab532a9d3b5c95481f3b0f71599a44 [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 Sharkey4ca728c2014-01-10 16:27:19 -080035import libcore.io.IoUtils;
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070036import libcore.io.Streams;
Jeff Sharkey4ca728c2014-01-10 16:27:19 -080037
Jeff Sharkey09734df2017-03-07 20:50:27 -070038import com.google.android.collect.Sets;
39
Jeff Sharkey20356142017-12-06 15:22:05 -070040import org.junit.After;
41import org.junit.Before;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import java.io.ByteArrayInputStream;
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070046import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import java.io.File;
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070048import java.io.FileInputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import java.io.FileOutputStream;
Jeff Sharkeyd9526902013-03-14 14:11:57 -070050import java.util.Arrays;
51import java.util.HashSet;
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070052import java.util.Random;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Jeff Sharkey20356142017-12-06 15:22:05 -070054@RunWith(AndroidJUnit4.class)
55public class FileUtilsTest {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 private static final String TEST_DATA =
57 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
58
Jeff Sharkeyd9526902013-03-14 14:11:57 -070059 private File mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 private File mTestFile;
61 private File mCopyFile;
Ben Kwa62539a22015-04-22 15:43:17 -070062 private File mTarget;
63
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070064 private final int[] DATA_SIZES = { 32, 32_000, 32_000_000 };
65
Jeff Sharkey20356142017-12-06 15:22:05 -070066 private Context getContext() {
67 return InstrumentationRegistry.getContext();
68 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Jeff Sharkey20356142017-12-06 15:22:05 -070070 @Before
71 public void setUp() throws Exception {
Jeff Sharkeyd9526902013-03-14 14:11:57 -070072 mDir = getContext().getDir("testing", Context.MODE_PRIVATE);
73 mTestFile = new File(mDir, "test.file");
74 mCopyFile = new File(mDir, "copy.file");
Ben Kwa62539a22015-04-22 15:43:17 -070075
76 mTarget = getContext().getFilesDir();
77 FileUtils.deleteContents(mTarget);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 }
79
Jeff Sharkey20356142017-12-06 15:22:05 -070080 @After
81 public void tearDown() throws Exception {
Jeff Sharkeyd9526902013-03-14 14:11:57 -070082 IoUtils.deleteContents(mDir);
Ben Kwa62539a22015-04-22 15:43:17 -070083 FileUtils.deleteContents(mTarget);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 }
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 // TODO: test setPermissions(), getPermissions()
87
Jeff Sharkey20356142017-12-06 15:22:05 -070088 @Test
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 public void testCopyFile() throws Exception {
Jeff Sharkeyb18f8992018-01-31 21:47:09 -070090 writeFile(mTestFile, TEST_DATA);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 assertFalse(mCopyFile.exists());
92 FileUtils.copyFile(mTestFile, mCopyFile);
93 assertTrue(mCopyFile.exists());
94 assertEquals(TEST_DATA, FileUtils.readTextFile(mCopyFile, 0, null));
95 }
96
Jeff Sharkey20356142017-12-06 15:22:05 -070097 @Test
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 public void testCopyToFile() throws Exception {
99 final String s = "Foo Bar";
100 assertFalse(mCopyFile.exists());
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700101 FileUtils.copyToFile(new ByteArrayInputStream(s.getBytes()), mCopyFile);
102 assertTrue(mCopyFile.exists());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 assertEquals(s, FileUtils.readTextFile(mCopyFile, 0, null));
104 }
105
Jeff Sharkey20356142017-12-06 15:22:05 -0700106 @Test
Jeff Sharkeyb18f8992018-01-31 21:47:09 -0700107 public void testCopy_FileToFile() throws Exception {
108 for (int size : DATA_SIZES) {
109 final File src = new File(mTarget, "src");
110 final File dest = new File(mTarget, "dest");
111
112 byte[] expected = new byte[size];
113 byte[] actual = new byte[size];
114 new Random().nextBytes(expected);
115 writeFile(src, expected);
116
117 try (FileInputStream in = new FileInputStream(src);
118 FileOutputStream out = new FileOutputStream(dest)) {
119 FileUtils.copy(in, out);
120 }
121
122 actual = readFile(dest);
123 assertArrayEquals(expected, actual);
124 }
125 }
126
127 @Test
128 public void testCopy_FileToPipe() throws Exception {
129 for (int size : DATA_SIZES) {
130 final File src = new File(mTarget, "src");
131
132 byte[] expected = new byte[size];
133 byte[] actual = new byte[size];
134 new Random().nextBytes(expected);
135 writeFile(src, expected);
136
137 try (FileInputStream in = new FileInputStream(src);
138 MemoryPipe out = MemoryPipe.createSink(actual)) {
139 FileUtils.copy(in.getFD(), out.getFD());
140 out.join();
141 }
142
143 assertArrayEquals(expected, actual);
144 }
145 }
146
147 @Test
148 public void testCopy_PipeToFile() throws Exception {
149 for (int size : DATA_SIZES) {
150 final File dest = new File(mTarget, "dest");
151
152 byte[] expected = new byte[size];
153 byte[] actual = new byte[size];
154 new Random().nextBytes(expected);
155
156 try (MemoryPipe in = MemoryPipe.createSource(expected);
157 FileOutputStream out = new FileOutputStream(dest)) {
158 FileUtils.copy(in.getFD(), out.getFD());
159 }
160
161 actual = readFile(dest);
162 assertArrayEquals(expected, actual);
163 }
164 }
165
166 @Test
167 public void testCopy_PipeToPipe() throws Exception {
168 for (int size : DATA_SIZES) {
169 byte[] expected = new byte[size];
170 byte[] actual = new byte[size];
171 new Random().nextBytes(expected);
172
173 try (MemoryPipe in = MemoryPipe.createSource(expected);
174 MemoryPipe out = MemoryPipe.createSink(actual)) {
175 FileUtils.copy(in.getFD(), out.getFD());
176 out.join();
177 }
178
179 assertArrayEquals(expected, actual);
180 }
181 }
182
183 @Test
Jeff Sharkey45c97df2018-02-01 16:01:52 -0700184 public void testCopy_ShortPipeToFile() throws Exception {
185 byte[] source = new byte[33_000_000];
186 new Random().nextBytes(source);
187
188 for (int size : DATA_SIZES) {
189 final File dest = new File(mTarget, "dest");
190
191 byte[] expected = Arrays.copyOf(source, size);
192 byte[] actual = new byte[size];
193
194 try (MemoryPipe in = MemoryPipe.createSource(source);
195 FileOutputStream out = new FileOutputStream(dest)) {
196 FileUtils.copy(in.getFD(), out.getFD(), null, null, size);
197 }
198
199 actual = readFile(dest);
200 assertArrayEquals(expected, actual);
201 }
202 }
203
204 @Test
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public void testIsFilenameSafe() throws Exception {
206 assertTrue(FileUtils.isFilenameSafe(new File("foobar")));
207 assertTrue(FileUtils.isFilenameSafe(new File("a_b-c=d.e/0,1+23")));
208 assertFalse(FileUtils.isFilenameSafe(new File("foo*bar")));
209 assertFalse(FileUtils.isFilenameSafe(new File("foo\nbar")));
210 }
211
Jeff Sharkey20356142017-12-06 15:22:05 -0700212 @Test
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 public void testReadTextFile() throws Exception {
Jeff Sharkeyb18f8992018-01-31 21:47:09 -0700214 writeFile(mTestFile, TEST_DATA);
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 0, null));
217
218 assertEquals("ABCDE", FileUtils.readTextFile(mTestFile, 5, null));
219 assertEquals("ABCDE<>", FileUtils.readTextFile(mTestFile, 5, "<>"));
220 assertEquals(TEST_DATA.substring(0, 51) + "<>",
221 FileUtils.readTextFile(mTestFile, 51, "<>"));
222 assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 52, "<>"));
223 assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 100, "<>"));
224
225 assertEquals("vwxyz", FileUtils.readTextFile(mTestFile, -5, null));
226 assertEquals("<>vwxyz", FileUtils.readTextFile(mTestFile, -5, "<>"));
227 assertEquals("<>" + TEST_DATA.substring(1, 52),
228 FileUtils.readTextFile(mTestFile, -51, "<>"));
229 assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, -52, "<>"));
230 assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, -100, "<>"));
231 }
232
Jeff Sharkey20356142017-12-06 15:22:05 -0700233 @Test
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 public void testReadTextFileWithZeroLengthFile() throws Exception {
Jeff Sharkeyb18f8992018-01-31 21:47:09 -0700235 writeFile(mTestFile, TEST_DATA);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 new FileOutputStream(mTestFile).close(); // Zero out the file
237 assertEquals("", FileUtils.readTextFile(mTestFile, 0, null));
238 assertEquals("", FileUtils.readTextFile(mTestFile, 1, "<>"));
239 assertEquals("", FileUtils.readTextFile(mTestFile, 10, "<>"));
240 assertEquals("", FileUtils.readTextFile(mTestFile, -1, "<>"));
241 assertEquals("", FileUtils.readTextFile(mTestFile, -10, "<>"));
242 }
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700243
Jeff Sharkey20356142017-12-06 15:22:05 -0700244 @Test
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800245 public void testContains() throws Exception {
246 assertTrue(FileUtils.contains(new File("/"), new File("/moo.txt")));
247 assertTrue(FileUtils.contains(new File("/"), new File("/")));
248
249 assertTrue(FileUtils.contains(new File("/sdcard"), new File("/sdcard")));
250 assertTrue(FileUtils.contains(new File("/sdcard/"), new File("/sdcard/")));
251
252 assertTrue(FileUtils.contains(new File("/sdcard"), new File("/sdcard/moo.txt")));
253 assertTrue(FileUtils.contains(new File("/sdcard/"), new File("/sdcard/moo.txt")));
254
255 assertFalse(FileUtils.contains(new File("/sdcard"), new File("/moo.txt")));
256 assertFalse(FileUtils.contains(new File("/sdcard/"), new File("/moo.txt")));
257
258 assertFalse(FileUtils.contains(new File("/sdcard"), new File("/sdcard.txt")));
259 assertFalse(FileUtils.contains(new File("/sdcard/"), new File("/sdcard.txt")));
260 }
261
Jeff Sharkey20356142017-12-06 15:22:05 -0700262 @Test
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700263 public void testDeleteOlderEmptyDir() throws Exception {
264 FileUtils.deleteOlderFiles(mDir, 10, WEEK_IN_MILLIS);
265 assertDirContents();
266 }
267
Jeff Sharkey20356142017-12-06 15:22:05 -0700268 @Test
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700269 public void testDeleteOlderTypical() throws Exception {
270 touch("file1", HOUR_IN_MILLIS);
271 touch("file2", 1 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
272 touch("file3", 2 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
273 touch("file4", 3 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
274 touch("file5", 4 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -0800275 assertTrue(FileUtils.deleteOlderFiles(mDir, 3, DAY_IN_MILLIS));
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700276 assertDirContents("file1", "file2", "file3");
277 }
278
Jeff Sharkey20356142017-12-06 15:22:05 -0700279 @Test
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700280 public void testDeleteOlderInFuture() throws Exception {
281 touch("file1", -HOUR_IN_MILLIS);
282 touch("file2", HOUR_IN_MILLIS);
283 touch("file3", WEEK_IN_MILLIS);
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -0800284 assertTrue(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS));
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700285 assertDirContents("file1", "file2");
286
287 touch("file1", -HOUR_IN_MILLIS);
288 touch("file2", HOUR_IN_MILLIS);
289 touch("file3", WEEK_IN_MILLIS);
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -0800290 assertTrue(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS));
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700291 assertDirContents("file1", "file2");
292 }
293
Jeff Sharkey20356142017-12-06 15:22:05 -0700294 @Test
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700295 public void testDeleteOlderOnlyAge() throws Exception {
296 touch("file1", HOUR_IN_MILLIS);
297 touch("file2", 1 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
298 touch("file3", 2 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
299 touch("file4", 3 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
300 touch("file5", 4 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -0800301 assertTrue(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS));
302 assertFalse(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS));
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700303 assertDirContents("file1");
304 }
305
Jeff Sharkey20356142017-12-06 15:22:05 -0700306 @Test
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700307 public void testDeleteOlderOnlyCount() throws Exception {
308 touch("file1", HOUR_IN_MILLIS);
309 touch("file2", 1 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
310 touch("file3", 2 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
311 touch("file4", 3 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
312 touch("file5", 4 * DAY_IN_MILLIS + HOUR_IN_MILLIS);
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -0800313 assertTrue(FileUtils.deleteOlderFiles(mDir, 2, 0));
314 assertFalse(FileUtils.deleteOlderFiles(mDir, 2, 0));
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700315 assertDirContents("file1", "file2");
316 }
317
Jeff Sharkey20356142017-12-06 15:22:05 -0700318 @Test
Jeff Sharkey0cce5352014-11-26 13:38:26 -0800319 public void testValidExtFilename() throws Exception {
320 assertTrue(FileUtils.isValidExtFilename("a"));
321 assertTrue(FileUtils.isValidExtFilename("foo.bar"));
322 assertTrue(FileUtils.isValidExtFilename("foo bar.baz"));
323 assertTrue(FileUtils.isValidExtFilename("foo.bar.baz"));
324 assertTrue(FileUtils.isValidExtFilename(".bar"));
325 assertTrue(FileUtils.isValidExtFilename("foo~!@#$%^&*()_[]{}+bar"));
326
327 assertFalse(FileUtils.isValidExtFilename(null));
328 assertFalse(FileUtils.isValidExtFilename("."));
329 assertFalse(FileUtils.isValidExtFilename("../foo"));
330 assertFalse(FileUtils.isValidExtFilename("/foo"));
331
332 assertEquals(".._foo", FileUtils.buildValidExtFilename("../foo"));
333 assertEquals("_foo", FileUtils.buildValidExtFilename("/foo"));
334 assertEquals("foo_bar", FileUtils.buildValidExtFilename("foo\0bar"));
335 assertEquals(".foo", FileUtils.buildValidExtFilename(".foo"));
336 assertEquals("foo.bar", FileUtils.buildValidExtFilename("foo.bar"));
337 }
338
Jeff Sharkey20356142017-12-06 15:22:05 -0700339 @Test
Jeff Sharkey0cce5352014-11-26 13:38:26 -0800340 public void testValidFatFilename() throws Exception {
341 assertTrue(FileUtils.isValidFatFilename("a"));
342 assertTrue(FileUtils.isValidFatFilename("foo bar.baz"));
343 assertTrue(FileUtils.isValidFatFilename("foo.bar.baz"));
344 assertTrue(FileUtils.isValidFatFilename(".bar"));
345 assertTrue(FileUtils.isValidFatFilename("foo.bar"));
346 assertTrue(FileUtils.isValidFatFilename("foo bar"));
347 assertTrue(FileUtils.isValidFatFilename("foo+bar"));
348 assertTrue(FileUtils.isValidFatFilename("foo,bar"));
349
350 assertFalse(FileUtils.isValidFatFilename("foo*bar"));
351 assertFalse(FileUtils.isValidFatFilename("foo?bar"));
352 assertFalse(FileUtils.isValidFatFilename("foo<bar"));
353 assertFalse(FileUtils.isValidFatFilename(null));
354 assertFalse(FileUtils.isValidFatFilename("."));
355 assertFalse(FileUtils.isValidFatFilename("../foo"));
356 assertFalse(FileUtils.isValidFatFilename("/foo"));
357
358 assertEquals(".._foo", FileUtils.buildValidFatFilename("../foo"));
359 assertEquals("_foo", FileUtils.buildValidFatFilename("/foo"));
360 assertEquals(".foo", FileUtils.buildValidFatFilename(".foo"));
361 assertEquals("foo.bar", FileUtils.buildValidFatFilename("foo.bar"));
362 assertEquals("foo_bar__baz", FileUtils.buildValidFatFilename("foo?bar**baz"));
363 }
364
Jeff Sharkey20356142017-12-06 15:22:05 -0700365 @Test
Jeff Sharkey4f5e8b32015-06-11 19:13:37 -0700366 public void testTrimFilename() throws Exception {
367 assertEquals("short.txt", FileUtils.trimFilename("short.txt", 16));
368 assertEquals("extrem...eme.txt", FileUtils.trimFilename("extremelylongfilename.txt", 16));
369
370 final String unicode = "a\u03C0\u03C0\u03C0\u03C0z";
371 assertEquals("a\u03C0\u03C0\u03C0\u03C0z", FileUtils.trimFilename(unicode, 10));
372 assertEquals("a\u03C0...\u03C0z", FileUtils.trimFilename(unicode, 9));
373 assertEquals("a...\u03C0z", FileUtils.trimFilename(unicode, 8));
374 assertEquals("a...\u03C0z", FileUtils.trimFilename(unicode, 7));
375 assertEquals("a...z", FileUtils.trimFilename(unicode, 6));
376 }
377
Jeff Sharkey20356142017-12-06 15:22:05 -0700378 @Test
Ben Kwa62539a22015-04-22 15:43:17 -0700379 public void testBuildUniqueFile_normal() throws Exception {
380 assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test"));
381 assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
382 assertNameEquals("test.jpeg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpeg"));
383 assertNameEquals("TEst.JPeg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "TEst.JPeg"));
384 assertNameEquals("test.png.jpg",
385 FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.png.jpg"));
386 assertNameEquals("test.png.jpg",
387 FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.png"));
388
389 assertNameEquals("test.flac", FileUtils.buildUniqueFile(mTarget, "audio/flac", "test"));
390 assertNameEquals("test.flac", FileUtils.buildUniqueFile(mTarget, "audio/flac", "test.flac"));
391 assertNameEquals("test.flac",
392 FileUtils.buildUniqueFile(mTarget, "application/x-flac", "test"));
393 assertNameEquals("test.flac",
394 FileUtils.buildUniqueFile(mTarget, "application/x-flac", "test.flac"));
395 }
396
Jeff Sharkey20356142017-12-06 15:22:05 -0700397 @Test
Ben Kwa62539a22015-04-22 15:43:17 -0700398 public void testBuildUniqueFile_unknown() throws Exception {
399 assertNameEquals("test",
400 FileUtils.buildUniqueFile(mTarget, "application/octet-stream", "test"));
401 assertNameEquals("test.jpg",
402 FileUtils.buildUniqueFile(mTarget, "application/octet-stream", "test.jpg"));
403 assertNameEquals(".test",
404 FileUtils.buildUniqueFile(mTarget, "application/octet-stream", ".test"));
405
406 assertNameEquals("test", FileUtils.buildUniqueFile(mTarget, "lolz/lolz", "test"));
407 assertNameEquals("test.lolz", FileUtils.buildUniqueFile(mTarget, "lolz/lolz", "test.lolz"));
408 }
409
Jeff Sharkey20356142017-12-06 15:22:05 -0700410 @Test
Ben Kwa62539a22015-04-22 15:43:17 -0700411 public void testBuildUniqueFile_dir() throws Exception {
412 assertNameEquals("test", FileUtils.buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test"));
413 new File(mTarget, "test").mkdir();
414 assertNameEquals("test (1)",
415 FileUtils.buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test"));
416
417 assertNameEquals("test.jpg",
418 FileUtils.buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test.jpg"));
419 new File(mTarget, "test.jpg").mkdir();
420 assertNameEquals("test.jpg (1)",
421 FileUtils.buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test.jpg"));
422 }
423
Jeff Sharkey20356142017-12-06 15:22:05 -0700424 @Test
Ben Kwa62539a22015-04-22 15:43:17 -0700425 public void testBuildUniqueFile_increment() throws Exception {
426 assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
427 new File(mTarget, "test.jpg").createNewFile();
428 assertNameEquals("test (1).jpg",
429 FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
430 new File(mTarget, "test (1).jpg").createNewFile();
431 assertNameEquals("test (2).jpg",
432 FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
433 }
434
Jeff Sharkey20356142017-12-06 15:22:05 -0700435 @Test
Jeff Sharkeyaa444762016-09-20 18:54:46 -0600436 public void testBuildUniqueFile_mimeless() throws Exception {
437 assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "test.jpg"));
438 new File(mTarget, "test.jpg").createNewFile();
439 assertNameEquals("test (1).jpg", FileUtils.buildUniqueFile(mTarget, "test.jpg"));
440
441 assertNameEquals("test", FileUtils.buildUniqueFile(mTarget, "test"));
442 new File(mTarget, "test").createNewFile();
443 assertNameEquals("test (1)", FileUtils.buildUniqueFile(mTarget, "test"));
444
445 assertNameEquals("test.foo.bar", FileUtils.buildUniqueFile(mTarget, "test.foo.bar"));
446 new File(mTarget, "test.foo.bar").createNewFile();
447 assertNameEquals("test.foo (1).bar", FileUtils.buildUniqueFile(mTarget, "test.foo.bar"));
448 }
449
Jeff Sharkey20356142017-12-06 15:22:05 -0700450 @Test
Jeff Sharkey373d0172017-02-22 15:47:27 -0700451 public void testRoundStorageSize() throws Exception {
Jeff Sharkey09734df2017-03-07 20:50:27 -0700452 final long M128 = 128000000L;
453 final long M256 = 256000000L;
454 final long M512 = 512000000L;
455 final long G1 = 1000000000L;
456 final long G2 = 2000000000L;
457 final long G16 = 16000000000L;
458 final long G32 = 32000000000L;
459 final long G64 = 64000000000L;
Jeff Sharkey373d0172017-02-22 15:47:27 -0700460
Jeff Sharkey09734df2017-03-07 20:50:27 -0700461 assertEquals(M128, roundStorageSize(M128));
462 assertEquals(M256, roundStorageSize(M128 + 1));
463 assertEquals(M256, roundStorageSize(M256 - 1));
464 assertEquals(M256, roundStorageSize(M256));
465 assertEquals(M512, roundStorageSize(M256 + 1));
466 assertEquals(M512, roundStorageSize(M512 - 1));
467 assertEquals(M512, roundStorageSize(M512));
468 assertEquals(G1, roundStorageSize(M512 + 1));
469 assertEquals(G1, roundStorageSize(G1));
470 assertEquals(G2, roundStorageSize(G1 + 1));
Jeff Sharkey373d0172017-02-22 15:47:27 -0700471
Jeff Sharkey09734df2017-03-07 20:50:27 -0700472 assertEquals(G16, roundStorageSize(G16));
473 assertEquals(G32, roundStorageSize(G16 + 1));
474 assertEquals(G32, roundStorageSize(G32 - 1));
475 assertEquals(G32, roundStorageSize(G32));
476 assertEquals(G64, roundStorageSize(G32 + 1));
Jeff Sharkey373d0172017-02-22 15:47:27 -0700477 }
478
Ben Kwa62539a22015-04-22 15:43:17 -0700479 private static void assertNameEquals(String expected, File actual) {
480 assertEquals(expected, actual.getName());
481 }
482
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700483 private void touch(String name, long age) throws Exception {
484 final File file = new File(mDir, name);
485 file.createNewFile();
486 file.setLastModified(System.currentTimeMillis() - age);
487 }
488
Jeff Sharkeyb18f8992018-01-31 21:47:09 -0700489 private void writeFile(File file, String data) throws Exception {
490 writeFile(file, data.getBytes());
491 }
492
493 private void writeFile(File file, byte[] data) throws Exception {
494 try (FileOutputStream out = new FileOutputStream(file)) {
495 out.write(data);
496 }
497 }
498
499 private byte[] readFile(File file) throws Exception {
500 try (FileInputStream in = new FileInputStream(file);
501 ByteArrayOutputStream out = new ByteArrayOutputStream()) {
502 Streams.copy(in, out);
503 return out.toByteArray();
Jeff Sharkeyd9526902013-03-14 14:11:57 -0700504 }
505 }
506
507 private void assertDirContents(String... expected) {
508 final HashSet<String> expectedSet = Sets.newHashSet(expected);
509 String[] actual = mDir.list();
510 if (actual == null) actual = new String[0];
511
512 assertEquals(
513 "Expected " + Arrays.toString(expected) + " but actual " + Arrays.toString(actual),
514 expected.length, actual.length);
515 for (String actualFile : actual) {
516 assertTrue("Unexpected actual file " + actualFile, expectedSet.contains(actualFile));
517 }
518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519}