@@ -7,45 +7,124 @@ import kotlinx.coroutines.runBlocking
7
7
import kotlinx.coroutines.test.runBlockingTest
8
8
import org.junit.jupiter.api.Assertions.assertTrue
9
9
import org.junit.jupiter.api.BeforeEach
10
+ import org.junit.jupiter.api.Nested
10
11
import org.junit.jupiter.api.Test
12
+ import org.junit.jupiter.api.assertThrows
11
13
import tech.relaycorp.gateway.data.disk.SensitiveStore
14
+ import kotlin.test.assertEquals
12
15
13
- internal class LocalConfigTest {
16
+ class LocalConfigTest {
14
17
15
18
private val sensitiveStore = mock<SensitiveStore >()
16
19
private val localConfig = LocalConfig (sensitiveStore)
17
20
18
21
@BeforeEach
19
- internal fun setUp () {
22
+ fun setUp () {
20
23
runBlocking {
21
- var stored : ByteArray? = null
24
+ val memoryStore = mutableMapOf< String , ByteArray >()
22
25
whenever(sensitiveStore.store(any(), any())).then {
23
- stored = it.getArgument(1 ) as ByteArray
26
+ val key = it.getArgument<String >(0 )
27
+ val value = it.getArgument(1 ) as ByteArray
28
+ memoryStore[key] = value
24
29
Unit
25
30
}
26
- whenever(sensitiveStore.read(any())).thenAnswer { stored }
31
+ whenever(sensitiveStore.read(any())).thenAnswer {
32
+ val key = it.getArgument<String >(0 )
33
+ memoryStore[key]
34
+ }
27
35
}
28
36
}
29
37
30
- @Test
31
- internal fun `get key pair stores and recovers the same key pair` () = runBlockingTest {
32
- val keyPair1 = localConfig.getKeyPair()
33
- val keyPair2 = localConfig.getKeyPair()
34
- assertTrue(keyPair2.private.encoded!! .contentEquals(keyPair1.private.encoded))
35
- assertTrue(keyPair2.public.encoded!! .contentEquals(keyPair1.public.encoded))
38
+ @Nested
39
+ inner class GetKeyPair {
40
+ @Test
41
+ fun `Key pair should be returned if it exists` () = runBlockingTest {
42
+ val keyPair = localConfig.generateKeyPair()
43
+
44
+ val retrievedKeyPair = localConfig.getKeyPair()
45
+
46
+ assertEquals(
47
+ keyPair.private.encoded.asList(),
48
+ retrievedKeyPair.private.encoded.asList()
49
+ )
50
+ }
51
+
52
+ @Test
53
+ fun `Exception should be thrown if key pair does not exist` () = runBlockingTest {
54
+ val exception = assertThrows<RuntimeException > {
55
+ localConfig.getKeyPair()
56
+ }
57
+
58
+ assertEquals(" No key pair was found" , exception.message)
59
+ }
36
60
}
37
61
38
62
@Test
39
- internal fun `get certificate stores and recovers the same certificate` () = runBlockingTest {
63
+ fun `get certificate stores and recovers the same certificate` () = runBlockingTest {
64
+ localConfig.generateKeyPair()
65
+
40
66
val certificate1 = localConfig.getCertificate().serialize()
41
67
val certificate2 = localConfig.getCertificate().serialize()
42
68
assertTrue(certificate1.contentEquals(certificate2))
43
69
}
44
70
45
- @Test
46
- internal fun `get CDA stores and recovers the same certificate` () = runBlockingTest {
47
- val certificate1 = localConfig.getCargoDeliveryAuth().serialize()
48
- val certificate2 = localConfig.getCargoDeliveryAuth().serialize()
49
- assertTrue(certificate1.contentEquals(certificate2))
71
+ @Nested
72
+ inner class GetCargoDeliveryAuth {
73
+ @Test
74
+ fun `Certificate should be returned if it exists` () = runBlockingTest {
75
+ localConfig.bootstrap()
76
+
77
+ val certificate1 = localConfig.getCargoDeliveryAuth().serialize()
78
+ val certificate2 = localConfig.getCargoDeliveryAuth().serialize()
79
+ assertTrue(certificate1.contentEquals(certificate2))
80
+ }
81
+
82
+ @Test
83
+ fun `Exception should be thrown if certificate does not exist yet` () = runBlockingTest {
84
+ val exception = assertThrows<RuntimeException > {
85
+ localConfig.getCargoDeliveryAuth()
86
+ }
87
+
88
+ assertEquals(" No CDA issuer was found" , exception.message)
89
+ }
90
+ }
91
+
92
+ @Nested
93
+ inner class Bootstrap {
94
+ @Test
95
+ fun `Key pair should be created if it doesn't already exist` () = runBlockingTest {
96
+ localConfig.bootstrap()
97
+
98
+ localConfig.getKeyPair()
99
+ }
100
+
101
+ @Test
102
+ fun `Key pair should not be created if it already exists` () = runBlockingTest {
103
+ localConfig.bootstrap()
104
+ val originalKeyPair = localConfig.getKeyPair()
105
+
106
+ localConfig.bootstrap()
107
+ val keyPair = localConfig.getKeyPair()
108
+
109
+ assertEquals(originalKeyPair.private.encoded.asList(), keyPair.private.encoded.asList())
110
+ }
111
+
112
+ @Test
113
+ fun `CDA issuer should be created if it doesn't already exist` () = runBlockingTest {
114
+ localConfig.bootstrap()
115
+
116
+ localConfig.getCargoDeliveryAuth()
117
+ }
118
+
119
+ @Test
120
+ fun `CDA issuer should not be created if it already exists` () = runBlockingTest {
121
+ localConfig.bootstrap()
122
+ val originalCDAIssuer = localConfig.getCargoDeliveryAuth()
123
+
124
+ localConfig.bootstrap()
125
+ val cdaIssuer = localConfig.getCargoDeliveryAuth()
126
+
127
+ assertEquals(originalCDAIssuer, cdaIssuer)
128
+ }
50
129
}
51
130
}
0 commit comments