2 * Copyright (c) 2010-2020 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.junit.Assert.assertThat;
17 import static org.openhab.binding.network.internal.WakeOnLanPacketSender.*;
19 import java.util.Arrays;
21 import org.junit.Test;
22 import org.openhab.core.util.HexUtils;
25 * Tests cases for {@link WakeOnLanPacketSender}.
27 * @author Wouter Born - Initial contribution
29 public class WakeOnLanPacketSenderTest {
31 private void assertValidMagicPacket(byte[] macBytes, byte[] packet) {
32 byte[] prefix = new byte[PREFIX_BYTE_SIZE];
33 Arrays.fill(prefix, (byte) 0xff);
35 assertThat(Arrays.copyOfRange(packet, 0, PREFIX_BYTE_SIZE), is(prefix));
37 for (int i = PREFIX_BYTE_SIZE; i < MAGIC_PACKET_BYTE_SIZE; i += MAC_BYTE_SIZE) {
38 assertThat(Arrays.copyOfRange(packet, i, i + MAC_BYTE_SIZE), is(macBytes));
43 public void sendWithColonSeparatedMacAddress() {
44 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
46 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f:70:65:6e:48:41",
47 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
51 assertValidMagicPacket(HexUtils.hexToBytes("6f:70:65:6e:48:41", ":"), actualPacket);
55 public void sendWithHyphenSeparatedMacAddress() {
56 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
58 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6F-70-65-6E-48-41",
59 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
63 assertValidMagicPacket(HexUtils.hexToBytes("6F-70-65-6E-48-41", "-"), actualPacket);
67 public void sendWithNoSeparatedMacAddress() {
68 byte[] actualPacket = new byte[MAGIC_PACKET_BYTE_SIZE];
70 WakeOnLanPacketSender sender = new WakeOnLanPacketSender("6f70656e4841",
71 bytes -> System.arraycopy(bytes, 0, actualPacket, 0, bytes.length));
75 assertValidMagicPacket(HexUtils.hexToBytes("6f70656e4841"), actualPacket);
78 @Test(expected = IllegalStateException.class, timeout = 10000)
79 public void sendWithEmptyMacAddressThrowsException() {
80 new WakeOnLanPacketSender("").sendPacket();
83 @Test(expected = IllegalStateException.class, timeout = 10000)
84 public void sendWithTooShortMacAddressThrowsException() {
85 new WakeOnLanPacketSender("6f:70:65:6e:48").sendPacket();
88 @Test(expected = IllegalStateException.class, timeout = 10000)
89 public void sendWithTooLongMacAddressThrowsException() {
90 new WakeOnLanPacketSender("6f:70:65:6e:48:41:42").sendPacket();
93 @Test(expected = IllegalStateException.class, timeout = 10000)
94 public void sendWithUnsupportedSeparatorInMacAddressThrowsException() {
95 new WakeOnLanPacketSender("6f=70=65=6e=48=41").sendPacket();