What changed, and why it matters
This commit simply moves existing unit tests from two separate files into a single shared test file. It does not change any production code, behavior, or security logic. There is no security impact.
No action required. This is a test-only refactor with no security relevance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors test code in rust-bitcoin’s script module. It removes #[cfg(test)] mod tests blocks from primitives/src/script/borrowed.rs and primitives/src/script/owned.rs, and moves those test functions into the existing primitives/src/script/tests.rs. The test bodies are preserved verbatim (with minor import adjustments and reordering). No production code, APIs, or consensus behavior are modified.
Changed components
primitives/src/script/borrowed.rsprimitives/src/script/owned.rsprimitives/src/script/tests.rsInspect captured patch +236 / −253
diff --git a/primitives/src/script/borrowed.rs b/primitives/src/script/borrowed.rs
index e7e452b3..a9456b1a 100644
--- a/primitives/src/script/borrowed.rs
+++ b/primitives/src/script/borrowed.rs
@@ -205,78 +205,3 @@ delegate_index!(
RangeToInclusive<usize>,
(Bound<usize>, Bound<usize>)
);
-
-#[cfg(test)]
-mod tests {
- // All tests should compile and pass no matter which script type you put here.
- type Script = super::super::ScriptSig;
-
- use alloc::{borrow::ToOwned, vec};
-
- #[test]
- fn script_from_bytes() {
- let script = Script::from_bytes(&[1, 2, 3]);
- assert_eq!(script.as_bytes(), [1, 2, 3]);
- }
-
- #[test]
- fn script_from_bytes_mut() {
- let bytes = &mut [1, 2, 3];
- let script = Script::from_bytes_mut(bytes);
- script.as_mut_bytes()[0] = 4;
- assert_eq!(script.as_mut_bytes(), [4, 2, 3]);
- }
-
- #[test]
- fn script_to_vec() {
- let script = Script::from_bytes(&[1, 2, 3]);
- assert_eq!(script.to_vec(), vec![1, 2, 3]);
- }
-
- #[test]
- fn script_len() {
- let script = Script::from_bytes(&[1, 2, 3]);
- assert_eq!(script.len(), 3);
- }
-
- #[test]
- fn script_is_empty() {
- let script: &Script = Default::default();
- assert!(script.is_empty());
-
- let script = Script::from_bytes(&[1, 2, 3]);
- assert!(!script.is_empty());
- }
-
- #[test]
- fn script_to_owned() {
- let script = Script::from_bytes(&[1, 2, 3]);
- let script_buf = script.to_owned();
- assert_eq!(script_buf.as_bytes(), [1, 2, 3]);
- }
-
- #[test]
- fn test_index() {
- let script = Script::from_bytes(&[1, 2, 3, 4, 5]);
-
- assert_eq!(script[1..3].as_bytes(), &[2, 3]);
- assert_eq!(script[2..].as_bytes(), &[3, 4, 5]);
- assert_eq!(script[..3].as_bytes(), &[1, 2, 3]);
- assert_eq!(script[..].as_bytes(), &[1, 2, 3, 4, 5]);
- assert_eq!(script[1..=3].as_bytes(), &[2, 3, 4]);
- assert_eq!(script[..=2].as_bytes(), &[1, 2, 3]);
- }
-
- #[test]
- fn encode() {
- // Consensus encoding includes the length of the encoded data
- // (compact size encoded length prefix).
- let consensus_encoded: [u8; 6] = [0x05, 1, 2, 3, 4, 5];
-
- // `from_bytes` does not expect the prefix.
- let script = Script::from_bytes(&consensus_encoded[1..]);
-
- let got = encoding::encode_to_vec(script);
- assert_eq!(got, consensus_encoded);
- }
-}
diff --git a/primitives/src/script/owned.rs b/primitives/src/script/owned.rs
index 479b7d71..961d27bb 100644
--- a/primitives/src/script/owned.rs
+++ b/primitives/src/script/owned.rs
@@ -211,140 +211,3 @@ impl<'a, T> Arbitrary<'a> for ScriptBuf<T> {
Ok(Self::from_bytes(v))
}
}
-
-#[cfg(test)]
-mod tests {
- // All tests should compile and pass no matter which script type you put here.
- type ScriptBuf = super::super::ScriptSigBuf;
-
- use alloc::string::ToString;
- use alloc::vec;
- #[cfg(feature = "std")]
- use std::error::Error as _;
-
- use super::*;
-
- #[test]
- fn script_buf_from_bytes() {
- let bytes = vec![1, 2, 3];
- let script = ScriptBuf::from_bytes(bytes.clone());
- assert_eq!(script.as_bytes(), bytes);
- }
-
- #[test]
- fn script_buf_as_script() {
- let bytes = vec![1, 2, 3];
- let script = ScriptBuf::from_bytes(bytes.clone());
- let script_ref = script.as_script();
- assert_eq!(script_ref.as_bytes(), bytes);
- }
-
- #[test]
- fn script_buf_as_mut_script() {
- let mut script = ScriptBuf::from_bytes(vec![1, 2, 3]);
- let script_mut_ref = script.as_mut_script();
- script_mut_ref.as_mut_bytes()[0] = 4;
- assert_eq!(script.as_mut_bytes(), &[4, 2, 3]);
- }
-
- #[test]
- fn script_buf_into_bytes() {
- let bytes = vec![1, 2, 3];
- let script = ScriptBuf::from_bytes(bytes.clone());
- let result = script.into_bytes();
- assert_eq!(result, bytes);
- }
-
- #[test]
- fn script_buf_into_boxed_script() {
- let bytes = vec![1, 2, 3];
- let script = ScriptBuf::from_bytes(bytes.clone());
- let boxed_script = script.into_boxed_script();
- assert_eq!(boxed_script.as_bytes(), bytes);
- }
-
- #[test]
- fn script_buf_capacity() {
- let script = ScriptBuf::with_capacity(10);
- assert!(script.capacity() >= 10);
- }
-
- #[test]
- fn script_buf_reserve() {
- let mut script = ScriptBuf::new();
- script.reserve(10);
- assert!(script.capacity() >= 10);
- }
-
- #[test]
- fn script_buf_reserve_exact() {
- let mut script = ScriptBuf::new();
- script.reserve_exact(10);
- assert!(script.capacity() >= 10);
- }
-
- #[test]
- fn script_buf_default() {
- let script: ScriptBuf = ScriptBuf::default();
- assert!(script.is_empty());
- }
-
- #[test]
- fn script_consensus_decode_empty() {
- let bytes = vec![0_u8];
- let mut push = bytes.as_slice();
- let mut decoder = ScriptBuf::decoder();
- decoder.push_bytes(&mut push).unwrap();
-
- let got = decoder.end().unwrap();
- let want = ScriptBuf::new();
-
- assert_eq!(got, want);
- }
-
- #[test]
- fn script_consensus_decode_empty_with_more_data() {
- // An empty script sig with a bunch of unrelated data at the end.
- let bytes = vec![0x00_u8, 0xff, 0xff, 0xff, 0xff];
- let mut push = bytes.as_slice();
- let mut decoder = ScriptBuf::decoder();
- decoder.push_bytes(&mut push).unwrap();
-
- let got = decoder.end().unwrap();
- let want = ScriptBuf::new();
-
- assert_eq!(got, want);
- }
-
- #[test]
- fn decoder_full_read_limit() {
- let mut decoder = ScriptBuf::decoder();
- // ByteVecDecoder length prefix is CompactSize: needs 1 byte.
- assert_eq!(decoder.read_limit(), 1);
-
- // Script length prefix = 32.
- let mut push = [32_u8].as_slice();
- decoder.push_bytes(&mut push).unwrap();
- // Limit is 32 for the script data.
- assert_eq!(decoder.read_limit(), 32);
-
- // Provide 1 byte of script data decreasing the read limit by 1.
- let mut push = [0xAA_u8].as_slice();
- decoder.push_bytes(&mut push).unwrap();
- assert_eq!(decoder.read_limit(), 31);
- }
-
- #[test]
- #[cfg(feature = "alloc")]
- fn decoder_error_display() {
- let bytes = vec![0x01_u8];
- let mut push = bytes.as_slice();
- let mut decoder = <ScriptBuf as Decodable>::Decoder::default();
- decoder.push_bytes(&mut push).unwrap();
-
- let err = decoder.end().unwrap_err();
- assert!(!err.to_string().is_empty());
- #[cfg(feature = "std")]
- assert!(err.source().is_some());
- }
-}
diff --git a/primitives/src/script/tests.rs b/primitives/src/script/tests.rs
index 4bccf3c2..0c69e9d3 100644
--- a/primitives/src/script/tests.rs
+++ b/primitives/src/script/tests.rs
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: CC0-1.0
use alloc::{format, vec};
+use alloc::string::ToString;
+use encoding::{Decodable, Decoder as _};
use hashes::{hash160, sha256};
use super::*;
@@ -10,6 +12,32 @@ use super::*;
type Script = ScriptSig;
type ScriptBuf = ScriptSigBuf;
+#[test]
+fn script_buf_capacity() {
+ let script = ScriptBuf::with_capacity(10);
+ assert!(script.capacity() >= 10);
+}
+
+#[test]
+fn script_buf_reserve() {
+ let mut script = ScriptBuf::new();
+ script.reserve(10);
+ assert!(script.capacity() >= 10);
+}
+
+#[test]
+fn script_buf_reserve_exact() {
+ let mut script = ScriptBuf::new();
+ script.reserve_exact(10);
+ assert!(script.capacity() >= 10);
+}
+
+#[test]
+fn script_buf_default() {
+ let script: ScriptBuf = ScriptBuf::default();
+ assert!(script.is_empty());
+}
+
#[test]
fn script_buf_from_vec_u8() {
let vec = vec![0x51, 0x52, 0x53];
@@ -18,6 +46,72 @@ fn script_buf_from_vec_u8() {
assert_eq!(result, vec);
}
+#[test]
+fn script_buf_from_bytes() {
+ let bytes = vec![1, 2, 3];
+ let script = ScriptBuf::from_bytes(bytes.clone());
+ assert_eq!(script.as_bytes(), bytes);
+}
+
+#[test]
+fn script_from_bytes() {
+ let script = Script::from_bytes(&[1, 2, 3]);
+ assert_eq!(script.as_bytes(), [1, 2, 3]);
+}
+
+#[test]
+fn script_from_bytes_mut() {
+ let bytes = &mut [1, 2, 3];
+ let script = Script::from_bytes_mut(bytes);
+ script.as_mut_bytes()[0] = 4;
+ assert_eq!(script.as_mut_bytes(), [4, 2, 3]);
+}
+
+#[test]
+fn script_buf_as_script() {
+ let bytes = vec![1, 2, 3];
+ let script = ScriptBuf::from_bytes(bytes.clone());
+ let script_ref = script.as_script();
+ assert_eq!(script_ref.as_bytes(), bytes);
+}
+
+#[test]
+fn script_buf_as_mut_script() {
+ let mut script = ScriptBuf::from_bytes(vec![1, 2, 3]);
+ let script_mut_ref = script.as_mut_script();
+ script_mut_ref.as_mut_bytes()[0] = 4;
+ assert_eq!(script.as_mut_bytes(), &[4, 2, 3]);
+}
+
+#[test]
+fn script_to_vec() {
+ let script = Script::from_bytes(&[1, 2, 3]);
+ assert_eq!(script.to_vec(), vec![1, 2, 3]);
+}
+
+#[test]
+fn script_to_owned() {
+ let script = Script::from_bytes(&[1, 2, 3]);
+ let script_buf = script.to_owned();
+ assert_eq!(script_buf.as_bytes(), [1, 2, 3]);
+}
+
+#[test]
+fn script_buf_into_bytes() {
+ let bytes = vec![1, 2, 3];
+ let script = ScriptBuf::from_bytes(bytes.clone());
+ let result = script.into_bytes();
+ assert_eq!(result, bytes);
+}
+
+#[test]
+fn script_buf_into_boxed_script() {
+ let bytes = vec![1, 2, 3];
+ let script = ScriptBuf::from_bytes(bytes.clone());
+ let boxed_script = script.into_boxed_script();
+ assert_eq!(boxed_script.as_bytes(), bytes);
+}
+
#[test]
fn script_buf_as_ref() {
let script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]);
@@ -76,6 +170,33 @@ fn script_as_mut() {
assert_eq!(script.as_bytes(), &[0x50, 0x51, 0x53]);
}
+#[test]
+fn script_len() {
+ let script = Script::from_bytes(&[1, 2, 3]);
+ assert_eq!(script.len(), 3);
+}
+
+#[test]
+fn script_is_empty() {
+ let script: &Script = Default::default();
+ assert!(script.is_empty());
+
+ let script = Script::from_bytes(&[1, 2, 3]);
+ assert!(!script.is_empty());
+}
+
+#[test]
+fn test_index() {
+ let script = Script::from_bytes(&[1, 2, 3, 4, 5]);
+
+ assert_eq!(script[1..3].as_bytes(), &[2, 3]);
+ assert_eq!(script[2..].as_bytes(), &[3, 4, 5]);
+ assert_eq!(script[..3].as_bytes(), &[1, 2, 3]);
+ assert_eq!(script[..].as_bytes(), &[1, 2, 3, 4, 5]);
+ assert_eq!(script[1..=3].as_bytes(), &[2, 3, 4]);
+ assert_eq!(script[..=2].as_bytes(), &[1, 2, 3]);
+}
+
#[test]
fn partial_ord() {
let script_small = Script::from_bytes(&[0x51, 0x52, 0x53]);
@@ -188,47 +309,6 @@ fn try_from_script_for_wscript_hash() {
assert!(WScriptHash::try_from(script).is_err());
}
-#[test]
-fn script_display() {
- let script = Script::from_bytes(&[0x00, 0xa1, 0xb2]);
- assert_eq!(format!("{}", script), "OP_0 OP_LESSTHANOREQUAL OP_CSV");
-
- #[cfg(feature = "hex")]
- {
- assert_eq!(format!("{:x}", script), "00a1b2");
- assert_eq!(format!("{:X}", script), "00A1B2");
- }
- assert!(!format!("{:?}", script).is_empty());
-}
-
-#[test]
-fn script_display_pushdata() {
- // OP_PUSHDATA1
- let script = Script::from_bytes(&[0x4c, 0x02, 0xab, 0xcd]);
- assert_eq!(format!("{}", script), "OP_PUSHDATA1 abcd");
-
- // OP_PUSHDATA2
- let script = Script::from_bytes(&[0x4d, 0x02, 0x00, 0x12, 0x34]);
- assert_eq!(format!("{}", script), "OP_PUSHDATA2 1234");
-
- // OP_PUSHDATA4
- let script = Script::from_bytes(&[0x4e, 0x02, 0x00, 0x00, 0x00, 0x56, 0x78]);
- assert_eq!(format!("{}", script), "OP_PUSHDATA4 5678");
-}
-
-#[test]
-fn script_buf_display() {
- let script_buf = ScriptBuf::from(vec![0x00, 0xa1, 0xb2]);
- assert_eq!(format!("{}", script_buf), "OP_0 OP_LESSTHANOREQUAL OP_CSV");
-
- #[cfg(feature = "hex")]
- {
- assert_eq!(format!("{:x}", script_buf), "00a1b2");
- assert_eq!(format!("{:X}", script_buf), "00A1B2");
- }
- assert!(!format!("{:?}", script_buf).is_empty());
-}
-
#[test]
fn cow_script_to_script_buf() {
let script = Script::from_bytes(&[0x51, 0x52, 0x53]);
@@ -335,6 +415,60 @@ fn legacy_opcode() {
assert_eq!(format!("{}", script), "OP_PUSHBYTES_3 aabbcc");
}
+#[test]
+fn script_display() {
+ let script = Script::from_bytes(&[0x00, 0xa1, 0xb2]);
+ assert_eq!(format!("{}", script), "OP_0 OP_LESSTHANOREQUAL OP_CSV");
+
+ #[cfg(feature = "hex")]
+ {
+ assert_eq!(format!("{:x}", script), "00a1b2");
+ assert_eq!(format!("{:X}", script), "00A1B2");
+ }
+ assert!(!format!("{:?}", script).is_empty());
+}
+
+#[test]
+fn script_display_pushdata() {
+ // OP_PUSHDATA1
+ let script = Script::from_bytes(&[0x4c, 0x02, 0xab, 0xcd]);
+ assert_eq!(format!("{}", script), "OP_PUSHDATA1 abcd");
+
+ // OP_PUSHDATA2
+ let script = Script::from_bytes(&[0x4d, 0x02, 0x00, 0x12, 0x34]);
+ assert_eq!(format!("{}", script), "OP_PUSHDATA2 1234");
+
+ // OP_PUSHDATA4
+ let script = Script::from_bytes(&[0x4e, 0x02, 0x00, 0x00, 0x00, 0x56, 0x78]);
+ assert_eq!(format!("{}", script), "OP_PUSHDATA4 5678");
+}
+
+#[test]
+fn script_buf_display() {
+ let script_buf = ScriptBuf::from(vec![0x00, 0xa1, 0xb2]);
+ assert_eq!(format!("{}", script_buf), "OP_0 OP_LESSTHANOREQUAL OP_CSV");
+
+ #[cfg(feature = "hex")]
+ {
+ assert_eq!(format!("{:x}", script_buf), "00a1b2");
+ assert_eq!(format!("{:X}", script_buf), "00A1B2");
+ }
+ assert!(!format!("{:?}", script_buf).is_empty());
+}
+
+#[test]
+fn encode() {
+ // Consensus encoding includes the length of the encoded data
+ // (compact size encoded length prefix).
+ let consensus_encoded: [u8; 6] = [0x05, 1, 2, 3, 4, 5];
+
+ // `from_bytes` does not expect the prefix.
+ let script = Script::from_bytes(&consensus_encoded[1..]);
+
+ let got = encoding::encode_to_vec(script);
+ assert_eq!(got, consensus_encoded);
+}
+
#[test]
#[cfg(feature = "hex")]
fn script_to_hex() {
@@ -350,3 +484,64 @@ fn script_buf_to_hex() {
let hex = format!("{script:x}");
assert_eq!(hex, "a1b2c3");
}
+
+#[test]
+fn script_consensus_decode_empty() {
+ let bytes = vec![0_u8];
+ let mut push = bytes.as_slice();
+ let mut decoder = ScriptBuf::decoder();
+ decoder.push_bytes(&mut push).unwrap();
+
+ let got = decoder.end().unwrap();
+ let want = ScriptBuf::new();
+
+ assert_eq!(got, want);
+}
+
+#[test]
+fn script_consensus_decode_empty_with_more_data() {
+ // An empty script sig with a bunch of unrelated data at the end.
+ let bytes = vec![0x00_u8, 0xff, 0xff, 0xff, 0xff];
+ let mut push = bytes.as_slice();
+ let mut decoder = ScriptBuf::decoder();
+ decoder.push_bytes(&mut push).unwrap();
+
+ let got = decoder.end().unwrap();
+ let want = ScriptBuf::new();
+
+ assert_eq!(got, want);
+}
+
+#[test]
+fn decoder_full_read_limit() {
+ let mut decoder = ScriptBuf::decoder();
+ // ByteVecDecoder length prefix is CompactSize: needs 1 byte.
+ assert_eq!(decoder.read_limit(), 1);
+
+ // Script length prefix = 32.
+ let mut push = [32_u8].as_slice();
+ decoder.push_bytes(&mut push).unwrap();
+ // Limit is 32 for the script data.
+ assert_eq!(decoder.read_limit(), 32);
+
+ // Provide 1 byte of script data decreasing the read limit by 1.
+ let mut push = [0xAA_u8].as_slice();
+ decoder.push_bytes(&mut push).unwrap();
+ assert_eq!(decoder.read_limit(), 31);
+}
+
+#[test]
+fn decoder_error_display() {
+ #[cfg(feature = "std")]
+ use std::error::Error as _;
+
+ let bytes = vec![0x01_u8];
+ let mut push = bytes.as_slice();
+ let mut decoder = <ScriptBuf as Decodable>::Decoder::default();
+ decoder.push_bytes(&mut push).unwrap();
+
+ let err = decoder.end().unwrap_err();
+ assert!(!err.to_string().is_empty());
+ #[cfg(feature = "std")]
+ assert!(err.source().is_some());
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.