123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package com.chameth.yaotp.accounts
-
- import com.chameth.yaotp.algos.hmacSha1
- import com.chameth.yaotp.algos.hmacSha256
- import com.chameth.yaotp.algos.hmacSha512
- import com.chameth.yaotp.toHexString
- import org.junit.Assert
- import org.junit.Rule
- import org.junit.Test
- import org.junit.rules.ExpectedException
- import java.net.URI
-
- class UriParserTest {
-
- @get:Rule
- val expectedExceptionRule = ExpectedException.none()
-
- @Test
- fun testToAccount_throwsForInvalidScheme() {
- expectedExceptionRule.expect(IllegalArgumentException::class.java)
- expectedExceptionRule.expectMessage("Scheme must be 'otpauth', got 'foo'")
-
- toAccount(URI.create("foo://hotp/baz?secret=23"))
- }
-
- @Test
- fun testToAccount_throwsForInvalidType() {
- expectedExceptionRule.expect(IllegalArgumentException::class.java)
- expectedExceptionRule.expectMessage("Unrecognised type: bar")
-
- toAccount(URI.create("otpauth://bar/baz?secret=23"))
- }
-
- @Test
- fun testToAccount_throwsForMissingLabel() {
- expectedExceptionRule.expect(IllegalArgumentException::class.java)
- expectedExceptionRule.expectMessage("No label specified")
-
- toAccount(URI.create("otpauth://hotp/?secret=23"))
- }
-
- @Test
- fun testToAccount_throwsForNoQueryString() {
- expectedExceptionRule.expect(IllegalArgumentException::class.java)
- expectedExceptionRule.expectMessage("No query string supplied")
-
- toAccount(URI.create("otpauth://totp/foo"))
- }
-
- @Test
- fun testToAccount_throwsForNoSecret() {
- expectedExceptionRule.expect(IllegalArgumentException::class.java)
- expectedExceptionRule.expectMessage("No 'secret' parameter specified, got [secrat]")
-
- toAccount(URI.create("otpauth://totp/foo?secrat=23"))
- }
-
- @Test
- fun testToAccount_throwsForNoCounter() {
- expectedExceptionRule.expect(IllegalArgumentException::class.java)
- expectedExceptionRule.expectMessage("No 'counter' parameter specified for HOTP")
-
- toAccount(URI.create("otpauth://hotp/foo?secret=23"))
- }
-
- @Test
- fun testToAccount_returnsHotpAccount_withDefaultArgs() {
- val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&counter=123")) as HotpAccount
- Assert.assertNull(account.issuer)
- Assert.assertEquals("foo", account.label)
- Assert.assertEquals(::hmacSha1, account.params.hmacFunc)
- Assert.assertEquals(6, account.params.length)
- Assert.assertEquals("0045adf4", account.params.key.toHexString())
- Assert.assertEquals(123L, account.counter)
- }
-
- @Test
- fun testToAccount_returnsHotpAccount_withIssuer() {
- val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&issuer=BigCorp%20Inc&counter=0")) as HotpAccount
- Assert.assertEquals("BigCorp Inc", account.issuer)
- }
-
- @Test
- fun testToAccount_returnsHotpAccount_withCustomDigits() {
- val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&digits=8&counter=0")) as HotpAccount
- Assert.assertEquals(8, account.params.length)
- }
-
- @Test
- fun testToAccount_returnsHotpAccount_withInvalidDigits() {
- val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&digits=HALLO&counter=0")) as HotpAccount
- Assert.assertEquals(6, account.params.length)
- }
-
- @Test
- fun testToAccount_returnsHotpAccount_withSha256() {
- val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&algorithm=Sha256&counter=0")) as HotpAccount
- Assert.assertEquals(::hmacSha256, account.params.hmacFunc)
- }
-
- @Test
- fun testToAccount_returnsHotpAccount_withSha512() {
- val account = toAccount(URI.create("otpauth://hotp/foo?secret=ABC234&algorithm=SHA512&counter=0")) as HotpAccount
- Assert.assertEquals(::hmacSha512, account.params.hmacFunc)
- }
-
- @Test
- fun testToAccount_returnsTotpAccount_withDefaultArgs() {
- val account = toAccount(URI.create("otpauth://totp/foo?secret=ABC234")) as TotpAccount
- Assert.assertNull(account.issuer)
- Assert.assertEquals("foo", account.label)
- Assert.assertEquals(::hmacSha1, account.params.hotpParams.hmacFunc)
- Assert.assertEquals(6, account.params.hotpParams.length)
- Assert.assertEquals("0045adf4", account.params.hotpParams.key.toHexString())
- Assert.assertEquals(0, account.params.startTime)
- Assert.assertEquals(30, account.params.step)
- }
-
- @Test
- fun testToAccount_returnsTotpAccount_withCustomStep() {
- val account = toAccount(URI.create("otpauth://totp/foo?secret=ABC234&step=60")) as TotpAccount
- Assert.assertEquals(60, account.params.step)
- }
-
- @Test
- fun testParseUri_returnsAccountIfValid() {
- Assert.assertNotNull(parseUri("otpauth://totp/foo?secret=ABC234&step=60"))
- Assert.assertNotNull(parseUri("otpauth://hotp/foo?secret=ABC234&counter=60"))
- }
-
- @Test
- fun testParseUri_returnsNullIfInvalid() {
- Assert.assertNull(parseUri(""))
- Assert.assertNull(parseUri("NOT A URI"))
- Assert.assertNull(parseUri("foo://hotp/baz?secret=23"))
- Assert.assertNull(parseUri("otpauth://hotp/baz?secret=23"))
- }
-
- }
|