blob: 88dc6bdb353f9b21fae5d7823d72fcd2ca83cf20 [file] [log] [blame]
Jonathan Hart2a655752015-04-07 16:46:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jonathan Hart2a655752015-04-07 16:46:33 -07003 *
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
17package org.onlab.packet;
18
Jian Li5fc14292015-12-04 11:30:46 -080019import org.apache.commons.lang3.StringUtils;
Jonathan Hart2a655752015-04-07 16:46:33 -070020import org.junit.Before;
21import org.junit.Test;
22
23import java.nio.ByteBuffer;
Jonathan Hart2a655752015-04-07 16:46:33 -070024
pierd1546902019-06-28 22:17:31 +020025import static junit.framework.TestCase.assertNotNull;
26import static org.hamcrest.core.Is.is;
Jonathan Hart2a655752015-04-07 16:46:33 -070027import static org.junit.Assert.assertEquals;
pierd1546902019-06-28 22:17:31 +020028import static org.junit.Assert.assertThat;
Jian Li5fc14292015-12-04 11:30:46 -080029import static org.junit.Assert.assertTrue;
pierd1546902019-06-28 22:17:31 +020030import static org.onlab.packet.ICMP.TYPE_ECHO_REQUEST;
31import static org.onlab.packet.ICMP6.ECHO_REQUEST;
32import static org.onlab.packet.IPv4.PROTOCOL_ICMP;
33import static org.onlab.packet.MPLS.PROTOCOL_IPV4;
34import static org.onlab.packet.MPLS.PROTOCOL_IPV6;
35import static org.onlab.packet.MPLS.PROTOCOL_MPLS;
Jonathan Hart2a655752015-04-07 16:46:33 -070036
37/**
38 * Unit tests for MPLS class.
39 */
40public class MplsTest {
41
42 private Deserializer<MPLS> deserializer;
43
44 private int label = 1048575;
45 private byte bos = 1;
46 private byte ttl = 20;
pierd1546902019-06-28 22:17:31 +020047 private byte protocol = PROTOCOL_IPV4;
Jonathan Hart2a655752015-04-07 16:46:33 -070048
49 private byte[] bytes;
pierd1546902019-06-28 22:17:31 +020050 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 Hart2a655752015-04-07 16:46:33 -070098
99 @Before
100 public void setUp() throws Exception {
pierd1546902019-06-28 22:17:31 +0200101 // Setup packets
Jonathan Hart2a655752015-04-07 16:46:33 -0700102 deserializer = MPLS.deserializer();
103
pierd1546902019-06-28 22:17:31 +0200104 byte[] ipv4 = IPV4.serialize();
105 ByteBuffer bb = ByteBuffer.allocate(MPLS.HEADER_LENGTH + IPV4.getTotalLength());
Jonathan Hart2a655752015-04-07 16:46:33 -0700106 bb.putInt(((label & 0x000fffff) << 12) | ((bos & 0x1) << 8 | (ttl & 0xff)));
pierd1546902019-06-28 22:17:31 +0200107 bb.put(ipv4);
Jonathan Hart2a655752015-04-07 16:46:33 -0700108
109 bytes = bb.array();
pierd1546902019-06-28 22:17:31 +0200110
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 Hart2a655752015-04-07 16:46:33 -0700127 }
128
129 @Test
130 public void testDeserializeBadInput() throws Exception {
131 PacketTestUtils.testDeserializeBadInput(deserializer);
132 }
133
134 @Test
135 public void testDeserializeTruncated() throws Exception {
pierd1546902019-06-28 22:17:31 +0200136 PacketTestUtils.testDeserializeTruncated(deserializer, truncatedBytes);
Jonathan Hart2a655752015-04-07 16:46:33 -0700137 }
138
Jian Li5fc14292015-12-04 11:30:46 -0800139 /**
140 * Tests deserialize and getters.
141 */
Jonathan Hart2a655752015-04-07 16:46:33 -0700142 @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 Li5fc14292015-12-04 11:30:46 -0800151
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 }
pierd1546902019-06-28 22:17:31 +0200165
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 Hart2a655752015-04-07 16:46:33 -0700239}