Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 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; |
| 24 | import java.util.HashMap; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 27 | import static org.junit.Assert.assertTrue; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Unit tests for MPLS class. |
| 31 | */ |
| 32 | public 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 Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Tests deserialize and getters. |
| 69 | */ |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 70 | @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 Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 79 | |
| 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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 93 | } |