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