Repository navigation
chore: test improvements #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| //! Every `VmError` a constructor can build must be one `is_valid_` accepts: | ||
| //! the constructors are what this executor emits, `is_valid_` is what a | ||
| //! validator checks a leader-proposed code against. | ||
|
|
||
| use genlayer_sdk::abi::consts::VmError; | ||
|
|
||
| fn assert_valid(err: VmError) { | ||
| let code: String = err.into(); | ||
| assert!( | ||
| VmError::is_valid_(&code), | ||
| "constructed code is rejected: {code:?}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn static_codes_are_valid() { | ||
| assert_valid(VmError::timeout()); | ||
| assert_valid(VmError::absent()); | ||
|
|
||
| assert_valid(VmError::oom().val()); | ||
| assert_valid(VmError::oom().storage()); | ||
| assert_valid(VmError::oom().ram().val()); | ||
| assert_valid(VmError::oom().ram().table()); | ||
| assert_valid(VmError::oom().ram().memory()); | ||
| assert_valid(VmError::oom().ram().limit()); | ||
| assert_valid(VmError::oom().receipt().nondet_output()); | ||
| assert_valid(VmError::oom().receipt().message().internal()); | ||
| assert_valid(VmError::oom().receipt().message().external()); | ||
| assert_valid(VmError::oom().fees().internal()); | ||
| assert_valid(VmError::oom().fees().external()); | ||
|
|
||
| assert_valid(VmError::invalid_contract().val()); | ||
| assert_valid(VmError::invalid_contract().absent_runner_comment()); | ||
| assert_valid(VmError::invalid_contract().not_utf8_text()); | ||
| assert_valid(VmError::invalid_contract().malformed_runner()); | ||
| assert_valid(VmError::invalid_contract().major_mismatch()); | ||
| assert_valid(VmError::invalid_contract().wasm().validating()); | ||
| assert_valid(VmError::invalid_contract().wasm().linking()); | ||
| assert_valid(VmError::invalid_contract().wasm().entrypoint()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn exit_code_is_valid() { | ||
| assert_valid(VmError::exit_code().val_i32(0)); | ||
| assert_valid(VmError::exit_code().val_i32(-1)); | ||
| assert_valid(VmError::exit_code().val_i32(i32::MAX)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn wasm_trap_is_valid() { | ||
| assert_valid(VmError::wasm_trap().val_str("fault")); | ||
| assert_valid(VmError::wasm_trap().val_str("unreachable code executed")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn host_is_valid() { | ||
| assert_valid(VmError::host().val_str("i_o_error")); | ||
| } | ||
|
|
||
| // ── Empty description: the one input a `$str` constructor must not take ──── | ||
|
|
||
| #[cfg(debug_assertions)] | ||
| #[test] | ||
| #[should_panic(expected = "non-empty description")] | ||
| fn wasm_trap_rejects_an_empty_description() { | ||
| let _ = VmError::wasm_trap().val_str(""); | ||
| } | ||
|
|
||
| #[cfg(debug_assertions)] | ||
| #[test] | ||
| #[should_panic(expected = "non-empty description")] | ||
| fn host_rejects_an_empty_description() { | ||
| let _ = VmError::host().val_str(""); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| //! The precompile cache is read with `Module::deserialize_file`, which trusts the | ||
| //! file as native code. Only runners the precompiler actually writes artifacts | ||
| //! for, the ones listed in `all.json`, may derive a path into it. | ||
|
|
||
| use genvm::runners; | ||
|
|
||
| const HASH: &str = "cnn3rjeozkptmzmzt4ymzznvkqfdedcvpapscmyt6r6cyzjrzsgq"; | ||
|
|
||
| fn unique_dir(tag: &str) -> std::path::PathBuf { | ||
| static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); | ||
| let n = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| let path = std::env::temp_dir().join(format!("genvm-{tag}-{}-{n}", std::process::id())); | ||
| std::fs::create_dir_all(&path).unwrap(); | ||
| path | ||
| } | ||
|
|
||
| /// A registry holding exactly one builtin runner, `py-genlayer:HASH`. | ||
| fn registry_with_one_builtin() -> (std::path::PathBuf, runners::cache::Reader) { | ||
| let root = unique_dir("registry"); | ||
| let runners_dir = root.join("runners"); | ||
| std::fs::create_dir_all(&runners_dir).unwrap(); | ||
| std::fs::write( | ||
| root.join("all.json"), | ||
| format!(r#"{{"py-genlayer": ["{HASH}"]}}"#), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let reader = runners::cache::Reader::new(&runners_dir, &root, false).unwrap(); | ||
| (root, reader) | ||
| } | ||
|
|
||
| #[test] | ||
| fn verify_runner_alone_accepts_custom_ids() { | ||
| // This is why the registry lookup exists: `custom:<hash>`, an id an attacker | ||
| // picks via RegisterRunner, is a well-formed `name:hash` pair, so the charset | ||
| // check waves it through. `chain:` and `contract` are excluded only | ||
| // incidentally, by their colon count. | ||
| assert_eq!( | ||
| runners::verify_runner(&format!("custom:{HASH}")), | ||
| Some(("custom", HASH)), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn custom_runner_is_not_in_the_registry() { | ||
| let (root, reader) = registry_with_one_builtin(); | ||
|
|
||
| assert!( | ||
| reader.has_in_all("py-genlayer", HASH), | ||
| "the builtin the registry lists must be found", | ||
| ); | ||
| assert!( | ||
| !reader.has_in_all("custom", HASH), | ||
| "a custom: runner must never resolve a precompiled artifact", | ||
| ); | ||
| assert!( | ||
| !reader.has_in_all("py-genlayer", "not-the-listed-hash"), | ||
| "a builtin name with an unlisted hash must not resolve either", | ||
| ); | ||
|
|
||
| std::fs::remove_dir_all(&root).unwrap(); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Align dynamic constructors with
VmError::is_valid_.__VmError::WasmTrap::val_str("")and__VmError::Host::val_str("")createwasm_trapandhost, but this validator rejects both values because the suffix is empty. Reject empty input in the constructors, or accept empty suffixes here. Add tests for both dynamic constructors.🤖 Prompt for AI Agents