Skip to content

Commit

Permalink
tests for MutBorrowField operation
Browse files Browse the repository at this point in the history
  • Loading branch information
olympichek committed Aug 29, 2024
1 parent a660084 commit f438c4a
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion crates/move-bytecode-verifier/src/type_safety_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn add_simple_struct(module: &mut CompiledModule) {
abilities: AbilitySet::EMPTY,
type_parameters: vec![],
};

module.struct_defs.push(struct_def);
module.struct_handles.push(struct_handle);

Expand Down Expand Up @@ -1333,3 +1333,73 @@ fn test_imm_borrow_field_no_arg() {
let _result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
}


#[test]
fn test_mut_borrow_field_correct_type() {
let code = vec![Bytecode::MutBorrowLoc(0), Bytecode::MutBorrowField(FieldHandleIndex(0))];
let mut module = make_module_with_local(code, SignatureToken::Struct(StructHandleIndex(0)));
add_simple_struct(&mut module);
let fun_context = get_fun_context(&module);
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
assert!(result.is_ok());
}

#[test]
fn test_mut_borrow_field_wrong_type() {
let code = vec![Bytecode::LdTrue, Bytecode::MutBorrowField(FieldHandleIndex(0))];
let mut module = make_module_with_local(code, SignatureToken::Struct(StructHandleIndex(0)));
add_simple_struct(&mut module);
let fun_context = get_fun_context(&module);
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
assert_eq!(
result.unwrap_err().major_status(),
StatusCode::BORROWFIELD_TYPE_MISMATCH_ERROR
);

let code = vec![Bytecode::ImmBorrowLoc(0), Bytecode::MutBorrowField(FieldHandleIndex(0))];
let mut module = make_module_with_local(code, SignatureToken::Struct(StructHandleIndex(0)));
add_simple_struct(&mut module);
let fun_context = get_fun_context(&module);
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
assert_eq!(
result.unwrap_err().major_status(),
StatusCode::BORROWFIELD_TYPE_MISMATCH_ERROR
);

}

#[test]
fn test_mut_borrow_field_mismatched_types() {
let code = vec![Bytecode::MutBorrowLoc(0), Bytecode::MutBorrowField(FieldHandleIndex(0))];
let mut module = make_module_with_local(code, SignatureToken::U64);
add_simple_struct(&mut module);
let fun_context = get_fun_context(&module);
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
assert_eq!(
result.unwrap_err().major_status(),
StatusCode::BORROWFIELD_TYPE_MISMATCH_ERROR
);
}

#[test]
fn test_mut_borrow_field_bad_field() {
let code = vec![Bytecode::MutBorrowLoc(0), Bytecode::MutBorrowField(FieldHandleIndex(0))];
let mut module = make_module_with_local(code, SignatureToken::Struct(StructHandleIndex(0)));
add_native_struct(&mut module);
let fun_context = get_fun_context(&module);
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
assert_eq!(
result.unwrap_err().major_status(),
StatusCode::BORROWFIELD_BAD_FIELD_ERROR
);
}

#[test]
#[should_panic]
fn test_mut_borrow_field_no_arg() {
let code = vec![Bytecode::MutBorrowField(FieldHandleIndex(0))];
let mut module = make_module_with_local(code, SignatureToken::Struct(StructHandleIndex(0)));
add_simple_struct(&mut module);
let fun_context = get_fun_context(&module);
let _result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
}

0 comments on commit f438c4a

Please sign in to comment.