From 1d30c1f4e298bd217bf3c1772fb6eb5b24d63061 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 16 Jan 2025 11:21:13 -0300 Subject: [PATCH] [download] add tests for AutoVerifyingInputStream --- .../glide/AutoVerifyingInputStreamTest.kt | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 libs/download/src/androidUnitTest/kotlin/org/fdroid/download/glide/AutoVerifyingInputStreamTest.kt diff --git a/libs/download/src/androidUnitTest/kotlin/org/fdroid/download/glide/AutoVerifyingInputStreamTest.kt b/libs/download/src/androidUnitTest/kotlin/org/fdroid/download/glide/AutoVerifyingInputStreamTest.kt new file mode 100644 index 000000000..942e16ef1 --- /dev/null +++ b/libs/download/src/androidUnitTest/kotlin/org/fdroid/download/glide/AutoVerifyingInputStreamTest.kt @@ -0,0 +1,69 @@ +package org.fdroid.download.glide + +import java.io.ByteArrayInputStream +import java.io.IOException +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +internal class AutoVerifyingInputStreamTest { + + private val testBytes = """ + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + this is test data for the AutoVerifyingInputStream + """.trimIndent().toByteArray() + private val testHash = "784973023c9e3f32a750f3bc566bb13ee3f46b3811c2f269e2d11b47f07d1dab" + + @Test + fun testSuccess() { + AutoVerifyingInputStream( + inputStream = ByteArrayInputStream(testBytes), + expectedHash = testHash, + ).use { inputStream -> + assertContentEquals(testBytes, inputStream.readBytes()) + } + } + + @Test + fun testHashMismatch() { + val e = assertFailsWith { + AutoVerifyingInputStream( + inputStream = ByteArrayInputStream(testBytes), + expectedHash = "684973023c9e3f32a750f3bc566bb13ee3f46b3811c2f269e2d11b47f07d1dab", + ).use { inputStream -> + inputStream.readBytes() + } + } + assertEquals("Hash not matching.", e.message) + } + + @Test + fun testMaxBytesToRead() { + val e = assertFailsWith { + AutoVerifyingInputStream( + inputStream = ByteArrayInputStream(testBytes), + expectedHash = testHash, + maxBytesToRead = 42, + ).use { inputStream -> + inputStream.readBytes() + } + } + assertEquals("Read ${testBytes.size} bytes, above maximum allowed.", e.message) + } + +}