Skip to content

Commit

Permalink
enum consistency test
Browse files Browse the repository at this point in the history
  • Loading branch information
iaik-jheher authored and JesusMcCloud committed Dec 9, 2024
1 parent 0c6c51a commit 683652e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
6 changes: 6 additions & 0 deletions indispensable/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ kotlin {
}
}

jvmTest {
dependencies {
implementation(kotlin("reflect"))
}
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions indispensable/src/commonTest/kotlin/CryptoSignatureTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CryptoSignatureTest : FreeSpec({
rsa1.hashCode() shouldNotBe rsa3.hashCode()

val ec4 = ec3.guessCurve()
ec4.scalarByteLength shouldBe ECCurve.values().minOf { it.scalarLength.bytes }
ec4.scalarByteLength shouldBe ECCurve.entries.minOf { it.scalarLength.bytes }
ec4 shouldBe ec4
ec4 shouldBe ec3
ec4.hashCode() shouldBe ec4.hashCode()
Expand Down Expand Up @@ -81,7 +81,7 @@ class CryptoSignatureTest : FreeSpec({
sig3.scalarByteLength shouldBe sig1.scalarByteLength
sig3.rawByteArray shouldBe encoded

val r2 = BigInteger.ONE.shl(ECCurve.values().maxOf { it.scalarLength.bits }.toInt() + 1)
val r2 = BigInteger.ONE.shl(ECCurve.entries.maxOf { it.scalarLength.bits }.toInt() + 1)
shouldThrow<IllegalArgumentException> { CryptoSignature.EC.fromRS(r2, s).guessCurve() }
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package at.asitplus.signum.indispensable

import at.asitplus.signum.indispensable.mac.MAC
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf
import java.util.Stack
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlin.reflect.full.companionObject
import kotlin.reflect.full.memberProperties

inline fun<reified T: Any> FreeSpec.enumConsistencyTest() {
T::class.simpleName!! {
val listed = T::class.companionObject!!.let { companion ->
@Suppress("UNCHECKED_CAST")
(companion.memberProperties.find { it.name == "entries" }.shouldNotBeNull()
as KProperty1<Any, *>).get(companion.objectInstance!!)
}.shouldBeInstanceOf<Iterable<T>>()

val discovered = mutableSetOf<T>()
val queue = Stack<KClass<out T>>().also { it.push(T::class) }
while (!queue.empty()) {
val cls = queue.pop()
if (cls.java.isEnum) {
discovered.addAll(cls.java.enumConstants!!)
continue
}
val o = cls.objectInstance
if (o != null) {
discovered.add(o)
continue
}
cls.sealedSubclasses.forEach(queue::push)
}

listed.toSet() shouldBe discovered.toSet()
}
}

class EnumConsistencyTests : FreeSpec({
enumConsistencyTest<MAC>()
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package at.asitplus.signum.indispensable.misc
package at.asitplus.signum.internals

import kotlin.experimental.xor

infix fun <T: Any> T?.orLazy(block: ()->T) = if (this != null) lazyOf(this) else lazy(block)

Expand All @@ -14,10 +16,6 @@ fun ByteArray.ensureSize(size: Int): ByteArray = (this.size - size).let { toDrop
@Suppress("NOTHING_TO_INLINE")
inline fun ByteArray.ensureSize(size: UInt) = ensureSize(size.toInt())

internal infix fun <T: Any> T?.orLazy(block: ()->T) =
if (this != null) lazyOf(this) else lazy(block)


infix fun ByteArray.xor(other: ByteArray): ByteArray {
check(this.size == other.size)
return ByteArray(this.size) { i -> this[i] xor other[i] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import at.asitplus.KmmResult
import at.asitplus.catching
import at.asitplus.signum.indispensable.mac.HMAC
import at.asitplus.signum.indispensable.mac.MAC
import at.asitplus.signum.indispensable.misc.xor
import at.asitplus.signum.internals.xor
import at.asitplus.signum.supreme.hash.digest


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import at.asitplus.signum.indispensable.BlockCipher
import at.asitplus.signum.indispensable.Ciphertext
import at.asitplus.signum.indispensable.SymmetricEncryptionAlgorithm
import at.asitplus.signum.indispensable.SymmetricEncryptionAlgorithm.AES
import at.asitplus.signum.internals.swiftcall
import at.asitplus.signum.internals.toNSData
import at.asitplus.signum.internals.toByteArray
import at.asitplus.signum.supreme.aes.CBC
import at.asitplus.signum.supreme.aes.GCM
import at.asitplus.signum.supreme.swiftcall
import at.asitplus.signum.supreme.toByteArray
import at.asitplus.signum.supreme.toNSData
import kotlinx.cinterop.ExperimentalForeignApi
import platform.CoreCrypto.kCCDecrypt
import platform.CoreCrypto.kCCEncrypt
Expand Down

0 comments on commit 683652e

Please sign in to comment.