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));
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));
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));
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() throws IOException, InterruptedException {
97 sendWOLTest(null, null);
101 public void sendWithHostnameNull() throws IOException, InterruptedException {
102 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()) {
115 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f70656e4841", hostname, port);
118 // This Test is only applicable for IP Requests
119 if (hostname != null && port != null) {
120 socket.receive(datagramPacket);
125 Assertions.assertTrue(datagramPacket.getData().length > 0);
129 public void sendWithEmptyMacAddressThrowsException() {
130 assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("").sendPacket());
134 public void sendWithTooShortMacAddressThrowsException() {
135 assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f:70:65:6e:48").sendPacket());
139 public void sendWithTooLongMacAddressThrowsException() {
140 assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f:70:65:6e:48:41:42").sendPacket());
144 public void sendWithUnsupportedSeparatorInMacAddressThrowsException() {
145 assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f=70=65=6e=48=41").sendPacket());