50 lines
2.7 KiB
Rust
50 lines
2.7 KiB
Rust
use super::*;
|
|
|
|
#[test]
|
|
fn token_random_effects_follow_actual_calls_and_navigate_bundled_source() {
|
|
let (project, program) = checked(include_str!("../../native/token.dever"), "token-effects");
|
|
for name in [
|
|
"dever.crypto.app.token",
|
|
"main.main.app.generate",
|
|
"main.main.app.handled",
|
|
] {
|
|
let function = program
|
|
.functions
|
|
.iter()
|
|
.find(|f| f.signature.source_name == name)
|
|
.unwrap();
|
|
assert!(function.effects.random && !function.effects.pure());
|
|
assert!(!function.effects.blocking && !function.effects.suspends);
|
|
}
|
|
let path = project.sources[0].path.to_str().unwrap();
|
|
success(cli(&["fmt", path]));
|
|
success(cli(&["fmt", path, "--check"]));
|
|
let index =
|
|
dever::compiler::inspect::analyze(&project, Some("main.main.app.generate"), 2, true)
|
|
.unwrap();
|
|
let text = serde_json::to_string(&index).unwrap();
|
|
assert!(text.contains("效果") && text.contains("dever.crypto.app.token"));
|
|
}
|
|
|
|
#[test]
|
|
fn tokens_cannot_hide_random_effects_or_escape_secret_and_failure_boundaries() {
|
|
let cases = [
|
|
"bad() (out: Secret) { out = dever.crypto.token(32) }",
|
|
"flow bad() (out: Bytes)\n| step | kind | do |\n| out | calc | dever.crypto.hmacSha256(dever.crypto.token(32), dever.bytes.fromText(\"message\")) |",
|
|
"flow bad() (out: Secret)\n| step | kind | do |\n| out | calc | parallel(dever.crypto.token(32)) |",
|
|
"flow bad() (out: Secret)\n| step | kind | do |\n| out | calc | blocking(dever.crypto.token(32)) |",
|
|
"flow bad() (out: Secret)\n| step | kind | do |\n| out | flow | dever.crypto.token(32) |\ncmd bad = bad",
|
|
"flow bad() (out: Text)\n| step | kind | do |\n| key | flow | dever.crypto.token(32) |\n| out | calc | dever.json.encode(key) |",
|
|
"type Attempt { Ready(value: Secret), error Failed(value: dever.crypto.TokenResult) }\nflow bad() (out: Int)\n| step | kind | do |\n| value: Attempt | flow | result(dever.crypto.token(15)) |\n| out | calc | 1 |",
|
|
"apply(create: handler(bytes: Int) (value: Secret)) (out: Secret) { out = create(32) }\nflow bad() (out: Secret)\n| step | kind | do |\n| out | calc | apply(dever.crypto.token) |",
|
|
];
|
|
for (index, source) in cases.iter().enumerate() {
|
|
let path = directory(&format!("invalid-token-{index}")).join("app.dever");
|
|
fs::write(&path, source).unwrap();
|
|
let project = Project::load(&path).unwrap();
|
|
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
|
let errors = program::analyze(&project.declarations, &project.modules).diagnostics;
|
|
assert!(!errors.is_empty(), "{index}: {source}");
|
|
}
|
|
}
|