2 * Copyright (c) 2010-2023 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.anel.internal;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
18 import java.util.Base64;
19 import java.util.function.BiFunction;
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;
27 * This class tests {@link AnelAuthentication}.
29 * @author Patrick Koenemann - Initial contribution
32 public class AnelAuthenticationTest {
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";
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));
56 public void encodeUserPasswordPlain() {
57 encodeUserPassword(AuthMethod.PLAIN, (u, p) -> u + p);
61 public void encodeUserPasswordBase64() {
62 encodeUserPassword(AuthMethod.BASE64, (u, p) -> base64(u + p));
66 public void encodeUserPasswordXorBase64() {
67 encodeUserPassword(AuthMethod.XORBASE64, (u, p) -> base64(xor(u + p, p)));
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("", ""))));
83 private static String base64(String string) {
84 return Base64.getEncoder().encodeToString(string.getBytes());
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())));