Yet Another OTP generator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TotpTest.kt 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package com.chameth.yaotp.algos
  2. import com.natpryce.hamkrest.assertion.assert
  3. import com.natpryce.hamkrest.equalTo
  4. import org.junit.Test
  5. class TotpTest {
  6. private val rfcKey = "12345678901234567890".toByteArray()
  7. @Test
  8. fun testCount_withDefaultStepAndStart_andPastValues() {
  9. assert.that(count(0, 30, 59), equalTo(1L))
  10. assert.that(count(0, 30, 1111111109), equalTo(37037036L))
  11. assert.that(count(0, 30, 1234567890), equalTo(41152263L))
  12. }
  13. @Test
  14. fun testCount_withDefaultStepAndStart_andFutureDates() {
  15. assert.that(count(0, 30, 2000000000), equalTo(66666666L))
  16. assert.that(count(0, 30, 20000000000), equalTo(666666666L))
  17. }
  18. @Test
  19. fun testCount_withCustomStepAndStart() {
  20. assert.that(count(2000000000, 30, 4000000000), equalTo(66666666L))
  21. assert.that(count(0, 60, 4000000000), equalTo(66666666L))
  22. }
  23. @Test
  24. fun testLongToByteArray_withRfcValues() {
  25. assert.that(1L.toByteArray().toHexString(), equalTo("0000000000000001"))
  26. assert.that(37037036L.toByteArray().toHexString(), equalTo("00000000023523ec"))
  27. assert.that(41152263L.toByteArray().toHexString(), equalTo("000000000273ef07"))
  28. assert.that(666666666L.toByteArray().toHexString(), equalTo("0000000027bc86aa"))
  29. }
  30. @Test
  31. fun testTotp_withRfcValues_usingSha1() {
  32. assert.that(totp(rfcKey, 0, 30, 59, 8), equalTo(94287082))
  33. assert.that(totp(rfcKey, 0, 30, 1111111109, 8), equalTo(7081804))
  34. assert.that(totp(rfcKey, 0, 30, 1111111111, 8), equalTo(14050471))
  35. assert.that(totp(rfcKey, 0, 30, 1234567890, 8), equalTo(89005924))
  36. assert.that(totp(rfcKey, 0, 30, 2000000000, 8), equalTo(69279037))
  37. assert.that(totp(rfcKey, 0, 30, 20000000000, 8), equalTo(65353130))
  38. }
  39. }