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.*;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.junit.jupiter.api.Test;
20 import org.openhab.core.net.NetworkAddressService;
21 import org.openhab.core.thing.Bridge;
23 import tuwien.auto.calimero.secure.KnxSecureException;
27 * @author Holger Friedrich - initial contribution
31 class KNXBridgeBaseThingHandlerTest {
34 void testSecurityHelpers() {
35 // now check router settings:
36 String bbKeyHex = "D947B12DDECAD528B1D5A88FD347F284";
37 byte[] bbKeyParsedLower = KNXBridgeBaseThingHandler.secHelperParseBackboneKey(bbKeyHex.toLowerCase());
38 byte[] bbKeyParsedUpper = KNXBridgeBaseThingHandler.secHelperParseBackboneKey(bbKeyHex);
39 assertEquals(16, bbKeyParsedUpper.length);
40 assertArrayEquals(bbKeyParsedUpper, bbKeyParsedLower);
44 @SuppressWarnings("null")
45 void testInitializeSecurity() {
46 Bridge bridge = mock(Bridge.class);
47 NetworkAddressService nas = mock(NetworkAddressService.class);
48 IPBridgeThingHandler handler = new IPBridgeThingHandler(bridge, nas);
51 assertFalse(handler.initializeSecurity("", "", "", ""));
53 // router password configured, length must be 16 bytes in hex notation
54 assertTrue(handler.initializeSecurity("D947B12DDECAD528B1D5A88FD347F284", "", "", ""));
55 assertTrue(handler.initializeSecurity("0xD947B12DDECAD528B1D5A88FD347F284", "", "", ""));
56 assertThrows(KnxSecureException.class, () -> {
57 handler.initializeSecurity("wrongLength", "", "", "");
60 // tunnel configuration
61 assertTrue(handler.initializeSecurity("", "da", "1", "pw"));
62 // cTunnelUser is restricted to a number >0
63 assertThrows(KnxSecureException.class, () -> {
64 handler.initializeSecurity("", "da", "0", "pw");
66 assertThrows(KnxSecureException.class, () -> {
67 handler.initializeSecurity("", "da", "eins", "pw");
69 // at least one setting for tunnel is given, count as try to configure secure tunnel
70 // plausibility is checked during initialize()
71 assertTrue(handler.initializeSecurity("", "da", "", ""));
72 assertTrue(handler.initializeSecurity("", "", "1", ""));
73 assertTrue(handler.initializeSecurity("", "", "", "pw"));