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
4 changes: 2 additions & 2 deletions .github/workflows/run_cedar_java_reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion CedarJavaFFI/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
30 changes: 30 additions & 0 deletions CedarJavaFFI/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JavaVM> = 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");
}
}
}
35 changes: 35 additions & 0 deletions CedarJavaFFI/src/jvm_test_utils.rs
Original file line number Diff line number Diff line change
@@ -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<JavaVM, jni::errors::StartJvmError>` - A Result containing either:
/// * `JavaVM` - The successfully created JVM instance
/// * `StartJvmError` - Error that occurred during JVM creation
///
pub(crate) fn create_jvm() -> Result<JavaVM, jni::errors::StartJvmError> {
let jvm_args = InitArgsBuilder::new()
.option("-Xcheck:jni")
.build()
.unwrap();

let jvm = JavaVM::new(jvm_args)?;
Ok(jvm)
}
1 change: 1 addition & 0 deletions CedarJavaFFI/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod answer;
mod interface;
mod jlist;
mod jset;
mod jvm_test_utils;
mod objects;
mod tests;
mod utils;
Expand Down