@@ -138,9 +138,10 @@ fn it_can_serialize_deserialize() {
138
138
139
139
#[ cfg( feature = "borsh" ) ]
140
140
mod borsh_tests {
141
- use rust_decimal:: Decimal ;
142
141
use std:: str:: FromStr ;
143
142
143
+ use rust_decimal:: Decimal ;
144
+
144
145
#[ test]
145
146
fn it_can_serialize_deserialize_borsh ( ) {
146
147
let tests = [
@@ -163,6 +164,42 @@ mod borsh_tests {
163
164
assert_eq ! ( test. to_string( ) , b. to_string( ) ) ;
164
165
}
165
166
}
167
+
168
+ #[ test]
169
+ fn invalid_flags_errors ( ) {
170
+ let mut bytes: Vec < u8 > = Vec :: new ( ) ;
171
+ // Invalid flags
172
+ borsh:: BorshSerialize :: serialize ( & u32:: MAX , & mut bytes) . unwrap ( ) ;
173
+ // high
174
+ borsh:: BorshSerialize :: serialize ( & u32:: MAX , & mut bytes) . unwrap ( ) ;
175
+ // lo
176
+ borsh:: BorshSerialize :: serialize ( & u32:: MAX , & mut bytes) . unwrap ( ) ;
177
+ // mid
178
+ borsh:: BorshSerialize :: serialize ( & u32:: MAX , & mut bytes) . unwrap ( ) ;
179
+
180
+ let _err =
181
+ <Decimal as borsh:: BorshDeserialize >:: deserialize ( & mut bytes. as_slice ( ) ) . expect_err ( "Invalid flags passed" ) ;
182
+ }
183
+
184
+ #[ test]
185
+ fn invalid_scale_errors ( ) {
186
+ let mut bytes: Vec < u8 > = Vec :: new ( ) ;
187
+ // Invalid scale
188
+ borsh:: BorshSerialize :: serialize ( & 0x00FF_0000_u32 , & mut bytes) . unwrap ( ) ;
189
+ // high
190
+ borsh:: BorshSerialize :: serialize ( & u32:: MAX , & mut bytes) . unwrap ( ) ;
191
+ // lo
192
+ borsh:: BorshSerialize :: serialize ( & u32:: MAX , & mut bytes) . unwrap ( ) ;
193
+ // mid
194
+ borsh:: BorshSerialize :: serialize ( & u32:: MAX , & mut bytes) . unwrap ( ) ;
195
+
196
+ let err =
197
+ <Decimal as borsh:: BorshDeserialize >:: deserialize ( & mut bytes. as_slice ( ) ) . expect_err ( "Invalid scale passed" ) ;
198
+ assert_eq ! (
199
+ err. downcast:: <rust_decimal:: Error >( ) . expect( "Expected str flags error" ) ,
200
+ rust_decimal:: Error :: ScaleExceedsMaximumPrecision ( 0xFF )
201
+ ) ;
202
+ }
166
203
}
167
204
168
205
#[ cfg( feature = "ndarray" ) ]
0 commit comments