Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onlab.packet; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | |
| 21 | import static org.junit.Assert.*; |
| 22 | |
| 23 | /** |
| 24 | * Tests for class {@link MacAddress}. |
| 25 | */ |
| 26 | public class MacAddressTest { |
| 27 | private static final MacAddress MAC_NORMAL = MacAddress.valueOf("00:00:00:00:00:01"); |
| 28 | private static final MacAddress MAC_BCAST = MacAddress.valueOf("ff:ff:ff:ff:ff:ff"); |
| 29 | private static final MacAddress MAC_MCAST = MacAddress.valueOf("01:00:5e:00:00:00"); |
| 30 | private static final MacAddress MAC_MCAST_2 = MacAddress.valueOf("01:00:0c:cc:cc:cc"); |
| 31 | private static final MacAddress MAC_LLDP = MacAddress.valueOf("01:80:c2:00:00:00"); |
| 32 | private static final MacAddress MAC_LLDP_2 = MacAddress.valueOf("01:80:c2:00:00:03"); |
| 33 | private static final MacAddress MAC_LLDP_3 = MacAddress.valueOf("01:80:c2:00:00:0e"); |
| 34 | private static final MacAddress MAC_ONOS = MacAddress.valueOf("a4:23:05:01:02:03"); |
| 35 | private static final MacAddress MAC_ONOS_EQUAL = MacAddress.valueOf("a4:23:05:01:02:03"); |
pierventre | 8ee219f | 2020-09-15 12:44:52 +0200 | [diff] [blame^] | 36 | // For range testing |
| 37 | private static final MacAddress MAC_IPV4_MCAST = MacAddress.valueOf("01:00:5e:00:00:00"); |
| 38 | private static final MacAddress MAC_IPV4_MCAST_ADDR = MacAddress.valueOf("01:00:5e:00:01:01"); |
| 39 | private static final MacAddress MAC_IPV4_MCAST_MASK = MacAddress.valueOf("ff:ff:ff:80:00:00"); |
| 40 | private static final MacAddress MAC_IPV6_MCAST = MacAddress.valueOf("33:33:00:00:00:00"); |
| 41 | private static final MacAddress MAC_IPV6_MCAST_ADDR = MacAddress.valueOf("33:33:00:00:00:01"); |
| 42 | private static final MacAddress MAC_IPV6_MCAST_MASK = MacAddress.valueOf("ff:ff:00:00:00:00"); |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 43 | |
| 44 | private static final byte[] OUI_ONOS = {(byte) 0xa4, (byte) 0x23, (byte) 0x05}; |
| 45 | private static final byte[] MAC_ONOS_BYTE = { |
| 46 | (byte) 0xa4, (byte) 0x23, (byte) 0x05, (byte) 0x01, (byte) 0x02, (byte) 0x03 |
| 47 | }; |
| 48 | private static final long MAC_ONOS_LONG = 180470314762755L; |
| 49 | private static final String MAC_ONOS_STR = "A4:23:05:01:02:03"; |
| 50 | private static final String MAC_ONOS_STR_NO_COLON = "A42305010203"; |
| 51 | |
| 52 | private static final int LENGTH = 6; |
| 53 | |
| 54 | private static final String INVALID_STR = "invalid"; |
| 55 | private static final byte[] INVALID_BYTE = {(byte) 0xaa}; |
Dominika Molenda | 52cc3a9 | 2017-06-19 17:08:35 +0200 | [diff] [blame] | 56 | private static final String INVALID_MAC_ADDITIONAL_COLON = "00:00:00:00:00:00:"; |
| 57 | private static final String INVALID_MAC_OCTET_TOO_LONG = "000:000:000:000:000:00"; |
| 58 | private static final String INVALID_MAC_OCTET_TOO_SHORT = "00:0:00:00:00:00"; |
| 59 | |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 60 | |
| 61 | @Test |
| 62 | public void testValueOfString() throws Exception { |
| 63 | assertArrayEquals(MAC_ONOS_BYTE, MacAddress.valueOf(MAC_ONOS_STR).toBytes()); |
| 64 | } |
| 65 | |
| 66 | @Test(expected = IllegalArgumentException.class) |
| 67 | public void testValueOfInvalidString() throws Exception { |
| 68 | MacAddress.valueOf(INVALID_STR); |
| 69 | } |
| 70 | |
Dominika Molenda | 52cc3a9 | 2017-06-19 17:08:35 +0200 | [diff] [blame] | 71 | @Test(expected = IllegalArgumentException.class) |
| 72 | public void testValueOfInvalidStringWithAdditionalColon() throws Exception { |
| 73 | MacAddress.valueOf(INVALID_MAC_ADDITIONAL_COLON); |
| 74 | } |
| 75 | |
| 76 | @Test(expected = IllegalArgumentException.class) |
| 77 | public void testValueOfInvalidStringWithTooLongOctet() throws Exception { |
| 78 | MacAddress.valueOf(INVALID_MAC_OCTET_TOO_LONG); |
| 79 | } |
| 80 | |
| 81 | @Test(expected = IllegalArgumentException.class) |
| 82 | public void testValueOfInvalidStringWithTooShortOctet() throws Exception { |
| 83 | MacAddress.valueOf(INVALID_MAC_OCTET_TOO_SHORT); |
| 84 | } |
| 85 | |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 86 | @Test |
| 87 | public void testValueOfByte() throws Exception { |
| 88 | assertArrayEquals(MAC_ONOS_BYTE, MacAddress.valueOf(MAC_ONOS_BYTE).toBytes()); |
| 89 | } |
| 90 | |
| 91 | @Test(expected = IllegalArgumentException.class) |
| 92 | public void testValueOfInvalidByte() throws Exception { |
| 93 | MacAddress.valueOf(INVALID_BYTE); |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testValueOfLong() throws Exception { |
| 98 | assertArrayEquals(MAC_ONOS_BYTE, MacAddress.valueOf(MAC_ONOS_LONG).toBytes()); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testLength() throws Exception { |
| 103 | assertEquals(LENGTH, MAC_NORMAL.length()); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testToBytes() throws Exception { |
| 108 | assertArrayEquals(MAC_ONOS_BYTE, MAC_ONOS.toBytes()); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testToLong() throws Exception { |
| 113 | assertEquals(MAC_ONOS_LONG, MAC_ONOS.toLong()); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testIsBroadcast() throws Exception { |
| 118 | assertFalse(MAC_NORMAL.isBroadcast()); |
| 119 | assertTrue(MAC_BCAST.isBroadcast()); |
| 120 | assertFalse(MAC_MCAST.isBroadcast()); |
| 121 | assertFalse(MAC_MCAST_2.isBroadcast()); |
| 122 | assertFalse(MAC_LLDP.isBroadcast()); |
| 123 | assertFalse(MAC_LLDP_2.isBroadcast()); |
| 124 | assertFalse(MAC_LLDP_3.isBroadcast()); |
| 125 | assertFalse(MAC_ONOS.isBroadcast()); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testIsMulticast() throws Exception { |
| 130 | // Note: LLDP MACs are also a multicast MACs |
| 131 | assertFalse(MAC_NORMAL.isMulticast()); |
| 132 | assertFalse(MAC_BCAST.isMulticast()); |
| 133 | assertTrue(MAC_MCAST.isMulticast()); |
| 134 | assertTrue(MAC_MCAST_2.isMulticast()); |
| 135 | assertTrue(MAC_LLDP.isMulticast()); |
| 136 | assertTrue(MAC_LLDP_2.isMulticast()); |
| 137 | assertTrue(MAC_LLDP_3.isMulticast()); |
| 138 | assertFalse(MAC_ONOS.isMulticast()); |
| 139 | } |
| 140 | |
| 141 | @Test |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 142 | public void testIsLldp() throws Exception { |
| 143 | assertFalse(MAC_NORMAL.isLldp()); |
| 144 | assertFalse(MAC_BCAST.isLldp()); |
| 145 | assertFalse(MAC_MCAST.isLldp()); |
| 146 | assertFalse(MAC_MCAST_2.isLldp()); |
| 147 | assertTrue(MAC_LLDP.isLldp()); |
| 148 | assertTrue(MAC_LLDP_2.isLldp()); |
| 149 | assertTrue(MAC_LLDP_3.isLldp()); |
| 150 | assertFalse(MAC_ONOS.isLldp()); |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void testIsOnos() throws Exception { |
| 155 | assertFalse(MAC_NORMAL.isOnos()); |
| 156 | assertFalse(MAC_BCAST.isOnos()); |
| 157 | assertFalse(MAC_MCAST.isOnos()); |
| 158 | assertFalse(MAC_MCAST_2.isOnos()); |
| 159 | assertFalse(MAC_LLDP.isOnos()); |
| 160 | assertFalse(MAC_LLDP_2.isOnos()); |
| 161 | assertFalse(MAC_LLDP_3.isOnos()); |
| 162 | assertTrue(MAC_ONOS.isOnos()); |
| 163 | } |
| 164 | |
| 165 | @Test |
| 166 | public void testOui() throws Exception { |
| 167 | assertArrayEquals(MAC_ONOS.oui(), OUI_ONOS); |
| 168 | } |
| 169 | |
| 170 | @Test |
pierventre | 8ee219f | 2020-09-15 12:44:52 +0200 | [diff] [blame^] | 171 | public void testContained() throws Exception { |
| 172 | assertFalse(MAC_NORMAL.inRange(MAC_IPV4_MCAST, MAC_IPV4_MCAST_MASK)); |
| 173 | assertFalse(MAC_NORMAL.inRange(MAC_IPV6_MCAST, MAC_IPV6_MCAST_MASK)); |
| 174 | // Contained in itself |
| 175 | assertTrue(MAC_IPV4_MCAST.inRange(MAC_IPV4_MCAST, MAC_IPV4_MCAST_MASK)); |
| 176 | assertTrue(MAC_IPV6_MCAST.inRange(MAC_IPV6_MCAST, MAC_IPV6_MCAST_MASK)); |
| 177 | // Verify the mcast addresses |
| 178 | assertTrue(MAC_IPV4_MCAST_ADDR.inRange(MAC_IPV4_MCAST, MAC_IPV4_MCAST_MASK)); |
| 179 | assertTrue(MAC_IPV6_MCAST_ADDR.inRange(MAC_IPV6_MCAST, MAC_IPV6_MCAST_MASK)); |
| 180 | } |
| 181 | |
| 182 | @Test |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 183 | public void testEquals() throws Exception { |
| 184 | assertTrue(MAC_ONOS.equals(MAC_ONOS)); |
| 185 | assertFalse(MAC_ONOS.equals(MAC_ONOS_STR)); |
| 186 | assertTrue(MAC_ONOS.equals(MAC_ONOS_EQUAL)); |
| 187 | assertFalse(MAC_ONOS.equals(MAC_NORMAL)); |
| 188 | assertFalse(MAC_ONOS.equals(MAC_BCAST)); |
| 189 | assertFalse(MAC_ONOS.equals(MAC_MCAST)); |
| 190 | assertFalse(MAC_ONOS.equals(MAC_MCAST_2)); |
| 191 | assertFalse(MAC_ONOS.equals(MAC_LLDP)); |
| 192 | assertFalse(MAC_ONOS.equals(MAC_LLDP_2)); |
| 193 | assertFalse(MAC_ONOS.equals(MAC_LLDP_3)); |
| 194 | } |
| 195 | |
| 196 | @Test |
| 197 | public void testHashCode() throws Exception { |
| 198 | assertEquals(Long.hashCode(MAC_ONOS_LONG), MAC_ONOS.hashCode()); |
| 199 | } |
| 200 | |
| 201 | @Test |
| 202 | public void testToString() throws Exception { |
| 203 | assertEquals(MAC_ONOS_STR, MAC_ONOS.toString()); |
| 204 | } |
| 205 | |
| 206 | @Test |
| 207 | public void testToStringNoColon() throws Exception { |
| 208 | assertEquals(MAC_ONOS_STR_NO_COLON, MAC_ONOS.toStringNoColon()); |
| 209 | } |
| 210 | |
| 211 | } |