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