diff --git a/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheSpanTest.java b/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheSpanTest.java new file mode 100644 index 0000000000..38008c814e --- /dev/null +++ b/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheSpanTest.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.upstream.cache; + +import com.google.android.exoplayer2.testutil.TestUtil; +import java.io.File; +import java.util.Random; +import junit.framework.TestCase; + +/** + * Unit tests for {@link CacheSpan}. + */ +public class CacheSpanTest extends TestCase { + + public void testCacheFile() throws Exception { + assertCacheSpan(new File("parent"), "key", 0, 0); + assertCacheSpan(new File("parent/"), "key", 1, 2); + assertCacheSpan(new File("parent"), "<>:\"/\\|?*%", 1, 2); + assertCacheSpan(new File("/"), "key", 1, 2); + + assertNullCacheSpan(new File("parent"), "", 1, 2); + assertNullCacheSpan(new File("parent"), "key", -1, 2); + assertNullCacheSpan(new File("parent"), "key", 1, -2); + + assertNotNull(CacheSpan.createCacheEntry(new File("/asd%aa.1.2.v2.exo"))); + assertNull(CacheSpan.createCacheEntry(new File("/asd%za.1.2.v2.exo"))); + + assertCacheSpan(new File("parent"), + "A newline (line feed) character \n" + + "A carriage-return character followed immediately by a newline character \r\n" + + "A standalone carriage-return character \r" + + "A next-line character \u0085" + + "A line-separator character \u2028" + + "A paragraph-separator character \u2029", 1, 2); + } + + public void testCacheFileNameRandomData() throws Exception { + Random random = new Random(0); + File parent = new File("parent"); + for (int i = 0; i < 1000; i++) { + String key = TestUtil.buildTestString(1000, random); + long offset = Math.abs(random.nextLong()); + long lastAccessTimestamp = Math.abs(random.nextLong()); + assertCacheSpan(parent, key, offset, lastAccessTimestamp); + } + } + + private void assertCacheSpan(File parent, String key, long offset, long lastAccessTimestamp) { + File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp); + CacheSpan cacheSpan = CacheSpan.createCacheEntry(cacheFile); + String message = cacheFile.toString(); + assertNotNull(message, cacheSpan); + assertEquals(message, parent, cacheFile.getParentFile()); + assertEquals(message, key, cacheSpan.key); + assertEquals(message, offset, cacheSpan.position); + assertEquals(message, lastAccessTimestamp, cacheSpan.lastAccessTimestamp); + } + + private void assertNullCacheSpan(File parent, String key, long offset, + long lastAccessTimestamp) { + File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp); + CacheSpan cacheSpan = CacheSpan.createCacheEntry(cacheFile); + assertNull(cacheFile.toString(), cacheSpan); + } + +} diff --git a/library/src/androidTest/java/com/google/android/exoplayer2/util/UtilTest.java b/library/src/androidTest/java/com/google/android/exoplayer2/util/UtilTest.java index af857c6e6b..e3d681d6dd 100644 --- a/library/src/androidTest/java/com/google/android/exoplayer2/util/UtilTest.java +++ b/library/src/androidTest/java/com/google/android/exoplayer2/util/UtilTest.java @@ -15,9 +15,11 @@ */ package com.google.android.exoplayer2.util; +import com.google.android.exoplayer2.testutil.TestUtil; import java.text.ParseException; import java.util.ArrayList; import java.util.List; +import java.util.Random; import junit.framework.TestCase; /** @@ -152,6 +154,12 @@ public class UtilTest extends TestCase { assertEscapeUnescapeFileName("just+a regular+fileName", "just+a regular+fileName"); assertEscapeUnescapeFileName("key:value", "key%3avalue"); assertEscapeUnescapeFileName("<>:\"/\\|?*%", "%3c%3e%3a%22%2f%5c%7c%3f%2a%25"); + + Random random = new Random(0); + for (int i = 0; i < 1000; i++) { + String string = TestUtil.buildTestString(1000, random); + assertEscapeUnescapeFileName(string); + } } private static void assertEscapeUnescapeFileName(String fileName, String escapedFileName) { @@ -159,4 +167,9 @@ public class UtilTest extends TestCase { assertEquals(fileName, Util.unescapeFileName(escapedFileName)); } + private static void assertEscapeUnescapeFileName(String fileName) { + String escapedFileName = Util.escapeFileName(fileName); + assertEquals(fileName, Util.unescapeFileName(escapedFileName)); + } + } diff --git a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheSpan.java b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheSpan.java index aba5677bee..363e19a1d5 100644 --- a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheSpan.java +++ b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheSpan.java @@ -27,9 +27,9 @@ public final class CacheSpan implements Comparable { private static final String SUFFIX = ".v2.exo"; private static final Pattern CACHE_FILE_PATTERN_V1 = - Pattern.compile("^(.+)\\.(\\d+)\\.(\\d+)\\.v1\\.exo$"); + Pattern.compile("^(.+)\\.(\\d+)\\.(\\d+)\\.v1\\.exo$", Pattern.DOTALL); private static final Pattern CACHE_FILE_PATTERN_V2 = - Pattern.compile("^(.+)\\.(\\d+)\\.(\\d+)\\.v2\\.exo$"); + Pattern.compile("^(.+)\\.(\\d+)\\.(\\d+)\\.v2\\.exo$", Pattern.DOTALL); /** * The cache key that uniquely identifies the original stream. diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java index 5c8de99842..bf4b4faccb 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java @@ -131,6 +131,15 @@ public class TestUtil { return source; } + public static String buildTestString(int maxLength, Random random) { + int length = random.nextInt(maxLength); + StringBuilder builder = new StringBuilder(length); + for (int i = 0; i < length; i++) { + builder.append((char) random.nextInt()); + } + return builder.toString(); + } + /** * Converts an array of integers in the range [0, 255] into an equivalent byte array. *