aboutsummaryrefslogtreecommitdiffstats
path: root/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt
blob: 4b022e5eff9eac9659fbd8c0fe3666ef6d4bfe81 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package dev.msfjarvis.aps.crypto

import com.github.michaelbull.result.unwrap
import com.github.michaelbull.result.unwrapError
import java.io.File
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.Rule
import org.junit.rules.TemporaryFolder

@OptIn(ExperimentalCoroutinesApi::class)
class PGPKeyManagerTest {

  @get:Rule val temporaryFolder: TemporaryFolder = TemporaryFolder()
  private val filesDir by unsafeLazy { temporaryFolder.root }
  private val keysDir by unsafeLazy { File(filesDir, PGPKeyManager.KEY_DIR_NAME) }
  private val dispatcher = StandardTestDispatcher()
  private val scope = TestScope(dispatcher)
  private val keyManager by unsafeLazy { PGPKeyManager(filesDir.absolutePath, dispatcher) }
  private val key = PGPKeyManager.makeKey(TestUtils.getArmoredPrivateKey())

  private fun <T> unsafeLazy(initializer: () -> T) =
    lazy(LazyThreadSafetyMode.NONE) { initializer.invoke() }

  @BeforeTest
  fun setUp() {
    Dispatchers.setMain(dispatcher)
  }

  @AfterTest
  fun tearDown() {
    Dispatchers.resetMain()
  }

  @Test
  fun testAddingKey() =
    scope.runTest {
      // Check if the key id returned is correct
      val keyId = keyManager.addKey(key).unwrap().getKeyId()
      assertEquals(CryptoConstants.KEY_ID, keyId)

      // Check if the keys directory have one file
      assertEquals(1, filesDir.list()?.size)

      // Check if the file name is correct
      val keyFile = keysDir.listFiles()?.first()
      assertEquals(keyFile?.name, "$keyId.${PGPKeyManager.KEY_EXTENSION}")
    }

  @Test
  fun testAddingKeyWithoutReplaceFlag() =
    scope.runTest {
      // Check adding the keys twice
      keyManager.addKey(key, false).unwrap()
      val error = keyManager.addKey(key, false).unwrapError()

      assertIs<KeyManagerException.KeyAlreadyExistsException>(error)
    }

  @Test
  fun testAddingKeyWithReplaceFlag() =
    scope.runTest {
      // Check adding the keys twice
      keyManager.addKey(key, true).unwrap()
      val keyId = keyManager.addKey(key, true).unwrap().getKeyId()

      assertEquals(CryptoConstants.KEY_ID, keyId)
    }

  @Test
  fun testRemovingKey() =
    scope.runTest {
      // Add key using KeyManager
      keyManager.addKey(key).unwrap()

      // Check if the key id returned is correct
      val keyId = keyManager.removeKey(key).unwrap().getKeyId()
      assertEquals(CryptoConstants.KEY_ID, keyId)

      // Check if the keys directory have 0 files
      val keysDir = File(filesDir, PGPKeyManager.KEY_DIR_NAME)
      assertEquals(0, keysDir.list()?.size)
    }

  @Test
  fun testGetExistingKey() =
    scope.runTest {
      // Add key using KeyManager
      keyManager.addKey(key).unwrap()

      // Check returned key id matches the expected id and the created key id
      val returnedKeyPair = keyManager.getKeyById(key.getKeyId()).unwrap()
      assertEquals(CryptoConstants.KEY_ID, key.getKeyId())
      assertEquals(key.getKeyId(), returnedKeyPair.getKeyId())
    }

  @Test
  fun testGetNonExistentKey() =
    scope.runTest {
      // Add key using KeyManager
      keyManager.addKey(key).unwrap()

      val randomKeyId = "0x123456789"

      // Check returned key
      val error = keyManager.getKeyById(randomKeyId).unwrapError()
      assertIs<KeyManagerException.KeyNotFoundException>(error)
      assertEquals("No key found with id: $randomKeyId", error.message)
    }

  @Test
  fun testFindKeysWithoutAdding() =
    scope.runTest {
      // Check returned key
      val error = keyManager.getKeyById("0x123456789").unwrapError()
      assertIs<KeyManagerException.NoKeysAvailableException>(error)
      assertEquals("No keys were found", error.message)
    }

  @Test
  fun testGettingAllKeys() =
    scope.runTest {
      // TODO: Should we check for more than 1 keys?
      // Check if KeyManager returns no key
      val noKeyList = keyManager.getAllKeys().unwrap()
      assertEquals(0, noKeyList.size)

      // Add key using KeyManager
      keyManager.addKey(key).unwrap()

      // Check if KeyManager returns one key
      val singleKeyList = keyManager.getAllKeys().unwrap()
      assertEquals(1, singleKeyList.size)
    }
}