Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import static org.junit.Assert.assertArrayEquals; |
| 4 | import static org.junit.Assert.assertEquals; |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 5 | import static org.junit.Assert.assertFalse; |
| 6 | import static org.junit.Assert.assertTrue; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 7 | import static org.junit.Assert.fail; |
| 8 | |
| 9 | import java.util.Arrays; |
| 10 | |
| 11 | import org.jboss.netty.buffer.ChannelBuffers; |
| 12 | import org.junit.Test; |
| 13 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 14 | |
| 15 | public 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 | |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 34 | String[] invalidMacStrings = { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 35 | "", |
| 36 | "1.2.3.4", |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 37 | "0T:00:01:02:03:04", |
| 38 | "00:01:02:03:04:05:06", |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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 | |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 46 | byte[][] invalidMacBytes = { |
| 47 | new byte[]{0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, |
| 48 | new byte[]{0x01, 0x01, 0x02, 0x03, 0x04} |
| 49 | }; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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 |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 83 | public void testInvalidMacStrings() throws OFParseError { |
| 84 | for(String invalid : invalidMacStrings) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 85 | try { |
| 86 | MacAddress.of(invalid); |
Rob Vaterlaus | bb9c72a | 2013-10-17 15:01:45 -0700 | [diff] [blame] | 87 | fail("Invalid MAC address "+invalid+ " should have raised IllegalArgumentException"); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 88 | } catch(IllegalArgumentException e) { |
| 89 | // ok |
| 90 | } |
| 91 | } |
| 92 | } |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 93 | |
| 94 | @Test |
| 95 | public void testInvalidMacBytes() throws OFParseError { |
| 96 | for(byte[] invalid : invalidMacBytes) { |
| 97 | try { |
| 98 | MacAddress.of(invalid); |
Rob Vaterlaus | bb9c72a | 2013-10-17 15:01:45 -0700 | [diff] [blame] | 99 | fail("Invalid MAC address bytes "+ Arrays.toString(invalid) + " should have raised IllegalArgumentException"); |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 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 | } |
Shudong Zhou | e5a3680 | 2014-02-09 14:54:14 -0800 | [diff] [blame] | 143 | |
| 144 | @Test |
Shudong Zhou | a4fb4c6 | 2014-02-10 16:57:45 -0800 | [diff] [blame] | 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()); |
Shudong Zhou | e5a3680 | 2014-02-09 14:54:14 -0800 | [diff] [blame] | 153 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 154 | } |