blob: 2ab8ff98f2a36d94e5d2d0bc1db6ff116ad24d3e [file] [log] [blame]
Jonathan Hart2a655752015-04-07 16:46:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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
17package org.onlab.packet;
18
19import org.junit.Before;
20import org.junit.Test;
21
22import java.nio.ByteBuffer;
23import java.util.HashMap;
24
25import static org.junit.Assert.assertEquals;
26
27/**
28 * Unit tests for MPLS class.
29 */
30public class MplsTest {
31
32 private Deserializer<MPLS> deserializer;
33
34 private int label = 1048575;
35 private byte bos = 1;
36 private byte ttl = 20;
37 private byte protocol = MPLS.PROTOCOL_IPV4;
38
39 private byte[] bytes;
40
41 @Before
42 public void setUp() throws Exception {
43 // Replace normal deserializer map with an empty map. This will cause
44 // the DataDeserializer to be used which will silently handle 0-byte input.
45 MPLS.protocolDeserializerMap = new HashMap<>();
46
47 deserializer = MPLS.deserializer();
48
49 ByteBuffer bb = ByteBuffer.allocate(MPLS.HEADER_LENGTH);
50 bb.putInt(((label & 0x000fffff) << 12) | ((bos & 0x1) << 8 | (ttl & 0xff)));
51
52 bytes = bb.array();
53 }
54
55 @Test
56 public void testDeserializeBadInput() throws Exception {
57 PacketTestUtils.testDeserializeBadInput(deserializer);
58 }
59
60 @Test
61 public void testDeserializeTruncated() throws Exception {
62 PacketTestUtils.testDeserializeTruncated(deserializer, bytes);
63 }
64
65 @Test
66 public void testDeserialize() throws Exception {
67 MPLS mpls = deserializer.deserialize(bytes, 0, bytes.length);
68
69 assertEquals(label, mpls.label);
70 assertEquals(bos, mpls.bos);
71 assertEquals(ttl, mpls.ttl);
72 assertEquals(protocol, mpls.protocol);
73 }
74}