From 74aa62a2642bc4b05410ff9315490973272b3f34 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 20 Aug 2019 13:04:09 +0200 Subject: [PATCH] Add instrumentation test for testing AES/GCM nonces are really unique --- app/build.gradle | 9 +++++ .../backup/CipherUniqueNonceTest.kt | 33 +++++++++++++++++++ .../{Test.kt => DocumentsStorageTest.kt} | 2 +- .../java/com/stevesoltys/backup/TestUtils.kt | 4 +-- .../backup/crypto/KeyManagerTestImpl.kt | 0 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 app/src/androidTest/java/com/stevesoltys/backup/CipherUniqueNonceTest.kt rename app/src/androidTest/java/com/stevesoltys/backup/{Test.kt => DocumentsStorageTest.kt} (98%) rename app/src/{test => sharedTest}/java/com/stevesoltys/backup/TestUtils.kt (84%) rename app/src/{test => sharedTest}/java/com/stevesoltys/backup/crypto/KeyManagerTestImpl.kt (100%) diff --git a/app/build.gradle b/app/build.gradle index 88a2f425..8fe39a43 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,6 +37,15 @@ android { } } + sourceSets { + test { + java.srcDirs += "$projectDir/src/sharedTest/java" + } + androidTest { + java.srcDirs += "$projectDir/src/sharedTest/java" + } + } + // optional signingConfigs def keystorePropertiesFile = rootProject.file("keystore.properties") if (keystorePropertiesFile.exists()) { diff --git a/app/src/androidTest/java/com/stevesoltys/backup/CipherUniqueNonceTest.kt b/app/src/androidTest/java/com/stevesoltys/backup/CipherUniqueNonceTest.kt new file mode 100644 index 00000000..f2ccaba0 --- /dev/null +++ b/app/src/androidTest/java/com/stevesoltys/backup/CipherUniqueNonceTest.kt @@ -0,0 +1,33 @@ +package com.stevesoltys.backup + +import android.util.Log +import androidx.test.filters.LargeTest +import androidx.test.runner.AndroidJUnit4 +import com.stevesoltys.backup.crypto.CipherFactoryImpl +import com.stevesoltys.backup.crypto.KeyManagerTestImpl +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith + +private val TAG = CipherUniqueNonceTest::class.java.simpleName +private const val ITERATIONS = 1_000_000 + +@LargeTest +@RunWith(AndroidJUnit4::class) +class CipherUniqueNonceTest { + + private val keyManager = KeyManagerTestImpl() + private val cipherFactory = CipherFactoryImpl(keyManager) + + private val nonceSet = HashSet() + + @Test + fun testUniqueNonce() { + for (i in 1..ITERATIONS) { + val iv = cipherFactory.createEncryptionCipher().iv + Log.w(TAG, "$i: ${iv.toHexString()}") + assertTrue(nonceSet.add(iv)) + } + } + +} diff --git a/app/src/androidTest/java/com/stevesoltys/backup/Test.kt b/app/src/androidTest/java/com/stevesoltys/backup/DocumentsStorageTest.kt similarity index 98% rename from app/src/androidTest/java/com/stevesoltys/backup/Test.kt rename to app/src/androidTest/java/com/stevesoltys/backup/DocumentsStorageTest.kt index 1dafbfea..96b89f71 100644 --- a/app/src/androidTest/java/com/stevesoltys/backup/Test.kt +++ b/app/src/androidTest/java/com/stevesoltys/backup/DocumentsStorageTest.kt @@ -17,7 +17,7 @@ import kotlin.random.Random private const val filename = "test-file" @RunWith(AndroidJUnit4::class) -class AndroidUnitTest { +class DocumentsStorageTest { private val context = InstrumentationRegistry.getInstrumentation().targetContext private val folderUri = getBackupFolderUri(context) diff --git a/app/src/test/java/com/stevesoltys/backup/TestUtils.kt b/app/src/sharedTest/java/com/stevesoltys/backup/TestUtils.kt similarity index 84% rename from app/src/test/java/com/stevesoltys/backup/TestUtils.kt rename to app/src/sharedTest/java/com/stevesoltys/backup/TestUtils.kt index 6644f3b1..3802de2d 100644 --- a/app/src/test/java/com/stevesoltys/backup/TestUtils.kt +++ b/app/src/sharedTest/java/com/stevesoltys/backup/TestUtils.kt @@ -1,11 +1,9 @@ package com.stevesoltys.backup -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.Assertions.fail import kotlin.random.Random fun assertContains(stack: String?, needle: String) { - assertTrue(stack?.contains(needle) ?: fail()) + if (stack?.contains(needle) != true) throw AssertionError() } fun getRandomByteArray(size: Int = Random.nextInt(1337)) = ByteArray(size).apply { diff --git a/app/src/test/java/com/stevesoltys/backup/crypto/KeyManagerTestImpl.kt b/app/src/sharedTest/java/com/stevesoltys/backup/crypto/KeyManagerTestImpl.kt similarity index 100% rename from app/src/test/java/com/stevesoltys/backup/crypto/KeyManagerTestImpl.kt rename to app/src/sharedTest/java/com/stevesoltys/backup/crypto/KeyManagerTestImpl.kt