2 * Copyright (c) 2010-2024 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.knx.internal.handler;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
19 import java.net.URISyntaxException;
20 import java.util.Properties;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.core.OpenHAB;
25 import org.openhab.core.net.NetworkAddressService;
26 import org.openhab.core.thing.Bridge;
28 import tuwien.auto.calimero.secure.KnxSecureException;
32 * @author Holger Friedrich - initial contribution
36 class KNXBridgeBaseThingHandlerTest {
39 void testSecurityHelpers() {
40 // now check router settings:
41 String bbKeyHex = "D947B12DDECAD528B1D5A88FD347F284";
42 byte[] bbKeyParsedLower = KNXBridgeBaseThingHandler.secHelperParseBackboneKey(bbKeyHex.toLowerCase());
43 byte[] bbKeyParsedUpper = KNXBridgeBaseThingHandler.secHelperParseBackboneKey(bbKeyHex);
44 assertEquals(16, bbKeyParsedUpper.length);
45 assertArrayEquals(bbKeyParsedUpper, bbKeyParsedLower);
49 @SuppressWarnings("null")
50 void testInitializeSecurity() {
51 Bridge bridge = mock(Bridge.class);
52 NetworkAddressService nas = mock(NetworkAddressService.class);
53 IPBridgeThingHandler handler = new IPBridgeThingHandler(bridge, nas);
56 assertFalse(handler.initializeSecurity("", "", "", "", "", "", ""));
58 // router password configured, length must be 16 bytes in hex notation
59 assertTrue(handler.initializeSecurity("", "", "D947B12DDECAD528B1D5A88FD347F284", "", "", "", ""));
60 assertTrue(handler.initializeSecurity("", "", "0xD947B12DDECAD528B1D5A88FD347F284", "", "", "", ""));
61 assertThrows(KnxSecureException.class, () -> {
62 handler.initializeSecurity("", "", "wrongLength", "", "", "", "");
65 // tunnel configuration
66 assertTrue(handler.initializeSecurity("", "", "", "da", "1", "pw", ""));
67 // cTunnelUser is restricted to a number >0
68 assertThrows(KnxSecureException.class, () -> {
69 handler.initializeSecurity("", "", "", "da", "0", "pw", "");
71 assertThrows(KnxSecureException.class, () -> {
72 handler.initializeSecurity("", "", "", "da", "eins", "pw", "");
74 // at least one setting for tunnel is given, count as try to configure secure tunnel
75 // plausibility is checked during initialize()
76 assertTrue(handler.initializeSecurity("", "", "", "da", "", "", ""));
77 assertTrue(handler.initializeSecurity("", "", "", "", "1", "", ""));
78 assertTrue(handler.initializeSecurity("", "", "", "", "", "pw", ""));
80 assertThrows(KnxSecureException.class, () -> {
81 handler.initializeSecurity("nonExistingFile.xml", "", "", "", "", "", "");
84 Properties pBackup = new Properties(System.getProperties());
86 final File testFile = new File(
87 getClass().getClassLoader().getResource("misc" + File.separator + "openhab6.knxkeys").toURI());
88 final String passwordString = "habopen";
90 Properties p = new Properties(System.getProperties());
91 p.put(OpenHAB.CONFIG_DIR_PROG_ARGUMENT, testFile.getParent().replaceAll("misc$", ""));
92 System.setProperties(p);
94 assertTrue(handler.initializeSecurity(testFile.getName().toString(), passwordString, "", "", "", "pw", ""));
96 assertThrows(KnxSecureException.class, () -> {
97 assertTrue(handler.initializeSecurity(testFile.getName().toString(), "wrong", "", "", "", "pw", ""));
99 } catch (URISyntaxException e) {
101 // properties are not persistent, but change may interference with other tests -> restore
102 System.setProperties(pBackup);