Skip to content

Commit 1483afa

Browse files
committed
tests for Pack operation
1 parent 5499319 commit 1483afa

File tree

1 file changed

+64
-4
lines changed
  • crates/move-bytecode-verifier/src/type_safety_tests

1 file changed

+64
-4
lines changed

crates/move-bytecode-verifier/src/type_safety_tests/mod.rs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use move_binary_format::file_format::{
2-
Bytecode, CodeUnit, FunctionDefinition, FunctionHandle,
3-
IdentifierIndex, ModuleHandleIndex, SignatureIndex,
4-
FunctionDefinitionIndex, empty_module
2+
empty_module, Bytecode, CodeUnit, FunctionDefinition, FunctionDefinitionIndex, FunctionHandle, IdentifierIndex, ModuleHandleIndex, SignatureIndex, StructDefinitionIndex
53
};
64

75
use move_core_types::{
@@ -10,7 +8,9 @@ use move_core_types::{
108

119
use move_binary_format::{
1210
CompiledModule,
13-
file_format::{ConstantPoolIndex, Constant, SignatureToken},
11+
file_format::{
12+
ConstantPoolIndex, Constant, SignatureToken, AbilitySet, StructHandle, TypeSignature, FieldDefinition, StructHandleIndex, StructFieldInformation, StructDefinition
13+
},
1414
};
1515

1616
use move_bytecode_verifier_meter::dummy::DummyMeter;
@@ -45,6 +45,32 @@ fn make_module(code: Vec<Bytecode>) -> CompiledModule {
4545
module
4646
}
4747

48+
fn add_simple_struct(module: &mut CompiledModule) {
49+
let struct_def = StructDefinition {
50+
struct_handle: StructHandleIndex(0),
51+
field_information: StructFieldInformation::Declared(vec![
52+
FieldDefinition {
53+
name: IdentifierIndex(5),
54+
signature: TypeSignature(SignatureToken::U32),
55+
},
56+
FieldDefinition {
57+
name: IdentifierIndex(6),
58+
signature: TypeSignature(SignatureToken::Bool),
59+
},
60+
]),
61+
};
62+
63+
let struct_handle = StructHandle {
64+
module: ModuleHandleIndex(0),
65+
name: IdentifierIndex(0),
66+
abilities: AbilitySet::EMPTY,
67+
type_parameters: vec![],
68+
};
69+
70+
module.struct_defs.push(struct_def);
71+
module.struct_handles.push(struct_handle);
72+
}
73+
4874
fn get_fun_context(module: &CompiledModule) -> FunctionContext {
4975
FunctionContext::new(
5076
&module,
@@ -631,3 +657,37 @@ fn test_ld_const_ok() {
631657
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
632658
assert!(result.is_ok());
633659
}
660+
661+
662+
#[test]
663+
fn test_pack_correct_types() {
664+
let code = vec![Bytecode::LdU32(42), Bytecode::LdTrue, Bytecode::Pack(StructDefinitionIndex(0))];
665+
let mut module: CompiledModule = make_module(code);
666+
add_simple_struct(&mut module);
667+
let fun_context = get_fun_context(&module);
668+
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
669+
assert!(result.is_ok());
670+
}
671+
672+
#[test]
673+
fn test_pack_mismatched_types() {
674+
let code = vec![Bytecode::LdTrue, Bytecode::LdU32(42), Bytecode::Pack(StructDefinitionIndex(0))];
675+
let mut module: CompiledModule = make_module(code);
676+
add_simple_struct(&mut module);
677+
let fun_context = get_fun_context(&module);
678+
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
679+
assert_eq!(
680+
result.unwrap_err().major_status(),
681+
StatusCode::PACK_TYPE_MISMATCH_ERROR
682+
);
683+
}
684+
685+
#[test]
686+
#[should_panic]
687+
fn test_pack_too_few_args() {
688+
let code = vec![Bytecode::LdTrue, Bytecode::Pack(StructDefinitionIndex(0))];
689+
let mut module: CompiledModule = make_module(code);
690+
add_simple_struct(&mut module);
691+
let fun_context = get_fun_context(&module);
692+
let _result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
693+
}

0 commit comments

Comments
 (0)