aboutsummaryrefslogtreecommitdiffstats
path: root/security-utils/src/test/java/com/yahoo/security/SslContextBuilderTest.java
blob: b08494bb8da31649e7be4daf6f3b96489465ed27 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.security;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.nio.file.Path;
import java.security.KeyPair;
import java.security.cert.X509Certificate;

import static com.yahoo.security.TestUtils.createCertificate;
import static com.yahoo.security.TestUtils.createKeystore;
import static com.yahoo.security.TestUtils.createKeystoreFile;

/**
 * @author bjorncs
 */
public class SslContextBuilderTest {

    private static final char[] PASSWORD = new char[0];

    @TempDir
    public File tempDirectory;

    @Test
    void can_build_sslcontext_with_truststore_only() throws Exception {
        new SslContextBuilder()
                .withTrustStore(createKeystore(KeyStoreType.JKS, PASSWORD))
                .build();
    }

    @Test
    void can_build_sslcontext_with_keystore_only() throws Exception {
        new SslContextBuilder()
                .withKeyStore(createKeystore(KeyStoreType.JKS, PASSWORD), PASSWORD)
                .build();
    }

    @Test
    void can_build_sslcontext_with_truststore_and_keystore() throws Exception {
        new SslContextBuilder()
                .withKeyStore(createKeystore(KeyStoreType.JKS, PASSWORD), PASSWORD)
                .withTrustStore(createKeystore(KeyStoreType.JKS, PASSWORD))
                .build();
    }

    @Test
    void can_build_sslcontext_with_keystore_from_private_key_and_certificate() throws Exception {
        KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.EC, 256);
        X509Certificate certificate = createCertificate(keyPair);
        new SslContextBuilder()
                .withKeyStore(keyPair.getPrivate(), certificate)
                .build();
    }

    @Test
    void can_build_sslcontext_with_jks_keystore_from_file() throws Exception {
        Path keystoreFile = File.createTempFile("junit", null, tempDirectory).toPath();
        createKeystoreFile(keystoreFile, KeyStoreType.JKS, PASSWORD);

        new SslContextBuilder()
                .withKeyStore(keystoreFile, PASSWORD, KeyStoreType.JKS)
                .build();
    }

    @Test
    void can_build_sslcontext_with_pcks12_keystore_from_file() throws Exception {
        Path keystoreFile = File.createTempFile("junit", null, tempDirectory).toPath();
        createKeystoreFile(keystoreFile, KeyStoreType.PKCS12, PASSWORD);

        new SslContextBuilder()
                .withKeyStore(keystoreFile, PASSWORD, KeyStoreType.PKCS12)
                .build();
    }

}