Skip to content

Commit 089b8d8

Browse files
committed
consolidate helpers
1 parent f341815 commit 089b8d8

4 files changed

Lines changed: 60 additions & 62 deletions

File tree

pinocchio/interface/tests/helpers/layout.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ pub fn assert_meta_compat(new: &Meta, legacy: &LegacyMeta) {
6262
);
6363
}
6464

65+
pub fn overwrite_tail(bytes: &mut [u8], tail: [u8; 4]) -> [u8; 4] {
66+
bytes[PADDING_OFFSET..STATE_LEN].copy_from_slice(&tail);
67+
tail
68+
}
69+
70+
pub fn assert_tail(layout_bytes: &[u8], tail: [u8; 4]) {
71+
assert_eq!(&layout_bytes[PADDING_OFFSET..STATE_LEN], &tail);
72+
}
73+
74+
pub fn assert_tail_zeroed(layout_bytes: &[u8]) {
75+
assert_eq!(&layout_bytes[PADDING_OFFSET..STATE_LEN], &[0u8; 4]);
76+
}
77+
78+
/// Verifies that the deserialized layout is a true zero-copy borrow into the original byte slice.
79+
pub fn assert_borrows_at<T>(borrow: &T, bytes: &[u8], offset: usize) {
80+
let ptr = borrow as *const T;
81+
let expected = unsafe { bytes.as_ptr().add(offset) };
82+
assert_eq!(ptr as *const u8, expected);
83+
}
84+
85+
/// Verifies that the deserialized mutable view is a true zero-copy borrow into the original
86+
/// byte slice at the given offset.
87+
pub fn assert_mut_borrows_at<T>(borrow: &mut T, base_ptr: *mut u8, offset: usize) {
88+
let ptr = borrow as *mut T as *mut u8;
89+
let expected = unsafe { base_ptr.add(offset) };
90+
assert_eq!(ptr, expected);
91+
}
92+
6593
pub fn assert_stake_compat(new: &Stake, legacy: &LegacyStake) {
6694
assert_eq!(
6795
new.delegation.voter_pubkey.to_bytes(),

pinocchio/interface/tests/transitions.rs

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,22 @@ use {
2222
test_case::test_case,
2323
};
2424

25-
fn assert_tail_zeroed(layout_bytes: &[u8]) {
26-
assert_eq!(&layout_bytes[PADDING_OFFSET..STATE_LEN], &[0u8; 4]);
27-
}
28-
29-
fn assert_tail(layout_bytes: &[u8], tail: [u8; 4]) {
30-
assert_eq!(&layout_bytes[PADDING_OFFSET..STATE_LEN], &tail);
31-
}
32-
33-
#[test]
34-
fn short_buffer_decode() {
35-
let mut data = vec![0u8; STATE_LEN - 1];
36-
let err = StakeStateV2::from_bytes_mut(&mut data).unwrap_err();
37-
assert!(matches!(err, StakeStateError::Decode));
38-
}
39-
40-
#[test]
41-
fn invalid_tag_err() {
42-
let mut data = [0u8; STATE_LEN];
43-
data[0..4].copy_from_slice(&999u32.to_le_bytes());
44-
let err = StakeStateV2::from_bytes_mut(&mut data).unwrap_err();
45-
assert!(matches!(err, StakeStateError::InvalidTag(999)));
25+
/// Creates a test buffer with the given base bytes placed at an optional 1-byte offset
26+
/// (for unaligned access testing) and trailing bytes filled with the given value.
27+
///
28+
/// Returns `(buffer, start)` where `start` is the offset of the layout bytes.
29+
fn test_buffer(
30+
base: &[u8],
31+
unaligned: bool,
32+
trailing_len: usize,
33+
trailing_fill: u8,
34+
) -> (Vec<u8>, usize) {
35+
assert_eq!(base.len(), STATE_LEN);
36+
let start = if unaligned { 1 } else { 0 };
37+
let mut buffer = vec![238u8; start + STATE_LEN + trailing_len];
38+
buffer[start..start + STATE_LEN].copy_from_slice(base);
39+
buffer[start + STATE_LEN..].fill(trailing_fill);
40+
(buffer, start)
4641
}
4742

4843
#[test]
@@ -149,16 +144,12 @@ fn invalid_transitions_err(unaligned: bool, trailing_len: usize) {
149144
StakeStateV2Tag::RewardsPool,
150145
];
151146

152-
let start = if unaligned { 1 } else { 0 };
153-
154147
for from in tags {
155148
for &op in &[Op::ToInitialized, Op::ToStake] {
156149
let mut base = empty_state_bytes(from);
157150
base[PADDING_OFFSET..STATE_LEN].copy_from_slice(&[250, 251, 252, 253]);
158151

159-
let mut buffer = vec![238u8; start + STATE_LEN + trailing_len];
160-
buffer[start..start + STATE_LEN].copy_from_slice(&base);
161-
buffer[start + STATE_LEN..start + STATE_LEN + trailing_len].fill(123);
152+
let (mut buffer, start) = test_buffer(&base, unaligned, trailing_len, 123);
162153

163154
let expected_layout = buffer[start..start + STATE_LEN].to_vec();
164155
let expected_trailing =
@@ -214,10 +205,7 @@ fn uninitialized_to_initialized(unaligned: bool, trailing_len: usize) {
214205
let mut base = [170u8; STATE_LEN];
215206
write_tag(&mut base, StakeStateV2Tag::Uninitialized);
216207

217-
let start = if unaligned { 1 } else { 0 };
218-
let mut buffer = vec![238u8; start + STATE_LEN + trailing_len];
219-
buffer[start..start + STATE_LEN].copy_from_slice(&base);
220-
buffer[start + STATE_LEN..start + STATE_LEN + trailing_len].fill(124);
208+
let (mut buffer, start) = test_buffer(&base, unaligned, trailing_len, 124);
221209

222210
let expected_trailing = buffer[start + STATE_LEN..start + STATE_LEN + trailing_len].to_vec();
223211

@@ -292,10 +280,7 @@ fn initialized_to_stake(unaligned: bool, trailing_len: usize) {
292280
let preserved_tail: [u8; 4] = [1, 222, 173, 190];
293281
base[PADDING_OFFSET..STATE_LEN].copy_from_slice(&preserved_tail);
294282

295-
let start = if unaligned { 1 } else { 0 };
296-
let mut buffer = vec![238u8; start + STATE_LEN + trailing_len];
297-
buffer[start..start + STATE_LEN].copy_from_slice(&base);
298-
buffer[start + STATE_LEN..start + STATE_LEN + trailing_len].fill(125);
283+
let (mut buffer, start) = test_buffer(&base, unaligned, trailing_len, 125);
299284

300285
let expected_trailing = buffer[start + STATE_LEN..start + STATE_LEN + trailing_len].to_vec();
301286

@@ -393,10 +378,7 @@ fn initialized_to_stake_meta_mut_works(unaligned: bool, trailing_len: usize) {
393378
let preserved_tail: [u8; 4] = [1, 222, 173, 190];
394379
base[PADDING_OFFSET..STATE_LEN].copy_from_slice(&preserved_tail);
395380

396-
let start = if unaligned { 1 } else { 0 };
397-
let mut buffer = vec![238u8; start + STATE_LEN + trailing_len];
398-
buffer[start..start + STATE_LEN].copy_from_slice(&base);
399-
buffer[start + STATE_LEN..start + STATE_LEN + trailing_len].fill(99);
381+
let (mut buffer, start) = test_buffer(&base, unaligned, trailing_len, 99);
400382

401383
let trailing_before = buffer[start + STATE_LEN..start + STATE_LEN + trailing_len].to_vec();
402384

@@ -560,10 +542,7 @@ fn stake_to_stake_preserves_tail(unaligned: bool, trailing_len: usize) {
560542
let preserved_tail: [u8; 4] = [1, 222, 173, 190];
561543
base[PADDING_OFFSET..STATE_LEN].copy_from_slice(&preserved_tail);
562544

563-
let start = if unaligned { 1 } else { 0 };
564-
let mut buffer = vec![238u8; start + STATE_LEN + trailing_len];
565-
buffer[start..start + STATE_LEN].copy_from_slice(&base);
566-
buffer[start + STATE_LEN..start + STATE_LEN + trailing_len].fill(126);
545+
let (mut buffer, start) = test_buffer(&base, unaligned, trailing_len, 126);
567546

568547
let expected_trailing = buffer[start + STATE_LEN..start + STATE_LEN + trailing_len].to_vec();
569548

pinocchio/interface/tests/view.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,6 @@ use {
2323
test_case::test_case,
2424
};
2525

26-
// Verifies that the deserialized layout is a true zero-copy borrow into the original byte slice.
27-
fn assert_borrows_at<T>(borrow: &T, bytes: &[u8], offset: usize) {
28-
let ptr = borrow as *const T;
29-
let expected = unsafe { bytes.as_ptr().add(offset) };
30-
assert_eq!(ptr as *const u8, expected);
31-
}
32-
33-
fn overwrite_tail(bytes: &mut [u8], tail: [u8; 4]) {
34-
bytes[PADDING_OFFSET..STATE_LEN].copy_from_slice(&tail);
35-
}
36-
3726
#[test]
3827
fn len_constant_is_200() {
3928
assert_eq!(StakeStateV2::LEN, 200);

pinocchio/interface/tests/view_mut.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ use {
2424
test_case::test_case,
2525
};
2626

27-
// Verifies that the deserialized mutable view is a true zero-copy borrow into the original
28-
// byte slice at the given offset.
29-
fn assert_mut_borrows_at<T>(borrow: &mut T, base_ptr: *mut u8, offset: usize) {
30-
let ptr = borrow as *mut T as *mut u8;
31-
let expected = unsafe { base_ptr.add(offset) };
32-
assert_eq!(ptr, expected);
27+
#[test]
28+
fn short_buffer_returns_decode() {
29+
let mut data = vec![0u8; STATE_LEN - 1];
30+
let err = StakeStateV2::from_bytes_mut(&mut data).unwrap_err();
31+
assert!(matches!(err, StakeStateError::Decode));
3332
}
3433

35-
fn overwrite_tail(bytes: &mut [u8], tail: [u8; 4]) -> [u8; 4] {
36-
bytes[PADDING_OFFSET..STATE_LEN].copy_from_slice(&tail);
37-
tail
34+
#[test]
35+
fn invalid_tag_returns_error() {
36+
let mut data = [0u8; STATE_LEN];
37+
data[0..4].copy_from_slice(&999u32.to_le_bytes());
38+
let err = StakeStateV2::from_bytes_mut(&mut data).unwrap_err();
39+
assert!(matches!(err, StakeStateError::InvalidTag(999)));
3840
}
3941

4042
#[test_case(StakeStateV2Tag::Uninitialized)]

0 commit comments

Comments
 (0)