Skip to content

Commit

Permalink
build(ci): better android finish test check
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
Berend Sliedrecht authored and karimStekelenburg committed Feb 23, 2024
1 parent d6ca21f commit ff2cf2d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 84 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/android_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ adb shell am start -a android.intent.action.MAIN -n "id.animo.example.android/an

sleep 30

adb logcat *:E android:V -d | tee ~/logcat.log
adb logcat RustStdoutStderr:D '*:S' | tee ~/logcat.log

if grep 'android' ~/logcat.log;
if grep 'RustStdoutStderr' ~/logcat.log;
then
echo "App running"
else
echo "::error:: App not running"
exit 1
fi

MSG=$(grep -e 'RustPanic' "$HOME"/logcat.log)
MSG=$(grep -e 'panicked' "$HOME"/logcat.log)
if [ -z "${MSG}" ]; then
exit 0
else
echo "::error:: Rust panicked! Tests failed. Logs will be uploaded"
exit 1
else
exit 0
fi
45 changes: 0 additions & 45 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,48 +106,3 @@ jobs:
cache-on-failure: true

- run: cargo check --target=${{ matrix.target }}

test-android:
name: Test Android

# macos is used here as it allows for
# hardware acceleration on the Android emulator
runs-on: macos-latest

steps:
- uses: actions/checkout@v4

- name: Install Rust Toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ env.RUST_VERSION }}
targets: x86_64-linux-android

- name: Install Cargo APK
run: cargo install cargo-apk

- name: Cargo apk build
run: cargo apk build --target x86_64-linux-android --manifest-path ./examples/android/Cargo.toml

- name: Cache cargo resources
uses: Swatinem/rust-cache@v2
with:
shared-key: deps
cache-on-failure: true

- name: Run Android Emulator and test
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 31
arch: x86_64
emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
disable-animations: true
force-avd-creation: false
script: ./.github/workflows/android_test.sh ./examples/android/target/debug/apk/android.apk

- name: Upload emulator logs
uses: actions/upload-artifact@v3
if: ${{ always() }}
with:
name: log
path: ~/logcat.log
26 changes: 7 additions & 19 deletions examples/android/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
use android_activity::AndroidApp;
use secure_env::{
error::SecureEnvResult, key::KeyOps, secure_environment::SecureEnvironmentOps,
SecureEnvironment,
};
use secure_env::{key::KeyOps, secure_environment::SecureEnvironmentOps, SecureEnvironment};

fn main() -> SecureEnvResult<()> {
let k = SecureEnvironment::generate_keypair("some-id")?;
let n_k = SecureEnvironment::get_keypair_by_id("some-id")?;
#[no_mangle]
fn android_main(_app: AndroidApp) {
let k = SecureEnvironment::generate_keypair("some-id").unwrap();
let n_k = SecureEnvironment::get_keypair_by_id("some-id").unwrap();

let k = k.get_public_key()?;
let n_k = n_k.get_public_key()?;
let k = k.get_public_key().unwrap();
let n_k = n_k.get_public_key().unwrap();

assert_eq!(k, n_k);
println!("Created and got key and they are equal");

Ok(())
}

#[no_mangle]
fn android_main(_app: AndroidApp) {
match main() {
Ok(_) => println!("Success!"),
Err(e) => eprintln!("Error!: {e:?}"),
}
}
52 changes: 38 additions & 14 deletions src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,49 @@ lazy_static::lazy_static! {
unsafe { jni::JavaVM::from_raw(ndk_context::android_context().vm().cast()) }.unwrap();
}

macro_rules! jni_call_method {
($env:expr, $cls:expr, $method:ident, $args:expr, $ret_typ:ident, $err:ident) => {
$env.call_method($cls, $method, concat_idents!($method, _SIG), $args)
.and_then(|v| v.$ret_typ())
.map_err(|e| $crate::error::SecureEnvError::$err(Some(e.to_string())))
macro_rules! jni_handle_error {
($env:expr, $err:ident) => {
if $env
.exception_check()
.map_err(|e| $crate::error::SecureEnvError::$err(Some(e.to_string())))?
{
let throwable = $env
.exception_occurred()
.map_err(|e| $crate::error::SecureEnvError::$err(Some(e.to_string())))?;
$env.exception_clear()
.map_err(|e| $crate::error::SecureEnvError::$err(Some(e.to_string())))?;

let message = $env
.call_method(
&throwable,
EXCEPTION_TO_STRING,
EXCEPTION_TO_STRING_SIG,
&[],
)
.and_then(|v| v.l())
.unwrap();

let msg_rust: String = $env
.get_string(&message.into())
.map_err($crate::error::SecureEnvError::UnableToCreateJavaValue)?
.into();

return Err($crate::error::SecureEnvError::$err(Some(msg_rust)));
}
};
}

macro_rules! jni_call_method {
($env:expr, $cls:expr, $method:ident, $args:expr, $ret_typ:ident, $err:ident) => {{
let res = $env
.call_method($cls, $method, concat_idents!($method, _SIG), $args)
($env:expr, $cls:expr, $method:ident, $args:expr, $ret_typ:ident, $err:ident) => {
$env.call_method($cls, $method, concat_idents!($method, _SIG), $args)
.map_err(|e| $crate::error::SecureEnvError::$err(Some(e.to_string())))
.and_then(|v| {
jni_handle_error!($env, $err);

v.$ret_typ()
.map_err(|e| $crate::error::SecureEnvError::$err(Some(e.to_string())))
});

jni_handle_error!($env, $err);

res
}};
})
};

($env:expr, $cls:expr, $method:ident, $ret_typ:ident, $err:ident) => {
jni_call_method!($env, $cls, $method, &[], $ret_typ, $err)
Expand Down Expand Up @@ -123,6 +142,11 @@ impl SecureEnvironmentOps<Key> for SecureEnvironment {
.map_err(SecureEnvError::UnableToAttachJVMToThread)?;

let ctx = ndk_context::android_context().context() as jni::sys::jobject;
if ctx.is_null() || !ctx.is_aligned() {
return Err(SecureEnvError::UnableToGenerateKey(Some(
"Could not acquire context. Null, or unaligned pointer, was found".to_owned(),
)));
}
let ctx = unsafe { JObject::from_raw(ctx) };

let id = id.into();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(concat_idents)]
#![feature(concat_idents, pointer_is_aligned)]

#[cfg(not(any(target_os = "android", target_os = "ios")))]
compile_error!("Only Android and iOS are supported targets");
Expand Down

0 comments on commit ff2cf2d

Please sign in to comment.