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