]> git.basschouten.com Git - openhab-addons.git/blob
fa67f9cc9ac389ac8fa0fc162507baab547f42b9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.boschshc.internal.devices.bridge;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.io.File;
18 import java.nio.file.Paths;
19 import java.security.KeyStore;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jetty.util.ssl.SslContextFactory;
23 import org.junit.jupiter.api.BeforeAll;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.boschshc.internal.exceptions.PairingFailedException;
26
27 /**
28  * Tests cases for {@link BoschSslUtil}.
29  *
30  * @author Gerd Zanker - Initial contribution
31  */
32 @NonNullByDefault
33 class BoschSslUtilTest {
34
35     @BeforeAll
36     static void beforeAll() {
37         prepareTempFolderForKeyStore();
38     }
39
40     public static void prepareTempFolderForKeyStore() {
41         // Use temp folder for userdata folder
42         String tmpDir = System.getProperty("java.io.tmpdir");
43         tmpDir = tmpDir != null ? tmpDir : "/tmp";
44         System.setProperty("openhab.userdata", tmpDir);
45         // prepare temp folder on local drive
46         File tempDir = Paths.get(tmpDir, "etc").toFile();
47         if (!tempDir.exists()) {
48             assertTrue(tempDir.mkdirs());
49         }
50     }
51
52     @Test
53     void getBoschShcClientId() {
54         // OpenSource Bosch SHC clients needs start with oss
55         assertTrue(BoschSslUtil.getBoschShcClientId().startsWith("oss"));
56     }
57
58     @Test
59     void getBoschShcServerId() {
60         // OpenSource Bosch SHC clients needs start with oss
61         assertTrue(BoschSslUtil.getBoschShcServerId("localhost").startsWith("oss"));
62         assertTrue(BoschSslUtil.getBoschShcServerId("localhost").contains("localhost"));
63     }
64
65     @Test
66     void getKeystorePath() {
67         BoschSslUtil sslUtil = new BoschSslUtil("123.45.67.89");
68         assertTrue(sslUtil.getKeystorePath().endsWith(".jks"));
69     }
70
71     /**
72      * Test if the keyStore can be created if it doesn't exist.
73      */
74     @Test
75     void keyStoreAndFactory() throws PairingFailedException {
76         BoschSslUtil sslUtil1 = new BoschSslUtil("127.0.0.1");
77
78         // remote old, existing jks
79         File keyStoreFile = new File(sslUtil1.getKeystorePath());
80         keyStoreFile.deleteOnExit();
81         if (keyStoreFile.exists()) {
82             assertTrue(keyStoreFile.delete());
83         }
84
85         assertFalse(keyStoreFile.exists());
86
87         BoschSslUtil sslUtil2 = new BoschSslUtil("127.0.0.1");
88         // fist call where keystore is created
89         KeyStore keyStore = sslUtil2.getKeyStoreAndCreateIfNecessary();
90         assertNotNull(keyStore);
91
92         assertTrue(keyStoreFile.exists());
93
94         // second call where keystore is reopened
95         KeyStore keyStore2 = sslUtil2.getKeyStoreAndCreateIfNecessary();
96         assertNotNull(keyStore2);
97
98         // basic test if a SSL factory instance can be created
99         SslContextFactory factory = sslUtil2.getSslContextFactory();
100         assertNotNull(factory);
101     }
102 }