blob: b08b2e93fdda12771dde34739da5d225578c4a9b [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
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;
24import java.util.HashMap;
25
26import static org.junit.Assert.assertEquals;
Jian Li5fc14292015-12-04 11:30:46 -080027import static org.junit.Assert.assertTrue;
Jonathan Hart2a655752015-04-07 16:46:33 -070028
29/**
30 * Unit tests for MPLS class.
31 */
32public class MplsTest {
33
34 private Deserializer<MPLS> deserializer;
35
36 private int label = 1048575;
37 private byte bos = 1;
38 private byte ttl = 20;
39 private byte protocol = MPLS.PROTOCOL_IPV4;
40
41 private byte[] bytes;
42
43 @Before
44 public void setUp() throws Exception {
45 // Replace normal deserializer map with an empty map. This will cause
46 // the DataDeserializer to be used which will silently handle 0-byte input.
47 MPLS.protocolDeserializerMap = new HashMap<>();
48
49 deserializer = MPLS.deserializer();
50
51 ByteBuffer bb = ByteBuffer.allocate(MPLS.HEADER_LENGTH);
52 bb.putInt(((label & 0x000fffff) << 12) | ((bos & 0x1) << 8 | (ttl & 0xff)));
53
54 bytes = bb.array();
55 }
56
57 @Test
58 public void testDeserializeBadInput() throws Exception {
59 PacketTestUtils.testDeserializeBadInput(deserializer);
60 }
61
62 @Test
63 public void testDeserializeTruncated() throws Exception {
64 PacketTestUtils.testDeserializeTruncated(deserializer, bytes);
65 }
66
Jian Li5fc14292015-12-04 11:30:46 -080067 /**
68 * Tests deserialize and getters.
69 */
Jonathan Hart2a655752015-04-07 16:46:33 -070070 @Test
71 public void testDeserialize() throws Exception {
72 MPLS mpls = deserializer.deserialize(bytes, 0, bytes.length);
73
74 assertEquals(label, mpls.label);
75 assertEquals(bos, mpls.bos);
76 assertEquals(ttl, mpls.ttl);
77 assertEquals(protocol, mpls.protocol);
78 }
Jian Li5fc14292015-12-04 11:30:46 -080079
80 /**
81 * Tests toString.
82 */
83 @Test
84 public void testToStringMpls() throws Exception {
85 MPLS mpls = deserializer.deserialize(bytes, 0, bytes.length);
86 String str = mpls.toString();
87
88 assertTrue(StringUtils.contains(str, "label=" + label));
89 assertTrue(StringUtils.contains(str, "bos=" + bos));
90 assertTrue(StringUtils.contains(str, "ttl=" + ttl));
91 assertTrue(StringUtils.contains(str, "protocol=" + protocol));
92 }
Jonathan Hart2a655752015-04-07 16:46:33 -070093}