2 * Copyright (c) 2010-2024 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.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.Timeout;
30 import org.openhab.core.util.HexUtils;
33 * Tests cases for {@link WakeOnLanPacketSender}.
35 * @author Wouter Born - Initial contribution
39 public class WakeOnLanPacketSenderTest {
41 private void assertValidMagicPacket(byte[] macBytes, byte[] packet) {
42 byte[] prefix = new byte[PREFIX_BYTE_SIZE];
43 Arrays.fill(prefix, (byte) 0xff);
45 assertThat(Arrays.copyOfRange(packet, 0, PREFIX_BYTE_SIZE), is(prefix));
47 for (int i = PREFIX_BYTE_SIZE; i < MAGIC_PACKET_BYTE_SIZE; i += MAC_BYTE_SIZE) {
48 assertThat(Arrays.copyOfRange(packet, i, i + MAC_BYTE_SIZE), is(macBytes));
53 public void sendWithColonSeparatedMacAddress() {
54 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
56 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f:70:65:6e:48:41",
57 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
59 sender.sendWakeOnLanPacketViaMac();
61 assertValidMagicPacket(HexUtils.hexToBytes("6f:70:65:6e:48:41", ":"), actualPacket);
65 public void sendWithHyphenSeparatedMacAddress() {
66 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
68 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6F-70-65-6E-48-41",
69 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
71 sender.sendWakeOnLanPacketViaMac();
73 assertValidMagicPacket(HexUtils.hexToBytes("6F-70-65-6E-48-41", "-"), actualPacket);
77 public void sendWithNoSeparatedMacAddress() {
78 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
80 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f70656e4841",
81 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
83 sender.sendWakeOnLanPacketViaMac();
85 assertValidMagicPacket(HexUtils.hexToBytes("6f70656e4841"), actualPacket);
89 public void sendWithHostnameAndPort() throws IOException, InterruptedException {
90 sendWOLTest("127.0.0.1", 4444);
94 public void sendWithHostnameAndPortNull() throws IOException, InterruptedException {
95 sendWOLTest("127.0.0.1", null);
99 public void sendWithHostnameNullAndPortNull() {
100 assertThrows(IllegalStateException.class, () -> sendWOLTest(null, null));
104 public void sendWithHostnameNull() {
105 assertThrows(IllegalStateException.class, () -> sendWOLTest(null, 4444));
108 private void sendWOLTest(@Nullable String hostname, @Nullable Integer port)
109 throws InterruptedException, IOException {
110 DatagramSocket socket = new DatagramSocket(4444);
112 byte[] buf = new byte[256];
113 DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
115 while (socket.isClosed()) {
120 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f70656e4841", hostname, port);
121 sender.sendWakeOnLanPacketViaIp();
123 // This Test is only applicable for IP Requests
124 if (hostname != null && port != null) {
125 socket.receive(datagramPacket);
128 Assertions.assertTrue(datagramPacket.getData().length > 0);
135 public void sendWithEmptyMacAddressThrowsException() {
136 assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("").sendWakeOnLanPacketViaMac());
140 public void sendWithTooShortMacAddressThrowsException() {
141 assertThrows(IllegalStateException.class,
142 () -> new WakeOnLanPacketSender("6f:70:65:6e:48").sendWakeOnLanPacketViaMac());
146 public void sendWithTooLongMacAddressThrowsException() {
147 assertThrows(IllegalStateException.class,
148 () -> new WakeOnLanPacketSender("6f:70:65:6e:48:41:42").sendWakeOnLanPacketViaMac());
152 public void sendWithUnsupportedSeparatorInMacAddressThrowsException() {
153 assertThrows(IllegalStateException.class,
154 () -> new WakeOnLanPacketSender("6f=70=65=6e=48=41").sendWakeOnLanPacketViaMac());