Skip to content

Commit 248111e

Browse files
committed
add tests for Eq and Neq operations
1 parent 7420fe7 commit 248111e

File tree

1 file changed

+112
-0
lines changed
  • crates/move-bytecode-verifier/src/type_safety_tests

1 file changed

+112
-0
lines changed

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

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@ fn add_simple_struct(module: &mut CompiledModule) {
7171
module.struct_handles.push(struct_handle);
7272
}
7373

74+
fn add_simple_struct_with_abilities(module: &mut CompiledModule, abilities: AbilitySet) {
75+
let struct_def = StructDefinition {
76+
struct_handle: StructHandleIndex(0),
77+
field_information: StructFieldInformation::Declared(vec![
78+
FieldDefinition {
79+
name: IdentifierIndex(5),
80+
signature: TypeSignature(SignatureToken::U32),
81+
},
82+
]),
83+
};
84+
85+
let struct_handle = StructHandle {
86+
module: ModuleHandleIndex(0),
87+
name: IdentifierIndex(0),
88+
abilities: abilities,
89+
type_parameters: vec![],
90+
};
91+
92+
module.struct_defs.push(struct_def);
93+
module.struct_handles.push(struct_handle);
94+
}
95+
96+
7497
fn get_fun_context(module: &CompiledModule) -> FunctionContext {
7598
FunctionContext::new(
7699
&module,
@@ -729,3 +752,92 @@ fn test_unpack_no_arg() {
729752
let fun_context = get_fun_context(&module);
730753
let _result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
731754
}
755+
756+
757+
#[test]
758+
fn test_eq_neq_correct_types() {
759+
for instr in vec![
760+
Bytecode::Eq,
761+
Bytecode::Neq,
762+
] {
763+
let code = vec![Bytecode::LdU32(42), Bytecode::LdU32(42), instr.clone()];
764+
let module = make_module(code);
765+
let fun_context = get_fun_context(&module);
766+
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
767+
assert!(result.is_ok());
768+
769+
let code = vec![
770+
Bytecode::LdU32(42),
771+
Bytecode::Pack(StructDefinitionIndex(0)),
772+
Bytecode::LdU32(51),
773+
Bytecode::Pack(StructDefinitionIndex(0)),
774+
instr.clone()
775+
];
776+
let mut module = make_module(code);
777+
add_simple_struct_with_abilities(&mut module, AbilitySet::PRIMITIVES);
778+
let fun_context = get_fun_context(&module);
779+
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
780+
assert!(result.is_ok());
781+
}
782+
}
783+
784+
#[test]
785+
fn test_eq_neq_mismatched_types() {
786+
for instr in vec![
787+
Bytecode::Eq,
788+
Bytecode::Neq,
789+
] {
790+
let code = vec![Bytecode::LdU32(42), Bytecode::LdU64(42), instr.clone()];
791+
let module = make_module(code);
792+
let fun_context = get_fun_context(&module);
793+
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
794+
assert_eq!(
795+
result.unwrap_err().major_status(),
796+
StatusCode::EQUALITY_OP_TYPE_MISMATCH_ERROR
797+
);
798+
}
799+
}
800+
801+
#[test]
802+
fn test_eq_neq_no_drop() {
803+
for instr in vec![
804+
Bytecode::Eq,
805+
Bytecode::Neq,
806+
] {
807+
let code = vec![
808+
Bytecode::LdU32(42),
809+
Bytecode::Pack(StructDefinitionIndex(0)),
810+
Bytecode::LdU32(51),
811+
Bytecode::Pack(StructDefinitionIndex(0)),
812+
instr.clone()
813+
];
814+
815+
let mut module = make_module(code);
816+
add_simple_struct_with_abilities(&mut module, AbilitySet::EMPTY);
817+
let fun_context = get_fun_context(&module);
818+
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
819+
assert_eq!(
820+
result.unwrap_err().major_status(),
821+
StatusCode::EQUALITY_OP_TYPE_MISMATCH_ERROR
822+
);
823+
}
824+
}
825+
826+
#[test]
827+
#[should_panic]
828+
fn test_eq_neq_too_few_args() {
829+
for instr in vec![
830+
Bytecode::Eq,
831+
Bytecode::Neq,
832+
] {
833+
let code = vec![Bytecode::LdU32(42), instr.clone()];
834+
let module = make_module(code);
835+
let fun_context = get_fun_context(&module);
836+
let _result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
837+
838+
let code = vec![instr.clone()];
839+
let module = make_module(code);
840+
let fun_context = get_fun_context(&module);
841+
let _result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
842+
}
843+
}

0 commit comments

Comments
 (0)