-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
[programmability] Add tests for struct_with_key_verifier #47
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
[programmability] Add tests for struct_with_key_verifier
- Loading branch information
commit 1b0c65012205d3b3b160ea1e7bcec1740f064c1e
There are no files selected for viewing
This file contains 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
221 changes: 221 additions & 0 deletions
221
fastx_programmability/verifier/tests/struct_with_key_verification_test.rs
This file contains 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,221 @@ | ||
use fastx_framework::FASTX_FRAMEWORK_ADDRESS; | ||
use fastx_verifier::verifier::verify_module; | ||
use move_binary_format::file_format::*; | ||
use move_core_types::{account_address::AccountAddress, identifier::Identifier}; | ||
|
||
fn make_module() -> CompiledModule { | ||
CompiledModule { | ||
version: move_binary_format::file_format_common::VERSION_MAX, | ||
module_handles: vec![ | ||
// ID module from fastx framework. | ||
ModuleHandle { | ||
address: AddressIdentifierIndex(0), | ||
name: IdentifierIndex(0), | ||
}, | ||
// Test module with same name ("ID") but at different address. | ||
ModuleHandle { | ||
address: AddressIdentifierIndex(1), | ||
name: IdentifierIndex(0), | ||
}, | ||
], | ||
self_module_handle_idx: ModuleHandleIndex(1), | ||
identifiers: vec![ | ||
Identifier::new("ID").unwrap(), // ID Module name as well as struct name | ||
Identifier::new("foo").unwrap(), // Test struct name | ||
Identifier::new("id").unwrap(), // id field | ||
], | ||
address_identifiers: vec![ | ||
FASTX_FRAMEWORK_ADDRESS, // ID Module address | ||
AccountAddress::new([1u8; AccountAddress::LENGTH]), // A random address | ||
], | ||
struct_handles: vec![ | ||
// The FASTX_FRAMEWORK_ADDRESS::ID::ID struct | ||
StructHandle { | ||
module: ModuleHandleIndex(0), | ||
name: IdentifierIndex(0), | ||
abilities: AbilitySet::EMPTY | Ability::Store | Ability::Drop, | ||
type_parameters: vec![], | ||
}, | ||
// A random ID::ID struct from a different address | ||
StructHandle { | ||
module: ModuleHandleIndex(1), | ||
name: IdentifierIndex(0), | ||
abilities: AbilitySet::EMPTY | Ability::Store | Ability::Drop, | ||
type_parameters: vec![], | ||
}, | ||
], | ||
struct_defs: vec![], | ||
function_handles: vec![], | ||
function_defs: vec![], | ||
signatures: vec![ | ||
Signature(vec![]), // void | ||
Signature(vec![SignatureToken::Signer]), // Signer | ||
], | ||
constant_pool: vec![], | ||
field_handles: vec![], | ||
friend_decls: vec![], | ||
struct_def_instantiations: vec![], | ||
function_instantiations: vec![], | ||
field_instantiations: vec![], | ||
} | ||
} | ||
|
||
#[test] | ||
fn key_struct_with_drop() { | ||
let mut module = make_module(); | ||
module.struct_handles.push(StructHandle { | ||
module: ModuleHandleIndex(1), | ||
name: IdentifierIndex(1), | ||
abilities: AbilitySet::EMPTY | Ability::Key | Ability::Drop, | ||
type_parameters: vec![], | ||
}); | ||
module.struct_defs.push(StructDefinition { | ||
struct_handle: StructHandleIndex(2), | ||
field_information: StructFieldInformation::Declared(vec![FieldDefinition { | ||
name: IdentifierIndex(2), | ||
signature: TypeSignature(SignatureToken::U64), | ||
}]), | ||
}); | ||
assert!(verify_module(&module) | ||
.unwrap_err() | ||
.to_string() | ||
.contains("Struct foo cannot have both key and drop abilities")); | ||
} | ||
|
||
#[test] | ||
fn non_key_struct_without_fields() { | ||
let mut module = make_module(); | ||
module.struct_handles.push(StructHandle { | ||
module: ModuleHandleIndex(1), | ||
name: IdentifierIndex(1), | ||
abilities: AbilitySet::EMPTY | Ability::Drop, | ||
type_parameters: vec![], | ||
}); | ||
module.struct_defs.push(StructDefinition { | ||
struct_handle: StructHandleIndex(2), | ||
field_information: StructFieldInformation::Declared(vec![]), | ||
}); | ||
assert!(verify_module(&module).is_ok()); | ||
} | ||
|
||
#[test] | ||
fn key_struct_without_fields() { | ||
let mut module = make_module(); | ||
module.struct_handles.push(StructHandle { | ||
module: ModuleHandleIndex(1), | ||
name: IdentifierIndex(1), | ||
abilities: AbilitySet::EMPTY | Ability::Key, | ||
type_parameters: vec![], | ||
}); | ||
module.struct_defs.push(StructDefinition { | ||
struct_handle: StructHandleIndex(2), | ||
field_information: StructFieldInformation::Declared(vec![]), | ||
}); | ||
assert!(verify_module(&module) | ||
.unwrap_err() | ||
.to_string() | ||
.contains("First field of struct foo must be 'id', no field found")); | ||
} | ||
|
||
#[test] | ||
fn key_struct_first_field_not_id() { | ||
let mut module = make_module(); | ||
module.struct_handles.push(StructHandle { | ||
module: ModuleHandleIndex(1), | ||
name: IdentifierIndex(1), | ||
abilities: AbilitySet::EMPTY | Ability::Key, | ||
type_parameters: vec![], | ||
}); | ||
module.struct_defs.push(StructDefinition { | ||
struct_handle: StructHandleIndex(2), | ||
field_information: StructFieldInformation::Declared(vec![FieldDefinition { | ||
name: IdentifierIndex(1), | ||
signature: TypeSignature(SignatureToken::U64), | ||
}]), | ||
}); | ||
assert!(verify_module(&module) | ||
.unwrap_err() | ||
.to_string() | ||
.contains("First field of struct foo must be 'id', foo found")); | ||
|
||
module.struct_defs.pop(); | ||
// Make id the second field, and it should still fail verification. | ||
module.struct_defs.push(StructDefinition { | ||
struct_handle: StructHandleIndex(2), | ||
field_information: StructFieldInformation::Declared(vec![ | ||
FieldDefinition { | ||
name: IdentifierIndex(1), | ||
signature: TypeSignature(SignatureToken::U64), | ||
}, | ||
FieldDefinition { | ||
name: IdentifierIndex(2), | ||
signature: TypeSignature(SignatureToken::Struct(StructHandleIndex(0))), | ||
}, | ||
]), | ||
}); | ||
assert!(verify_module(&module) | ||
.unwrap_err() | ||
.to_string() | ||
.contains("First field of struct foo must be 'id', foo found")); | ||
} | ||
|
||
#[test] | ||
fn key_struct_id_field_incorrect_type() { | ||
let mut module = make_module(); | ||
module.struct_handles.push(StructHandle { | ||
module: ModuleHandleIndex(0), | ||
name: IdentifierIndex(1), | ||
abilities: AbilitySet::EMPTY | Ability::Key, | ||
type_parameters: vec![], | ||
}); | ||
module.struct_defs.push(StructDefinition { | ||
struct_handle: StructHandleIndex(2), | ||
field_information: StructFieldInformation::Declared(vec![FieldDefinition { | ||
name: IdentifierIndex(2), | ||
signature: TypeSignature(SignatureToken::U64), | ||
}]), | ||
}); | ||
assert!(verify_module(&module) | ||
.unwrap_err() | ||
.to_string() | ||
.contains("First field of struct foo must be of ID type, U64 type found")); | ||
|
||
module.struct_defs.pop(); | ||
module.struct_defs.push(StructDefinition { | ||
struct_handle: StructHandleIndex(2), | ||
field_information: StructFieldInformation::Declared(vec![FieldDefinition { | ||
name: IdentifierIndex(2), | ||
signature: TypeSignature(SignatureToken::Struct(StructHandleIndex(1))), | ||
}]), | ||
}); | ||
assert!(verify_module(&module).unwrap_err().to_string().contains("First field of struct foo must be of type 00000000000000000000000000000002::ID::ID, 01010101010101010101010101010101::ID::ID type found")); | ||
|
||
module.struct_defs.pop(); | ||
module.struct_defs.push(StructDefinition { | ||
struct_handle: StructHandleIndex(2), | ||
field_information: StructFieldInformation::Declared(vec![FieldDefinition { | ||
name: IdentifierIndex(2), | ||
signature: TypeSignature(SignatureToken::Struct(StructHandleIndex(2))), | ||
}]), | ||
}); | ||
assert!(verify_module(&module).unwrap_err().to_string().contains("First field of struct foo must be of type 00000000000000000000000000000002::ID::ID, 00000000000000000000000000000002::ID::foo type found")); | ||
} | ||
|
||
#[test] | ||
fn key_struct_id_field_valid() { | ||
let mut module = make_module(); | ||
module.struct_handles.push(StructHandle { | ||
module: ModuleHandleIndex(1), | ||
name: IdentifierIndex(2), | ||
abilities: AbilitySet::EMPTY | Ability::Key, | ||
type_parameters: vec![], | ||
}); | ||
module.struct_defs.push(StructDefinition { | ||
struct_handle: StructHandleIndex(2), | ||
field_information: StructFieldInformation::Declared(vec![FieldDefinition { | ||
name: IdentifierIndex(2), | ||
signature: TypeSignature(SignatureToken::Struct(StructHandleIndex(0))), | ||
}]), | ||
}); | ||
assert!(verify_module(&module).is_ok()); | ||
} |
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.
This will actually get rejected by the regular bytecode verifier--it insists that every struct has at least one field (otherwise, the struct isn't serializable). So we probably don't want this test?
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.
The problem is that we have to handle this case in the verifier code and error out if it happens (since we run our verifier before move verifier). Since we verify it anyway, we might as well test it