All platforms working with a smoke test, seems theres a bug with JS compiler (TODO in smoke test desccribes it)

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-08-08 19:54:48 +02:00
1 parent 1f0eaf59ca
commit 5d61858f81
14 files changed
+231 -62

No files matched your search

@@ -9,8 +9,8 @@ expect class Sha512State
expect class GenericHashState
expect class Crypto {
fun crypto_hash_sha256_init(): Sha256State
expect class Crypto internal constructor() {
fun crypto_hash_sha256_init_spec(): Sha256State
fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray)
@@ -0,0 +1,30 @@
package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString
import debug.test.Crypto
import kotlin.test.Test
import kotlin.test.assertTrue
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 08-Aug-2020
*/
class SmokeTest {
@Test
fun testIfLibraryIsNotOnFire() = testBlocking {
Initializer.initialize()
val crypto = Crypto()
val state256 = crypto.crypto_hash_sha256_init_spec() //TODO seems to be a bug in JS compiler, if we have the same method name in crypto an in JsSodiumInterface, method tries to call wrong method name (unneeded suffix _0)
crypto.crypto_hash_sha256_update(state256, "Hello".encodeToUByteArray())
val result = crypto.crypto_hash_sha256_final(state256).toHexString()
println("Result: $result")
assertTrue {
"185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969" == result
}
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2019 Ugljesa Jovanovic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ionspin.kotlin.crypto.util
import kotlin.coroutines.Continuation
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.startCoroutine
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 20-Jul-2019
*/
fun testBlocking(block : suspend () -> Unit) {
val continuation = Continuation<Unit>(EmptyCoroutineContext) {
//Do nothing
if (it.isFailure) {
throw it.exceptionOrNull()!!
}
}
block.startCoroutine(continuation)
}
@@ -13,8 +13,8 @@ actual typealias Sha512State = Any
actual typealias GenericHashState = Any
actual class Crypto {
actual fun crypto_hash_sha256_init(): dynamic {
actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha256_init_spec(): dynamic {
println("Debug")
return getSodium().crypto_hash_sha256_init()
}
@@ -1,8 +1,8 @@
package debug.test
import ByteArray
import com.goterl.lazycode.lazysodium.SodiumJava
import com.goterl.lazycode.lazysodium.interfaces.Hash
import kotlin.ByteArray
import kotlin.Int
import kotlin.UByteArray
@@ -14,10 +14,12 @@ actual typealias Sha512State = Hash.State512
actual typealias GenericHashState = ByteArray
actual class Crypto {
actual fun crypto_hash_sha256_init(state: Sha256State): Sha256State {
actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha256_init_spec(): Sha256State {
val state = debug.test.Sha256State()
println("Debug")
return sodium.crypto_hash_sha256_init(state)
sodium.crypto_hash_sha256_init(state)
return state
}
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
@@ -25,14 +27,18 @@ actual class Crypto {
sodium.crypto_hash_sha256_update(state, input.asByteArray(), input.size.toLong())
}
actual fun crypto_hash_sha256_final(state: Sha256State, out: UByteArray): UByteArray {
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
val out = UByteArray(32)
println("Debug")
return sodium.crypto_hash_sha256_final(state, out.asByteArray())
sodium.crypto_hash_sha256_final(state, out.asByteArray())
return out
}
actual fun crypto_hash_sha512_init(state: Sha512State): Sha512State {
actual fun crypto_hash_sha512_init(): Sha512State {
val state = debug.test.Sha512State()
println("Debug")
return sodium.crypto_hash_sha512_init(state)
sodium.crypto_hash_sha512_init(state)
return state
}
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
@@ -40,17 +46,17 @@ actual class Crypto {
sodium.crypto_hash_sha512_update(state, input.asByteArray(), input.size.toLong())
}
actual fun crypto_hash_sha512_final(state: Sha512State, out: UByteArray): UByteArray {
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
val out = UByteArray(64)
println("Debug")
return sodium.crypto_hash_sha512_final(state, out.asByteArray())
sodium.crypto_hash_sha512_final(state, out.asByteArray())
return out
}
actual fun crypto_generichash_init(
state: GenericHashState,
key: UByteArray,
outlen: Int
): GenericHashState {
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
val state = debug.test.GenericHashState(sodium.crypto_generichash_statebytes())
println("Debug")
return sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen)
sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen)
return state
}
}
@@ -22,28 +22,25 @@ actual typealias Sha512State = crypto_hash_sha512_state
actual typealias GenericHashState = crypto_generichash_blake2b_state
actual class Crypto {
actual class Crypto internal actual constructor() {
val _emitByte: Byte = 0
val _emitByteArray: ByteArray = ByteArray(0) {}
val _emitByteArray: ByteArray = ByteArray(0)
actual fun crypto_hash_sha256_init(): Sha256State {
actual fun crypto_hash_sha256_init_spec(): Sha256State {
val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!!
val state = allocated.reinterpret<debug.test.Sha256State>().pointed
println("Debug")
libsodium.crypto_hash_sha256_init(state.ptr)
return state
}
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
println("Debug")
val pinnedInput = input.pin()
libsodium.crypto_hash_sha256_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
pinnedInput.unpin()
}
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
println("Debug")
val out = UByteArray(32)
val pinnedOut = out.pin()
libsodium.crypto_hash_sha256_final(state.ptr, pinnedOut.addressOf(0))
@@ -54,20 +51,17 @@ actual class Crypto {
actual fun crypto_hash_sha512_init(): Sha512State {
val allocated = sodium_malloc(debug.test.Sha512State.size.convert())!!
val state = allocated.reinterpret<debug.test.Sha512State>().pointed
println("Debug")
libsodium.crypto_hash_sha512_init(state.ptr)
return state
}
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
println("Debug")
val pinnedInput = input.pin()
libsodium.crypto_hash_sha512_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
pinnedInput.unpin()
}
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
println("Debug")
val out = UByteArray(64)
val pinnedOut = out.pin()
libsodium.crypto_hash_sha512_final(state.ptr, pinnedOut.addressOf(0))
@@ -78,7 +72,6 @@ actual class Crypto {
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
val allocated = sodium_malloc(debug.test.GenericHashState.size.convert())!!
val state = allocated.reinterpret<debug.test.GenericHashState>().pointed
println("Debug")
val pinnedKey = key.pin()
libsodium.crypto_generichash_init(state.ptr, pinnedKey.addressOf(0), key.size.convert(),
outlen.convert())