]> git.basschouten.com Git - openhab-addons.git/blob
8b106f1431b38f3094a25544556cd0387448259c
[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.paradoxalarm.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.Arrays;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.paradoxalarm.internal.communication.crypto.EncryptionHandler;
22 import org.openhab.binding.paradoxalarm.internal.communication.messages.HeaderCommand;
23 import org.openhab.binding.paradoxalarm.internal.communication.messages.ParadoxIPPacket;
24 import org.openhab.binding.paradoxalarm.internal.util.ParadoxUtil;
25
26 /**
27  * The {@link TestEncryptionHandler} This test tests various functions from ParadoxUtils class
28  *
29  * @author Konstantin Polihronov - Initial contribution
30  */
31 @NonNullByDefault
32 public class TestEncryptionHandler {
33
34     private static final String INPUT_STRING = "My test string for encryption.";
35     private static final String KEY = "MyKeyToEncrypt";
36
37     @Test
38     public void testEncryptDecryptString() {
39         EncryptionHandler.getInstance().updateKey(ParadoxUtil.getBytesFromString(KEY));
40
41         byte[] originalBytes = ParadoxUtil.getBytesFromString(INPUT_STRING);
42         ParadoxUtil.printByteArray("Original=", originalBytes);
43
44         byte[] encrypted = EncryptionHandler.getInstance().encrypt(originalBytes);
45         assertNotEquals(originalBytes, encrypted);
46
47         byte[] decrypted = EncryptionHandler.getInstance().decrypt(encrypted);
48         byte[] result = decrypted.length != originalBytes.length ? Arrays.copyOf(decrypted, originalBytes.length)
49                 : decrypted;
50         ParadoxUtil.printByteArray("Result=", result);
51         assertEquals(originalBytes.length, result.length);
52
53         assertEquals(INPUT_STRING, new String(result));
54     }
55
56     private static final byte[] ENCRYPTION_KEY_BYTES = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
57     private static final byte[] ENCRYPTED_EXPECTED2 = { (byte) 0xAA, 0x0A, 0x00, 0x03, 0x09, (byte) 0xF0, 0x00, 0x00,
58             0x01, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE,
59             (byte) 0xF9, 0x11, 0x5A, (byte) 0xD7, 0x7C, (byte) 0xCB, (byte) 0xF4, 0x75, (byte) 0xB0, 0x49, (byte) 0xC3,
60             0x11, 0x1A, 0x41, (byte) 0x94, (byte) 0xE0 };
61
62     @Test
63     public void testCreateAndEncryptStartingPacket() {
64         ParadoxIPPacket paradoxIPPacket = new ParadoxIPPacket(ENCRYPTION_KEY_BYTES, false)
65                 .setCommand(HeaderCommand.CONNECT_TO_IP_MODULE);
66
67         EncryptionHandler.getInstance().updateKey(ENCRYPTION_KEY_BYTES);
68         paradoxIPPacket.encrypt();
69
70         final byte[] packetBytes = paradoxIPPacket.getBytes();
71         ParadoxUtil.printByteArray("Expected=", ENCRYPTED_EXPECTED2);
72         ParadoxUtil.printByteArray("Packet=  ", packetBytes);
73
74         assertTrue(Arrays.equals(packetBytes, ENCRYPTED_EXPECTED2));
75     }
76 }