Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
google()
}

dependencies {
testImplementation(kotlin("test"))
testImplementation(gradleTestKit())
}
73 changes: 73 additions & 0 deletions buildSrc/src/main/kotlin/NativeBuildResolver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import org.gradle.api.provider.Provider
import org.gradle.nativeplatform.platform.NativePlatform
import java.io.File
import org.gradle.api.file.Directory

/**
* Gradle mostly uses Java os.arch names for architectures which feeds into default
* targetPlatform names. Notable exception Gradle 6.9.x reports MacOS/ARM as arm-v8.
*
* The Maven osdetector plugin (which we recommend to developers) uses different
* arch names, so that's what we need for artifacts.
*
* This class encapsulates both naming schemes as well as other per-platform information
* about native builds, more of which will migrate in here over time.
*/
enum class NativeBuildVariant(
val os: String,
val mavenArch: String, // osdetector / Maven architecture name
val gradleArch: String, // Gradle architecture, used for things like NDK or toolchain selection
val boringBuildDir: String = "build64", // Where to find prebuilt libcrypto
) {
WINDOWS_X64("windows", "x86_64", "x86-64"),
LINUX_X64("linux", "x86_64", "x86-64"),
OSX_X64("osx", "x86_64", "x86-64", "build.x86"),
OSX_ARM64("osx", "aarch_64", "aarch64", "build.arm");

override fun toString(): String
= "<os=$os target=$mavenArch gradle=$gradleArch boring=$boringBuildDir>"

companion object {
fun find(os: String, arch: String)
= values().find { it.os == os && it.mavenArch == arch }
fun findForGradle(os: String, arch: String)
= values().find { it.os == os && it.gradleArch == arch }
fun findAll(os: String) = values().filter { it.os == os }
}
}

data class NativeBuildInfo(
val buildDir: Provider<Directory>,
private val variant: NativeBuildVariant
) {
val mavenClassifier: String = "${variant.os}-${variant.mavenArch}"
val targetPlatform: String = "${variant.os}_${variant.gradleArch}"

val nativeResourcesDir: String
get() = File(buildDir.get().asFile, "$mavenClassifier/native-resources").absolutePath

val jarNativeResourcesDir: String
get() = File(nativeResourcesDir, "META-INF/native").absolutePath

val boringBuildDir
get() = variant.boringBuildDir

override fun toString(): String
= "NativeBuildInfo<buildDir=${buildDir.get()} variant=$variant>"
}

class NativeBuildResolver(private val buildDir: Provider<Directory>) {
private fun wrap(variant: NativeBuildVariant?) = variant?.let {
NativeBuildInfo(buildDir, it)
}

fun find(os: String, arch: String) = wrap(NativeBuildVariant.find(os, arch))

fun find(nativePlatform: NativePlatform) = wrap(NativeBuildVariant.findForGradle(
nativePlatform.operatingSystem.name,
nativePlatform.architecture.name))

fun findAll(os: String): List<NativeBuildInfo> = NativeBuildVariant.findAll(os). map {
NativeBuildInfo(buildDir, it)
}
}
63 changes: 63 additions & 0 deletions buildSrc/src/test/kotlin/NativeBuildResolverTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import org.gradle.testfixtures.ProjectBuilder
import kotlin.test.*

class NativeBuildResolverTest {
@Test
fun findByOsdetectorExact() {
assertEquals(NativeBuildVariant.OSX_ARM64,
NativeBuildVariant.find("osx", "aarch_64"))
assertEquals(NativeBuildVariant.LINUX_X64,
NativeBuildVariant.find("linux", "x86_64"))
}

@Test
fun findByGradleExact() {
assertEquals(NativeBuildVariant.OSX_X64,
NativeBuildVariant.findForGradle("osx", "x86-64"))
assertEquals(NativeBuildVariant.OSX_ARM64,
NativeBuildVariant.findForGradle("osx", "aarch64"))
}

@Test
fun findUnknownReturnsNull() {
assertNull(NativeBuildVariant.find("linux", "armv7"))
assertNull(NativeBuildVariant.findForGradle("windows", "aarch64"))
}

@Test
fun findAllByOs() {
val osx = NativeBuildVariant.findAll("osx").toSet()
assertEquals(setOf(NativeBuildVariant.OSX_X64, NativeBuildVariant.OSX_ARM64), osx)
}

@Test
fun computedStringsAreStable() {
assertEquals("osx-aarch_64", NativeBuildVariant.OSX_ARM64.let { "${it.os}-${it.mavenArch}" })
assertEquals("osx_aarch64", NativeBuildVariant.OSX_ARM64.let { "${it.os}_${it.gradleArch}" })
assertEquals("build.arm", NativeBuildVariant.OSX_ARM64.boringBuildDir)
assertEquals("build64", NativeBuildVariant.LINUX_X64.boringBuildDir)
}

@Test
fun directoriesAreDerivedCorrectlyFromBuilddir() {
val tmp = createTempDir().apply { deleteOnExit() }
val project = ProjectBuilder.builder().withProjectDir(tmp).build()
val info = NativeBuildInfo(project.layout.buildDirectory, NativeBuildVariant.OSX_X64)

assertTrue(info.nativeResourcesDir.replace('\\', '/')
.endsWith("osx-x86_64/native-resources"))
assertTrue(info.jarNativeResourcesDir.replace('\\', '/')
.endsWith("osx-x86_64/native-resources/META-INF/native"))
assertEquals("osx-x86_64", info.mavenClassifier)
assertEquals("osx_x86-64", info.targetPlatform)
}

@Test fun resolverWrapsVariants() {
val project = ProjectBuilder.builder().build()
val resolver = NativeBuildResolver(project.layout.buildDirectory)

val info = resolver.findAll("linux").single() // Only one for now
assertEquals("linux-x86_64", info.mavenClassifier)
assertEquals(project.layout.buildDirectory.get().asFile, info.buildDir.get().asFile)
}
}
22 changes: 18 additions & 4 deletions common/src/test/java/org/conscrypt/EdDsaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -29,6 +30,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -328,7 +330,8 @@ public void serializePrivateKey_isEqualToTestVector() throws Exception {

String classNameHex = TestUtils.encodeHex(
privateKey.getClass().getName().getBytes(StandardCharsets.UTF_8));
String expectedHexEncoding = "aced000573720024" + classNameHex
String expectedHexEncoding = "aced0005737200"
+ Integer.toHexString(privateKey.getClass().getName().length()) + classNameHex
+ "d479f95a133abadc" // serialVersionUID
+ "0200015b000f"
+ "707269766174654b65794279746573" // hex("privateKeyBytes")
Expand Down Expand Up @@ -357,7 +360,8 @@ public void serializePublicKey_isEqualToTestVector() throws Exception {

String classNameHex = TestUtils.encodeHex(
publicKey.getClass().getName().getBytes(StandardCharsets.UTF_8));
String expectedHexEncoding = "aced000573720023" + classNameHex
String expectedHexEncoding = "aced0005737200"
+ Integer.toHexString(publicKey.getClass().getName().length()) + classNameHex
+ "064c7113d078e42d" // serialVersionUID
+ "0200015b000e"
+ "7075626c69634b65794279746573" // hex("publicKeyBytes")
Expand Down Expand Up @@ -386,7 +390,12 @@ public void deserializeInvalidPrivateKey_fails() throws Exception {
new ByteArrayInputStream(TestUtils.decodeHex(invalidPrivateKeySerialized));
ObjectInputStream ois = new ObjectInputStream(bais);

assertThrows(IllegalArgumentException.class, () -> ois.readObject());
try {
ois.readObject();
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException | EOFException e) {
// Expected
}
}

@Test
Expand All @@ -408,6 +417,11 @@ public void deserializeInvalidPublicKey_fails() throws Exception {
new ByteArrayInputStream(TestUtils.decodeHex(invalidPublicKeySerialized));
ObjectInputStream ois = new ObjectInputStream(bais);

assertThrows(IllegalArgumentException.class, () -> ois.readObject());
try {
ois.readObject();
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException | EOFException e) {
// Expected
}
}
}
23 changes: 20 additions & 3 deletions common/src/test/java/org/conscrypt/MlDsaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -27,6 +28,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -451,7 +453,12 @@ public void deserializePrivateKeyWithWrongSuffix_fails() throws Exception {
new ByteArrayInputStream(TestUtils.decodeHex(invalidPrivateKey));
ObjectInputStream ois = new ObjectInputStream(bais);

assertThrows(IllegalArgumentException.class, () -> ois.readObject());
try {
ois.readObject();
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException | EOFException e) {
// Expected
}
}

@Test
Expand Down Expand Up @@ -479,7 +486,12 @@ public void deserializePrivateKeyWithWrongSize_fails() throws Exception {
new ByteArrayInputStream(TestUtils.decodeHex(invalidPrivateKey));
ObjectInputStream ois = new ObjectInputStream(bais);

assertThrows(IllegalArgumentException.class, () -> ois.readObject());
try {
ois.readObject();
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException | EOFException e) {
// Expected
}
}

@Test
Expand All @@ -504,6 +516,11 @@ public void deserializeInvalidPublicKey_fails() throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(TestUtils.decodeHex(hexPublicKey));
ObjectInputStream ois = new ObjectInputStream(bais);

assertThrows(IllegalArgumentException.class, () -> ois.readObject());
try {
ois.readObject();
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException | EOFException e) {
// Expected
}
}
}
Loading
Loading