Skip to content

Commit bb0c07e

Browse files
committed
test(runtime): add unit tests for borsh buffer and deserialization
1 parent 0900208 commit bb0c07e

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

hsrs/src/borsh_buffer.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,42 @@ fn hsrs_borsh_ptr(buf: &BorshBuffer) -> *const u8 {
3535
fn hsrs_borsh_free(buf: repr_c::Box<BorshBuffer>) {
3636
drop(buf);
3737
}
38+
39+
#[cfg(test)]
40+
#[allow(
41+
clippy::all,
42+
clippy::pedantic,
43+
clippy::nursery,
44+
clippy::cargo,
45+
clippy::unwrap_used,
46+
clippy::expect_used,
47+
clippy::missing_docs_in_private_items,
48+
clippy::exhaustive_enums,
49+
clippy::exhaustive_structs,
50+
clippy::indexing_slicing,
51+
clippy::shadow_reuse,
52+
clippy::shadow_same,
53+
clippy::shadow_unrelated
54+
)]
55+
mod tests {
56+
use super::*;
57+
58+
#[test]
59+
fn from_borsh_serializes_value() {
60+
let buf = BorshBuffer::from_borsh(&42u32);
61+
assert!(!buf.bytes.is_empty());
62+
}
63+
64+
#[test]
65+
fn borsh_len_returns_byte_count() {
66+
let buf = BorshBuffer::from_borsh(&42u32);
67+
assert_eq!(hsrs_borsh_len(&buf), buf.bytes.len() as u64);
68+
}
69+
70+
#[test]
71+
fn borsh_ptr_returns_valid_pointer() {
72+
let buf = BorshBuffer::from_borsh(&42u32);
73+
let ptr = hsrs_borsh_ptr(&buf);
74+
assert_eq!(ptr, buf.bytes.as_ptr());
75+
}
76+
}

hsrs/src/ffi_utils.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,32 @@ pub unsafe fn borsh_deserialize<T: borsh::BorshDeserialize>(ptr: *const u8, len:
1111
let bytes = unsafe { core::slice::from_raw_parts(ptr, len as usize) };
1212
borsh::from_slice(bytes).expect("hsrs: borsh deserialization failed")
1313
}
14+
15+
#[cfg(test)]
16+
#[allow(
17+
clippy::all,
18+
clippy::pedantic,
19+
clippy::nursery,
20+
clippy::cargo,
21+
clippy::unwrap_used,
22+
clippy::expect_used,
23+
unsafe_code,
24+
clippy::missing_docs_in_private_items,
25+
clippy::exhaustive_enums,
26+
clippy::exhaustive_structs,
27+
clippy::indexing_slicing,
28+
clippy::shadow_reuse,
29+
clippy::shadow_same,
30+
clippy::shadow_unrelated
31+
)]
32+
mod tests {
33+
use super::*;
34+
35+
#[test]
36+
fn borsh_deserialize_roundtrips() {
37+
let original: u32 = 42;
38+
let bytes = borsh::to_vec(&original).unwrap();
39+
let result: u32 = unsafe { borsh_deserialize(bytes.as_ptr(), bytes.len() as u64) };
40+
assert_eq!(result, original);
41+
}
42+
}

0 commit comments

Comments
 (0)