2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.boschshc.internal.devices.bridge;
15 import static org.junit.jupiter.api.Assertions.*;
18 import java.nio.file.Paths;
19 import java.security.KeyStore;
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;
28 * Tests cases for {@link BoschSslUtil}.
30 * @author Gerd Zanker - Initial contribution
33 class BoschSslUtilTest {
36 static void beforeAll() {
37 prepareTempFolderForKeyStore();
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());
53 void getBoschShcClientId() {
54 // OpenSource Bosch SHC clients needs start with oss
55 assertTrue(BoschSslUtil.getBoschShcClientId().startsWith("oss"));
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"));
66 void getKeystorePath() {
67 BoschSslUtil sslUtil = new BoschSslUtil("123.45.67.89");
68 assertTrue(sslUtil.getKeystorePath().endsWith(".jks"));
72 * Test if the keyStore can be created if it doesn't exist.
75 void keyStoreAndFactory() throws PairingFailedException {
76 BoschSslUtil sslUtil1 = new BoschSslUtil("127.0.0.1");
78 // remote old, existing jks
79 File keyStoreFile = new File(sslUtil1.getKeystorePath());
80 keyStoreFile.deleteOnExit();
81 if (keyStoreFile.exists()) {
82 assertTrue(keyStoreFile.delete());
85 assertFalse(keyStoreFile.exists());
87 BoschSslUtil sslUtil2 = new BoschSslUtil("127.0.0.1");
88 // fist call where keystore is created
89 KeyStore keyStore = sslUtil2.getKeyStoreAndCreateIfNecessary();
90 assertNotNull(keyStore);
92 assertTrue(keyStoreFile.exists());
94 // second call where keystore is reopened
95 KeyStore keyStore2 = sslUtil2.getKeyStoreAndCreateIfNecessary();
96 assertNotNull(keyStore2);
98 // basic test if a SSL factory instance can be created
99 SslContextFactory factory = sslUtil2.getSslContextFactory();
100 assertNotNull(factory);