@layerzerolabs/common-utils-stellar-contracts 0.2.122

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,160 @@
1
+ use crate::{bytes_ext::BytesExt, tests::test_helper::assert_panics_contains};
2
+ use soroban_sdk::{Bytes, Env};
3
+
4
+ // ============================================
5
+ // Helper functions
6
+ // ============================================
7
+
8
+ /// Helper to assert that `to_array::<N>()` panics with BytesExtError::LengthMismatch (1400)
9
+ fn assert_to_array_panics<const N: usize>(case: &str, raw: &[u8]) {
10
+ const EXPECTED: &str = "Error(Contract, #1040)"; // LengthMismatch
11
+ assert_panics_contains(case, EXPECTED, || {
12
+ let env = Env::default();
13
+ let bytes = Bytes::from_slice(&env, raw);
14
+ let _arr: [u8; N] = bytes.to_array();
15
+ });
16
+ }
17
+
18
+ // ============================================
19
+ // Successful conversion tests
20
+ // ============================================
21
+
22
+ #[test]
23
+ fn test_to_array_various_sizes() {
24
+ let env = Env::default();
25
+
26
+ // Empty array
27
+ let bytes = Bytes::from_array(&env, &[]);
28
+ let arr: [u8; 0] = bytes.to_array();
29
+ let expected: [u8; 0] = [];
30
+ assert_eq!(arr, expected);
31
+
32
+ // Single byte
33
+ let bytes = Bytes::from_array(&env, &[0xAB]);
34
+ assert_eq!(bytes.to_array::<1>(), [0xAB]);
35
+
36
+ // Two bytes
37
+ let bytes = Bytes::from_array(&env, &[0xAB, 0xCD]);
38
+ assert_eq!(bytes.to_array::<2>(), [0xAB, 0xCD]);
39
+
40
+ // Four bytes
41
+ let bytes = Bytes::from_array(&env, &[0x01, 0x02, 0x03, 0x04]);
42
+ assert_eq!(bytes.to_array::<4>(), [0x01, 0x02, 0x03, 0x04]);
43
+
44
+ // Eight bytes - verify order preserved
45
+ let bytes = Bytes::from_array(&env, &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
46
+ let arr = bytes.to_array::<8>();
47
+ assert_eq!(arr[0], 0x01);
48
+ assert_eq!(arr[7], 0x08);
49
+
50
+ // 32 bytes
51
+ let data: [u8; 32] = [0x42; 32];
52
+ let bytes = Bytes::from_array(&env, &data);
53
+ assert_eq!(bytes.to_array::<32>(), data);
54
+
55
+ // 64 bytes
56
+ let data: [u8; 64] = core::array::from_fn(|i| i as u8);
57
+ let bytes = Bytes::from_array(&env, &data);
58
+ assert_eq!(bytes.to_array::<64>(), data);
59
+ }
60
+
61
+ #[test]
62
+ fn test_to_array_boundary_values() {
63
+ let env = Env::default();
64
+
65
+ // All zeros
66
+ let data: [u8; 16] = [0x00; 16];
67
+ let bytes = Bytes::from_array(&env, &data);
68
+ let arr: [u8; 16] = bytes.to_array();
69
+ assert_eq!(arr, data);
70
+
71
+ // All ones
72
+ let data: [u8; 16] = [0xFF; 16];
73
+ let bytes = Bytes::from_array(&env, &data);
74
+ let arr: [u8; 16] = bytes.to_array();
75
+ assert_eq!(arr, data);
76
+
77
+ // Boundary byte values
78
+ let bytes = Bytes::from_array(&env, &[0x00, 0x7F, 0x80, 0xFF]);
79
+ let arr: [u8; 4] = bytes.to_array();
80
+ assert_eq!(arr[0], 0x00); // min
81
+ assert_eq!(arr[1], 0x7F); // max positive signed
82
+ assert_eq!(arr[2], 0x80); // min negative signed
83
+ assert_eq!(arr[3], 0xFF); // max
84
+
85
+ // Sequential values
86
+ let data: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
87
+ let bytes = Bytes::from_array(&env, &data);
88
+ let arr: [u8; 10] = bytes.to_array();
89
+ for i in 0..10 {
90
+ assert_eq!(arr[i], i as u8);
91
+ }
92
+ }
93
+
94
+ // ============================================
95
+ // Length mismatch tests (error conditions)
96
+ // ============================================
97
+
98
+ #[test]
99
+ fn test_to_array_length_mismatch_panics_table() {
100
+ // Bytes too short
101
+ assert_to_array_panics::<4>("3 bytes to [u8; 4]", &[0x01, 0x02, 0x03]);
102
+ // Bytes too long
103
+ assert_to_array_panics::<4>("5 bytes to [u8; 4]", &[0x01, 0x02, 0x03, 0x04, 0x05]);
104
+ // Empty to non-empty
105
+ assert_to_array_panics::<4>("0 bytes to [u8; 4]", &[]);
106
+ // Off-by-one short
107
+ assert_to_array_panics::<8>("7 bytes to [u8; 8]", &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]);
108
+ // Off-by-one long
109
+ assert_to_array_panics::<8>("9 bytes to [u8; 8]", &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09]);
110
+ // Significantly wrong size
111
+ assert_to_array_panics::<32>("4 bytes to [u8; 32]", &[0x01, 0x02, 0x03, 0x04]);
112
+ }
113
+
114
+ #[test]
115
+ fn test_to_array_large_sizes() {
116
+ let env = Env::default();
117
+
118
+ // Test with 128 bytes
119
+ let data: [u8; 128] = core::array::from_fn(|i| (i % 256) as u8);
120
+ let bytes = Bytes::from_array(&env, &data);
121
+ assert_eq!(bytes.to_array::<128>(), data);
122
+
123
+ // Test with 256 bytes
124
+ let data: [u8; 256] = core::array::from_fn(|i| (i % 256) as u8);
125
+ let bytes = Bytes::from_array(&env, &data);
126
+ assert_eq!(bytes.to_array::<256>(), data);
127
+ }
128
+
129
+ #[test]
130
+ fn test_to_array_very_large_size() {
131
+ let env = Env::default();
132
+
133
+ // Test with 512 bytes to ensure it works beyond 256
134
+ let data: [u8; 512] = core::array::from_fn(|i| (i % 256) as u8);
135
+ let bytes = Bytes::from_array(&env, &data);
136
+ let arr = bytes.to_array::<512>();
137
+
138
+ // Verify first and last elements
139
+ assert_eq!(arr[0], 0);
140
+ assert_eq!(arr[255], 255);
141
+ assert_eq!(arr[256], 0);
142
+ assert_eq!(arr[511], 255);
143
+
144
+ // Verify full array matches
145
+ assert_eq!(arr, data);
146
+ }
147
+
148
+ #[test]
149
+ fn test_to_array_preserves_all_values() {
150
+ let env = Env::default();
151
+
152
+ // Test that all byte values (0-255) are preserved correctly
153
+ let data: [u8; 256] = core::array::from_fn(|i| i as u8);
154
+ let bytes = Bytes::from_array(&env, &data);
155
+ let arr: [u8; 256] = bytes.to_array();
156
+
157
+ for i in 0..256 {
158
+ assert_eq!(arr[i], i as u8, "Mismatch at index {}", i);
159
+ }
160
+ }
@@ -0,0 +1,13 @@
1
+ mod auth;
2
+ mod buffer_reader;
3
+ mod buffer_writer;
4
+ mod bytes_ext;
5
+ mod multisig;
6
+ mod option_ext;
7
+ mod ownable;
8
+ mod rbac;
9
+ mod test_helper;
10
+ mod testing_utils;
11
+ mod ttl_configurable;
12
+ mod ttl_extendable;
13
+ mod upgradeable;