]> git.basschouten.com Git - openhab-addons.git/blob
8ae930142c7203d1a541bf57d70c42bc44366ea8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.knx.internal.handler;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
17
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;
22
23 import tuwien.auto.calimero.secure.KnxSecureException;
24
25 /**
26  *
27  * @author Holger Friedrich - initial contribution
28  *
29  */
30 @NonNullByDefault
31 class KNXBridgeBaseThingHandlerTest {
32
33     @Test
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);
41     }
42
43     @Test
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);
49
50         // no config given
51         assertFalse(handler.initializeSecurity("", "", "", ""));
52
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", "", "", "");
58         });
59
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");
65         });
66         assertThrows(KnxSecureException.class, () -> {
67             handler.initializeSecurity("", "da", "eins", "pw");
68         });
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"));
74     }
75 }