2 * Copyright (c) 2010-2021 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.network.internal;
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.*;
20 import java.io.IOException;
21 import java.net.DatagramPacket;
22 import java.net.DatagramSocket;
23 import java.util.Arrays;
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;
31 * Tests cases for {@link WakeOnLanPacketSender}.
33 * @author Wouter Born - Initial contribution
36 public class WakeOnLanPacketSenderTest {
38 private void assertValidMagicPacket(byte[] macBytes, byte[] packet) {
39 byte[] prefix = new byte[PREFIX_BYTE_SIZE];
40 Arrays.fill(prefix, (byte) 0xff);
42 assertThat(Arrays.copyOfRange(packet, 0, PREFIX_BYTE_SIZE), is(prefix));
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));
50 public void sendWithColonSeparatedMacAddress() {
51 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
53 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f:70:65:6e:48:41",
54 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
56 sender.sendWakeOnLanPacketViaMac();
58 assertValidMagicPacket(HexUtils.hexToBytes("6f:70:65:6e:48:41", ":"), actualPacket);
62 public void sendWithHyphenSeparatedMacAddress() {
63 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
65 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6F-70-65-6E-48-41",
66 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
68 sender.sendWakeOnLanPacketViaMac();
70 assertValidMagicPacket(HexUtils.hexToBytes("6F-70-65-6E-48-41", "-"), actualPacket);
74 public void sendWithNoSeparatedMacAddress() {
75 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
77 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f70656e4841",
78 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
80 sender.sendWakeOnLanPacketViaMac();
82 assertValidMagicPacket(HexUtils.hexToBytes("6f70656e4841"), actualPacket);
86 public void sendWithHostnameAndPort() throws IOException, InterruptedException {
87 sendWOLTest("127.0.0.1", 4444);
91 public void sendWithHostnameAndPortNull() throws IOException, InterruptedException {
92 sendWOLTest("127.0.0.1", null);
96 public void sendWithHostnameNullAndPortNull() {
97 assertThrows(IllegalStateException.class, () -> sendWOLTest(null, null));
101 public void sendWithHostnameNull() {
102 assertThrows(IllegalStateException.class, () -> sendWOLTest(null, 4444));
105 private void sendWOLTest(String hostname, Integer port) throws InterruptedException, IOException {
106 DatagramSocket socket = new DatagramSocket(4444);
108 byte[] buf = new byte[256];
109 DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
111 while (socket.isClosed()) {
116 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f70656e4841", hostname, port);
117 sender.sendWakeOnLanPacketViaIp();
119 // This Test is only applicable for IP Requests
120 if (hostname != null && port != null) {
121 socket.receive(datagramPacket);
124 Assertions.assertTrue(datagramPacket.getData().length > 0);
131 public void sendWithEmptyMacAddressThrowsException() {
132 assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("").sendWakeOnLanPacketViaMac());
136 public void sendWithTooShortMacAddressThrowsException() {
137 assertThrows(IllegalStateException.class,
138 () -> new WakeOnLanPacketSender("6f:70:65:6e:48").sendWakeOnLanPacketViaMac());
142 public void sendWithTooLongMacAddressThrowsException() {
143 assertThrows(IllegalStateException.class,
144 () -> new WakeOnLanPacketSender("6f:70:65:6e:48:41:42").sendWakeOnLanPacketViaMac());
148 public void sendWithUnsupportedSeparatorInMacAddressThrowsException() {
149 assertThrows(IllegalStateException.class,
150 () -> new WakeOnLanPacketSender("6f=70=65=6e=48=41").sendWakeOnLanPacketViaMac());