Skip to content

Commit ce0ff23

Browse files
committed
add in tests for the totp time_until functions
1 parent 9b35e8b commit ce0ff23

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/totp.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ impl TOTP {
167167
/// specified start time in case an offset is desired. Both values must be
168168
/// in seconds.
169169
pub fn time_until_refresh_with_start(&self, time: u64, time_start: u64) -> u64 {
170-
(time - time_start) % period
170+
let time_until = (time - time_start) % self.period;
171+
if time_until == 0 {
172+
return self.period;
173+
}
174+
time_until
171175
}
172176
}
173177

tests/totp.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,28 @@ fn rfc_test_6_sha512() {
215215
47863826
216216
)
217217
}
218+
219+
// Tests to check the time_until_refresh methods.
220+
#[test]
221+
fn test_time_until() {
222+
let totp = TOTP::default_from_base32("SecretKey");
223+
assert_eq!(totp.time_until_refresh(15), 15);
224+
}
225+
226+
#[test]
227+
fn test_time_until_at_edge() {
228+
let totp = TOTP::default_from_base32("SecretKey");
229+
assert_eq!(totp.time_until_refresh(30), 30)
230+
}
231+
232+
#[test]
233+
fn test_time_until_with_start() {
234+
let totp = TOTP::default_from_base32("SecretKey");
235+
assert_eq!(totp.time_until_refresh_with_start(30, 15), 15)
236+
}
237+
238+
#[test]
239+
fn test_time_until_with_start_at_edge() {
240+
let totp = TOTP::default_from_base32("SecretKey");
241+
assert_eq!(totp.time_until_refresh_with_start(45, 15), 30)
242+
}

0 commit comments

Comments
 (0)