Skip to content

Commit

Permalink
Add samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Tipnos committed May 24, 2024
1 parent 16748c9 commit 97d0a9f
Show file tree
Hide file tree
Showing 8 changed files with 957 additions and 14 deletions.
148 changes: 148 additions & 0 deletions biscuit-auth/examples/testcases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ fn main() {

add_test_result(&mut results, null(&target, &root, test));

add_test_result(&mut results, heterogeneous_equal(&target, &root, test));

add_test_result(&mut results, heterogeneous_not_equal(&target, &root, test));

add_test_result(&mut results, strict_equal(&target, &root, test));

add_test_result(&mut results, strict_not_equal(&target, &root, test));

if json {
let s = serde_json::to_string_pretty(&TestCases {
root_private_key: hex::encode(root.private().to_bytes()),
Expand Down Expand Up @@ -2033,6 +2041,146 @@ fn null(target: &str, root: &KeyPair, test: bool) -> TestResult {
}
}

fn heterogeneous_equal(target: &str, root: &KeyPair, test: bool) -> TestResult {
let mut rng: StdRng = SeedableRng::seed_from_u64(1234);
let title = "test heterogeneous equal".to_string();
let filename = "test031_heterogeneous_equal".to_string();
let token;

let biscuit = biscuit!(
r#"
check if fact(1, $value), 1 == $value;
"#
)
.build_with_rng(&root, SymbolTable::default(), &mut rng)
.unwrap();
token = print_blocks(&biscuit);

let data = write_or_load_testcase(target, &filename, root, &biscuit, test);

let mut validations = BTreeMap::new();
validations.insert(
"authorized same type".to_string(),
validate_token(root, &data[..], "fact(1, 1); allow if true"),
);
validations.insert(
"unauthorized failed logic different type".to_string(),
validate_token(root, &data[..], "fact(1, true); allow if true"),
);

TestResult {
title,
filename,
token,
validations,
}
}

fn heterogeneous_not_equal(target: &str, root: &KeyPair, test: bool) -> TestResult {
let mut rng: StdRng = SeedableRng::seed_from_u64(1234);
let title = "test heterogeneous not equal".to_string();
let filename = "test032_heterogeneous_not_equal".to_string();
let token;

let biscuit = biscuit!(
r#"
check if fact(1, $value), 1 != $value;
"#
)
.build_with_rng(&root, SymbolTable::default(), &mut rng)
.unwrap();
token = print_blocks(&biscuit);

let data = write_or_load_testcase(target, &filename, root, &biscuit, test);

let mut validations = BTreeMap::new();
validations.insert(
"unauthorized failed logic same type".to_string(),
validate_token(root, &data[..], "fact(1, 1); allow if true"),
);
validations.insert(
"authorized different type".to_string(),
validate_token(root, &data[..], "fact(1, true); allow if true"),
);

TestResult {
title,
filename,
token,
validations,
}
}

fn strict_equal(target: &str, root: &KeyPair, test: bool) -> TestResult {
let mut rng: StdRng = SeedableRng::seed_from_u64(1234);
let title = "test strict equal".to_string();
let filename = "test033_strict_equal".to_string();
let token;

let biscuit = biscuit!(
r#"
check if fact(1, $value), 1 === $value;
"#
)
.build_with_rng(&root, SymbolTable::default(), &mut rng)
.unwrap();
token = print_blocks(&biscuit);

let data = write_or_load_testcase(target, &filename, root, &biscuit, test);

let mut validations = BTreeMap::new();
validations.insert(
"authorized same type".to_string(),
validate_token(root, &data[..], "fact(1, 1); allow if true"),
);
validations.insert(
"error invalid type".to_string(),
validate_token(root, &data[..], "fact(1, true); allow if true"),
);

TestResult {
title,
filename,
token,
validations,
}
}

fn strict_not_equal(target: &str, root: &KeyPair, test: bool) -> TestResult {
let mut rng: StdRng = SeedableRng::seed_from_u64(1234);
let title = "test strict not equal".to_string();
let filename = "test034_strict_not_equal".to_string();
let token;

let biscuit = biscuit!(
r#"
check if fact(1, $value), 1 !== $value;
"#
)
.build_with_rng(&root, SymbolTable::default(), &mut rng)
.unwrap();
token = print_blocks(&biscuit);

let data = write_or_load_testcase(target, &filename, root, &biscuit, test);

let mut validations = BTreeMap::new();
validations.insert(
"unauthorized failed logic same type".to_string(),
validate_token(root, &data[..], "fact(1, 1); allow if true"),
);
validations.insert(
"error invalid type".to_string(),
validate_token(root, &data[..], "fact(1, true); allow if true"),
);

TestResult {
title,
filename,
token,
validations,
}
}

fn print_blocks(token: &Biscuit) -> Vec<BlockContent> {
let mut v = Vec::new();

Expand Down
Loading

0 comments on commit 97d0a9f

Please sign in to comment.