diff --git a/multiplatform-crypto-delegated/build.gradle.kts b/multiplatform-crypto-delegated/build.gradle.kts index 77fd751..b53574c 100644 --- a/multiplatform-crypto-delegated/build.gradle.kts +++ b/multiplatform-crypto-delegated/build.gradle.kts @@ -17,30 +17,28 @@ @file:Suppress("UnstableApiUsage") -import com.moowork.gradle.node.task.NodeTask -import org.gradle.api.tasks.testing.logging.TestLogging -import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest +import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest +import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile -import org.jetbrains.kotlin.konan.library.konanCommonLibraryPath plugins { kotlin(PluginsDeps.multiplatform) - id (PluginsDeps.mavenPublish) - id (PluginsDeps.signing) - id (PluginsDeps.node) version Versions.nodePlugin - id (PluginsDeps.dokka) version Versions.dokkaPlugin + id(PluginsDeps.mavenPublish) + id(PluginsDeps.signing) + id(PluginsDeps.node) version Versions.nodePlugin + id(PluginsDeps.dokka) version Versions.dokkaPlugin } val sonatypeStaging = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" val sonatypeSnapshots = "https://oss.sonatype.org/content/repositories/snapshots/" -val sonatypePassword : String? by project +val sonatypePassword: String? by project -val sonatypeUsername : String? by project +val sonatypeUsername: String? by project -val sonatypePasswordEnv : String? = System.getenv()["SONATYPE_PASSWORD"] -val sonatypeUsernameEnv : String? = System.getenv()["SONATYPE_USERNAME"] +val sonatypePasswordEnv: String? = System.getenv()["SONATYPE_PASSWORD"] +val sonatypeUsernameEnv: String? = System.getenv()["SONATYPE_USERNAME"] repositories { mavenCentral() @@ -61,34 +59,18 @@ fun getHostOsName(): String { } kotlin { - - val libsodiumCompilation : org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithHostTests.() -> Unit = { - compilations.getByName("main") { - val libsodiumCinterop by cinterops.creating { - defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) - } - } - } - val hostOsName = getHostOsName() - if (ideaActive) { - when(hostOsName) { - "linux" -> linuxX64("native", libsodiumCompilation) - "macos" -> macosX64("native", libsodiumCompilation) - "windows" -> mingwX64("native", libsodiumCompilation) - } - } if (hostOsName == "linux") { jvm() js { - browser { - testTask { - enabled = true //Until I sort out testing on travis - useKarma { - useChrome() + browser { + testTask { + enabled = true //Until I sort out testing on travis + useKarma { + useChrome() + } } } - } nodejs { testTask { useMocha() { @@ -99,7 +81,6 @@ kotlin { } linuxX64("linux") { - libsodiumCompilation(this) binaries { staticLib { } @@ -192,41 +173,43 @@ kotlin { } } - val nativeMain = if (ideaActive) { - val nativeMain by getting { - dependsOn(commonMain) - dependencies { - implementation(Deps.Native.coroutines) - } + val nativeMain by creating { + dependsOn(commonMain) + dependencies { + implementation(Deps.Native.coroutines) } - nativeMain - } else { - val nativeMain by creating { - dependsOn(commonMain) - dependencies { - implementation(Deps.Native.coroutines) - } - } - nativeMain } - val nativeTest = if (ideaActive) { - val nativeTest by getting { - dependsOn(commonTest) - dependencies { - implementation(Deps.Native.coroutines) - } + + + val nativeTest by creating { + dependsOn(commonTest) + dependencies { + implementation(Deps.Native.coroutines) } - nativeTest - } else { - val nativeTest by creating { - dependsOn(commonTest) - dependencies { - implementation(Deps.Native.coroutines) - } - } - nativeTest } + targets.withType { + println("Target $name") + compilations.getByName("main") { + println("Setting cinterop for $name") + defaultSourceSet.dependsOn(nativeMain) + if (!name.contains("ios")) { + val libsodiumCinterop by cinterops.creating { + defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) + } + } + } + compilations.getByName("test") { + println("Setting cinterop for $name") + if (!name.contains("ios")) { + defaultSourceSet.dependsOn(nativeTest) + } + } + } + + + + if (hostOsName == "linux") { val jvmMain by getting { dependencies { @@ -261,10 +244,22 @@ kotlin { } val linuxMain by getting { dependsOn(nativeMain) + //Force idea to consider native sourceset + if (ideaActive) { + kotlin.srcDir("src/nativeMain/kotlin") + } } val linuxTest by getting { dependsOn(nativeTest) + //Force idea to consider native sourceset + if (ideaActive) { + kotlin.srcDir("src/nativeTest/kotlin") + } } + + + + //Not supported in coroutines at the moment // val linuxArm32HfpMain by getting { // dependsOn(nativeMain) @@ -354,9 +349,9 @@ kotlin { task("copyPackageJson") { dependsOn("compileKotlinJs") println("Copying package.json from $projectDir/core/src/jsMain/npm") - from ("$projectDir/src/jsMain/npm") + from("$projectDir/src/jsMain/npm") println("Node modules dir ${node.nodeModulesDir}") - into ("${node.nodeModulesDir}") + into("${node.nodeModulesDir}") } tasks { @@ -369,14 +364,15 @@ tasks { } dokka { - println ("Dokka !") + println("Dokka !") impliedPlatforms = mutableListOf("Common") kotlinTasks { listOf() } sourceRoot { - println ("Common !") - path = "/home/ionspin/Projects/Future/kotlin-multiplatform-crypto/crypto/src/commonMain" //TODO remove static path! + println("Common !") + path = + "/home/ionspin/Projects/Future/kotlin-multiplatform-crypto/crypto/src/commonMain" //TODO remove static path! platforms = listOf("Common") } } diff --git a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/SRNGTest.kt b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/SRNGTest.kt new file mode 100644 index 0000000..696f131 --- /dev/null +++ b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/SRNGTest.kt @@ -0,0 +1,39 @@ +/* + * 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 + +import com.ionspin.kotlin.crypto.util.testBlocking +import kotlin.test.Test +import kotlin.test.assertTrue + +/** + * Created by Ugljesa Jovanovic + * ugljesa.jovanovic@ionspin.com + * on 21-Sep-2019 + */ +class SRNGTest { + @Test + fun testSrng() = testBlocking { + Crypto.initialize() + //Just a sanity test, need to add better srng tests. + val randomBytes1 = SRNG.getRandomBytes(10) + val randomBytes2 = SRNG.getRandomBytes(10) + randomBytes1.forEach { println("RB1: $it")} + randomBytes2.forEach { println("RB2: $it")} + assertTrue { !randomBytes1.contentEquals(randomBytes2) } + } +} \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/Initializer.kt b/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/Initializer.kt deleted file mode 100644 index 1f82177..0000000 --- a/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/Initializer.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.ionspin.kotlin.crypto - -actual object Initializer { - actual suspend fun initialize() { -// sodi - } - - actual fun initializeWithCallback(done: () -> Unit) { - } -} \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder b/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/Placeholder similarity index 100% rename from multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder rename to multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/Placeholder diff --git a/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder b/multiplatform-crypto-delegated/src/linuxTest/kotlin/com/ionspin/kotlin/crypto/Placeholder similarity index 100% rename from multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder rename to multiplatform-crypto-delegated/src/linuxTest/kotlin/com/ionspin/kotlin/crypto/Placeholder diff --git a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/Initializer.kt b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/Initializer.kt new file mode 100644 index 0000000..c537533 --- /dev/null +++ b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/Initializer.kt @@ -0,0 +1,19 @@ +package com.ionspin.kotlin.crypto + +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import libsodium.sodium_init + +actual object Initializer { + actual suspend fun initialize() { + GlobalScope.launch { + sodium_init() + } + + } + + actual fun initializeWithCallback(done: () -> Unit) { + sodium_init() + done() + } +} \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt index fe7c281..b8c3f28 100644 --- a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt +++ b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt @@ -17,6 +17,7 @@ package com.ionspin.kotlin.crypto import kotlinx.cinterop.* +import libsodium.randombytes_buf import platform.posix.* //import libsod @@ -30,7 +31,7 @@ actual object SRNG { actual fun getRandomBytes(amount: Int): UByteArray { memScoped { val array = allocArray(amount) -// randombytes_buf() + randombytes_buf(array, amount.toULong()) return UByteArray(amount) { array[it] } diff --git a/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt similarity index 99% rename from multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt rename to multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt index cc72c28..619ac9e 100644 --- a/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt +++ b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt @@ -1,6 +1,5 @@ package com.ionspin.kotlin.crypto.hash.blake2b import com.ionspin.kotlin.crypto.util.toHexString -import interop.* import kotlinx.cinterop.* import libsodium.* /** diff --git a/multiplatform-crypto-delegated/src/linuxTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt b/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt similarity index 89% rename from multiplatform-crypto-delegated/src/linuxTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt rename to multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt index 30d4fba..9a71168 100644 --- a/multiplatform-crypto-delegated/src/linuxTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt +++ b/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt @@ -6,6 +6,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b * on 24-May-2020 */ +import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bStateless import com.ionspin.kotlin.crypto.util.testBlocking import interop.* import kotlinx.cinterop.* diff --git a/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/bignum/crypto/util/testBlocking.kt b/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/util/testBlocking.kt similarity index 100% rename from multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/bignum/crypto/util/testBlocking.kt rename to multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/util/testBlocking.kt diff --git a/multiplatform-crypto/build.gradle.kts b/multiplatform-crypto/build.gradle.kts index a55a0ca..67bb0fa 100644 --- a/multiplatform-crypto/build.gradle.kts +++ b/multiplatform-crypto/build.gradle.kts @@ -16,30 +16,26 @@ */ @file:Suppress("UnstableApiUsage") - -import com.moowork.gradle.node.task.NodeTask -import org.gradle.api.tasks.testing.logging.TestLogging import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest -import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile plugins { kotlin(PluginsDeps.multiplatform) - id (PluginsDeps.mavenPublish) - id (PluginsDeps.signing) - id (PluginsDeps.node) version Versions.nodePlugin - id (PluginsDeps.dokka) version Versions.dokkaPlugin + id(PluginsDeps.mavenPublish) + id(PluginsDeps.signing) + id(PluginsDeps.node) version Versions.nodePlugin + id(PluginsDeps.dokka) version Versions.dokkaPlugin } val sonatypeStaging = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" val sonatypeSnapshots = "https://oss.sonatype.org/content/repositories/snapshots/" -val sonatypePassword : String? by project +val sonatypePassword: String? by project -val sonatypeUsername : String? by project +val sonatypeUsername: String? by project -val sonatypePasswordEnv : String? = System.getenv()["SONATYPE_PASSWORD"] -val sonatypeUsernameEnv : String? = System.getenv()["SONATYPE_USERNAME"] +val sonatypePasswordEnv: String? = System.getenv()["SONATYPE_PASSWORD"] +val sonatypeUsernameEnv: String? = System.getenv()["SONATYPE_USERNAME"] repositories { mavenCentral() @@ -61,13 +57,6 @@ fun getHostOsName(): String { kotlin { val hostOsName = getHostOsName() - if (ideaActive) { - when(hostOsName) { - "linux" -> linuxX64("native") - "macos" -> macosX64("native") - "windows" -> mingwX64("native") - } - } if (hostOsName == "linux") { jvm() js { @@ -196,41 +185,39 @@ kotlin { } } - val nativeMain = if (ideaActive) { - val nativeMain by getting { - dependsOn(commonMain) - dependencies { - implementation(Deps.Native.coroutines) - } + + val nativeMain by creating { + dependsOn(commonMain) + dependencies { + implementation(Deps.Native.coroutines) } - nativeMain - } else { - val nativeMain by creating { - dependsOn(commonMain) - dependencies { - implementation(Deps.Native.coroutines) - } - } - nativeMain } - val nativeTest = if (ideaActive) { - val nativeTest by getting { - dependsOn(commonTest) - dependencies { - implementation(Deps.Native.coroutines) - } + + + val nativeTest by creating { + dependsOn(commonTest) + dependencies { + implementation(Deps.Native.coroutines) } - nativeTest - } else { - val nativeTest by creating { - dependsOn(commonTest) - dependencies { - implementation(Deps.Native.coroutines) - } - } - nativeTest } + targets.withType { + compilations.getByName("main") { + println("Setting native sourceset dependancy for $name") + if (!name.contains("ios")) { + defaultSourceSet.dependsOn(nativeMain) + } + } + compilations.getByName("test") { + println("Setting native sourceset dependancy for $name") + if (!name.contains("ios")) { + defaultSourceSet.dependsOn(nativeTest) + } + } + } + + + if (hostOsName == "linux") { val jvmMain by getting { dependencies { @@ -262,9 +249,17 @@ kotlin { } val linuxMain by getting { dependsOn(nativeMain) + //Force idea to consider native sourceset + if (ideaActive) { + kotlin.srcDir("src/nativeMain/kotlin") + } } val linuxTest by getting { dependsOn(nativeTest) + //Force idea to consider native sourceset + if (ideaActive) { + kotlin.srcDir("src/nativeTest/kotlin") + } } //Not supported in coroutines at the moment // val linuxArm32HfpMain by getting { @@ -355,9 +350,9 @@ kotlin { task("copyPackageJson") { dependsOn("compileKotlinJs") println("Copying package.json from $projectDir/core/src/jsMain/npm") - from ("$projectDir/src/jsMain/npm") + from("$projectDir/src/jsMain/npm") println("Node modules dir ${node.nodeModulesDir}") - into ("${node.nodeModulesDir}") + into("${node.nodeModulesDir}") } tasks { @@ -370,23 +365,20 @@ tasks { } dokka { - println ("Dokka !") + println("Dokka !") impliedPlatforms = mutableListOf("Common") kotlinTasks { listOf() } sourceRoot { - println ("Common !") - path = "/home/ionspin/Projects/Future/kotlin-multiplatform-crypto/crypto/src/commonMain" //TODO remove static path! + println("Common !") + path = + "/home/ionspin/Projects/Future/kotlin-multiplatform-crypto/crypto/src/commonMain" //TODO remove static path! platforms = listOf("Common") } } if (getHostOsName() == "linux") { - val npmInstall by getting - val compileKotlinJs by getting(AbstractCompile::class) - val compileTestKotlinJs by getting(Kotlin2JsCompile::class) - val jvmTest by getting(Test::class) { testLogging { events("PASSED", "FAILED", "SKIPPED") @@ -397,7 +389,7 @@ tasks { testLogging { events("PASSED", "FAILED", "SKIPPED") - // showStandardStreams = true +// showStandardStreams = true } } diff --git a/sample/build.gradle.kts b/sample/build.gradle.kts index c933fcd..34149b4 100644 --- a/sample/build.gradle.kts +++ b/sample/build.gradle.kts @@ -61,13 +61,6 @@ fun getHostOsName(): String { kotlin { val hostOsName = getHostOsName() - if (ideaActive) { - when(hostOsName) { - "linux" -> linuxX64("native") - "macos" -> macosX64("native") - "windows" -> mingwX64("native") - } - } if (hostOsName == "linux") { jvm() js { @@ -197,39 +190,34 @@ kotlin { } } - val nativeMain = if (ideaActive) { - val nativeMain by getting { - dependsOn(commonMain) - dependencies { - implementation(Deps.Native.coroutines) - } + + val nativeMain by creating { + dependsOn(commonMain) + dependencies { + implementation(Deps.Native.coroutines) } - nativeMain - } else { - val nativeMain by creating { - dependsOn(commonMain) - dependencies { - implementation(Deps.Native.coroutines) - } - } - nativeMain } - val nativeTest = if (ideaActive) { - val nativeTest by getting { - dependsOn(commonTest) - dependencies { - implementation(Deps.Native.coroutines) + + val nativeTest by creating { + dependsOn(commonTest) + dependencies { + implementation(Deps.Native.coroutines) + } + } + + targets.withType { + compilations.getByName("main") { + println("Setting native sourceset dependancy for $name") + if (!name.contains("ios")) { + defaultSourceSet.dependsOn(nativeMain) } } - nativeTest - } else { - val nativeTest by creating { - dependsOn(commonTest) - dependencies { - implementation(Deps.Native.coroutines) + compilations.getByName("test") { + println("Setting native sourceset dependancy for $name") + if (!name.contains("ios")) { + defaultSourceSet.dependsOn(nativeTest) } } - nativeTest } if (hostOsName == "linux") { @@ -263,9 +251,17 @@ kotlin { } val linuxMain by getting { dependsOn(nativeMain) + //Force idea to consider native sourceset + if (ideaActive) { + kotlin.srcDir("src/nativeMain/kotlin") + } } val linuxTest by getting { dependsOn(nativeTest) + //Force idea to consider native sourceset + if (ideaActive) { + kotlin.srcDir("src/nativeTest/kotlin") + } } //Not supported in coroutines at the moment // val linuxArm32HfpMain by getting {