aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
diff options
context:
space:
mode:
authorAnonymous <no-reply@google.com>2018-05-07 11:39:31 -0700
committerJeff Davidson <jpd@google.com>2018-05-10 20:11:47 +0000
commit9a128540048414c78a2be9524f93b6234c640b3a (patch)
treef39fa51c442a9c6922d1f6def2e75d5dca677f20 /src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
parentaee0d184daa41c0abb5bf88580ad6cf7fea63d72 (diff)
downloadvolley-9a128540048414c78a2be9524f93b6234c640b3a.tar.gz
Import of Volley from GitHub to AOSP.android-o-mr1-iot-release-1.0.1
Adds android-support-annotations as a runtime dependency in Android.mk. - 28588322fa4eca3c1a7922b8a2f7b4a97d5c099a Fix concurrent modification errors in ImageContainer. (#1... by Jeff Davidson <jpd236@cornell.edu> - ba3b40457a97b3b3812cb00191b41cb0af4149df Fix unchecked operations in Volley build. (#178) by Jeff Davidson <jpd236@cornell.edu> - 62c19019e43a76ba4a084b25548e7fb1cfd4a457 Always close HttpURLConnections in HurlStack. (#176) by Jeff Davidson <jpd236@cornell.edu> - 36c80f25a78cf2ba2feb2d697f12f9bbca96bf43 Port dispatcher tests to mockito. (#172) by Jeff Davidson <jpd236@cornell.edu> - 15b55093fc8ac86902a745f46a2bdea825e797a3 Cleaning up parameter comments + unnecessary generics. (#... by Jeff Davidson <jpd236@cornell.edu> - 6b341755e522f23de149544536b9b69c7b7b8305 Remove all wildcard imports. (#170) by Jeff Davidson <jpd236@cornell.edu> - a8424005b3ba6d17fc9e123a2176da702f3ead10 Use actual annotations instead of comments. (#169) by Jeff Davidson <jpd236@cornell.edu> - bdc80555aee738235bd33c26878bbe8ce382d795 Apply google-java-format to all code. (#165) by Jeff Davidson <jpd236@cornell.edu> - 5307293b15dd0c32f53b8f1fd8c198caa443982e Integrate Volley builds with errorprone. (#162) by Jeff Davidson <jpd236@cornell.edu> - fa586029500e6813d654f1b454cd3345b0a43897 Fail Travis builds if snapshot uploading fails. (#163) by Jeff Davidson <jpd236@cornell.edu> - 0ebb97e9b0192e879088b38b5992158e99364dc6 Stop publishing bintray-info.json. (#141) by Jeff Davidson <jpd236@cornell.edu> - b89dfbd2e6964acfe5561e268300611a83957a15 fix: request time contains RequestQueue blocking time. (#... by dezng <dezng123@gmail.com> - c2bfd86596e588c924c2c7e0ae0940053800af5c Guard against illegal argument and OOM exceptions in read... by Joe Bowbeer <joe.bowbeer@gmail.com> - fc8ff0423fe9a3af6595088b34f912fb2d920ffb Log fix (#112) by Navid Ht <navid.htb@gmail.com> - 47586f0fd42acd5ce09dfc767387df685edfa547 Bump version to 1.1.1-SNAPSHOT. (#123) by Jeff Davidson <jpd236@cornell.edu> - 1392f961af17d0f5ba367d8d199a18e5325b882e Update deploy credentials. (#122) by Jeff Davidson <jpd236@cornell.edu> - ffe9281709f38f84492ce898603d3ec084326a57 Set project group and version. (#120) by Jeff Davidson <jpd236@cornell.edu> GitOrigin-RevId: 28588322fa4eca3c1a7922b8a2f7b4a97d5c099a Change-Id: Ifeea069df2f0a5fd10bc3d4686601c88906ee653
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);