File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -167,7 +167,11 @@ impl TOTP {
167
167
/// specified start time in case an offset is desired. Both values must be
168
168
/// in seconds.
169
169
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
171
175
}
172
176
}
173
177
Original file line number Diff line number Diff line change @@ -215,3 +215,28 @@ fn rfc_test_6_sha512() {
215
215
47863826
216
216
)
217
217
}
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
+ }
You can’t perform that action at this time.
0 commit comments