]> git.basschouten.com Git - openhab-addons.git/blob
b33c4d4b7c9c00e4fd3b253d20296e03789dada0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.anel.internal;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.util.Base64;
19 import java.util.function.BiFunction;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.anel.internal.auth.AnelAuthentication;
24 import org.openhab.binding.anel.internal.auth.AnelAuthentication.AuthMethod;
25
26 /**
27  * This class tests {@link AnelAuthentication}.
28  *
29  * @author Patrick Koenemann - Initial contribution
30  */
31 @NonNullByDefault
32 public class AnelAuthenticationTest {
33
34     private static final String STATUS_HUT_V4 = "NET-PwrCtrl:NET-CONTROL :192.168.178.148:255.255.255.0:192.168.178.1:0.4.163.10.9.107:Nr. 1,1:Nr. 2,1:Nr. 3,1:Nr. 4,0:Nr. 5,0:Nr. 6,0:Nr. 7,1:Nr. 8,1:0:80:IO-1,0,0:IO-2,0,0:IO-3,0,0:IO-4,0,0:IO-5,0,0:IO-6,0,0:IO-7,0,0:IO-8,0,0:27.7°C:NET-PWRCTRL_04.0";
35     private static final String STATUS_HUT_V5 = "NET-PwrCtrl:ANEL2          :192.168.0.244:255.255.255.0:192.168.0.1:0.4.163.10.9.107:Nr. 1,1:Nr. 2,1:Nr. 3,1:Nr. 4,0:Nr. 5,0:Nr. 6,0:Nr. 7,1:Nr. 8,1:0:80:IO-1,0,0:IO-2,0,0:IO-3,0,0:IO-4,0,0:IO-5,0,0:IO-6,0,0:IO-7,0,0:IO-8,0,0:27.9*C:NET-PWRCTRL_05.0";
36     private static final String STATUS_HOME_V4_6 = "NET-PwrCtrl:NET-CONTROL    :192.168.0.244:255.255.255.0:192.168.0.1:0.5.163.21.4.71:Nr. 1,0:Nr. 2,1:Nr. 3,0:Nr. 4,0:Nr. 5,0:Nr. 6,0:Nr. 7,0:Nr. 8,0:248:80:NET-PWRCTRL_04.6:H:xor:";
37     private static final String STATUS_UDP_SPEC_EXAMPLE_V7 = "NET-PwrCtrl:NET-CONTROL :192.168.178.148:255.255.255.0:192.168.178.1:0.4.163.10.9.107:Nr. 1,1:Nr. 2,1:Nr. 3,1:Nr. 4,0:Nr. 5,0:Nr. 6,0:Nr. 7,1:Nr. 8,1:0:80:IO-1,0,0:IO-2,0,0:IO-3,0,0:IO-4,0,0:IO-5,0,0:IO-6,0,0:IO-7,0,0:IO-8,0,0:27.7°C:NET-PWRCTRL_06.1:h:p:225.9:0.0004:50.056:0.04:0.00:0.0:1.0000:s:20.61:40.7:7.0:xor";
38     private static final String STATUS_PRO_EXAMPLE_V4_5 = "172.25.3.147776172NET-PwrCtrl:DT-BT14-IPL-1 :172.25.3.14:255.255.0.0:172.25.1.1:0.4.163.19.3.129:Nr. 1,0:Nr. 2,0:Nr. 3,0:Nr. 4,0:Nr. 5,0:Nr. 6,0:Nr. 7,0:Nr. 8,0:0:80:NET-PWRCTRL_04.5:xor:";
39     private static final String STATUS_IO_EXAMPLE_V6_5 = "NET-PwrCtrl:NET-CONTROL :192.168.0.244:255.255.255.0:192.168.0.1:0.4.163.20.7.65:Nr.1,0:Nr.2,1:Nr.3,0:Nr.4,0:Nr.5,0:Nr.6,0:Nr.7,0:Nr.8,0:0:80:IO-1,0,1:IO-2,0,0:IO-3,0,0:IO-4,0,0:IO-5,0,0:IO-6,0,0:IO-7,0,0:IO-8,0,0:23.1°C:NET-PWRCTRL_06.5:i:n:xor:";
40     private static final String STATUS_EXAMPLE_V6_0 = " NET-PwrCtrl:NET-CONTROL :192.168.178.148:255.255.255.0:192.168.178.1:0.4.163.10.9.107:Nr. 1,1:Nr. 2,1:Nr. 3,1:Nr. 4,0:Nr. 5,0:Nr. 6,0:Nr. 7,1:Nr. 8,1:0:80:IO-1,0,0:IO-2,0,0:IO-3,0,0:IO-4,0,0:IO-5,0,0:IO-6,0,0:IO-7,0,0:IO-8,0,0:27.7°C:NET-PWRCTRL_06.0:o:p:225.9:0.0004:50.056:0.04:0.00:0.0:1.0000";
41
42     @Test
43     public void authenticationMethod() {
44         assertThat(AuthMethod.of(""), is(AuthMethod.PLAIN));
45         assertThat(AuthMethod.of(" \n"), is(AuthMethod.PLAIN));
46         assertThat(AuthMethod.of(STATUS_HUT_V4), is(AuthMethod.PLAIN));
47         assertThat(AuthMethod.of(STATUS_HUT_V5), is(AuthMethod.PLAIN));
48         assertThat(AuthMethod.of(STATUS_HOME_V4_6), is(AuthMethod.XORBASE64));
49         assertThat(AuthMethod.of(STATUS_UDP_SPEC_EXAMPLE_V7), is(AuthMethod.XORBASE64));
50         assertThat(AuthMethod.of(STATUS_PRO_EXAMPLE_V4_5), is(AuthMethod.XORBASE64));
51         assertThat(AuthMethod.of(STATUS_IO_EXAMPLE_V6_5), is(AuthMethod.XORBASE64));
52         assertThat(AuthMethod.of(STATUS_EXAMPLE_V6_0), is(AuthMethod.BASE64));
53     }
54
55     @Test
56     public void encodeUserPasswordPlain() {
57         encodeUserPassword(AuthMethod.PLAIN, (u, p) -> u + p);
58     }
59
60     @Test
61     public void encodeUserPasswordBase64() {
62         encodeUserPassword(AuthMethod.BASE64, (u, p) -> base64(u + p));
63     }
64
65     @Test
66     public void encodeUserPasswordXorBase64() {
67         encodeUserPassword(AuthMethod.XORBASE64, (u, p) -> base64(xor(u + p, p)));
68     }
69
70     private void encodeUserPassword(AuthMethod authMethod, BiFunction<String, String, String> expectedEncoding) {
71         assertThat(AnelAuthentication.getUserPasswordString("admin", "anel", authMethod),
72                 is(equalTo(expectedEncoding.apply("admin", "anel"))));
73         assertThat(AnelAuthentication.getUserPasswordString("", "", authMethod),
74                 is(equalTo(expectedEncoding.apply("", ""))));
75         assertThat(AnelAuthentication.getUserPasswordString(null, "", authMethod),
76                 is(equalTo(expectedEncoding.apply("", ""))));
77         assertThat(AnelAuthentication.getUserPasswordString("", null, authMethod),
78                 is(equalTo(expectedEncoding.apply("", ""))));
79         assertThat(AnelAuthentication.getUserPasswordString(null, null, authMethod),
80                 is(equalTo(expectedEncoding.apply("", ""))));
81     }
82
83     private static String base64(String string) {
84         return Base64.getEncoder().encodeToString(string.getBytes());
85     }
86
87     private String xor(String text, String key) {
88         final StringBuilder sb = new StringBuilder();
89         for (int i = 0; i < text.length(); i++) {
90             sb.append((char) (text.charAt(i) ^ key.charAt(i % key.length())));
91         }
92         return sb.toString();
93     }
94 }