diff options
Diffstat (limited to 'wrappers/java-android')
| -rw-r--r-- | wrappers/java-android/.gitignore | 9 | ||||
| -rw-r--r-- | wrappers/java-android/Makefile | 31 | ||||
| -rw-r--r-- | wrappers/java-android/README.md | 84 | ||||
| -rw-r--r-- | wrappers/java-android/build.gradle | 23 | ||||
| -rw-r--r-- | wrappers/java-android/gradle.properties | 24 | ||||
| -rw-r--r-- | wrappers/java-android/gradle/wrapper/gradle-wrapper.jar | bin | 0 -> 59536 bytes | |||
| -rw-r--r-- | wrappers/java-android/gradle/wrapper/gradle-wrapper.properties | 5 | ||||
| -rw-r--r-- | wrappers/java-android/gradlew | 234 | ||||
| -rw-r--r-- | wrappers/java-android/gradlew.bat | 89 | ||||
| -rw-r--r-- | wrappers/java-android/lib/.gitignore | 2 | ||||
| -rw-r--r-- | wrappers/java-android/lib/CMakeLists.txt | 38 | ||||
| -rw-r--r-- | wrappers/java-android/lib/build.gradle | 107 | ||||
| -rw-r--r-- | wrappers/java-android/lib/consumer-rules.pro | 0 | ||||
| -rw-r--r-- | wrappers/java-android/lib/proguard-rules.pro | 21 | ||||
| -rw-r--r-- | wrappers/java-android/lib/src/main/AndroidManifest.xml | 4 | ||||
| -rw-r--r-- | wrappers/java-android/lib/src/test/java/org/eduvpn/common/VerifyTests.java | 77 | ||||
| -rw-r--r-- | wrappers/java-android/settings.gradle | 9 |
17 files changed, 757 insertions, 0 deletions
diff --git a/wrappers/java-android/.gitignore b/wrappers/java-android/.gitignore new file mode 100644 index 0000000..41445b2 --- /dev/null +++ b/wrappers/java-android/.gitignore @@ -0,0 +1,9 @@ +*.iml +.gradle/ +.idea/ +/build/ +/captures/ +.cxx/ +.externalNativeBuild/ +CMakeFiles/ +local.properties diff --git a/wrappers/java-android/Makefile b/wrappers/java-android/Makefile new file mode 100644 index 0000000..d249a50 --- /dev/null +++ b/wrappers/java-android/Makefile @@ -0,0 +1,31 @@ +.PHONY: build test android-test clean + +EXPORTS_PATH ?= ../../exports +include $(EXPORTS_PATH)/common.mk + +ifeq ($(NO_DAEMON),1) +override GRADLE_FLAGS += --no-daemon +endif + +build: + ./gradlew $(GRADLE_FLAGS) assembleRelease + +# Unit tests use library for desktop OS platform, so we still need .try-build-lib for this +# The unit tests find this library through the library path set in common.mk +unit-test: .try-build-lib + ./gradlew $(GRADLE_FLAGS) test + +android-test: + ./gradlew $(GRADLE_FLAGS) pixel2DebugAndroidTest + +connected-android-test: + ./gradlew $(GRADLE_FLAGS) connectedAndroidTest + +test: .try-build-lib + ./gradlew $(GRADLE_FLAGS) test pixel2DebugAndroidTest + +clean: + rm -rf lib/build lib/.cxx lib/CMakeFiles lib/src/test/resources/* +ifeq ($(CLEAN_ALL),1) + rm -rf .gradle/ +endif diff --git a/wrappers/java-android/README.md b/wrappers/java-android/README.md new file mode 100644 index 0000000..50e56fb --- /dev/null +++ b/wrappers/java-android/README.md @@ -0,0 +1,84 @@ +# Android (Java) wrapper + +## Requirements + +You will need to install the JDK compatible with Gradle and the Android Gradle plugin. Gradle specifies +a [maximum supported JDK version](https://docs.gradle.org/current/userguide/compatibility.html), while the Android +Gradle plugin specifies a [minimum supported JDK version](https://developer.android.com/studio/releases/gradle-plugin) ( +see 'Compatibility' table for the right Gradle version). Additionally, the Android Gradle plugin requires +a [certain Gradle version range](https://developer.android.com/studio/releases/gradle-plugin#updating-gradle). Lastly, ( +older versions of) Android Studio and especially IntelliJ Ultimate may not support some newer Android Gradle plugin +versions. + +If you see `Unsupported class file major version xx` then Gradle wants you to use an older Java version. If you +see `Android Gradle plugin requires Java xx to run.` then the Android Gradle plugin wants you to use a newer Java +version. + +See the [list of Gradle releases](https://github.com/gradle/gradle/releases) +and [list of Android Gradle plugin releases](https://maven.google.com/web/?q=com.android.tools.build#com.android.tools.build:gradle) +. + +Versions are managed per project by the Gradle wrapper (see `gradle*` files). Run `./gradlew --version` to get the +project Gradle version. Look at the dependencies in `/build.gradle` for the current Android Gradle plugin version. This +means that you do not need to install Gradle separately yourself. + +Currently, versions are Gradle 7.0.2 and Android Gradle plugin 7.0.4, which means you have to install a **JDK with a +version between 11 and 16**. If desired, these can be upgraded to e.g. 7.4 and 7.1.1 respectively without problems, but +as mentioned, IDE support may be limited with newer versions. The Gradle wrapper may be updated using +e.g. `./gradlew wrapper --gradle-version 7.4`. After that, the Android Gradle plugin may be updated by changing the +version in `/build.gradle`. + +You will also need the Android SDK, which comes with [Android Studio](https://developer.android.com/studio/). + +## Build & test + +Build AAR (Gradle will also run unit tests): + +```shell +make +``` + +This will build an AAR in `lib/build/outputs/aar`, which will include the shared Go library for all Android +architectures, which it will build using the Android NDK via CMake, which calls `make` with the right compiler. + +Run unit tests without an Android emulator: + +```shell +make unit-test +``` + +This will build the library for your current desktop OS and use that. + +Run Android instrumented tests on a new emulator: + +```shell +make android-test +``` + +This uses [Gradle managed virtual devices](https://developer.android.com/studio/preview/features#gmd). This feature is +enabled in `gradle.properties`. It is normal that tests that pass are not logged. + +Run Android instrumented tests on an already running emulator: + +```shell +make connected-android-test +``` + +This will be faster when used multiple times, as the emulator is reused. + +Run both unit tests and Android instrumented tests on a new emulator: + +```shell +make test +``` + +For all commands you can specify options to pass to Gradle via `GRADLE_FLAGS=`. Specify `NO_DAEMON=1` to +add `--no-daemon`. + +## Notes + +The same Java code is used for the Android instrumented tests as for the unit tests. Both use Java resources that are +copied from the `../../test_data` folder by Gradle. + +This library uses JNA, not JNI. Hence, there is no C wrapper. The library is dynamically opened with `dlopen` +via `libjnidispatch.so` which comes with the JNA AAR. diff --git a/wrappers/java-android/build.gradle b/wrappers/java-android/build.gradle new file mode 100644 index 0000000..c9f995e --- /dev/null +++ b/wrappers/java-android/build.gradle @@ -0,0 +1,23 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +buildscript { + repositories { + google() + mavenCentral() + } + dependencies { + classpath 'com.android.tools.build:gradle:7.0.4' + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + tasks.withType(JavaCompile) { + options.deprecation = true + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/wrappers/java-android/gradle.properties b/wrappers/java-android/gradle.properties new file mode 100644 index 0000000..4b5f10f --- /dev/null +++ b/wrappers/java-android/gradle.properties @@ -0,0 +1,24 @@ +# Project-wide Gradle settings. +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true +# AndroidX package structure to make it clearer which packages are bundled with the +# Android operating system, and which are packaged with your app's APK +# https://developer.android.com/topic/libraries/support-library/androidx-rn +android.useAndroidX=true +# Automatically convert third-party libraries to use AndroidX +android.enableJetifier=true +# Enable all warnings +org.gradle.warning.mode=all +# Enable Gradle managed devices for older Android plugin versions +android.experimental.androidTest.useUnifiedTestPlatform=true +# To test with ATD images (but native stuff seems to malfunction?): android.sdk.channel=3 diff --git a/wrappers/java-android/gradle/wrapper/gradle-wrapper.jar b/wrappers/java-android/gradle/wrapper/gradle-wrapper.jar Binary files differnew file mode 100644 index 0000000..7454180 --- /dev/null +++ b/wrappers/java-android/gradle/wrapper/gradle-wrapper.jar diff --git a/wrappers/java-android/gradle/wrapper/gradle-wrapper.properties b/wrappers/java-android/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..0f80bbf --- /dev/null +++ b/wrappers/java-android/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/wrappers/java-android/gradlew b/wrappers/java-android/gradlew new file mode 100644 index 0000000..1b6c787 --- /dev/null +++ b/wrappers/java-android/gradlew @@ -0,0 +1,234 @@ +#!/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. +# + +############################################################################## +# +# 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/master/subprojects/plugins/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 + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# 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 + which java >/dev/null 2>&1 || 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 + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + 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 + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# 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/wrappers/java-android/gradlew.bat b/wrappers/java-android/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/wrappers/java-android/gradlew.bat @@ -0,0 +1,89 @@ +@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 + +@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=. +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="-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%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +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%"=="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! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/wrappers/java-android/lib/.gitignore b/wrappers/java-android/lib/.gitignore new file mode 100644 index 0000000..1ac6136 --- /dev/null +++ b/wrappers/java-android/lib/.gitignore @@ -0,0 +1,2 @@ +/build +/src/test/resources/* diff --git a/wrappers/java-android/lib/CMakeLists.txt b/wrappers/java-android/lib/CMakeLists.txt new file mode 100644 index 0000000..9bea062 --- /dev/null +++ b/wrappers/java-android/lib/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 3.18.1) +project(eduvpn_common) + +set(CMAKE_VERBOSE_MAKEFILE on) + +# Android -> Go architecture map +set(arch_map_x86 386) +set(arch_map_x86_64 amd64) +set(arch_map_arm arm) +set(arch_map_arm64 arm64) + +set(GOARCH ${arch_map_${ANDROID_ARCH_NAME}}) + +find_program(MAKE_EXECUTABLE + NAMES gmake mingw32-make make + NAMES_PER_DIR + DOC "GNU Make" + REQUIRED +) + +# Inspired by https://github.com/WireGuard/wireguard-android/blob/1.0.20211029/tunnel/tools/CMakeLists.txt + +# --target has to be specified to compiler & linker as e.g. ANDROID_C_COMPILER may just be 'clang' without prefixes +# CGO_CPPFLAGS are concatenated to CGO_CFLAGS and CGO_CXXFLAGS +add_custom_target(shared-lib + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../../../exports" + COMMENT "Building shared library for ${ANDROID_LLVM_TRIPLE}" + VERBATIM + COMMAND ${MAKE_EXECUTABLE} + GOOS=android GOARCH=${GOARCH} + CC=${ANDROID_C_COMPILER} CXX=${ANDROID_CXX_COMPILER} + CGO_CPPFLAGS=--target=${ANDROID_LLVM_TRIPLE} CGO_CFLAGS=${CMAKE_C_FLAGS} CGO_CXXFLAGS=${CMAKE_CXX_FLAGS} + CGO_LDFLAGS=${CMAKE_SHARED_LINKER_FLAGS}\ --target=${ANDROID_LLVM_TRIPLE} + COPY_LIB_TO=${CMAKE_LIBRARY_OUTPUT_DIRECTORY} +) + +# Note about COPY_LIB_TO: this is an easy cross-platform alternative to calling `cp` +# file(COPY ...) does not work since it runs at the configure stage... diff --git a/wrappers/java-android/lib/build.gradle b/wrappers/java-android/lib/build.gradle new file mode 100644 index 0000000..be666aa --- /dev/null +++ b/wrappers/java-android/lib/build.gradle @@ -0,0 +1,107 @@ +import com.android.build.api.dsl.ManagedVirtualDevice + +plugins { + id 'com.android.library' // Build AAR +} + +android { + compileSdk 31 + + defaultConfig { + minSdk 21 + targetSdk 31 + versionCode 1 + versionName '1.0' + + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' + + consumerProguardFiles 'consumer-rules.pro' + + externalNativeBuild { + cmake { + // Specify which target we want to build + targets 'shared-lib' + } + } + } + + buildTypes { + release { + minifyEnabled true + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + + // Support Java 8+ + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + + externalNativeBuild { + cmake { + version '3.18.1' // See cmake_minimum_required in CMakeLists.txt + path 'CMakeLists.txt' + } + } + + sourceSets { + androidTest { + // Use same sources & resources for Android instrumented tests as unit tests + java.srcDirs = sourceSets.test.java.srcDirs + resources.srcDirs = sourceSets.test.resources.srcDirs + } + } + + task copyTestData(type: Copy, description: 'Copy test_data to test resources') { + from('../../../test_data') { + exclude '**/*.py', '**/*.sh' + } + into sourceSets.test.resources.srcDirs[0].toPath().resolve('org/eduvpn/common') + } + + // Copy test_data to Java resources before these are processed + // (.named does not find all tasks. Task names were obtained with com.dorongold.task-tree plugin) + tasks.matching { t -> + t.name in [ + 'processDebugUnitTestJavaRes', 'processReleaseUnitTestJavaRes', + 'processDebugAndroidTestJavaRes', 'processReleaseAndroidTestJavaRes'] + }.all { + dependsOn copyTestData + } + + // Do not cache unit test results as the shared library may have changed + tasks.matching { t -> t.name in ['testDebugUnitTest', 'testReleaseUnitTest'] }.all { + outputs.upToDateWhen { false } + } + + testOptions { + // Display full exceptions and passed tests for unit tests + unitTests.all { + testLogging { + events 'passed', 'skipped', 'failed' + exceptionFormat 'full' + } + } + + devices { + pixel2(ManagedVirtualDevice) { + device = 'Pixel 2' + apiLevel = 30 + systemImageSource = 'aosp' + abi = 'x86_64' + } + } + } +} + +dependencies { + implementation 'net.java.dev.jna:jna:5.10.0@aar' + + testImplementation 'commons-io:commons-io:2.11.0' + testImplementation 'net.java.dev.jna:jna:5.10.0' // Include jnidispatch library in unit tests + testImplementation 'junit:junit:4.+' + + androidTestImplementation 'commons-io:commons-io:2.11.0' + androidTestImplementation 'androidx.test:runner:1.4.0' +} diff --git a/wrappers/java-android/lib/consumer-rules.pro b/wrappers/java-android/lib/consumer-rules.pro new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/wrappers/java-android/lib/consumer-rules.pro diff --git a/wrappers/java-android/lib/proguard-rules.pro b/wrappers/java-android/lib/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/wrappers/java-android/lib/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/wrappers/java-android/lib/src/main/AndroidManifest.xml b/wrappers/java-android/lib/src/main/AndroidManifest.xml new file mode 100644 index 0000000..5a49838 --- /dev/null +++ b/wrappers/java-android/lib/src/main/AndroidManifest.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest package="org.eduvpn.common"> + +</manifest> diff --git a/wrappers/java-android/lib/src/test/java/org/eduvpn/common/VerifyTests.java b/wrappers/java-android/lib/src/test/java/org/eduvpn/common/VerifyTests.java new file mode 100644 index 0000000..a73ac75 --- /dev/null +++ b/wrappers/java-android/lib/src/test/java/org/eduvpn/common/VerifyTests.java @@ -0,0 +1,77 @@ +package org.eduvpn.common; + +import org.apache.commons.io.IOUtils; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class VerifyTests { + private static byte[] readAll(String resource) throws IOException { + try (InputStream stream = VerifyTests.class.getResourceAsStream(resource)) { + return IOUtils.toByteArray(stream); + } + } + + @SuppressWarnings("OptionalGetWithoutIsPresent") + @BeforeClass + public static void oneTimeSetup() throws IOException { + try (BufferedReader reader = new BufferedReader(new InputStreamReader( + VerifyTests.class.getResourceAsStream("public.key")))) { + Discovery.insecureTestingSetExtraKey(reader.lines().reduce((a, b) -> b).get()); + } + } + + @Test + public void testValid() throws IOException, VerifyException { + Discovery.verify( + readAll("server_list.json.minisig"), + readAll("server_list.json"), + "server_list.json", + 0 + ); + } + + @Test(expected = InvalidSignatureException.class) + public void testInvalidSignature() throws IOException, VerifyException { + Discovery.verify( + readAll("random.txt"), + readAll("server_list.json"), + "server_list.json", + 0 + ); + } + + @Test(expected = InvalidSignatureUnknownKeyException.class) + public void testWrongKey() throws IOException, VerifyException { + Discovery.verify( + readAll("server_list.json.wrong_key.minisig"), + readAll("server_list.json"), + "server_list.json", + 0 + ); + } + + @Test(expected = SignatureTooOldException.class) + public void testOldSignature() throws IOException, VerifyException { + Discovery.verify( + readAll("server_list.json.minisig"), + readAll("server_list.json"), + "server_list.json", + Long.MAX_VALUE + ); + } + + @Test(expected = IllegalArgumentException.class) + public void testUnknownExpectedFile() throws IOException, VerifyException { + Discovery.verify( + readAll("other_list.json.minisig"), + readAll("other_list.json"), + "other_list.json", + 0 + ); + } +} diff --git a/wrappers/java-android/settings.gradle b/wrappers/java-android/settings.gradle new file mode 100644 index 0000000..b603bb3 --- /dev/null +++ b/wrappers/java-android/settings.gradle @@ -0,0 +1,9 @@ +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + google() + mavenCentral() + } +} +rootProject.name = 'eduvpn_common' +include ':lib' |
