Skip to content
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 1 commit into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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
lxfind committed Dec 9, 2021
commit 1b0c65012205d3b3b160ea1e7bcec1740f064c1e
2 changes: 2 additions & 0 deletions fastx_programmability/verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ publish = false
[dependencies]
## uncomment for debugging with local copy of diem repo
# move-binary-format = { path = "../../../diem/language/move-binary-format" }
# move-core-types = { path = "../../../diem/language/move-core/types" }

move-binary-format = { git = "https://github.com/diem/diem", rev="661a2d1367a64a02027e4ed8f4b18f0a37cfaa17" }
move-core-types = { git = "https://github.com/diem/diem", rev="661a2d1367a64a02027e4ed8f4b18f0a37cfaa17" }

fastx-types = { path = "../../fastx_types" }

Expand Down
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() {
Copy link
Collaborator

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?

Copy link
Contributor Author

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

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());
}