The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Nguyen | 1a44d5d | 2010-01-13 10:42:43 -0800 | [diff] [blame] | 17 | package android.os; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Jeff Sharkey | 09734df | 2017-03-07 20:50:27 -0700 | [diff] [blame] | 19 | import static android.os.FileUtils.roundStorageSize; |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 20 | import static android.text.format.DateUtils.DAY_IN_MILLIS; |
| 21 | import static android.text.format.DateUtils.HOUR_IN_MILLIS; |
| 22 | import static android.text.format.DateUtils.WEEK_IN_MILLIS; |
| 23 | |
Jeff Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 24 | import static org.junit.Assert.assertArrayEquals; |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertFalse; |
| 27 | import static org.junit.Assert.assertTrue; |
| 28 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Jeff Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 30 | import android.os.FileUtils.MemoryPipe; |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 31 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 32 | import android.support.test.InstrumentationRegistry; |
| 33 | import android.support.test.runner.AndroidJUnit4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
Jeff Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 35 | import libcore.io.Streams; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 36 | |
Jeff Sharkey | 09734df | 2017-03-07 20:50:27 -0700 | [diff] [blame] | 37 | import com.google.android.collect.Sets; |
| 38 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 39 | import org.junit.After; |
| 40 | import org.junit.Before; |
| 41 | import org.junit.Test; |
| 42 | import org.junit.runner.RunWith; |
| 43 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import java.io.ByteArrayInputStream; |
Jeff Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 45 | import java.io.ByteArrayOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import java.io.File; |
Jeff Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 47 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import java.io.FileOutputStream; |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 49 | import java.util.Arrays; |
| 50 | import java.util.HashSet; |
Jeff Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 51 | import java.util.Random; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 53 | @RunWith(AndroidJUnit4.class) |
| 54 | public class FileUtilsTest { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | private static final String TEST_DATA = |
| 56 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 57 | |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 58 | private File mDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | private File mTestFile; |
| 60 | private File mCopyFile; |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 61 | private File mTarget; |
| 62 | |
Jeff Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 63 | private final int[] DATA_SIZES = { 32, 32_000, 32_000_000 }; |
| 64 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 65 | private Context getContext() { |
| 66 | return InstrumentationRegistry.getContext(); |
| 67 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 69 | @Before |
| 70 | public void setUp() throws Exception { |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 71 | mDir = getContext().getDir("testing", Context.MODE_PRIVATE); |
| 72 | mTestFile = new File(mDir, "test.file"); |
| 73 | mCopyFile = new File(mDir, "copy.file"); |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 74 | |
| 75 | mTarget = getContext().getFilesDir(); |
| 76 | FileUtils.deleteContents(mTarget); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 79 | @After |
| 80 | public void tearDown() throws Exception { |
Neil Fuller | 72b61e6 | 2018-10-10 14:49:24 +0100 | [diff] [blame] | 81 | FileUtils.deleteContents(mDir); |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 82 | FileUtils.deleteContents(mTarget); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | // TODO: test setPermissions(), getPermissions() |
| 86 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 87 | @Test |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | public void testCopyFile() throws Exception { |
Jeff Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 89 | writeFile(mTestFile, TEST_DATA); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | assertFalse(mCopyFile.exists()); |
| 91 | FileUtils.copyFile(mTestFile, mCopyFile); |
| 92 | assertTrue(mCopyFile.exists()); |
| 93 | assertEquals(TEST_DATA, FileUtils.readTextFile(mCopyFile, 0, null)); |
| 94 | } |
| 95 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 96 | @Test |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | public void testCopyToFile() throws Exception { |
| 98 | final String s = "Foo Bar"; |
| 99 | assertFalse(mCopyFile.exists()); |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 100 | FileUtils.copyToFile(new ByteArrayInputStream(s.getBytes()), mCopyFile); |
| 101 | assertTrue(mCopyFile.exists()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | assertEquals(s, FileUtils.readTextFile(mCopyFile, 0, null)); |
| 103 | } |
| 104 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 105 | @Test |
Jeff Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 106 | 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 Sharkey | 45c97df | 2018-02-01 16:01:52 -0700 | [diff] [blame] | 183 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | 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 Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 211 | @Test |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | public void testReadTextFile() throws Exception { |
Jeff Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 213 | writeFile(mTestFile, TEST_DATA); |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 214 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | 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 Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 232 | @Test |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | public void testReadTextFileWithZeroLengthFile() throws Exception { |
Jeff Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 234 | writeFile(mTestFile, TEST_DATA); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | 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 Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 242 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 243 | @Test |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 244 | 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 Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 261 | @Test |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 262 | public void testDeleteOlderEmptyDir() throws Exception { |
| 263 | FileUtils.deleteOlderFiles(mDir, 10, WEEK_IN_MILLIS); |
| 264 | assertDirContents(); |
| 265 | } |
| 266 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 267 | @Test |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 268 | 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 Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 274 | assertTrue(FileUtils.deleteOlderFiles(mDir, 3, DAY_IN_MILLIS)); |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 275 | assertDirContents("file1", "file2", "file3"); |
| 276 | } |
| 277 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 278 | @Test |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 279 | public void testDeleteOlderInFuture() throws Exception { |
| 280 | touch("file1", -HOUR_IN_MILLIS); |
| 281 | touch("file2", HOUR_IN_MILLIS); |
| 282 | touch("file3", WEEK_IN_MILLIS); |
Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 283 | assertTrue(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS)); |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 284 | assertDirContents("file1", "file2"); |
| 285 | |
| 286 | touch("file1", -HOUR_IN_MILLIS); |
| 287 | touch("file2", HOUR_IN_MILLIS); |
| 288 | touch("file3", WEEK_IN_MILLIS); |
Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 289 | assertTrue(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS)); |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 290 | assertDirContents("file1", "file2"); |
| 291 | } |
| 292 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 293 | @Test |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 294 | 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 Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 300 | assertTrue(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS)); |
| 301 | assertFalse(FileUtils.deleteOlderFiles(mDir, 0, DAY_IN_MILLIS)); |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 302 | assertDirContents("file1"); |
| 303 | } |
| 304 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 305 | @Test |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 306 | 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 Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 312 | assertTrue(FileUtils.deleteOlderFiles(mDir, 2, 0)); |
| 313 | assertFalse(FileUtils.deleteOlderFiles(mDir, 2, 0)); |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 314 | assertDirContents("file1", "file2"); |
| 315 | } |
| 316 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 317 | @Test |
Jeff Sharkey | 0cce535 | 2014-11-26 13:38:26 -0800 | [diff] [blame] | 318 | 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 Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 338 | @Test |
Jeff Sharkey | 0cce535 | 2014-11-26 13:38:26 -0800 | [diff] [blame] | 339 | 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 Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 364 | @Test |
Jeff Sharkey | 4f5e8b3 | 2015-06-11 19:13:37 -0700 | [diff] [blame] | 365 | 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 Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 377 | @Test |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 378 | 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 Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 396 | @Test |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 397 | 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 Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 409 | @Test |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 410 | 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 Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 423 | @Test |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 424 | 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 Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 434 | @Test |
Jeff Sharkey | aa44476 | 2016-09-20 18:54:46 -0600 | [diff] [blame] | 435 | 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 Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 449 | @Test |
Jeff Sharkey | 373d017 | 2017-02-22 15:47:27 -0700 | [diff] [blame] | 450 | public void testRoundStorageSize() throws Exception { |
Jeff Sharkey | 09734df | 2017-03-07 20:50:27 -0700 | [diff] [blame] | 451 | 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 Sharkey | 373d017 | 2017-02-22 15:47:27 -0700 | [diff] [blame] | 459 | |
Jeff Sharkey | 09734df | 2017-03-07 20:50:27 -0700 | [diff] [blame] | 460 | 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 Sharkey | 373d017 | 2017-02-22 15:47:27 -0700 | [diff] [blame] | 470 | |
Jeff Sharkey | 09734df | 2017-03-07 20:50:27 -0700 | [diff] [blame] | 471 | 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 Sharkey | 373d017 | 2017-02-22 15:47:27 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 478 | private static void assertNameEquals(String expected, File actual) { |
| 479 | assertEquals(expected, actual.getName()); |
| 480 | } |
| 481 | |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 482 | 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 Sharkey | b18f899 | 2018-01-31 21:47:09 -0700 | [diff] [blame] | 488 | 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 Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 503 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | } |