]> git.basschouten.com Git - openhab-addons.git/blob
517521d4464bd254210d67f40fe8b263492f5047
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.network.internal;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertThrows;
18 import static org.openhab.binding.network.internal.WakeOnLanPacketSender.*;
19
20 import java.io.IOException;
21 import java.net.DatagramPacket;
22 import java.net.DatagramSocket;
23 import java.util.Arrays;
24
25 import org.junit.jupiter.api.Assertions;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.Timeout;
28 import org.openhab.core.util.HexUtils;
29
30 /**
31  * Tests cases for {@link WakeOnLanPacketSender}.
32  *
33  * @author Wouter Born - Initial contribution
34  */
35 @Timeout(value = 10)
36 public class WakeOnLanPacketSenderTest {
37
38     private void assertValidMagicPacket(byte[] macBytes, byte[] packet) {
39         byte[] prefix = new byte[PREFIX_BYTE_SIZE];
40         Arrays.fill(prefix, (byte) 0xff);
41
42         assertThat(Arrays.copyOfRange(packet, 0, PREFIX_BYTE_SIZE), is(prefix));
43
44         for (int i = PREFIX_BYTE_SIZE; i < MAGIC_PACKET_BYTE_SIZE; i += MAC_BYTE_SIZE) {
45             assertThat(Arrays.copyOfRange(packet, i, i + MAC_BYTE_SIZE), is(macBytes));
46         }
47     }
48
49     @Test
50     public void sendWithColonSeparatedMacAddress() {
51         byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
52
53         WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f:70:65:6e:48:41",
54                 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
55
56         sender.sendPacket();
57
58         assertValidMagicPacket(HexUtils.hexToBytes("6f:70:65:6e:48:41", ":"), actualPacket);
59     }
60
61     @Test
62     public void sendWithHyphenSeparatedMacAddress() {
63         byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
64
65         WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6F-70-65-6E-48-41",
66                 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
67
68         sender.sendPacket();
69
70         assertValidMagicPacket(HexUtils.hexToBytes("6F-70-65-6E-48-41", "-"), actualPacket);
71     }
72
73     @Test
74     public void sendWithNoSeparatedMacAddress() {
75         byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
76
77         WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f70656e4841",
78                 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
79
80         sender.sendPacket();
81
82         assertValidMagicPacket(HexUtils.hexToBytes("6f70656e4841"), actualPacket);
83     }
84
85     @Test
86     public void sendWithHostnameAndPort() throws IOException, InterruptedException {
87         sendWOLTest("127.0.0.1", 4444);
88     }
89
90     @Test
91     public void sendWithHostnameAndPortNull() throws IOException, InterruptedException {
92         sendWOLTest("127.0.0.1", null);
93     }
94
95     @Test
96     public void sendWithHostnameNullAndPortNull() throws IOException, InterruptedException {
97         sendWOLTest(null, null);
98     }
99
100     @Test
101     public void sendWithHostnameNull() throws IOException, InterruptedException {
102         sendWOLTest(null, 4444);
103     }
104
105     private void sendWOLTest(String hostname, Integer port) throws InterruptedException, IOException {
106         DatagramSocket socket = new DatagramSocket(4444);
107
108         byte[] buf = new byte[256];
109         DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
110
111         while (socket.isClosed()) {
112             Thread.sleep(100);
113         }
114
115         WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f70656e4841", hostname, port);
116         sender.sendPacket();
117
118         // This Test is only applicable for IP Requests
119         if (hostname != null && port != null) {
120             socket.receive(datagramPacket);
121         }
122
123         socket.close();
124
125         Assertions.assertTrue(datagramPacket.getData().length > 0);
126     }
127
128     @Test
129     public void sendWithEmptyMacAddressThrowsException() {
130         assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("").sendPacket());
131     }
132
133     @Test
134     public void sendWithTooShortMacAddressThrowsException() {
135         assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f:70:65:6e:48").sendPacket());
136     }
137
138     @Test
139     public void sendWithTooLongMacAddressThrowsException() {
140         assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f:70:65:6e:48:41:42").sendPacket());
141     }
142
143     @Test
144     public void sendWithUnsupportedSeparatorInMacAddressThrowsException() {
145         assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f=70=65=6e=48=41").sendPacket());
146     }
147 }