diff --git a/briar-android/src/androidTestOfficial/assets/gimp_crash.gif b/briar-android/src/androidTestOfficial/assets/gimp_crash.gif new file mode 100644 index 000000000..f55d45417 Binary files /dev/null and b/briar-android/src/androidTestOfficial/assets/gimp_crash.gif differ diff --git a/briar-android/src/androidTestOfficial/assets/image_io_crash.png b/briar-android/src/androidTestOfficial/assets/image_io_crash.png new file mode 100644 index 000000000..36c456b75 Binary files /dev/null and b/briar-android/src/androidTestOfficial/assets/image_io_crash.png differ diff --git a/briar-android/src/androidTestOfficial/assets/kitten.png b/briar-android/src/androidTestOfficial/assets/kitten.png new file mode 100644 index 000000000..5f5d43791 Binary files /dev/null and b/briar-android/src/androidTestOfficial/assets/kitten.png differ diff --git a/briar-android/src/androidTestOfficial/assets/kitten_original.jpg b/briar-android/src/androidTestOfficial/assets/kitten_original.jpg new file mode 100644 index 000000000..1f3838ff3 Binary files /dev/null and b/briar-android/src/androidTestOfficial/assets/kitten_original.jpg differ diff --git a/briar-android/src/androidTestOfficial/assets/kitten_small.jpg b/briar-android/src/androidTestOfficial/assets/kitten_small.jpg new file mode 100644 index 000000000..a8ecd92d7 Binary files /dev/null and b/briar-android/src/androidTestOfficial/assets/kitten_small.jpg differ diff --git a/briar-android/src/androidTestOfficial/assets/libraw_error.jpg b/briar-android/src/androidTestOfficial/assets/libraw_error.jpg new file mode 100644 index 000000000..0f3bbd0a7 Binary files /dev/null and b/briar-android/src/androidTestOfficial/assets/libraw_error.jpg differ diff --git a/briar-android/src/androidTestOfficial/assets/lottapixel.jpg b/briar-android/src/androidTestOfficial/assets/lottapixel.jpg new file mode 100644 index 000000000..e25c2d301 Binary files /dev/null and b/briar-android/src/androidTestOfficial/assets/lottapixel.jpg differ diff --git a/briar-android/src/androidTestOfficial/assets/opti_png_afl.gif b/briar-android/src/androidTestOfficial/assets/opti_png_afl.gif new file mode 100644 index 000000000..c4fbe309b Binary files /dev/null and b/briar-android/src/androidTestOfficial/assets/opti_png_afl.gif differ diff --git a/briar-android/src/androidTestOfficial/assets/uber.gif b/briar-android/src/androidTestOfficial/assets/uber.gif new file mode 100644 index 000000000..a7a6d7d1b Binary files /dev/null and b/briar-android/src/androidTestOfficial/assets/uber.gif differ diff --git a/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentRetrieverIntegrationTest.java b/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentRetrieverIntegrationTest.java index aa21c0913..55e847ce0 100644 --- a/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentRetrieverIntegrationTest.java +++ b/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentRetrieverIntegrationTest.java @@ -1,7 +1,5 @@ package org.briarproject.briar.android.attachment; -import android.content.res.AssetManager; - import org.briarproject.bramble.api.UniqueId; import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.briar.api.messaging.Attachment; @@ -9,14 +7,12 @@ import org.briarproject.briar.api.messaging.AttachmentHeader; import org.junit.Test; import org.junit.runner.RunWith; -import java.io.IOException; import java.io.InputStream; -import java.net.URL; import java.util.Random; import androidx.test.ext.junit.runners.AndroidJUnit4; -import static androidx.test.core.app.ApplicationProvider.getApplicationContext; +import static androidx.test.InstrumentationRegistry.getContext; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -24,25 +20,6 @@ import static org.junit.Assert.assertTrue; @RunWith(AndroidJUnit4.class) public class AttachmentRetrieverIntegrationTest { - private static final String smallKitten = - "https://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Kitten_in_Rizal_Park%2C_Manila.jpg/160px-Kitten_in_Rizal_Park%2C_Manila.jpg"; - private static final String originalKitten = - "https://upload.wikimedia.org/wikipedia/commons/0/06/Kitten_in_Rizal_Park%2C_Manila.jpg"; - private static final String pngKitten = - "https://upload.wikimedia.org/wikipedia/commons/c/c8/Young_cat.png"; - private static final String uberGif = - "https://raw.githubusercontent.com/fuzzdb-project/fuzzdb/master/attack/file-upload/malicious-images/uber.gif"; - private static final String lottaPixel = - "https://raw.githubusercontent.com/fuzzdb-project/fuzzdb/master/attack/file-upload/malicious-images/lottapixel.jpg"; - private static final String imageIoCrash = - "https://www.landaire.net/img/crasher.png"; - private static final String gimpCrash = - "https://gitlab.gnome.org/GNOME/gimp/uploads/75f5b7ed3b09b3f1c13f1f65bffe784f/31153c919d3aa634e8e6cff82219fe7352dd8a37.png"; - private static final String optiPngAfl = - "https://sourceforge.net/p/optipng/bugs/64/attachment/test.gif"; - private static final String librawError = - "https://www.libraw.org/sites/libraw.org/files/P1010671.JPG"; - private final AttachmentDimensions dimensions = new AttachmentDimensions( 100, 50, 200, 75, 300 ); @@ -56,7 +33,7 @@ public class AttachmentRetrieverIntegrationTest { @Test public void testSmallJpegImage() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); - InputStream is = getUrlInputStream(smallKitten); + InputStream is = getAssetInputStream("kitten_small.jpg"); Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(a, true); assertEquals(msgId, item.getMessageId()); @@ -65,14 +42,14 @@ public class AttachmentRetrieverIntegrationTest { assertEquals(160, item.getThumbnailWidth()); assertEquals(240, item.getThumbnailHeight()); assertEquals("image/jpeg", item.getMimeType()); - assertEquals("jpg", item.getExtension()); + assertJpgOrJpeg(item.getExtension()); assertFalse(item.hasError()); } @Test public void testBigJpegImage() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); - InputStream is = getUrlInputStream(originalKitten); + InputStream is = getAssetInputStream("kitten_original.jpg"); Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(a, true); assertEquals(msgId, item.getMessageId()); @@ -81,14 +58,14 @@ public class AttachmentRetrieverIntegrationTest { assertEquals(dimensions.maxWidth, item.getThumbnailWidth()); assertEquals(dimensions.maxHeight, item.getThumbnailHeight()); assertEquals("image/jpeg", item.getMimeType()); - assertEquals("jpg", item.getExtension()); + assertJpgOrJpeg(item.getExtension()); assertFalse(item.hasError()); } @Test public void testSmallPngImage() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/png"); - InputStream is = getUrlInputStream(pngKitten); + InputStream is = getAssetInputStream("kitten.png"); Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(a, true); assertEquals(msgId, item.getMessageId()); @@ -103,8 +80,8 @@ public class AttachmentRetrieverIntegrationTest { @Test public void testUberGif() throws Exception { - AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); - InputStream is = getUrlInputStream(uberGif); + AttachmentHeader h = new AttachmentHeader(msgId, "image/gif"); + InputStream is = getAssetInputStream("uber.gif"); Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(a, true); assertEquals(1, item.getWidth()); @@ -119,7 +96,7 @@ public class AttachmentRetrieverIntegrationTest { @Test public void testLottaPixels() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); - InputStream is = getUrlInputStream(lottaPixel); + InputStream is = getAssetInputStream("lottapixel.jpg"); Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(a, true); assertEquals(64250, item.getWidth()); @@ -127,14 +104,14 @@ public class AttachmentRetrieverIntegrationTest { assertEquals(dimensions.maxWidth, item.getThumbnailWidth()); assertEquals(dimensions.maxWidth, item.getThumbnailHeight()); assertEquals("image/jpeg", item.getMimeType()); - assertEquals("jpg", item.getExtension()); + assertJpgOrJpeg(item.getExtension()); assertFalse(item.hasError()); } @Test public void testImageIoCrash() throws Exception { - AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); - InputStream is = getUrlInputStream(imageIoCrash); + AttachmentHeader h = new AttachmentHeader(msgId, "image/png"); + InputStream is = getAssetInputStream("image_io_crash.png"); Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(a, true); assertEquals(1184, item.getWidth()); @@ -148,8 +125,8 @@ public class AttachmentRetrieverIntegrationTest { @Test public void testGimpCrash() throws Exception { - AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); - InputStream is = getUrlInputStream(gimpCrash); + AttachmentHeader h = new AttachmentHeader(msgId, "image/gif"); + InputStream is = getAssetInputStream("gimp_crash.gif"); Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(a, true); assertEquals(1, item.getWidth()); @@ -163,8 +140,8 @@ public class AttachmentRetrieverIntegrationTest { @Test public void testOptiPngAfl() throws Exception { - AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); - InputStream is = getUrlInputStream(optiPngAfl); + AttachmentHeader h = new AttachmentHeader(msgId, "image/gif"); + InputStream is = getAssetInputStream("opti_png_afl.gif"); Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(a, true); assertEquals(32, item.getWidth()); @@ -179,7 +156,7 @@ public class AttachmentRetrieverIntegrationTest { @Test public void testLibrawError() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); - InputStream is = getUrlInputStream(librawError); + InputStream is = getAssetInputStream("libraw_error.jpg"); Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(a, true); assertTrue(item.hasError()); @@ -241,7 +218,7 @@ public class AttachmentRetrieverIntegrationTest { assertEquals(dimensions.minWidth, item.getThumbnailWidth()); assertEquals(dimensions.maxHeight, item.getThumbnailHeight()); assertEquals("image/jpeg", item.getMimeType()); - assertEquals("jpg", item.getExtension()); + assertJpgOrJpeg(item.getExtension()); assertFalse(item.hasError()); } @@ -256,17 +233,18 @@ public class AttachmentRetrieverIntegrationTest { assertEquals(dimensions.maxWidth, item.getThumbnailWidth()); assertEquals(dimensions.minHeight, item.getThumbnailHeight()); assertEquals("image/jpeg", item.getMimeType()); - assertEquals("jpg", item.getExtension()); + assertJpgOrJpeg(item.getExtension()); assertFalse(item.hasError()); } - private InputStream getUrlInputStream(String url) throws IOException { - return new URL(url).openStream(); + private InputStream getAssetInputStream(String name) throws Exception { + // pm.getResourcesForApplication(packageName).getAssets() did not work + //noinspection deprecation + return getContext().getAssets().open(name); } - private InputStream getAssetInputStream(String name) throws IOException { - AssetManager assets = getApplicationContext().getAssets(); - return assets.open(name); + private void assertJpgOrJpeg(String extension) { + assertTrue("jpg".equals(extension) || "jpeg".equals(extension)); } public static byte[] getRandomBytes(int length) {