diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index f2cf7630..e166d79b 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -10,6 +10,38 @@ env: CARGO_TERM_COLOR: always jobs: + kotlin: + name: Kotlin/JVM artifact + runs-on: ubuntu-latest + concurrency: + group: kotlin-jvm-${{ github.event.pull_request.number }} + cancel-in-progress: true + + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-java@v6.0.1 + with: + distribution: temurin + java-version: "17" + - uses: cachix/install-nix-action@v31.11.1 + with: + github_access_token: ${{ secrets.GITHUB_TOKEN }} + - uses: gradle/actions/setup-gradle@v6 + - uses: Swatinem/rust-cache@v2 + with: + key: kotlin-jvm + - name: Exercise packaged Kotlin/JVM binding + run: nix develop --command just test-kotlin + - name: Upload JVM test results + if: always() + uses: actions/upload-artifact@v7 + with: + name: kotlin-jvm-test-results + path: | + kt/web-transport/build/reports/tests/jvmTest/ + kt/web-transport/build/test-results/jvmTest/ + if-no-files-found: ignore + build: runs-on: ubuntu-latest diff --git a/justfile b/justfile index 0c892969..b07bccd3 100644 --- a/justfile +++ b/justfile @@ -120,6 +120,10 @@ build-ffi: build-ffi-target target: ./rs/web-transport-ffi/build.sh --target {{target}} --output rs/web-transport-ffi/dist +# Build and exercise the Linux Kotlin/JVM JAR, including its packaged native library. +test-kotlin: + ./kt/scripts/check.sh + # Delete build artifacts and caches to reclaim disk space. web-transport keeps # a single root justfile (unlike moq's per-language modules), so the per-language # cleanups are inlined here. Sweeps the shared caches, then recurses into any diff --git a/kt/.gitignore b/kt/.gitignore index ae4fb81e..49b32ff3 100644 --- a/kt/.gitignore +++ b/kt/.gitignore @@ -1,4 +1,5 @@ .gradle/ +.kotlin/ build/ local.properties **/jniLibs/ diff --git a/kt/README.md b/kt/README.md index f4dfb7df..5748d2c2 100644 --- a/kt/README.md +++ b/kt/README.md @@ -17,14 +17,46 @@ dependencies { ## Build ```sh -# Build host cdylib, regenerate UniFFI bindings, run :web-transport:jvmTest. -just kt check +# Build host cdylib, regenerate UniFFI bindings, and test the packaged JVM JAR. +# Requires JDK 17 via JAVA_HOME or PATH; Nix supplies Rust, Just, and OpenSSL. +nix develop --command just test-kotlin + +# Equivalent without Nix, with Rust, Just, OpenSSL, and JDK 17 installed: +just test-kotlin # Assemble the KMP artifacts for publishing (requires per-target libs). -gradle -p kt :web-transport:assemble +./kt/gradlew -p kt :web-transport:assemble ``` -`kt/scripts/check.sh` builds `web-transport-ffi` for the host, regenerates the UniFFI Kotlin bindings, drops the host cdylib into the JNA-resource layout, and runs `gradle :web-transport:jvmTest`. Skips cleanly without a JDK or `cargo`. +`kt/scripts/check.sh` uses the checksum-pinned Gradle 8.10.2 wrapper, builds +`web-transport-ffi` in release mode for the host, regenerates UniFFI Kotlin +bindings, and stages the host library in the JNA resource layout. Missing tools +or an unsupported host fail the command. This focused smoke supports Linux GNU +x86_64 and aarch64. Other desktop platforms remain in the release packaging +matrix. + +`check.sh` enables the smoke source set with `-PartifactSmoke=true`. Ordinary +Gradle `build`/`check` does not compile or run these externally staged tests. +The opted-in `:web-transport:jvmTest` loads main classes and native resources +from `jvmJar`, +with loose build outputs removed from its runtime classpath. The smoke test +checks JNA extraction, connects a loopback client/server on an ephemeral port, +exchanges a bidirectional stream, and closes sessions and endpoints. Fresh test +credentials live under `.tmp/` and are removed on exit; the loopback client +disables certificate verification. No publishing or Android SDK is required. +The independent Kotlin/JVM PR job runs this same command on Linux and uploads +JUnit XML and HTML results. Other staged platform libraries are preserved. The +release matrix and packaging remain separate. + +After staging with `just test-kotlin`, rerun through that command so test +credentials are regenerated. Results are in +`kt/web-transport/build/reports/tests/jvmTest/index.html`. + +Keep each generated `Session` handle alive while using its streams. To close a +session or endpoint, supply code and reason explicitly, for example +`session.close(0u, "done")` or `client.close(0uL, "done")`. Parameterless `close()` +is UniFFI's `AutoCloseable` handle destruction; `use { ... }` releases handles +after explicit lifecycle cleanup. The Android target is opt-in via `-Pandroid.enabled=true`. Local dev without the Android SDK still builds the JVM variant. @@ -37,12 +69,12 @@ kt/ gradle.properties Defaults: version, android.useAndroidX, etc. web-transport/ build.gradle.kts KMP plugin, jvm() always, androidTarget() conditional - android.gradle.kts Applied only when -Pandroid.enabled=true src/ commonMain/ Public Kotlin facade (reserved for future K/N) jvmAndAndroidMain/ UniFFI-generated kotlin (populated, gitignored) jvmMain/resources/ Native libs at JNA paths (populated, gitignored) androidMain/jniLibs/ JNI .so per ABI (populated, gitignored) + jvmArtifactTest/ Opt-in packaged-artifact lifecycle smoke scripts/ check.sh, package.sh ``` diff --git a/kt/gradle/wrapper/gradle-wrapper.jar b/kt/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..a4b76b95 Binary files /dev/null and b/kt/gradle/wrapper/gradle-wrapper.jar differ diff --git a/kt/gradle/wrapper/gradle-wrapper.properties b/kt/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..6ae7acbc --- /dev/null +++ b/kt/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,8 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip +distributionSha256Sum=31c55713e40233a8303827ceb42ca48a47267a0ad4bab9177123121e71524c26 +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/kt/gradlew b/kt/gradlew new file mode 100755 index 00000000..d95bf613 --- /dev/null +++ b/kt/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# 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 +# +# https://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. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='-Dfile.encoding=UTF-8 "-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/kt/gradlew.bat b/kt/gradlew.bat new file mode 100644 index 00000000..640d6868 --- /dev/null +++ b/kt/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS=-Dfile.encoding=UTF-8 "-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/kt/justfile b/kt/justfile deleted file mode 100644 index 9a1e2ac3..00000000 --- a/kt/justfile +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env just --justfile -# -# Language-specific recipes for the Kotlin Multiplatform package under -# kt/. Invoked from the repo root as `just kt ` via the `mod kt` -# import. - -set working-directory := '.' - -default: - just check - -# Build web-transport-ffi for the host, regenerate uniffi bindings, run :web-transport:jvmTest. -# Skips cleanly if cargo, java, or gradle is missing. -check: - bash scripts/check.sh - -# Assemble the KMP module from per-target web-transport-ffi binaries + bindings. -# Used by .github/workflows/release-kt.yml; see kt/README.md for the -# expected --lib-dir layout. -package *args: - bash scripts/package.sh {{ args }} diff --git a/kt/scripts/check.sh b/kt/scripts/check.sh index 25e6890d..a4046aa3 100755 --- a/kt/scripts/check.sh +++ b/kt/scripts/check.sh @@ -1,65 +1,59 @@ #!/usr/bin/env bash set -euo pipefail -# Local smoke check for the Kotlin wrapper. -# -# Builds web-transport-ffi for the host target, drops the cdylib into the JNA-resource -# layout of the :web-transport KMP module, regenerates the bindings, and runs -# `:web-transport:jvmTest`. Intended for `just kt check`. -# -# Skipped cleanly on hosts without `java` or `cargo`. - +# Build and test the host JVM JAR with its packaged JNA native resource. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" KT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" WORKSPACE_DIR="$(cd "$KT_DIR/.." && pwd)" -if ! command -v java >/dev/null 2>&1; then - echo "kt check: no JDK on PATH, skipping" >&2 - exit 0 -fi -if ! command -v cargo >/dev/null 2>&1; then - echo "kt check: no cargo on PATH, skipping" >&2 - exit 0 +# Match the Gradle wrapper's JDK selection, including Nix shells that reset PATH. +if [[ -n "${JAVA_HOME:-}" ]]; then + export PATH="$JAVA_HOME/bin:$PATH" fi +for tool in java cargo rustc openssl; do + command -v "$tool" >/dev/null 2>&1 || { echo "kt check: required tool not on PATH: $tool" >&2; exit 1; } +done HOST_TARGET=$(rustc -vV | awk '/^host:/ {print $2}') +case "$HOST_TARGET" in + x86_64-unknown-linux-gnu) RESOURCE_PREFIX=linux-x86-64;; + aarch64-unknown-linux-gnu) RESOURCE_PREFIX=linux-aarch64;; + *) echo "kt check: Linux GNU x86_64 or aarch64 required, got $HOST_TARGET" >&2; exit 1;; +esac + +mkdir -p "$WORKSPACE_DIR/.tmp" +STAGING=$(mktemp -d "$WORKSPACE_DIR/.tmp/kotlin-smoke.XXXXXX") +trap 'rm -rf "$STAGING"' EXIT + +# An explicit target/profile keeps resource staging independent of Cargo defaults. echo "kt check: building web-transport-ffi for $HOST_TARGET..." -cargo build --release --package web-transport-ffi \ +cargo build --release --target "$HOST_TARGET" --package web-transport-ffi \ --manifest-path "$WORKSPACE_DIR/Cargo.toml" - TARGET_BASE=$(cargo metadata --format-version 1 --manifest-path "$WORKSPACE_DIR/Cargo.toml" --no-deps \ | sed -n 's/.*"target_directory":"\([^"]*\)".*/\1/p') - -case "$HOST_TARGET" in - *-apple-*) CDYLIB="$TARGET_BASE/release/libweb_transport_ffi.dylib"; OS_TAG="darwin";; - *-windows-*) CDYLIB="$TARGET_BASE/release/web_transport_ffi.dll"; OS_TAG="win32";; - *) CDYLIB="$TARGET_BASE/release/libweb_transport_ffi.so"; OS_TAG="linux";; -esac -case "$HOST_TARGET" in - aarch64-*) ARCH_TAG="aarch64";; - x86_64-*) ARCH_TAG="x86-64";; - *) echo "kt check: unsupported host arch in $HOST_TARGET" >&2; exit 1;; -esac - +CDYLIB="$TARGET_BASE/$HOST_TARGET/release/libweb_transport_ffi.so" [[ -f "$CDYLIB" ]] || { echo "kt check: cdylib not found at $CDYLIB" >&2; exit 1; } -RES_DIR="$KT_DIR/web-transport/src/jvmMain/resources/${OS_TAG}-${ARCH_TAG}" -mkdir -p "$RES_DIR" -cp "$CDYLIB" "$RES_DIR/" - -BINDGEN_OUT=$(mktemp -d) -trap 'rm -rf "$BINDGEN_OUT"' EXIT -cargo run --release --package web-transport-ffi --bin uniffi-bindgen \ +cargo run --release --target "$HOST_TARGET" --package web-transport-ffi --bin uniffi-bindgen \ --manifest-path "$WORKSPACE_DIR/Cargo.toml" -- \ - generate --library "$CDYLIB" --language kotlin --out-dir "$BINDGEN_OUT" - -mkdir -p "$KT_DIR/web-transport/src/jvmAndAndroidMain/kotlin/uniffi/web_transport" -cp "$BINDGEN_OUT/uniffi/web_transport/web_transport.kt" "$KT_DIR/web-transport/src/jvmAndAndroidMain/kotlin/uniffi/web_transport/web_transport.kt" - -GRADLE_CMD="${GRADLE_CMD:-$(command -v gradle || true)}" -if [[ -z "$GRADLE_CMD" ]]; then - echo "kt check: gradle not on PATH, skipping" >&2 - exit 0 -fi - -"$GRADLE_CMD" -p "$KT_DIR" -Pwebtransportffi.version=0.0.0-dev :web-transport:jvmTest + generate --library "$CDYLIB" --language kotlin --out-dir "$STAGING/bindings" + +GENERATED_DIR="$KT_DIR/web-transport/src/jvmAndAndroidMain/kotlin/uniffi" +RES_DIR="$KT_DIR/web-transport/src/jvmMain/resources" +# Replace this host's inputs while preserving other staged release libraries. +rm -rf "$GENERATED_DIR" "$RES_DIR/$RESOURCE_PREFIX" +mkdir -p "$GENERATED_DIR" "$RES_DIR/$RESOURCE_PREFIX" +cp -R "$STAGING/bindings/uniffi/"* "$GENERATED_DIR/" +cp "$CDYLIB" "$RES_DIR/$RESOURCE_PREFIX/" + +# Fresh loopback-only credentials, never packaged into the published artifact. +openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes \ + -subj /CN=localhost -days 1 -outform DER -out "$STAGING/cert.der" -keyout "$STAGING/key.pem" +openssl pkcs8 -topk8 -nocrypt -in "$STAGING/key.pem" -outform DER -out "$STAGING/key.der" +export WEB_TRANSPORT_TEST_CERT="$STAGING/cert.der" +export WEB_TRANSPORT_TEST_KEY="$STAGING/key.der" + +# Always execute the test, including when Gradle has cached an earlier smoke run. +"$KT_DIR/gradlew" --no-daemon --console=plain --stacktrace -p "$KT_DIR" \ + -Pkotlin.compiler.execution.strategy=in-process \ + -PartifactSmoke=true -Pwebtransportffi.version=0.0.0-dev :web-transport:jvmTest --rerun diff --git a/kt/web-transport/build.gradle.kts b/kt/web-transport/build.gradle.kts index fe5c988c..076c765c 100644 --- a/kt/web-transport/build.gradle.kts +++ b/kt/web-transport/build.gradle.kts @@ -36,9 +36,12 @@ // the signing key) don't trip on "no configured signatory". import com.vanniktech.maven.publish.SonatypeHost +import java.time.Duration +import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.jetbrains.kotlin.gradle.dsl.JvmTarget val androidEnabled = providers.gradleProperty("android.enabled").orNull == "true" +val artifactSmokeEnabled = providers.gradleProperty("artifactSmoke").orNull == "true" plugins { kotlin("multiplatform") version "2.0.21" @@ -101,6 +104,9 @@ kotlin { } val jvmTest by getting { dependsOn(jvmAndAndroidTest) + if (artifactSmokeEnabled) { + kotlin.srcDir("src/jvmArtifactTest/kotlin") + } } if (androidEnabled) { @@ -140,6 +146,23 @@ if (androidEnabled) { } } +// Exercise the same JAR consumers load, without loose main classes/resources. +if (artifactSmokeEnabled) { + tasks.named("jvmTest") { + val jar = tasks.named("jvmJar") + val main = kotlin.targets.getByName("jvm").compilations.getByName("main") + classpath = files(jar) + (classpath - main.output.allOutputs) + systemProperty("jna.tmpdir", temporaryDir.resolve("jna").apply { mkdirs() }) + systemProperty("jna.debug_load", "true") + timeout.set(Duration.ofMinutes(2)) + testLogging { + events("passed", "skipped", "failed") + exceptionFormat = TestExceptionFormat.FULL + showStandardStreams = true + } + } +} + mavenPublishing { publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, automaticRelease = true) // Only sign when a non-empty signing key is configured. PR smoke tests diff --git a/kt/web-transport/src/jvmArtifactTest/kotlin/dev/moq/webtransport/ArtifactSmokeTest.kt b/kt/web-transport/src/jvmArtifactTest/kotlin/dev/moq/webtransport/ArtifactSmokeTest.kt new file mode 100644 index 00000000..e81b1e2c --- /dev/null +++ b/kt/web-transport/src/jvmArtifactTest/kotlin/dev/moq/webtransport/ArtifactSmokeTest.kt @@ -0,0 +1,110 @@ +package dev.moq.webtransport + +import com.sun.jna.Native +import com.sun.jna.Platform +import java.io.File +import java.net.JarURLConnection +import kotlinx.coroutines.async +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue +import org.junit.Test +import uniffi.web_transport.Client +import uniffi.web_transport.ClientConfig +import uniffi.web_transport.CongestionControl +import uniffi.web_transport.Server +import uniffi.web_transport.ServerConfig +import uniffi.web_transport.UniffiLib + +class ArtifactSmokeTest { + @Test + fun packagedClientServerLifecycle() = runBlocking { + withTimeout(30_000) { + val artifact = File(Client::class.java.protectionDomain.codeSource.location.toURI()) + assertTrue(artifact.isFile && artifact.extension == "jar", "Classes must load from the JVM JAR: $artifact") + val resourcePath = "${Platform.RESOURCE_PREFIX}/${System.mapLibraryName("web_transport_ffi")}" + val resource = assertNotNull( + Client::class.java.classLoader.getResource(resourcePath), + "Missing native resource $resourcePath in $artifact", + ) + assertEquals("jar", resource.protocol, "Native resource must be packaged in the JAR") + assertEquals(artifact.toURI(), (resource.openConnection() as JarURLConnection).jarFileURL.toURI()) + + Client(ClientConfig( + noCertVerification = true, + congestionControl = CongestionControl.DEFAULT, + )).use { client -> + // UniFFI has now loaded the library. Require JNA's extracted resource path. + val loaded = Native.getNativeLibrary(UniffiLib::class.java).file + assertEquals(File(System.getProperty("jna.tmpdir")).canonicalFile, loaded.canonicalFile.parentFile) + println("Loaded native resource from $resource via $loaded") + + try { + Server(ServerConfig( + certificateChain = listOf(File(requireNotNull(System.getenv("WEB_TRANSPORT_TEST_CERT")) { + "Missing WEB_TRANSPORT_TEST_CERT; run just test-kotlin to generate test credentials" + }).readBytes()), + privateKey = File(requireNotNull(System.getenv("WEB_TRANSPORT_TEST_KEY")) { + "Missing WEB_TRANSPORT_TEST_KEY; run just test-kotlin to generate test credentials" + }).readBytes(), + bind = "127.0.0.1:0", + congestionControl = CongestionControl.DEFAULT, + )).use { server -> + try { + val url = "https://127.0.0.1:${server.localAddr().port}/kotlin-smoke" + val accepted = async { + assertNotNull(server.accept()).use { request -> + assertEquals(url, request.url()) + request.accept() + } + } + client.connect(url).use { outgoing -> + accepted.await().use { incoming -> + try { + assertFalse(outgoing.isClosed()) + assertFalse(incoming.isClosed()) + val payload = "Kotlin JVM artifact smoke".encodeToByteArray() + val stream = outgoing.openBi() + stream.send.use { send -> + stream.recv.use { recv -> + send.write(payload) + send.finish() + val peer = incoming.acceptBi() + peer.send.use { reply -> + peer.recv.use { request -> + assertContentEquals(payload, request.readToEnd(1024uL)) + reply.write(payload) + reply.finish() + assertContentEquals(payload, recv.readToEnd(1024uL)) + } + } + } + } + } finally { + outgoing.close(0u, "smoke complete") + } + outgoing.waitClosed() + incoming.waitClosed() + assertTrue(outgoing.isClosed()) + assertTrue(incoming.isClosed()) + } + } + } finally { + server.close(0uL, "smoke cleanup") + server.waitClosed() + } + assertNull(server.accept(), "Closed server must stop accepting") + } + } finally { + client.close(0uL, "smoke cleanup") + client.waitClosed() + } + } + } + } +} diff --git a/quest/a1/bindings/README.md b/quest/a1/bindings/README.md index 2421e7c5..9ecc7899 100644 --- a/quest/a1/bindings/README.md +++ b/quest/a1/bindings/README.md @@ -11,5 +11,4 @@ The children own their language jobs. This epic owns the final coverage map and ## Required - [Node runtime CI](/quest/a1/bindings/node.md) - exercise the built package on Linux -- [Kotlin/JVM runtime CI](/quest/a1/bindings/kotlin.md) - exercise the built package on Linux - [Swift runtime CI](/quest/a1/bindings/swift.md) - exercise the built package on macOS diff --git a/quest/a1/bindings/kotlin.md b/quest/a1/bindings/kotlin.md deleted file mode 100644 index f26877de..00000000 --- a/quest/a1/bindings/kotlin.md +++ /dev/null @@ -1,11 +0,0 @@ -# [M] Test the Kotlin/JVM package at runtime in CI - -## Goal - -Pull requests exercise the built Kotlin/JVM binding on Linux, including loading and basic WebTransport lifecycle behavior. - -## Plan - -Generate the bindings and native library, build the JVM artifact and exercise a client/server smoke test through that artifact. Test loading from packaged resources. Fix README commands that reference a nonexistent `just kt` module. Android emulators are outside this initial scope. - -Wire the focused command into PR CI with useful failure output and cleanup. Any path filters must cover the shared Rust implementation, generation/build scripts and relevant workflow files. Preserve release packaging; do not require publishing to test a package. Document the local command alongside the binding. Keep this independently shippable from the other language jobs.