Merge rust-bitcoin/rust-bitcoin#4347: primitives: Increase test coverage in `Script`

b4b61cb884 Add tests to increase coverage (Jamil Lambert, PhD)
6bcc6b703d Expand display test to cover debug and OP_0 (Jamil Lambert, PhD)
8a096b77d5 Expand as_ref and as_mut tests (Jamil Lambert, PhD)
afa51f7bfa Add missing empty line, and remove extra one (Jamil Lambert, PhD)

Pull request description:

  Expand existing tests and add new ones to increase the test coverage in `primitives/src/script/` to 100% excluding `serde` and `arbitrary` features.

ACKs for top commit:
  apoelstra:
    ACK b4b61cb8849d5d536532474691c5d9f10c146672; successfully ran local tests
  tcharding:
    ACK b4b61cb884

Tree-SHA512: 6c37a194e5a94fe04bb26d4e904bd98efc75bd433b59aa09b1c102df96b753602d3ad6540ed9941827f03c7d38e7bbb8b10bbcc8c6ab52b8f96b83743eec4519
This commit is contained in:
merge-script 2025-04-17 15:05:33 +00:00
commit 30c1933d16
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 124 additions and 9 deletions

View File

@ -632,30 +632,57 @@ mod tests {
let script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]); let script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]);
let script_ref: &[u8] = script_buf.as_ref(); let script_ref: &[u8] = script_buf.as_ref();
assert_eq!(script_ref, &[0x51, 0x52, 0x53]); assert_eq!(script_ref, &[0x51, 0x52, 0x53]);
let script_ref: &Script = script_buf.as_ref();
assert_eq!(script_ref.as_bytes(), &[0x51, 0x52, 0x53]);
} }
#[test] #[test]
fn scriptbuf_as_mut() { fn scriptbuf_as_mut() {
let mut script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]); let mut script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]);
let script_mut: &mut [u8] = script_buf.as_mut(); let script_mut: &mut [u8] = script_buf.as_mut();
script_mut[0] = 0x50; script_mut[0] = 0x50;
assert_eq!(script_mut, [0x50, 0x52, 0x53]); assert_eq!(script_mut, [0x50, 0x52, 0x53]);
let script_mut: &mut Script = script_buf.as_mut();
script_mut.as_mut_bytes()[1] = 0x51;
assert_eq!(script_buf.as_bytes(), &[0x50, 0x51, 0x53]);
} }
#[test] #[test]
fn scriptbuf_borrow_mut() {
let mut script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]);
let script_mut: &mut Script = script_buf.borrow_mut();
script_mut.as_mut_bytes()[0] = 0x50;
assert_eq!(script_buf.as_bytes(), &[0x50, 0x52, 0x53]);
}
#[test]
#[allow(clippy::useless_asref)]
fn script_as_ref() { fn script_as_ref() {
let script = Script::from_bytes(&[0x51, 0x52, 0x53]); let script = Script::from_bytes(&[0x51, 0x52, 0x53]);
let script_ref: &[u8] = script.as_ref(); let script_ref: &[u8] = script.as_ref();
assert_eq!(script_ref, &[0x51, 0x52, 0x53]); assert_eq!(script_ref, &[0x51, 0x52, 0x53]);
let script_ref: &Script = script.as_ref();
assert_eq!(script_ref.as_bytes(), &[0x51, 0x52, 0x53]);
} }
#[test] #[test]
#[allow(clippy::useless_asref)]
fn script_as_mut() { fn script_as_mut() {
let bytes = &mut [0x51, 0x52, 0x53]; let bytes = &mut [0x51, 0x52, 0x53];
let script = Script::from_bytes_mut(bytes); let script = Script::from_bytes_mut(bytes);
let script_mut: &mut [u8] = script.as_mut(); let script_mut: &mut [u8] = script.as_mut();
script_mut[0] = 0x50; script_mut[0] = 0x50;
assert_eq!(script_mut, [0x50, 0x52, 0x53]); assert_eq!(script_mut, [0x50, 0x52, 0x53]);
let script_mut: &mut Script = script.as_mut();
script_mut.as_mut_bytes()[1] = 0x51;
assert_eq!(script.as_bytes(), &[0x50, 0x51, 0x53]);
} }
#[test] #[test]
@ -725,6 +752,7 @@ mod tests {
let script = ScriptBuf::from(vec![0x51; 521]); let script = ScriptBuf::from(vec![0x51; 521]);
assert!(ScriptHash::try_from(&script).is_err()); assert!(ScriptHash::try_from(&script).is_err());
} }
#[test] #[test]
fn try_from_script_for_scripthash() { fn try_from_script_for_scripthash() {
let script = Script::from_bytes(&[0x51; 520]); let script = Script::from_bytes(&[0x51; 520]);
@ -752,7 +780,6 @@ mod tests {
assert!(WScriptHash::try_from(&script).is_err()); assert!(WScriptHash::try_from(&script).is_err());
} }
#[test] #[test]
fn try_from_script_for_wscript_hash() { fn try_from_script_for_wscript_hash() {
let script = Script::from_bytes(&[0x51; 10_000]); let script = Script::from_bytes(&[0x51; 10_000]);
@ -764,26 +791,28 @@ mod tests {
#[test] #[test]
fn script_display() { fn script_display() {
let script = Script::from_bytes(&[0xa1, 0xb2, 0xc3]); let script = Script::from_bytes(&[0x00, 0xa1, 0xb2]);
assert_eq!(format!("{}", script), "OP_LESSTHANOREQUAL OP_CSV OP_RETURN_195"); assert_eq!(format!("{}", script), "OP_0 OP_LESSTHANOREQUAL OP_CSV");
#[cfg(feature = "hex")] #[cfg(feature = "hex")]
{ {
assert_eq!(format!("{:x}", script), "a1b2c3"); assert_eq!(format!("{:x}", script), "00a1b2");
assert_eq!(format!("{:X}", script), "A1B2C3"); assert_eq!(format!("{:X}", script), "00A1B2");
} }
assert!(!format!("{:?}", script).is_empty());
} }
#[test] #[test]
fn scriptbuf_display() { fn scriptbuf_display() {
let script_buf = ScriptBuf::from(vec![0xa1, 0xb2, 0xc3]); let script_buf = ScriptBuf::from(vec![0x00, 0xa1, 0xb2]);
assert_eq!(format!("{}", script_buf), "OP_LESSTHANOREQUAL OP_CSV OP_RETURN_195"); assert_eq!(format!("{}", script_buf), "OP_0 OP_LESSTHANOREQUAL OP_CSV");
#[cfg(feature = "hex")] #[cfg(feature = "hex")]
{ {
assert_eq!(format!("{:x}", script_buf), "a1b2c3"); assert_eq!(format!("{:x}", script_buf), "00a1b2");
assert_eq!(format!("{:X}", script_buf), "A1B2C3"); assert_eq!(format!("{:X}", script_buf), "00A1B2");
} }
assert!(!format!("{:?}", script_buf).is_empty());
} }
#[test] #[test]
@ -811,6 +840,14 @@ mod tests {
assert_eq!(script_buf2, script_buf); assert_eq!(script_buf2, script_buf);
} }
#[test]
fn cow_owned_to_scriptbuf() {
let script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]);
let cow_owned: Cow<Script> = Cow::Owned(script_buf.clone());
let script_buf_2: ScriptBuf = cow_owned.into();
assert_eq!(script_buf_2, script_buf);
}
#[test] #[test]
fn cow_script_to_box_script() { fn cow_script_to_box_script() {
let script = Script::from_bytes(&[0x51, 0x52, 0x53]); let script = Script::from_bytes(&[0x51, 0x52, 0x53]);
@ -824,4 +861,82 @@ mod tests {
let cow_from_script: Cow<Script> = Cow::from(script); let cow_from_script: Cow<Script> = Cow::from(script);
assert_eq!(cow_from_script.as_ref().as_bytes(), &[0x51, 0x52, 0x53]); assert_eq!(cow_from_script.as_ref().as_bytes(), &[0x51, 0x52, 0x53]);
} }
#[test]
fn redeem_script_size_error() {
let script = ScriptBuf::from(vec![0x51; 521]);
let result = ScriptHash::try_from(script);
let err = result.unwrap_err();
assert_eq!(err.invalid_size(), 521);
let err_msg = format!("{}", err);
assert!(err_msg.contains("521"));
}
#[test]
fn witness_script_size_error() {
let script = ScriptBuf::from(vec![0x51; 10_001]);
let result = WScriptHash::try_from(script);
let err = result.unwrap_err();
assert_eq!(err.invalid_size(), 10_001);
let err_msg = format!("{}", err);
assert!(err_msg.contains("10001"));
}
#[test]
#[cfg(target_has_atomic = "ptr")]
fn script_to_arc() {
let script = Script::from_bytes(&[0x51, 0x52, 0x53]);
let arc_script: Arc<Script> = Arc::from(script);
assert_eq!(arc_script.as_bytes(), script.as_bytes());
assert_eq!(Arc::strong_count(&arc_script), 1);
}
#[test]
fn script_to_rc() {
let script = Script::from_bytes(&[0x51, 0x52, 0x53]);
let rc_script: Rc<Script> = Rc::from(script);
assert_eq!(rc_script.as_bytes(), script.as_bytes());
assert_eq!(Rc::strong_count(&rc_script), 1);
}
#[test]
fn pushdata_end_conditions() {
let push_past_end_script = Script::from_bytes(&[0x4c, 0x02]);
let formatted_script = format!("{}", push_past_end_script);
assert!(formatted_script.contains("<push past end>"));
let unexpected_end_script = Script::from_bytes(&[0x4c]);
let formatted_script = format!("{}", unexpected_end_script);
assert!(formatted_script.contains("<unexpected end>"));
}
#[test]
fn legacy_opcode() {
let script = Script::from_bytes(&[0x03, 0xaa, 0xbb, 0xcc]);
assert_eq!(format!("{}", script), "OP_PUSHBYTES_3 aabbcc");
}
#[test]
#[cfg(feature = "alloc")]
#[cfg(feature = "hex")]
fn script_to_hex() {
let script = Script::from_bytes(&[0xa1, 0xb2, 0xc3]);
let hex = script.to_hex();
assert_eq!(hex, "a1b2c3");
}
#[test]
#[cfg(feature = "alloc")]
#[cfg(feature = "hex")]
fn scriptbuf_to_hex() {
let script = ScriptBuf::from_bytes(vec![0xa1, 0xb2, 0xc3]);
let hex = script.to_hex();
assert_eq!(hex, "a1b2c3");
}
} }