blob: 6b81135477670c3f59764b4927a20574d55a64e2 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.util;
2
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08006
Jonathan Harta88fd242014-04-03 11:24:54 -07007import org.junit.Test;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08008
9/**
10 * @author Sho Shimizu (sho.shimizu@gmail.com)
11 */
12public class MACAddressTest {
13 @Test
14 public void testValueOf() {
15 MACAddress address = MACAddress.valueOf("00:01:02:03:04:05");
16 assertEquals(address,
17 MACAddress.valueOf(new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}));
18 assertEquals("00:01:02:03:04:05", address.toString());
19
20 address = MACAddress.valueOf("FF:FE:FD:10:20:30");
21 assertEquals(address,
22 MACAddress.valueOf(new byte[]{(byte) 0xFF, (byte) 0xFE, (byte) 0xFD, 0x10, 0x20, 0x30}));
Pavlin Radoslavovaf839eb2013-12-19 00:02:34 -080023 assertEquals("ff:fe:fd:10:20:30", address.toString());
Ray Milkey269ffb92014-04-03 14:43:30 -070024
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080025 address = MACAddress.valueOf("00:11:22:aa:bb:cc");
26 assertEquals(address,
Ray Milkey269ffb92014-04-03 14:43:30 -070027 MACAddress.valueOf(new byte[]{0x00, 0x11, 0x22, (byte) 0xaa, (byte) 0xbb, (byte) 0xcc}));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080028 }
29
Ray Milkey269ffb92014-04-03 14:43:30 -070030 @Test(expected = NumberFormatException.class)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080031 public void testIllegalFormat() {
32 MACAddress.valueOf("0T:00:01:02:03:04");
33 }
34
Ray Milkey269ffb92014-04-03 14:43:30 -070035 @Test(expected = IllegalArgumentException.class)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080036 public void testLongStringFields() {
37 MACAddress.valueOf("00:01:02:03:04:05:06");
38 }
39
Ray Milkey269ffb92014-04-03 14:43:30 -070040 @Test(expected = IllegalArgumentException.class)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080041 public void testShortStringFields() {
42 MACAddress.valueOf("00:01:02:03:04");
43 }
44
Ray Milkey269ffb92014-04-03 14:43:30 -070045 @Test(expected = IllegalArgumentException.class)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080046 public void testLongByteFields() {
47 MACAddress.valueOf(new byte[]{0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06});
48 }
49
Ray Milkey269ffb92014-04-03 14:43:30 -070050 @Test(expected = IllegalArgumentException.class)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080051 public void testShortByteField() {
52 MACAddress.valueOf(new byte[]{0x01, 0x01, 0x02, 0x03, 0x04});
53 }
54
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070055 // Test data is imported from net.onrc.onos.core.packet.EthernetTest
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080056 @Test
57 public void testToLong() {
58 assertEquals(
59 281474976710655L,
60 MACAddress.valueOf(new byte[]{(byte) 0xff, (byte) 0xff,
61 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}).toLong());
62
63 assertEquals(
64 1103823438081L,
Ray Milkey269ffb92014-04-03 14:43:30 -070065 MACAddress.valueOf(new byte[]{(byte) 0x01, (byte) 0x01,
66 (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01}).toLong());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080067
68 assertEquals(
69 141289400074368L,
Ray Milkey269ffb92014-04-03 14:43:30 -070070 MACAddress.valueOf(new byte[]{(byte) 0x80, (byte) 0x80,
71 (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80}).toLong());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080072
73 }
Ray Milkey269ffb92014-04-03 14:43:30 -070074
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080075 @Test
76 public void testIsBroadcast() {
77 assertTrue(MACAddress.valueOf("FF:FF:FF:FF:FF:FF").isBroadcast());
78 assertFalse(MACAddress.valueOf("11:22:33:44:55:66").isBroadcast());
79 }
80
81 @Test
82 public void testIsMulticast() {
83 assertTrue(MACAddress.valueOf("01:80:C2:00:00:00").isMulticast());
84 assertFalse(MACAddress.valueOf("FF:FF:FF:FF:FF:FF").isMulticast());
85 assertFalse(MACAddress.valueOf("11:22:33:44:55:66").isBroadcast());
86 }
87}