Make all tests use runTest coroutine wrapper

This commit is contained in:
Ugljesa Jovanovic 2021-02-09 14:45:45 +01:00
parent 8e295feb46
commit 9e46cbc7ea
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
19 changed files with 69 additions and 51 deletions

View File

@ -18,7 +18,8 @@ The libsodium binding library is not published yet, once the sample showing the
Before using the wrapper you need to initialize the underlying libsodium library. You can use either a callback or coroutines approach
```
LibsodiumInitializer.initializeWithCallback {
= runTest {
LibsodiumInitializer.initializeWithCallback {
// Libsodium initialized
}
```

View File

@ -4,6 +4,7 @@ import com.ionspin.kotlin.crypto.generichash.GenericHash
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertTrue

View File

@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.aead
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
@ -13,7 +14,7 @@ import kotlin.test.assertTrue
*/
class AuthenticatedEncryptionWithAssociatedDataTest {
@Test
fun testXChaCha20Poly1305Ieft() {
fun testXChaCha20Poly1305Ieft() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()
@ -66,7 +67,7 @@ class AuthenticatedEncryptionWithAssociatedDataTest {
}
@Test
fun testXChaCha20Poly1305IeftDetached() {
fun testXChaCha20Poly1305IeftDetached() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()
@ -121,7 +122,7 @@ class AuthenticatedEncryptionWithAssociatedDataTest {
}
@Test
fun testChaCha20Poly1305Ieft() {
fun testChaCha20Poly1305Ieft() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()
@ -173,7 +174,7 @@ class AuthenticatedEncryptionWithAssociatedDataTest {
}
@Test
fun testChaCha20Poly1305IeftDetached() {
fun testChaCha20Poly1305IeftDetached() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()
@ -227,7 +228,7 @@ class AuthenticatedEncryptionWithAssociatedDataTest {
}
@Test
fun testChaCha20Poly1305() {
fun testChaCha20Poly1305() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()
@ -278,7 +279,7 @@ class AuthenticatedEncryptionWithAssociatedDataTest {
}
@Test
fun testChaCha20Poly1305Detached() {
fun testChaCha20Poly1305Detached() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()

View File

@ -4,6 +4,7 @@ import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import com.ionspin.kotlin.crypto.util.toHexString
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
@ -15,7 +16,7 @@ import kotlin.test.assertTrue
*/
class AuthTest {
@Test
fun testAuth() {
fun testAuth() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("I wonder if it would be possible" +
" to get some lyrics in these tests").encodeToUByteArray()
@ -41,7 +42,7 @@ class AuthTest {
}
@Test
fun testAuthHmacSha256() {
fun testAuthHmacSha256() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("I wonder if it would be possible" +
" to get some lyrics in these tests").encodeToUByteArray()
@ -66,7 +67,7 @@ class AuthTest {
}
@Test
fun testAuthHmacSha512() {
fun testAuthHmacSha512() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("I wonder if it would be possible" +
" to get some lyrics in these tests").encodeToUByteArray()
@ -93,7 +94,7 @@ class AuthTest {
}
@Test
fun simpleKeygenTest() {
fun simpleKeygenTest() = runTest {
LibsodiumInitializer.initializeWithCallback {
val authKey = Auth.authKeygen()
assertTrue { authKey.size == crypto_auth_KEYBYTES }

View File

@ -4,6 +4,7 @@ import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import kotlin.random.Random
import kotlin.random.nextUBytes
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
@ -15,7 +16,7 @@ import kotlin.test.assertTrue
*/
class BoxTest {
@Test
fun keypairTest() {
fun keypairTest() = runTest {
LibsodiumInitializer.initializeWithCallback {
val keypair = Box.keypair()
assertTrue {
@ -28,7 +29,7 @@ class BoxTest {
}
@Test
fun testBoxEasy() {
fun testBoxEasy() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = "Message message message".encodeToUByteArray()
val senderKeypair = Box.keypair()
@ -49,7 +50,7 @@ class BoxTest {
}
@Test
fun testBoxEasyDetached() {
fun testBoxEasyDetached() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = "Message message message".encodeToUByteArray()
val senderKeypair = Box.keypair()
@ -82,7 +83,7 @@ class BoxTest {
}
@Test
fun testBeforeNonceAndMessage() {
fun testBeforeNonceAndMessage() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = "Message message message".encodeToUByteArray()
val senderKeypair = Box.keypair()
@ -109,7 +110,7 @@ class BoxTest {
}
@Test
fun testSeal() {
fun testSeal() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = "Message message message".encodeToUByteArray()
val recipientKeypair = Box.keypair()

View File

@ -5,6 +5,7 @@ import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString
import kotlin.test.BeforeTest
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertTrue
@ -17,7 +18,7 @@ class GenericHashTest {
@Test
fun testGenericHash() {
fun testGenericHash() = runTest {
LibsodiumInitializer.initializeWithCallback {
val inputString = "1234567890"
val inputStringBuilder = StringBuilder()
@ -46,7 +47,7 @@ class GenericHashTest {
}
@Test
fun testGenericHashMultipart() {
fun testGenericHashMultipart() = runTest {
LibsodiumInitializer.initializeWithCallback {
val updates = 14
val input = "1234567890"

View File

@ -5,6 +5,7 @@ import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import com.ionspin.kotlin.crypto.util.runTest
import com.ionspin.kotlin.crypto.util.toHexString
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertTrue
@ -17,6 +18,7 @@ class HashTest {
//Not present in Lazy sodium
// @Test
// fun hashTest() {
// = runTest {
// LibsodiumInitializer.initializeWithCallback {
// val input = ("Input for various hash functions").encodeToUByteArray()
// val expected = ("34fcbcdcfe9e6aa3e6d5a64649afcfafb449c4b8435a65e5e7b7c2b6af3b04da350acee" +

View File

@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.kdf
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertFails
import kotlin.test.assertFalse
@ -11,7 +12,7 @@ import kotlin.test.assertTrue
*/
class KdfTest {
@Test
fun testKdf() {
fun testKdf() = runTest {
LibsodiumInitializer.initializeWithCallback {
val masterKey = Kdf.keygen()
val subkey1 = Kdf.deriveFromKey(1, crypto_kdf_BYTES_MAX, "test1234", masterKey)
@ -31,4 +32,4 @@ class KdfTest {
}
}
}
}
}

View File

@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.keyexchange
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.util.toHexString
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertTrue
@ -12,7 +13,7 @@ import kotlin.test.assertTrue
*/
class KeyExchangeTest {
@Test
fun testKeyExchange() {
fun testKeyExchange() = runTest {
LibsodiumInitializer.initializeWithCallback {
val keypairClient = KeyExchange.keypair()
val keypairServer = KeyExchange.keypair()
@ -40,7 +41,7 @@ class KeyExchangeTest {
}
@Test
fun testKeyExchangeSeeded() {
fun testKeyExchangeSeeded() = runTest {
LibsodiumInitializer.initializeWithCallback {
val seed = UByteArray(crypto_kx_SEEDBYTES) { 7U }
val keypairClient = KeyExchange.seedKeypair(seed)

View File

@ -4,6 +4,7 @@ import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.util.toHexString
import kotlin.random.Random
import kotlin.random.nextUBytes
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertTrue
@ -14,7 +15,7 @@ import kotlin.test.assertTrue
*/
class PasswordHashTest {
@Test
fun testPasswordHash() {
fun testPasswordHash() = runTest {
LibsodiumInitializer.initializeWithCallback {
val randomBytes = Random(0).nextUBytes(crypto_pwhash_SALTBYTES)
val password = "correct horse battery staple"
@ -35,8 +36,7 @@ class PasswordHashTest {
}
@Test
fun testPasswordHashForStorage() {
fun testPasswordHashForStorage() = runTest {
LibsodiumInitializer.initializeWithCallback {
val password = "correct horse battery staple"
val hashedPassword = PasswordHash.str(

View File

@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.scalarmult
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.util.toHexString
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertTrue
@ -29,7 +30,7 @@ class ScalarMultiplicationTest {
val expectedSharedSecretString = "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"
@Test
fun testScalarMultiplication() {
fun testScalarMultiplication() = runTest {
LibsodiumInitializer.initializeWithCallback {
val alicePublicKey = ScalarMultiplication.scalarMultiplicationBase(aliceSecretKey)
assertTrue {

View File

@ -3,6 +3,7 @@ package com.ionspin.kotlin.crypto.secretbox
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.toHexString
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
@ -15,7 +16,7 @@ import kotlin.test.assertTrue
class SecretBoxTest {
@Test
fun secretBoxTestEasy() {
fun secretBoxTestEasy() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()
@ -50,7 +51,7 @@ class SecretBoxTest {
}
@Test
fun secretBoxTestDetached() {
fun secretBoxTestDetached() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()

View File

@ -4,6 +4,7 @@ import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
@ -17,7 +18,7 @@ class SecretStreamTest {
@Test
fun testSecretStream() = testBlocking {
fun testSecretStream() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()

View File

@ -5,6 +5,7 @@ import com.ionspin.kotlin.crypto.shortinputhash.ShortHash
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import com.ionspin.kotlin.crypto.util.toHexString
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertTrue
@ -16,7 +17,7 @@ import kotlin.test.assertTrue
class ShortHashTest {
@Test
fun testShortHash() {
fun testShortHash() = runTest {
LibsodiumInitializer.initializeWithCallback {
val expected = "00e5d509c14e81bb".hexStringToUByteArray()
val input = "Libsodium test"
@ -30,7 +31,7 @@ class ShortHashTest {
}
@Test
fun testKeygen() {
fun testKeygen() = runTest {
LibsodiumInitializer.initializeWithCallback {
assertTrue {
val key = ShortHash.shortHashKeygen()

View File

@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.signature
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
@ -12,7 +13,7 @@ import kotlin.test.assertTrue
class SignatureTest {
@Test
fun testSignAndVerify() {
fun testSignAndVerify() = runTest {
LibsodiumInitializer.initializeWithCallback {
val keys = Signature.keypair()
val message = "Some text that will be signed".encodeToUByteArray()
@ -33,7 +34,7 @@ class SignatureTest {
}
@Test
fun testDetachedSignAndVerify() {
fun testDetachedSignAndVerify() = runTest {
LibsodiumInitializer.initializeWithCallback {
val keys = Signature.keypair()
val message = "Some text that will be signed".encodeToUByteArray()
@ -51,7 +52,7 @@ class SignatureTest {
}
@Test
fun testMultipart() {
fun testMultipart() = runTest {
LibsodiumInitializer.initializeWithCallback {
val keys = Signature.keypair()
val message1 = "Some text that ".encodeToUByteArray()

View File

@ -7,6 +7,7 @@ import com.ionspin.kotlin.crypto.util.randombytes_SEEDBYTES
import com.ionspin.kotlin.crypto.util.toHexString
import kotlin.random.Random
import kotlin.random.nextUBytes
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertTrue
@ -20,7 +21,7 @@ class StreamTest {
val seed = UByteArray(randombytes_SEEDBYTES) { 0U }
@Test
fun testChaCha20Stream() {
fun testChaCha20Stream() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = "This is a cha cha message".encodeToUByteArray()
val nonce = LibsodiumRandom.bufDeterministic(crypto_stream_chacha20_NONCEBYTES, seed)
@ -47,7 +48,7 @@ class StreamTest {
}
@Test
fun testChaCha20IetfStream() {
fun testChaCha20IetfStream() = runTest {
LibsodiumInitializer.initializeWithCallback {
val message = "This is a cha cha message".encodeToUByteArray()
val nonce = LibsodiumRandom.bufDeterministic(crypto_stream_chacha20_ietf_NONCEBYTES, seed)

View File

@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.util
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
@ -13,7 +14,7 @@ import kotlin.test.assertTrue
class LibsodiumRandomTest {
@Test
fun testRandom() {
fun testRandom()= runTest {
//This is just a sanity test, it should fail on occasion though, with probability of 1/2^32
LibsodiumInitializer.initializeWithCallback {
val random = LibsodiumRandom.random()
@ -23,7 +24,7 @@ class LibsodiumRandomTest {
}
@Test
fun testRandomUniform() {
fun testRandomUniform() = runTest {
//This is just a sanity test, it should fail on occasion though, with probability of 1/2^31
LibsodiumInitializer.initializeWithCallback {
val random = LibsodiumRandom.uniform(UInt.MAX_VALUE / 2U)
@ -32,7 +33,7 @@ class LibsodiumRandomTest {
}
@Test
fun testRandomBuffer() {
fun testRandomBuffer() = runTest {
//This is just a sanity test, it should fail on occasion though, with probability of 1/2^52
LibsodiumInitializer.initializeWithCallback {
val result = LibsodiumRandom.buf(20)
@ -42,7 +43,7 @@ class LibsodiumRandomTest {
}
@Test
fun testRandomBufferDeterministic() {
fun testRandomBufferDeterministic() = runTest {
//This is just a sanity test, it should fail on occasion though, with probability of 1/2^52
LibsodiumInitializer.initializeWithCallback {
val seed = UByteArray(randombytes_SEEDBYTES) { 1U }

View File

@ -3,6 +3,7 @@ package com.ionspin.kotlin.crypto.util
import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import kotlin.math.exp
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
@ -15,7 +16,7 @@ import kotlin.test.assertTrue
class LibsodiumUtilTest {
@Test
fun testMemzero() {
fun testMemzero() = runTest {
LibsodiumInitializer.initializeWithCallback {
val input = ubyteArrayOf(1U, 2U, 3U, 4U, 5U, 32U, 64U, 128U, 255U)
LibsodiumUtil.memzero(input)
@ -26,7 +27,7 @@ class LibsodiumUtilTest {
}
@Test
fun testMemcmp() {
fun testMemcmp() = runTest {
LibsodiumInitializer.initializeWithCallback {
val input = ubyteArrayOf(1U, 2U, 3U, 4U, 5U, 32U, 64U, 128U, 255U)
val input2 = ubyteArrayOf(1U, 2U, 3U, 4U, 5U, 32U, 64U, 128U, 255U)
@ -41,7 +42,7 @@ class LibsodiumUtilTest {
}
@Test
fun testPadding() {
fun testPadding() = runTest {
LibsodiumInitializer.initializeWithCallback {
val input = ubyteArrayOf(1U, 2U)
val blocksize = 16
@ -57,7 +58,7 @@ class LibsodiumUtilTest {
}
@Test
fun testPaddingChars() {
fun testPaddingChars() = runTest {
LibsodiumInitializer.initializeWithCallback {
val input = charArrayOf('a', 'b', 'c', 'd').map { it.toByte().toUByte() }.toUByteArray()
val blocksize = 4
@ -73,7 +74,7 @@ class LibsodiumUtilTest {
}
@Test
fun testPaddingAligned() {
fun testPaddingAligned() = runTest {
LibsodiumInitializer.initializeWithCallback {
val input = ubyteArrayOf(1U, 2U)
val blocksize = 2
@ -91,7 +92,7 @@ class LibsodiumUtilTest {
}
@Test
fun testPaddingMultiblock() {
fun testPaddingMultiblock() = runTest {
LibsodiumInitializer.initializeWithCallback {
val input = ubyteArrayOf(1U, 2U, 3U, 4U, 5U, 6U)
val blocksize = 4
@ -109,7 +110,7 @@ class LibsodiumUtilTest {
}
@Test
fun testToBase64() {
fun testToBase64() = runTest {
LibsodiumInitializer.initializeWithCallback {
val input = ubyteArrayOf(1U, 2U, 3U, 4U, 5U, 32U, 64U, 128U, 255U)
val expected = "AQIDBAUgQID_"
@ -128,7 +129,7 @@ class LibsodiumUtilTest {
}
@Test
fun testToBase64Unaligned() {
fun testToBase64Unaligned() = runTest {
LibsodiumInitializer.initializeWithCallback {
val input = ubyteArrayOf(1U, 2U, 3U, 4U, 5U, 32U, 64U, 128U, 255U, 128U)
val expected = "AQIDBAUgQID_gA"
@ -147,7 +148,7 @@ class LibsodiumUtilTest {
}
@Test
fun toHex() {
fun toHex() = runTest {
LibsodiumInitializer.initializeWithCallback {
val input = ubyteArrayOf(1U, 2U, 3U, 4U, 5U, 32U, 64U, 128U, 255U, 128U)
val hex = LibsodiumUtil.toHex(input)

View File

@ -11,7 +11,7 @@ import kotlin.browser.window
fun main() {
val runningOnNode = jsTypeOf(window) == "undefined"
if (!runningOnNode) {
if (!runningOnNode) = runTest {
LibsodiumInitializer.initializeWithCallback {
render(document.getElementById("root")) {
app {
@ -19,7 +19,7 @@ fun main() {
}
}
}
} else {
} else = runTest {
LibsodiumInitializer.initializeWithCallback {
val hash = Hash.sha512("123".encodeToUByteArray())
println("Hash (SHA512) of 123: ${hash.toHexString()}")