@@ -204,7 +204,7 @@ impl OTPElement {
204204 }
205205 }
206206
207- pub fn format_code ( & self , value : u32 ) -> Result < String , OtpError > {
207+ fn format_code ( & self , value : u32 ) -> Result < String , OtpError > {
208208 // Get the formatted code
209209 let exponential = 10_u32
210210 . checked_pow ( self . digits as u32 )
@@ -234,6 +234,7 @@ mod test {
234234 use crate :: otp:: otp_element:: OTPType :: Totp ;
235235
236236 use crate :: otp:: from_otp_uri:: FromOtpUri ;
237+ use crate :: otp:: otp_error:: OtpError ;
237238
238239 #[ test]
239240 fn test_serialization_otp_uri_full_element ( ) {
@@ -290,4 +291,28 @@ mod test {
290291 let otp_uri = "otpauth://totp/2Ponies%40Github%20No.1?secret=JBSWY3DPEHPK3PXP&algorithm=SHA1&digits=6&period=30&lock=false&issuer=test" ;
291292 assert_eq ! ( true , OTPElement :: from_otp_uri( otp_uri) . is_ok( ) )
292293 }
294+
295+ #[ test]
296+ fn test_invalid_digits_should_not_overflow ( ) {
297+ // Arrange
298+ let invalid_digits_value = 10 ;
299+
300+ let element = OTPElement {
301+ secret : "xr5gh44x7bprcqgrdtulafeevt5rxqlbh5wvked22re43dh2d4mapv5g" . to_uppercase ( ) ,
302+ issuer : String :: from ( "IssuerText" ) ,
303+ label : String :: from ( "LabelText" ) ,
304+ digits : invalid_digits_value,
305+ type_ : Totp ,
306+ algorithm : Sha1 ,
307+ period : 30 ,
308+ counter : None ,
309+ pin : None ,
310+ } ;
311+
312+ // Act
313+ let result = element. get_otp_code ( ) ;
314+
315+ // Assert
316+ assert_eq ! ( Err ( OtpError :: InvalidDigits ) , result)
317+ }
293318}
0 commit comments