Skip to content

Commit e4d550d

Browse files
committed
test: fix tests
1 parent 71d3933 commit e4d550d

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/otp/otp_element.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ pub struct OTPElement {
154154
pub pin: Option<String>,
155155
}
156156

157+
static ALLOWED_DIGITS_RANGE: std::ops::RangeInclusive<u64> = 1..=10;
158+
157159
impl OTPElement {
158160
pub fn get_otpauth_uri(&self) -> String {
159161
let otp_type = self.type_.to_string().to_lowercase();
@@ -183,6 +185,10 @@ impl OTPElement {
183185
}
184186

185187
pub fn get_otp_code(&self) -> Result<String, OtpError> {
188+
if !ALLOWED_DIGITS_RANGE.contains(&self.digits) {
189+
return Err(OtpError::InvalidDigits);
190+
}
191+
186192
match self.type_ {
187193
OTPType::Totp => {
188194
let code = totp(&self.secret, self.algorithm)?;
@@ -361,7 +367,7 @@ mod test {
361367
#[test]
362368
fn test_invalid_digits_should_not_overflow() {
363369
// Arrange
364-
let invalid_digits_value = 10;
370+
let invalid_digits_value = 11;
365371

366372
let element = OTPElement {
367373
secret: "xr5gh44x7bprcqgrdtulafeevt5rxqlbh5wvked22re43dh2d4mapv5g".to_uppercase(),
@@ -382,6 +388,30 @@ mod test {
382388
assert_eq!(Err(OtpError::InvalidDigits), result);
383389
}
384390

391+
#[test]
392+
fn test_10_digits_should_be_allowed() {
393+
// Arrange
394+
let invalid_digits_value = 10;
395+
396+
let element = OTPElement {
397+
secret: "xr5gh44x7bprcqgrdtulafeevt5rxqlbh5wvked22re43dh2d4mapv5g".to_uppercase(),
398+
issuer: String::from("IssuerText"),
399+
label: String::from("LabelText"),
400+
digits: invalid_digits_value,
401+
type_: Totp,
402+
algorithm: Sha1,
403+
period: 30,
404+
counter: None,
405+
pin: None,
406+
};
407+
408+
// Act
409+
let result = element.get_otp_code();
410+
411+
// Assert
412+
assert!(result.is_ok());
413+
}
414+
385415
#[test]
386416
fn test_lowercase_secret() {
387417
// Arrange / Act

0 commit comments

Comments
 (0)