Import PKIUtilTest
[netconf.git] / apps / netconf-topology / src / test / java / org / opendaylight / netconf / topology / spi / PKIUtilTest.java
1 /*
2  * Copyright (c) 2017 Brocade Communication Systems and others.  All rights reserved.
3  * Copyright (c) 2024 PANTHEON.tech, s.r.o.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.netconf.topology.spi;
10
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertThrows;
14
15 import java.nio.charset.StandardCharsets;
16 import java.security.KeyPair;
17 import org.bouncycastle.openssl.EncryptionException;
18 import org.junit.jupiter.api.Test;
19
20 class PKIUtilTest {
21     @Test
22     void testRSAKey() throws Exception {
23         assertNotNull(decodePrivateKey("rsa", ""));
24     }
25
26     @Test
27     void testRSAEncryptedKey() throws Exception {
28         assertNotNull(decodePrivateKey("rsa_encrypted", "passphrase"));
29     }
30
31     @Test
32     void testRSAWrongPassphrase() {
33         final var ex = assertThrows(EncryptionException.class, () -> decodePrivateKey("rsa_encrypted", "wrong"));
34         assertEquals("exception using cipher - please check password and data.", ex.getMessage());
35     }
36
37     @Test
38     void testDSAKey() throws Exception {
39         assertNotNull(decodePrivateKey("dsa", ""));
40     }
41
42     @Test
43     void testDSAEncryptedKey() throws Exception {
44         assertNotNull(decodePrivateKey("dsa_encrypted", "passphrase"));
45     }
46
47     @Test
48     void testDSAWrongPassphrase() {
49         final var ex = assertThrows(EncryptionException.class, () -> decodePrivateKey("dsa_encrypted", "wrong"));
50         assertEquals("exception using cipher - please check password and data.", ex.getMessage());
51     }
52
53     @Test
54     @SuppressWarnings("AbbreviationAsWordInName")
55     void testECDSAKey() throws Exception {
56         assertNotNull(decodePrivateKey("ecdsa", ""));
57     }
58
59     @Test
60     @SuppressWarnings("AbbreviationAsWordInName")
61     void testECDSAEncryptedKey() throws Exception {
62         assertNotNull(decodePrivateKey("ecdsa_encrypted", "passphrase"));
63     }
64
65     @Test
66     @SuppressWarnings("AbbreviationAsWordInName")
67     void testECDSAWrongPassphrase() {
68         final var ex = assertThrows(EncryptionException.class, () -> decodePrivateKey("ecdsa_encrypted", "wrong"));
69         assertEquals("exception using cipher - please check password and data.", ex.getMessage());
70     }
71
72     private static KeyPair decodePrivateKey(final String resourceName, final String password) throws Exception {
73         return NetconfClientConfigurationBuilderFactoryImpl.decodePrivateKey(
74             new String(PKIUtilTest.class.getResourceAsStream("/pki/" + resourceName).readAllBytes(),
75                 StandardCharsets.UTF_8),
76             password);
77     }
78 }