aboutsummaryrefslogtreecommitdiffstats
path: root/crypto-pgp/src/androidTest/kotlin/dev/msfjarvis/aps/crypto/GPGKeyPairTest.kt
blob: 2340d9a5291009189cfb28e07e265978bf780fb8 (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
/*
 * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */

package dev.msfjarvis.aps.crypto

import androidx.test.platform.app.InstrumentationRegistry
import com.proton.Gopenpgp.crypto.Key
import dev.msfjarvis.aps.crypto.utils.CryptoConstants
import dev.msfjarvis.aps.cryptopgp.test.R
import dev.msfjarvis.aps.data.crypto.GPGKeyPair
import dev.msfjarvis.aps.data.crypto.KeyPairException
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import org.junit.Test

public class GPGKeyPairTest {

  @Test
  public fun testIfKeyIdIsCorrect() {
    val gpgKey = Key(getKey())
    val keyPair = GPGKeyPair(gpgKey)

    assertEquals(CryptoConstants.KEY_ID, keyPair.getKeyId())
  }

  @Test
  public fun testBuildingKeyPairWithoutPrivateKey() {
    assertFailsWith<KeyPairException.PrivateKeyUnavailableException>(
      "GPGKeyPair does not have a private sub key"
    ) {
      // Get public key object from private key
      val gpgKey = Key(getKey()).toPublic()
      // Try creating a KeyPair from public key
      val keyPair = GPGKeyPair(gpgKey)

      keyPair.getPrivateKey()
    }
  }

  private companion object {

    fun getKey(): String =
      InstrumentationRegistry.getInstrumentation()
        .context
        .resources
        .openRawResource(R.raw.private_key)
        .readBytes()
        .decodeToString()
  }
}