Skip to content

Commit

Permalink
add stringadapter test
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbei101 committed Oct 6, 2024
1 parent 9d3bbdc commit 7764fee
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/adapter/string_adapater.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
use crate::{
adapter::{Adapter, Filter},
error::{AdapterError, ModelError},
model::Model,
util::parse_csv_line,
Result,
};
use async_trait::async_trait;
use std::fmt::Write;

#[cfg(feature = "runtime-async-std")]
use async_std::{
fs::File as file,
io::prelude::*,
io::{
BufReader as ioBufReader, Error as ioError, ErrorKind as ioErrorKind,
},
path::Path as ioPath,
prelude::*,
};
type LoadPolicyFileHandler = fn(String, &mut dyn Model);
type LoadFilteredPolicyFileHandler<'a> =
fn(String, &mut dyn Model, f: &Filter<'a>) -> bool;

pub struct StringAdapter {
policy_string: String,
is_filtered: bool,
Expand Down Expand Up @@ -154,3 +178,64 @@ impl Adapter for StringAdapter {
self.is_filtered
}
}

fn load_policy_line(line: String, m: &mut dyn Model) {
if line.is_empty() || line.starts_with('#') {
return;
}

if let Some(tokens) = parse_csv_line(line) {
let key = &tokens[0];

if let Some(ref sec) = key.chars().next().map(|x| x.to_string()) {
if let Some(ast_map) = m.get_mut_model().get_mut(sec) {
if let Some(ast) = ast_map.get_mut(key) {
ast.policy.insert(tokens[1..].to_vec());
}
}
}
}
}

fn load_filtered_policy_line(
line: String,
m: &mut dyn Model,
f: &Filter<'_>,
) -> bool {
if line.is_empty() || line.starts_with('#') {
return false;
}

if let Some(tokens) = parse_csv_line(line) {
let key = &tokens[0];

let mut is_filtered = false;
if let Some(ref sec) = key.chars().next().map(|x| x.to_string()) {
if sec == "p" {
for (i, rule) in f.p.iter().enumerate() {
if !rule.is_empty() && rule != &tokens[i + 1] {
is_filtered = true;
}
}
}
if sec == "g" {
for (i, rule) in f.g.iter().enumerate() {
if !rule.is_empty() && rule != &tokens[i + 1] {
is_filtered = true;
}
}
}
if !is_filtered {
if let Some(ast_map) = m.get_mut_model().get_mut(sec) {
if let Some(ast) = ast_map.get_mut(key) {
ast.policy.insert(tokens[1..].to_vec());
}
}
}
}

is_filtered
} else {
false
}
}
33 changes: 33 additions & 0 deletions src/enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1583,4 +1583,37 @@ mod tests {
true
);
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg_attr(
all(feature = "runtime-async-std", not(target_arch = "wasm32")),
async_std::test
)]
#[cfg_attr(
all(feature = "runtime-tokio", not(target_arch = "wasm32")),
tokio::test
)]
async fn test_string_adapter() {
use crate::StringAdapter;

let m1 = DefaultModel::from_file("examples/basic_model.conf")
.await
.unwrap();
let basic_policy_str = r#"
p, alice, data1, read
p, bob, data2, write
"#;
let adapter1 = StringAdapter::new(basic_policy_str.to_string());
let mut e = Enforcer::new(m1, adapter1).await.unwrap();

assert_eq!(false, e.enforce(("root", "data1", "read")).unwrap());

let m2 = DefaultModel::from_file("examples/basic_with_root_model.conf")
.await
.unwrap();
let adapter2 = StringAdapter::new(basic_policy_str.to_string());
let e2 = Enforcer::new(m2, adapter2).await.unwrap();

e.model = e2.model;
assert_eq!(true, e.enforce(("root", "data1", "read")).unwrap());
}
}

0 comments on commit 7764fee

Please sign in to comment.