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.

UriParserTest.kt 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.chameth.yaotp.accounts
  2. import com.chameth.yaotp.algos.hmacSha1
  3. import com.chameth.yaotp.algos.hmacSha256
  4. import com.chameth.yaotp.algos.hmacSha512
  5. import com.chameth.yaotp.toHexString
  6. import org.junit.Assert
  7. import org.junit.Rule
  8. import org.junit.Test
  9. import org.junit.rules.ExpectedException
  10. import java.net.URI
  11. class UriParserTest {
  12. @get:Rule
  13. val expectedExceptionRule = ExpectedException.none()
  14. @Test
  15. fun testToAccount_throwsForInvalidScheme() {
  16. expectedExceptionRule.expect(IllegalArgumentException::class.java)
  17. expectedExceptionRule.expectMessage("Scheme must be 'otpauth', got 'foo'")
  18. toAccount(URI.create("foo://hotp/baz?secret=23"))
  19. }
  20. @Test
  21. fun testToAccount_throwsForInvalidType() {
  22. expectedExceptionRule.expect(IllegalArgumentException::class.java)
  23. expectedExceptionRule.expectMessage("Unrecognised type: bar")
  24. toAccount(URI.create("otpauth://bar/baz?secret=23"))
  25. }
  26. @Test
  27. fun testToAccount_throwsForMissingLabel() {
  28. expectedExceptionRule.expect(IllegalArgumentException::class.java)
  29. expectedExceptionRule.expectMessage("No label specified")
  30. toAccount(URI.create("otpauth://hotp/?secret=23"))
  31. }
  32. @Test
  33. fun testToAccount_throwsForNoQueryString() {
  34. expectedExceptionRule.expect(IllegalArgumentException::class.java)
  35. expectedExceptionRule.expectMessage("No query string supplied")
  36. toAccount(URI.create("otpauth://totp/foo"))
  37. }
  38. @Test
  39. fun testToAccount_throwsForNoSecret() {
  40. expectedExceptionRule.expect(IllegalArgumentException::class.java)
  41. expectedExceptionRule.expectMessage("No 'secret' parameter specified, got [secrat]")
  42. toAccount(URI.create("otpauth://totp/foo?secrat=23"))
  43. }
  44. @Test
  45. fun testToAccount_throwsForNoCounter() {
  46. expectedExceptionRule.expect(IllegalArgumentException::class.java)
  47. expectedExceptionRule.expectMessage("No 'counter' parameter specified for HOTP")
  48. toAccount(URI.create("otpauth://hotp/foo?secret=23"))
  49. }
  50. @Test
  51. fun testToAccount_returnsHotpAccount_withDefaultArgs() {
  52. val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&counter=123")) as HotpAccount
  53. Assert.assertNull(account.issuer)
  54. Assert.assertEquals("foo", account.label)
  55. Assert.assertEquals(::hmacSha1, account.params.hmacFunc)
  56. Assert.assertEquals(6, account.params.length)
  57. Assert.assertEquals("0045adf4", account.params.key.toHexString())
  58. Assert.assertEquals(123L, account.counter)
  59. }
  60. @Test
  61. fun testToAccount_returnsHotpAccount_withIssuer() {
  62. val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&issuer=BigCorp%20Inc&counter=0")) as HotpAccount
  63. Assert.assertEquals("BigCorp Inc", account.issuer)
  64. }
  65. @Test
  66. fun testToAccount_returnsHotpAccount_withCustomDigits() {
  67. val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&digits=8&counter=0")) as HotpAccount
  68. Assert.assertEquals(8, account.params.length)
  69. }
  70. @Test
  71. fun testToAccount_returnsHotpAccount_withInvalidDigits() {
  72. val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&digits=HALLO&counter=0")) as HotpAccount
  73. Assert.assertEquals(6, account.params.length)
  74. }
  75. @Test
  76. fun testToAccount_returnsHotpAccount_withSha256() {
  77. val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&algorithm=Sha256&counter=0")) as HotpAccount
  78. Assert.assertEquals(::hmacSha256, account.params.hmacFunc)
  79. }
  80. @Test
  81. fun testToAccount_returnsHotpAccount_withSha512() {
  82. val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&algorithm=SHA512&counter=0")) as HotpAccount
  83. Assert.assertEquals(::hmacSha512, account.params.hmacFunc)
  84. }
  85. @Test
  86. fun testToAccount_returnsTotpAccount_withDefaultArgs() {
  87. val account = toAccount(URI.create("otpauth://totp/foo?secret=ABC234")) as TotpAccount
  88. Assert.assertNull(account.issuer)
  89. Assert.assertEquals("foo", account.label)
  90. Assert.assertEquals(::hmacSha1, account.params.hotpParams.hmacFunc)
  91. Assert.assertEquals(6, account.params.hotpParams.length)
  92. Assert.assertEquals("0045adf4", account.params.hotpParams.key.toHexString())
  93. Assert.assertEquals(0, account.params.startTime)
  94. Assert.assertEquals(30, account.params.step)
  95. }
  96. @Test
  97. fun testToAccount_returnsTotpAccount_withCustomStep() {
  98. val account = toAccount(URI.create("otpauth://totp/foo?secret=ABC234&step=60")) as TotpAccount
  99. Assert.assertEquals(60, account.params.step)
  100. }
  101. @Test
  102. fun testParseUri_returnsAccountIfValid() {
  103. Assert.assertNotNull(parseUri("otpauth://totp/foo?secret=ABC234&step=60"))
  104. Assert.assertNotNull(parseUri("otpauth://hotp/foo?secret=ABC234&counter=60"))
  105. }
  106. @Test
  107. fun testParseUri_returnsNullIfInvalid() {
  108. Assert.assertNull(parseUri(""))
  109. Assert.assertNull(parseUri("NOT A URI"))
  110. Assert.assertNull(parseUri("foo://hotp/baz?secret=23"))
  111. Assert.assertNull(parseUri("otpauth://hotp/baz?secret=23"))
  112. }
  113. }