@@ -32,9 +32,11 @@ import java.security.InvalidAlgorithmParameterException
3232import java.security.KeyPairGenerator
3333import java.security.KeyStore
3434import java.security.KeyStoreException
35+ import java.security.NoSuchAlgorithmException
3536import java.security.PrivateKey
3637import java.security.ProviderException
3738import java.security.PublicKey
39+ import java.security.UnrecoverableKeyException
3840import java.security.cert.Certificate
3941import javax.security.auth.x500.X500Principal
4042
@@ -191,6 +193,54 @@ public class DPoPKeyStoreTest {
191193 assertThat(exception.cause, `is `(cause))
192194 }
193195
196+ @Test
197+ public fun `getKeyPair should throw KEY_STORE_ERROR on NoSuchAlgorithmException` () {
198+ val cause = NoSuchAlgorithmException (" Test Exception" )
199+ whenever(mockKeyStore.getKey(any(), anyOrNull())).thenThrow(cause)
200+
201+ val exception = assertThrows(DPoPException ::class .java) {
202+ dpopKeyStore.getKeyPair()
203+ }
204+ assertEquals(exception.message, DPoPException .KEY_STORE_ERROR .message)
205+ assertThat(exception.cause, `is `(cause))
206+ }
207+
208+ @Test
209+ public fun `getKeyPair should throw KEY_STORE_ERROR on UnrecoverableKeyException` () {
210+ val cause = UnrecoverableKeyException (" Test Exception" )
211+ whenever(mockKeyStore.getKey(any(), anyOrNull())).thenThrow(cause)
212+
213+ val exception = assertThrows(DPoPException ::class .java) {
214+ dpopKeyStore.getKeyPair()
215+ }
216+ assertEquals(exception.message, DPoPException .KEY_STORE_ERROR .message)
217+ assertThat(exception.cause, `is `(cause))
218+ }
219+
220+ @Test
221+ public fun `getKeyPair should throw KEY_STORE_ERROR on ClassCastException` () {
222+ val cause = ClassCastException (" Test Exception" )
223+ whenever(mockKeyStore.getKey(any(), anyOrNull())).thenThrow(cause)
224+
225+ val exception = assertThrows(DPoPException ::class .java) {
226+ dpopKeyStore.getKeyPair()
227+ }
228+ assertEquals(exception.message, DPoPException .KEY_STORE_ERROR .message)
229+ assertThat(exception.cause, `is `(cause))
230+ }
231+
232+ @Test
233+ public fun `getKeyPair should throw UNKNOWN_ERROR on unhandled exception` () {
234+ val cause = RuntimeException (" Unexpected error" )
235+ whenever(mockKeyStore.getKey(any(), anyOrNull())).thenThrow(cause)
236+
237+ val exception = assertThrows(DPoPException ::class .java) {
238+ dpopKeyStore.getKeyPair()
239+ }
240+ assertEquals(exception.message, DPoPException .UNKNOWN_ERROR .message)
241+ assertThat(exception.cause, `is `(cause))
242+ }
243+
194244 @Test
195245 public fun `hasKeyPair should return true when alias exists` () {
196246 whenever(mockKeyStore.containsAlias(any())).thenReturn(true )
@@ -217,6 +267,18 @@ public class DPoPKeyStoreTest {
217267 assertThat(exception.cause, `is `(cause))
218268 }
219269
270+ @Test
271+ public fun `hasKeyPair should throw UNKNOWN_ERROR on unhandled exception` () {
272+ val cause = RuntimeException (" Unexpected error" )
273+ whenever(mockKeyStore.containsAlias(any())).thenThrow(cause)
274+
275+ val exception = assertThrows(DPoPException ::class .java) {
276+ dpopKeyStore.hasKeyPair()
277+ }
278+ assertEquals(exception.message, DPoPException .UNKNOWN_ERROR .message)
279+ assertThat(exception.cause, `is `(cause))
280+ }
281+
220282 @Test
221283 public fun `deleteKeyPair should call deleteEntry` () {
222284 dpopKeyStore.deleteKeyPair()
@@ -235,6 +297,19 @@ public class DPoPKeyStoreTest {
235297 assertThat(exception.cause, `is `(cause))
236298 }
237299
300+
301+ @Test
302+ public fun `deleteKeyPair should throw UNKNOWN_ERROR on unhandled exception` () {
303+ val cause = RuntimeException (" Unexpected error" )
304+ whenever(mockKeyStore.deleteEntry(any())).thenThrow(cause)
305+
306+ val exception = assertThrows(DPoPException ::class .java) {
307+ dpopKeyStore.deleteKeyPair()
308+ }
309+ assertEquals(exception.message, DPoPException .UNKNOWN_ERROR .message)
310+ assertThat(exception.cause, `is `(cause))
311+ }
312+
238313 @Test
239314 public fun `generateKeyPair should retry without StrongBox when ProviderException occurs with StrongBox enabled` () {
240315 val providerException = ProviderException (" StrongBox attestation failed" )
0 commit comments