diff --git a/.github/workflows/run_cedar_java_reusable.yml b/.github/workflows/run_cedar_java_reusable.yml index 4b81772e..972757ca 100644 --- a/.github/workflows/run_cedar_java_reusable.yml +++ b/.github/workflows/run_cedar_java_reusable.yml @@ -31,12 +31,12 @@ jobs: - name: Checkout Cedar Java uses: actions/checkout@v4 with: - repository: cedar-policy/cedar-java + repository: muditchaudhary/cedar-java ref: ${{ inputs.cedar_java_ref }} - name: Checkout cedar-policy uses: actions/checkout@v4 with: - repository: cedar-policy/cedar + repository: muditchaudhary/cedar ref: ${{ inputs.cedar_policy_ref }} path: ./cedar - name: Prepare Rust Build diff --git a/CedarJavaFFI/Cargo.toml b/CedarJavaFFI/Cargo.toml index 67445b1a..1bfc6884 100644 --- a/CedarJavaFFI/Cargo.toml +++ b/CedarJavaFFI/Cargo.toml @@ -15,7 +15,7 @@ thiserror = "2.0" itertools = "0.14" # JNI Support -jni = "0.21.0" +jni = { version = "0.21.1", features = ["invocation"] } jni_fn = "0.1.0" [features] diff --git a/CedarJavaFFI/src/interface.rs b/CedarJavaFFI/src/interface.rs index 94e30491..d11e02df 100644 --- a/CedarJavaFFI/src/interface.rs +++ b/CedarJavaFFI/src/interface.rs @@ -623,3 +623,33 @@ fn policies_str_to_pretty_internal<'a>( } } } + +#[cfg(test)] +mod interface_tests { + use super::*; + use crate::jvm_test_utils::*; + use jni::JavaVM; + use std::sync::LazyLock; + + // Static JVM to be used by all the tests. LazyLock for lazy thread-safe lazy initialization + static JVM: LazyLock = LazyLock::new(|| create_jvm().unwrap()); + + mod policy_tests { + use super::*; + + fn policy_effect_test_util(env: &mut JNIEnv, policy: &str, expected_effect: &str) { + let policy_string = env.new_string(policy).unwrap(); + let effect_result = policy_effect_jni_internal(env, policy_string).unwrap(); + let effect_jstr = JString::cast(env, effect_result.l().unwrap()).unwrap(); + let effect = String::from(env.get_string(&effect_jstr).unwrap()); + assert_eq!(effect, expected_effect); + } + + #[test] + fn policy_effect_tests() { + let mut env = JVM.attach_current_thread().unwrap(); + policy_effect_test_util(&mut env, "permit(principal,action,resource);", "permit"); + policy_effect_test_util(&mut env, "forbid(principal,action,resource);", "forbid"); + } + } +} diff --git a/CedarJavaFFI/src/jvm_test_utils.rs b/CedarJavaFFI/src/jvm_test_utils.rs new file mode 100644 index 00000000..1c57386f --- /dev/null +++ b/CedarJavaFFI/src/jvm_test_utils.rs @@ -0,0 +1,35 @@ +/* + * Copyright Cedar Contributors + * + * 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. + */ +#![cfg(test)] + +use jni::{InitArgsBuilder, JavaVM}; + +/// Creates a new Java Virtual Machine (JVM) instance with basic configuration for tests +/// +/// # Returns +/// * `Result` - A Result containing either: +/// * `JavaVM` - The successfully created JVM instance +/// * `StartJvmError` - Error that occurred during JVM creation +/// +pub(crate) fn create_jvm() -> Result { + let jvm_args = InitArgsBuilder::new() + .option("-Xcheck:jni") + .build() + .unwrap(); + + let jvm = JavaVM::new(jvm_args)?; + Ok(jvm) +} diff --git a/CedarJavaFFI/src/lib.rs b/CedarJavaFFI/src/lib.rs index 11f0b6f8..194b0ace 100644 --- a/CedarJavaFFI/src/lib.rs +++ b/CedarJavaFFI/src/lib.rs @@ -19,6 +19,7 @@ mod answer; mod interface; mod jlist; mod jset; +mod jvm_test_utils; mod objects; mod tests; mod utils;