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