Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -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 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 19 | import org.apache.commons.lang3.StringUtils; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | import java.nio.ByteBuffer; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 24 | |
pier | f8e328d | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 25 | import static junit.framework.TestCase.assertNotNull; |
| 26 | import static org.hamcrest.core.Is.is; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 27 | import static org.junit.Assert.assertEquals; |
pier | f8e328d | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 28 | import static org.junit.Assert.assertThat; |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 29 | import static org.junit.Assert.assertTrue; |
pier | f8e328d | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 30 | import static org.onlab.packet.ICMP.TYPE_ECHO_REQUEST; |
| 31 | import static org.onlab.packet.ICMP6.ECHO_REQUEST; |
| 32 | import static org.onlab.packet.IPv4.PROTOCOL_ICMP; |
| 33 | import static org.onlab.packet.MPLS.PROTOCOL_IPV4; |
| 34 | import static org.onlab.packet.MPLS.PROTOCOL_IPV6; |
| 35 | import static org.onlab.packet.MPLS.PROTOCOL_MPLS; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Unit tests for MPLS class. |
| 39 | */ |
| 40 | public class MplsTest { |
| 41 | |
| 42 | private Deserializer<MPLS> deserializer; |
| 43 | |
| 44 | private int label = 1048575; |
| 45 | private byte bos = 1; |
| 46 | private byte ttl = 20; |
pier | f8e328d | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 47 | private byte protocol = PROTOCOL_IPV4; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 48 | |
| 49 | private byte[] bytes; |
pier | f8e328d | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 50 | private byte[] truncatedBytes; |
| 51 | |
| 52 | // Define packets to deserialize |
| 53 | private static final MacAddress SRC_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 54 | private static final MacAddress DST_MAC = MacAddress.valueOf("00:00:00:00:00:02"); |
| 55 | |
| 56 | private static final ICMPEcho ICMP_ECHO = new ICMPEcho() |
| 57 | .setIdentifier((short) 0) |
| 58 | .setSequenceNum((short) 0); |
| 59 | |
| 60 | private static final ICMP ICMP = (ICMP) new ICMP() |
| 61 | .setIcmpType(TYPE_ECHO_REQUEST) |
| 62 | .setPayload(ICMP_ECHO); |
| 63 | |
| 64 | private static final Ip4Address SRC_IPV4 = Ip4Address.valueOf("10.0.1.1"); |
| 65 | private static final Ip4Address DST_IPV4 = Ip4Address.valueOf("10.0.0.254"); |
| 66 | |
| 67 | private static final IPv4 IPV4 = (IPv4) new IPv4() |
| 68 | .setDestinationAddress(DST_IPV4.toInt()) |
| 69 | .setSourceAddress(SRC_IPV4.toInt()) |
| 70 | .setTtl((byte) 64) |
| 71 | .setProtocol(PROTOCOL_ICMP) |
| 72 | .setPayload(ICMP); |
| 73 | |
| 74 | private static final ICMP6 ICMP6 = new ICMP6() |
| 75 | .setIcmpType(ECHO_REQUEST); |
| 76 | |
| 77 | private static final Ip6Address SRC_IPV6 = Ip6Address.valueOf("2000::101"); |
| 78 | private static final Ip6Address DST_IPV6 = Ip6Address.valueOf("2000::ff"); |
| 79 | |
| 80 | private static final IPv6 IPV6 = (IPv6) new IPv6() |
| 81 | .setDestinationAddress(DST_IPV6.toOctets()) |
| 82 | .setSourceAddress(SRC_IPV6.toOctets()) |
| 83 | .setHopLimit((byte) 255) |
| 84 | .setNextHeader(IPv6.PROTOCOL_ICMP6) |
| 85 | .setPayload(ICMP6); |
| 86 | |
| 87 | private static final MPLS MPLS_IPV4 = new MPLS(); |
| 88 | private static final MPLS MPLS_BOS_IPV4 = new MPLS(); |
| 89 | private static final MPLS MPLS_IPV6 = new MPLS(); |
| 90 | private static final MPLS MPLS_BOS_IPV6 = new MPLS(); |
| 91 | |
| 92 | private static final Ethernet ETH_IPV4 = (Ethernet) new Ethernet() |
| 93 | .setEtherType(Ethernet.MPLS_UNICAST) |
| 94 | .setDestinationMACAddress(DST_MAC) |
| 95 | .setSourceMACAddress(SRC_MAC) |
| 96 | .setPayload(MPLS_IPV4); |
| 97 | |
| 98 | private static final Ethernet ETH_IPV6 = (Ethernet) new Ethernet() |
| 99 | .setEtherType(Ethernet.MPLS_UNICAST) |
| 100 | .setDestinationMACAddress(DST_MAC) |
| 101 | .setSourceMACAddress(SRC_MAC) |
| 102 | .setPayload(MPLS_IPV6); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 103 | |
| 104 | @Before |
| 105 | public void setUp() throws Exception { |
pier | f8e328d | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 106 | // Setup packets |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 107 | deserializer = MPLS.deserializer(); |
| 108 | |
pier | f8e328d | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 109 | byte[] ipv4 = IPV4.serialize(); |
| 110 | ByteBuffer bb = ByteBuffer.allocate(MPLS.HEADER_LENGTH + IPV4.getTotalLength()); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 111 | bb.putInt(((label & 0x000fffff) << 12) | ((bos & 0x1) << 8 | (ttl & 0xff))); |
pier | f8e328d | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 112 | bb.put(ipv4); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 113 | |
| 114 | bytes = bb.array(); |
pier | f8e328d | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 115 | |
| 116 | bb = ByteBuffer.allocate(MPLS.HEADER_LENGTH); |
| 117 | bb.putInt(((label & 0x000fffff) << 12) | ((bos & 0x1) << 8 | (ttl & 0xff))); |
| 118 | |
| 119 | truncatedBytes = bb.array(); |
| 120 | |
| 121 | MPLS_BOS_IPV4.setLabel(101); |
| 122 | MPLS_BOS_IPV4.setPayload(IPV4); |
| 123 | MPLS_IPV4.setLabel(1); |
| 124 | MPLS_IPV4.setPayload(MPLS_BOS_IPV4); |
| 125 | ETH_IPV4.setPayload(MPLS_IPV4); |
| 126 | |
| 127 | MPLS_BOS_IPV6.setLabel(201); |
| 128 | MPLS_BOS_IPV6.setPayload(IPV6); |
| 129 | MPLS_IPV6.setLabel(2); |
| 130 | MPLS_IPV6.setPayload(MPLS_BOS_IPV6); |
| 131 | ETH_IPV6.setPayload(MPLS_IPV6); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testDeserializeBadInput() throws Exception { |
| 136 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void testDeserializeTruncated() throws Exception { |
pier | f8e328d | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 141 | PacketTestUtils.testDeserializeTruncated(deserializer, truncatedBytes); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 144 | /** |
| 145 | * Tests deserialize and getters. |
| 146 | */ |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 147 | @Test |
| 148 | public void testDeserialize() throws Exception { |
| 149 | MPLS mpls = deserializer.deserialize(bytes, 0, bytes.length); |
| 150 | |
| 151 | assertEquals(label, mpls.label); |
| 152 | assertEquals(bos, mpls.bos); |
| 153 | assertEquals(ttl, mpls.ttl); |
| 154 | assertEquals(protocol, mpls.protocol); |
| 155 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * Tests toString. |
| 159 | */ |
| 160 | @Test |
| 161 | public void testToStringMpls() throws Exception { |
| 162 | MPLS mpls = deserializer.deserialize(bytes, 0, bytes.length); |
| 163 | String str = mpls.toString(); |
| 164 | |
| 165 | assertTrue(StringUtils.contains(str, "label=" + label)); |
| 166 | assertTrue(StringUtils.contains(str, "bos=" + bos)); |
| 167 | assertTrue(StringUtils.contains(str, "ttl=" + ttl)); |
| 168 | assertTrue(StringUtils.contains(str, "protocol=" + protocol)); |
| 169 | } |
pier | f8e328d | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 170 | |
| 171 | @Test |
| 172 | public void testIpv4OverMplsDeserialize() throws Exception { |
| 173 | // Serialize |
| 174 | byte[] packet = ETH_IPV4.serialize(); |
| 175 | assertThat(MPLS_IPV4.protocol, is(PROTOCOL_MPLS)); |
| 176 | assertThat(MPLS_IPV4.bos, is((byte) 0)); |
| 177 | assertThat(MPLS_BOS_IPV4.protocol, is(PROTOCOL_IPV4)); |
| 178 | assertThat(MPLS_BOS_IPV4.bos, is((byte) 1)); |
| 179 | // Deserialize |
| 180 | Ethernet ethernet; |
| 181 | ethernet = Ethernet.deserializer().deserialize(packet, 0, packet.length); |
| 182 | |
| 183 | // Verify |
| 184 | assertNotNull(ethernet); |
| 185 | assertThat(ethernet.getSourceMAC(), is(ETH_IPV4.getSourceMAC())); |
| 186 | assertThat(ethernet.getDestinationMAC(), is(ETH_IPV4.getDestinationMAC())); |
| 187 | assertThat(ethernet.getEtherType(), is(Ethernet.MPLS_UNICAST)); |
| 188 | assertTrue(ethernet.getPayload() instanceof MPLS); |
| 189 | MPLS mpls = (MPLS) ethernet.getPayload(); |
| 190 | assertThat(mpls.getLabel(), is(1)); |
| 191 | assertThat(mpls.getTtl(), is((byte) 0)); |
| 192 | assertThat(mpls.protocol, is(PROTOCOL_MPLS)); |
| 193 | assertThat(mpls.bos, is((byte) 0)); |
| 194 | assertTrue(mpls.getPayload() instanceof MPLS); |
| 195 | mpls = (MPLS) mpls.getPayload(); |
| 196 | assertThat(mpls.getLabel(), is(101)); |
| 197 | assertThat(mpls.getTtl(), is((byte) 0)); |
| 198 | assertThat(mpls.protocol, is(PROTOCOL_IPV4)); |
| 199 | assertThat(mpls.bos, is((byte) 1)); |
| 200 | IPv4 ip = (IPv4) mpls.getPayload(); |
| 201 | assertThat(ip.getSourceAddress(), is(SRC_IPV4.toInt())); |
| 202 | assertThat(ip.getDestinationAddress(), is(DST_IPV4.toInt())); |
| 203 | assertTrue(ip.getPayload() instanceof ICMP); |
| 204 | ICMP icmp = (ICMP) ip.getPayload(); |
| 205 | assertThat(icmp.getIcmpType(), is(TYPE_ECHO_REQUEST)); |
| 206 | } |
| 207 | |
| 208 | @Test |
| 209 | public void testIpv6OverMplsDeserialize() throws Exception { |
| 210 | // Serialize |
| 211 | byte[] packet = ETH_IPV6.serialize(); |
| 212 | assertThat(MPLS_IPV6.protocol, is(PROTOCOL_MPLS)); |
| 213 | assertThat(MPLS_IPV6.bos, is((byte) 0)); |
| 214 | assertThat(MPLS_BOS_IPV6.protocol, is(PROTOCOL_IPV6)); |
| 215 | assertThat(MPLS_BOS_IPV6.bos, is((byte) 1)); |
| 216 | // Deserialize |
| 217 | Ethernet ethernet; |
| 218 | ethernet = Ethernet.deserializer().deserialize(packet, 0, packet.length); |
| 219 | |
| 220 | // Verify |
| 221 | assertNotNull(ethernet); |
| 222 | assertThat(ethernet.getSourceMAC(), is(ETH_IPV6.getSourceMAC())); |
| 223 | assertThat(ethernet.getDestinationMAC(), is(ETH_IPV6.getDestinationMAC())); |
| 224 | assertThat(ethernet.getEtherType(), is(Ethernet.MPLS_UNICAST)); |
| 225 | assertTrue(ethernet.getPayload() instanceof MPLS); |
| 226 | MPLS mpls = (MPLS) ethernet.getPayload(); |
| 227 | assertThat(mpls.getLabel(), is(2)); |
| 228 | assertThat(mpls.getTtl(), is((byte) 0)); |
| 229 | assertThat(mpls.protocol, is(PROTOCOL_MPLS)); |
| 230 | assertThat(mpls.bos, is((byte) 0)); |
| 231 | assertTrue(mpls.getPayload() instanceof MPLS); |
| 232 | mpls = (MPLS) mpls.getPayload(); |
| 233 | assertThat(mpls.getLabel(), is(201)); |
| 234 | assertThat(mpls.getTtl(), is((byte) 0)); |
| 235 | assertThat(mpls.protocol, is(PROTOCOL_IPV6)); |
| 236 | assertThat(mpls.bos, is((byte) 1)); |
| 237 | IPv6 ip = (IPv6) mpls.getPayload(); |
| 238 | assertThat(ip.getSourceAddress(), is(SRC_IPV6.toOctets())); |
| 239 | assertThat(ip.getDestinationAddress(), is(DST_IPV6.toOctets())); |
| 240 | assertTrue(ip.getPayload() instanceof ICMP6); |
| 241 | ICMP6 icmp = (ICMP6) ip.getPayload(); |
| 242 | assertThat(icmp.getIcmpType(), is(ECHO_REQUEST)); |
| 243 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 244 | } |