aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/test/java/dev/msfjarvis/aps/util/totp/UriTotpFinderTest.kt
blob: 37cbef2fd7ff274cab76c398cf924bdf05794b03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */

package dev.msfjarvis.aps.util.totp

import kotlin.test.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [23])
class UriTotpFinderTest {

  private val totpFinder = UriTotpFinder()

  @Test
  fun findSecret() {
    assertEquals("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", totpFinder.findSecret(TOTP_URI))
    assertEquals(
      "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ",
      totpFinder.findSecret("name\npassword\ntotp: HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ")
    )
    assertEquals("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", totpFinder.findSecret(PASS_FILE_CONTENT))
  }

  @Test
  fun findDigits() {
    assertEquals("12", totpFinder.findDigits(TOTP_URI))
    assertEquals("12", totpFinder.findDigits(PASS_FILE_CONTENT))
  }

  @Test
  fun findPeriod() {
    assertEquals(25, totpFinder.findPeriod(TOTP_URI))
    assertEquals(25, totpFinder.findPeriod(PASS_FILE_CONTENT))
  }

  @Test
  fun findAlgorithm() {
    assertEquals("SHA256", totpFinder.findAlgorithm(TOTP_URI))
    assertEquals("SHA256", totpFinder.findAlgorithm(PASS_FILE_CONTENT))
  }

  @Test
  fun findIssuer() {
    assertEquals("ACME Co", totpFinder.findIssuer(TOTP_URI))
    assertEquals("ACME Co", totpFinder.findIssuer(PASS_FILE_CONTENT))
  }

  companion object {

    const val TOTP_URI =
      "otpauth://totp/ACME%20Co:john@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA256&digits=12&period=25"
    const val PASS_FILE_CONTENT = "password\n$TOTP_URI"
  }
}