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.util.Arrays;
21 import java.util.concurrent.TimeUnit;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.Timeout;
25 import org.openhab.core.util.HexUtils;
28 * Tests cases for {@link WakeOnLanPacketSender}.
30 * @author Wouter Born - Initial contribution
32 @Timeout(value = 10, unit = TimeUnit.SECONDS)
33 public class WakeOnLanPacketSenderTest {
35 private void assertValidMagicPacket(byte[] macBytes, byte[] packet) {
36 byte[] prefix = new byte[PREFIX_BYTE_SIZE];
37 Arrays.fill(prefix, (byte) 0xff);
39 assertThat(Arrays.copyOfRange(packet, 0, PREFIX_BYTE_SIZE), is(prefix));
41 for (int i = PREFIX_BYTE_SIZE; i < MAGIC_PACKET_BYTE_SIZE; i += MAC_BYTE_SIZE) {
42 assertThat(Arrays.copyOfRange(packet, i, i + MAC_BYTE_SIZE), is(macBytes));
47 public void sendWithColonSeparatedMacAddress() {
48 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
50 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f:70:65:6e:48:41",
51 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
55 assertValidMagicPacket(HexUtils.hexToBytes("6f:70:65:6e:48:41", ":"), actualPacket);
59 public void sendWithHyphenSeparatedMacAddress() {
60 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
62 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6F-70-65-6E-48-41",
63 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
67 assertValidMagicPacket(HexUtils.hexToBytes("6F-70-65-6E-48-41", "-"), actualPacket);
71 public void sendWithNoSeparatedMacAddress() {
72 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
74 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f70656e4841",
75 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
79 assertValidMagicPacket(HexUtils.hexToBytes("6f70656e4841"), actualPacket);
83 public void sendWithEmptyMacAddressThrowsException() {
84 assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("").sendPacket());
88 public void sendWithTooShortMacAddressThrowsException() {
89 assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f:70:65:6e:48").sendPacket());
93 public void sendWithTooLongMacAddressThrowsException() {
94 assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f:70:65:6e:48:41:42").sendPacket());
98 public void sendWithUnsupportedSeparatorInMacAddressThrowsException() {
99 assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f=70=65=6e=48=41").sendPacket());