]> git.basschouten.com Git - openhab-addons.git/blob
4c331a812b8f6c923be09ea5c77569bc9d0365ea
[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 java.io.File;
19 import java.net.URISyntaxException;
20 import java.util.Properties;
21
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;
27
28 import tuwien.auto.calimero.secure.KnxSecureException;
29
30 /**
31  *
32  * @author Holger Friedrich - initial contribution
33  *
34  */
35 @NonNullByDefault
36 class KNXBridgeBaseThingHandlerTest {
37
38     @Test
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);
46     }
47
48     @Test
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);
54
55         // no config given
56         assertFalse(handler.initializeSecurity("", "", "", "", "", "", ""));
57
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", "", "", "", "");
63         });
64
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", "");
70         });
71         assertThrows(KnxSecureException.class, () -> {
72             handler.initializeSecurity("", "", "", "da", "eins", "pw", "");
73         });
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", ""));
79
80         assertThrows(KnxSecureException.class, () -> {
81             handler.initializeSecurity("nonExistingFile.xml", "", "", "", "", "", "");
82         });
83
84         Properties pBackup = new Properties(System.getProperties());
85         try {
86             final File testFile = new File(
87                     getClass().getClassLoader().getResource("misc" + File.separator + "openhab6.knxkeys").toURI());
88             final String passwordString = "habopen";
89
90             Properties p = new Properties(System.getProperties());
91             p.put(OpenHAB.CONFIG_DIR_PROG_ARGUMENT, testFile.getParent().replaceAll("misc$", ""));
92             System.setProperties(p);
93
94             assertTrue(handler.initializeSecurity(testFile.getName().toString(), passwordString, "", "", "", "pw", ""));
95
96             assertThrows(KnxSecureException.class, () -> {
97                 assertTrue(handler.initializeSecurity(testFile.getName().toString(), "wrong", "", "", "", "pw", ""));
98             });
99         } catch (URISyntaxException e) {
100         } finally {
101             // properties are not persistent, but change may interference with other tests -> restore
102             System.setProperties(pBackup);
103         }
104     }
105 }