aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java')
-rw-r--r--src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java108
1 files changed, 67 insertions, 41 deletions
diff --git a/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java b/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
index 04c071e..fb6392c 100644
--- a/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
+++ b/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
@@ -16,21 +16,26 @@
package com.android.volley.toolbox;
+import static org.hamcrest.Matchers.arrayWithSize;
+import static org.hamcrest.Matchers.emptyArray;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
import com.android.volley.Cache;
import com.android.volley.Header;
import com.android.volley.toolbox.DiskBasedCache.CacheHeader;
import com.android.volley.toolbox.DiskBasedCache.CountingInputStream;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.TemporaryFolder;
-import org.junit.runner.RunWith;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.annotation.Config;
-
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
@@ -42,38 +47,28 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import java.util.Random;
-
-import static org.hamcrest.Matchers.arrayWithSize;
-import static org.hamcrest.Matchers.emptyArray;
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThat;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.Mockito.atLeastOnce;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.doThrow;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
-@Config(manifest="src/main/AndroidManifest.xml", sdk=16)
+@Config(manifest = "src/main/AndroidManifest.xml", sdk = 16)
public class DiskBasedCacheTest {
private static final int MAX_SIZE = 1024 * 1024;
private Cache cache;
- @Rule
- public TemporaryFolder temporaryFolder = new TemporaryFolder();
+ @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
- @Rule
- public ExpectedException exception = ExpectedException.none();
+ @Rule public ExpectedException exception = ExpectedException.none();
@Before
public void setup() throws IOException {
@@ -280,6 +275,33 @@ public class DiskBasedCacheTest {
}
@Test
+ public void testReadHeaderListWithNegativeSize() throws IOException {
+ // If a cached header list is corrupted and begins with a negative size,
+ // verify that readHeaderList will throw an IOException.
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ DiskBasedCache.writeInt(baos, -1); // negative size
+ CountingInputStream cis =
+ new CountingInputStream(
+ new ByteArrayInputStream(baos.toByteArray()), Integer.MAX_VALUE);
+ // Expect IOException due to negative size
+ exception.expect(IOException.class);
+ DiskBasedCache.readHeaderList(cis);
+ }
+
+ @Test
+ public void testReadHeaderListWithGinormousSize() throws IOException {
+ // If a cached header list is corrupted and begins with 2GB size, verify
+ // that readHeaderList will throw EOFException rather than OutOfMemoryError.
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ DiskBasedCache.writeInt(baos, Integer.MAX_VALUE); // 2GB size
+ CountingInputStream cis =
+ new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()), baos.size());
+ // Expect EOFException when end of stream is reached
+ exception.expect(EOFException.class);
+ DiskBasedCache.readHeaderList(cis);
+ }
+
+ @Test
public void testFileIsDeletedWhenWriteHeaderFails() throws IOException {
// Create DataOutputStream that throws IOException
OutputStream mockedOutputStream = spy(OutputStream.class);
@@ -314,8 +336,7 @@ public class DiskBasedCacheTest {
doThrow(IOException.class).when(mockedInputStream).read();
// Create broken cache that fails to read anything
- DiskBasedCache broken =
- spy(new DiskBasedCache(temporaryFolder.getRoot()));
+ DiskBasedCache broken = spy(new DiskBasedCache(temporaryFolder.getRoot()));
doReturn(mockedInputStream).when(broken).createInputStream(any(File.class));
// Attempt to initialize
@@ -379,13 +400,15 @@ public class DiskBasedCacheTest {
/* Serialization tests */
- @Test public void testEmptyReadThrowsEOF() throws IOException {
- ByteArrayInputStream empty = new ByteArrayInputStream(new byte[]{});
+ @Test
+ public void testEmptyReadThrowsEOF() throws IOException {
+ ByteArrayInputStream empty = new ByteArrayInputStream(new byte[] {});
exception.expect(EOFException.class);
DiskBasedCache.readInt(empty);
}
- @Test public void serializeInt() throws IOException {
+ @Test
+ public void serializeInt() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DiskBasedCache.writeInt(baos, 0);
DiskBasedCache.writeInt(baos, 19791214);
@@ -400,7 +423,8 @@ public class DiskBasedCacheTest {
assertEquals(DiskBasedCache.readInt(bais), Integer.MAX_VALUE);
}
- @Test public void serializeLong() throws Exception {
+ @Test
+ public void serializeLong() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DiskBasedCache.writeLong(baos, 0);
DiskBasedCache.writeLong(baos, 31337);
@@ -419,7 +443,8 @@ public class DiskBasedCacheTest {
assertEquals(DiskBasedCache.readLong(bais), Long.MAX_VALUE);
}
- @Test public void serializeString() throws Exception {
+ @Test
+ public void serializeString() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DiskBasedCache.writeString(baos, "");
DiskBasedCache.writeString(baos, "This is a string.");
@@ -431,7 +456,8 @@ public class DiskBasedCacheTest {
assertEquals(DiskBasedCache.readString(cis), "ファイカス");
}
- @Test public void serializeHeaders() throws Exception {
+ @Test
+ public void serializeHeaders() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
List<Header> empty = new ArrayList<>();
DiskBasedCache.writeHeaderList(empty, baos);