Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.util; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import static org.junit.Assert.*; |
| 6 | |
| 7 | /** |
| 8 | * @author Sho Shimizu (sho.shimizu@gmail.com) |
| 9 | */ |
| 10 | public class MACAddressTest { |
| 11 | @Test |
| 12 | public void testValueOf() { |
| 13 | MACAddress address = MACAddress.valueOf("00:01:02:03:04:05"); |
| 14 | assertEquals(address, |
| 15 | MACAddress.valueOf(new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05})); |
| 16 | assertEquals("00:01:02:03:04:05", address.toString()); |
| 17 | |
| 18 | address = MACAddress.valueOf("FF:FE:FD:10:20:30"); |
| 19 | assertEquals(address, |
| 20 | MACAddress.valueOf(new byte[]{(byte) 0xFF, (byte) 0xFE, (byte) 0xFD, 0x10, 0x20, 0x30})); |
| 21 | assertEquals("FF:FE:FD:10:20:30", address.toString()); |
| 22 | |
| 23 | address = MACAddress.valueOf("00:11:22:aa:bb:cc"); |
| 24 | assertEquals(address, |
| 25 | MACAddress.valueOf(new byte[]{0x00, 0x11, 0x22, (byte)0xaa, (byte)0xbb, (byte)0xcc})); |
| 26 | } |
| 27 | |
| 28 | @Test(expected=NumberFormatException.class) |
| 29 | public void testIllegalFormat() { |
| 30 | MACAddress.valueOf("0T:00:01:02:03:04"); |
| 31 | } |
| 32 | |
| 33 | @Test(expected=IllegalArgumentException.class) |
| 34 | public void testLongStringFields() { |
| 35 | MACAddress.valueOf("00:01:02:03:04:05:06"); |
| 36 | } |
| 37 | |
| 38 | @Test(expected=IllegalArgumentException.class) |
| 39 | public void testShortStringFields() { |
| 40 | MACAddress.valueOf("00:01:02:03:04"); |
| 41 | } |
| 42 | |
| 43 | @Test(expected=IllegalArgumentException.class) |
| 44 | public void testLongByteFields() { |
| 45 | MACAddress.valueOf(new byte[]{0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}); |
| 46 | } |
| 47 | |
| 48 | @Test(expected=IllegalArgumentException.class) |
| 49 | public void testShortByteField() { |
| 50 | MACAddress.valueOf(new byte[]{0x01, 0x01, 0x02, 0x03, 0x04}); |
| 51 | } |
| 52 | |
| 53 | // Test data is imported from net.floodlightcontroller.packet.EthernetTest |
| 54 | @Test |
| 55 | public void testToLong() { |
| 56 | assertEquals( |
| 57 | 281474976710655L, |
| 58 | MACAddress.valueOf(new byte[]{(byte) 0xff, (byte) 0xff, |
| 59 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}).toLong()); |
| 60 | |
| 61 | assertEquals( |
| 62 | 1103823438081L, |
| 63 | MACAddress.valueOf(new byte[] { (byte) 0x01, (byte) 0x01, |
| 64 | (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01 }).toLong()); |
| 65 | |
| 66 | assertEquals( |
| 67 | 141289400074368L, |
| 68 | MACAddress.valueOf(new byte[] { (byte) 0x80, (byte) 0x80, |
| 69 | (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80 }).toLong()); |
| 70 | |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testIsBroadcast() { |
| 75 | assertTrue(MACAddress.valueOf("FF:FF:FF:FF:FF:FF").isBroadcast()); |
| 76 | assertFalse(MACAddress.valueOf("11:22:33:44:55:66").isBroadcast()); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void testIsMulticast() { |
| 81 | assertTrue(MACAddress.valueOf("01:80:C2:00:00:00").isMulticast()); |
| 82 | assertFalse(MACAddress.valueOf("FF:FF:FF:FF:FF:FF").isMulticast()); |
| 83 | assertFalse(MACAddress.valueOf("11:22:33:44:55:66").isBroadcast()); |
| 84 | } |
| 85 | } |