blob: a13fdd4eef5318014a2c3d8e0cda5a198226f642 [file] [log] [blame]
alshabib1f44e8e2014-08-14 15:19:57 -07001package org.projectfloodlight.openflow.types;
2
3import static org.junit.Assert.assertArrayEquals;
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertTrue;
7import static org.junit.Assert.fail;
8
9import java.util.Arrays;
10
11import org.jboss.netty.buffer.ChannelBuffers;
12import org.junit.Test;
13import org.projectfloodlight.openflow.exceptions.OFParseError;
14
15public class MacAddressTest {
16 byte[][] testAddresses = new byte[][] {
17 {0x01, 0x02, 0x03, 0x04, 0x05, 0x06 },
18 {(byte) 0x80, 0x0, 0x0, 0x0, 0x0, 0x01},
19 {(byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255 }
20 };
21
22 String[] testStrings = {
23 "01:02:03:04:05:06",
24 "80:00:00:00:00:01",
25 "ff:ff:ff:ff:ff:ff"
26 };
27
28 long[] testInts = {
29 0x00010203040506L,
30 0x00800000000001L,
31 0x00ffffffffffffL
32 };
33
34 String[] invalidMacStrings = {
35 "",
36 "1.2.3.4",
37 "0T:00:01:02:03:04",
38 "00:01:02:03:04:05:06",
39 "00:ff:ef:12:12:ff:",
40 "00:fff:ef:12:12:ff",
41 "01:02:03:04:05;06",
42 "0:1:2:3:4:5:6",
43 "01:02:03:04"
44 };
45
46 byte[][] invalidMacBytes = {
47 new byte[]{0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06},
48 new byte[]{0x01, 0x01, 0x02, 0x03, 0x04}
49 };
50
51 @Test
52 public void testOfString() {
53 for(int i=0; i < testAddresses.length; i++ ) {
54 MacAddress ip = MacAddress.of(testStrings[i]);
55 assertEquals(testInts[i], ip.getLong());
56 assertArrayEquals(testAddresses[i], ip.getBytes());
57 assertEquals(testStrings[i], ip.toString());
58 }
59 }
60
61 @Test
62 public void testOfByteArray() {
63 for(int i=0; i < testAddresses.length; i++ ) {
64 MacAddress ip = MacAddress.of(testAddresses[i]);
65 assertEquals("error checking long representation of "+Arrays.toString(testAddresses[i]) + "(should be "+Long.toHexString(testInts[i]) +")", testInts[i], ip.getLong());
66 assertArrayEquals(testAddresses[i], ip.getBytes());
67 assertEquals(testStrings[i], ip.toString());
68 }
69 }
70
71 @Test
72 public void testReadFrom() throws OFParseError {
73 for(int i=0; i < testAddresses.length; i++ ) {
74 MacAddress ip = MacAddress.read6Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
75 assertEquals(testInts[i], ip.getLong());
76 assertArrayEquals(testAddresses[i], ip.getBytes());
77 assertEquals(testStrings[i], ip.toString());
78 }
79 }
80
81
82 @Test
83 public void testInvalidMacStrings() throws OFParseError {
84 for(String invalid : invalidMacStrings) {
85 try {
86 MacAddress.of(invalid);
87 fail("Invalid MAC address "+invalid+ " should have raised IllegalArgumentException");
88 } catch(IllegalArgumentException e) {
89 // ok
90 }
91 }
92 }
93
94 @Test
95 public void testInvalidMacBytes() throws OFParseError {
96 for(byte[] invalid : invalidMacBytes) {
97 try {
98 MacAddress.of(invalid);
99 fail("Invalid MAC address bytes "+ Arrays.toString(invalid) + " should have raised IllegalArgumentException");
100 } catch(IllegalArgumentException e) {
101 // ok
102 }
103 }
104 }
105
106 // Test data is imported from org.projectfloodlight.packet.EthernetTest
107 @Test
108 public void testToLong() {
109 assertEquals(
110 281474976710655L,
111 MacAddress.of(new byte[]{(byte) 0xff, (byte) 0xff,
112 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}).getLong());
113
114 assertEquals(
115 1103823438081L,
116 MacAddress.of(new byte[] { (byte) 0x01, (byte) 0x01,
117 (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01 }).getLong());
118
119 assertEquals(
120 141289400074368L,
121 MacAddress.of(new byte[] { (byte) 0x80, (byte) 0x80,
122 (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80 }).getLong());
123
124 }
125
126 @Test
127 public void testIsBroadcast() {
128 assertTrue(MacAddress.of("FF:FF:FF:FF:FF:FF").isBroadcast());
129 assertTrue(MacAddress.of(-1).isBroadcast());
130 assertTrue(MacAddress.of(0x05FFFFFFFFFFFFL).isBroadcast());
131 assertFalse(MacAddress.of("11:22:33:44:55:66").isBroadcast());
132 }
133
134 @Test
135 public void testIsMulticast() {
136 assertTrue(MacAddress.of("01:80:C2:00:00:00").isMulticast());
137 assertFalse(MacAddress.of("00:80:C2:00:00:00").isMulticast());
138 assertFalse(MacAddress.of("FE:80:C2:00:00:00").isMulticast());
139 assertFalse(MacAddress.of(-1).isMulticast());
140 assertFalse(MacAddress.of(0x05FFFFFFFFFFFFL).isMulticast());
141 assertFalse(MacAddress.of("FF:FF:FF:FF:FF:FF").isMulticast());
142 }
143
144 @Test
145 public void testIsLLDPAddress() {
146 assertTrue(MacAddress.of("01:80:C2:00:00:00").isLLDPAddress());
147 assertTrue(MacAddress.of("01:80:C2:00:00:0f").isLLDPAddress());
148 assertFalse(MacAddress.of("01:80:C2:00:00:50").isLLDPAddress());
149 assertFalse(MacAddress.of("01:80:C2:00:10:00").isLLDPAddress());
150 assertFalse(MacAddress.of("01:80:C2:40:00:01").isLLDPAddress());
151 assertFalse(MacAddress.of("00:80:C2:f0:00:00").isLLDPAddress());
152 assertFalse(MacAddress.of("FE:80:C2:00:00:00").isLLDPAddress());
153 }
154}