CacheSpan and Util.un/escapeFileName tests.

Fixed CacheSpan regexp for line breaks characters in key.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129631446
This commit is contained in:
eguven 2016-08-08 08:20:59 -07:00 committed by Oliver Woodman
parent b15ceba780
commit bd37216902
4 changed files with 103 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -27,9 +27,9 @@ public final class CacheSpan implements Comparable<CacheSpan> {
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.

View File

@ -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.
*