jaegonkim | 1179d83 | 2017-06-13 19:30:55 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
jaegonkim | 1179d83 | 2017-06-13 19:30:55 +0900 | [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 | |
| 18 | |
| 19 | package org.onlab.packet; |
| 20 | |
| 21 | import com.google.common.testing.EqualsTester; |
| 22 | import org.apache.commons.lang3.StringUtils; |
| 23 | import org.junit.BeforeClass; |
| 24 | import org.junit.Test; |
| 25 | |
| 26 | import static org.hamcrest.Matchers.is; |
| 27 | import static org.junit.Assert.assertArrayEquals; |
| 28 | import static org.junit.Assert.assertThat; |
| 29 | import static org.junit.Assert.assertTrue; |
| 30 | |
| 31 | /** |
| 32 | * Tests for class {@link VXLAN}. |
| 33 | */ |
| 34 | public class VXLANTest { |
| 35 | |
| 36 | private static final byte[] BYTE_PACKET_VXLAN = { |
| 37 | (byte) 0x08, // flags (8 bits) |
| 38 | (byte) 0x00, (byte) 0x00, (byte) 0x00, // rsvd1 (24 bits) |
| 39 | (byte) 0x12, (byte) 0x34, (byte) 0x56, // vni (24 bits) |
| 40 | (byte) 0x00, // rsvd2 (8 bits) |
| 41 | }; |
| 42 | |
| 43 | private static Deserializer<VXLAN> deserializer; |
| 44 | |
| 45 | private static final UDP UDP_HDR = new UDP(); |
| 46 | |
| 47 | private static final int TEST_UDP_SRCPORT = 0x50; |
| 48 | private static final int TEST_FLAGS = 0x08; |
| 49 | private static final int TEST_VNI1 = 0x123456; |
| 50 | private static final int TEST_VNI2 = 0x654321; |
| 51 | |
| 52 | |
| 53 | @BeforeClass |
| 54 | public static void setUpBeforeClass() throws Exception { |
| 55 | deserializer = VXLAN.deserializer(); |
| 56 | UDP_HDR.setSourcePort(TEST_UDP_SRCPORT); |
| 57 | UDP_HDR.setDestinationPort(UDP.VXLAN_UDP_PORT); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Tests serialize and setters. |
| 62 | */ |
| 63 | @Test |
| 64 | public void testSerialize() { |
| 65 | VXLAN vxlan = new VXLAN(); |
| 66 | vxlan.setFlag((byte) TEST_FLAGS); |
| 67 | vxlan.setVni(TEST_VNI1); |
| 68 | vxlan.setParent(UDP_HDR); |
| 69 | assertArrayEquals("Serialized packet is not matched", BYTE_PACKET_VXLAN, vxlan.serialize()); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Tests deserialize bad input. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testDeserializeBadInput() throws Exception { |
| 77 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Tests deserialize truncated. |
| 82 | */ |
| 83 | @Test |
| 84 | public void testDeserializeTruncated() throws Exception { |
| 85 | PacketTestUtils.testDeserializeTruncated(deserializer, BYTE_PACKET_VXLAN); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Tests deserialize and getters. |
| 90 | */ |
| 91 | @Test |
| 92 | public void testDeserialize() throws Exception { |
| 93 | VXLAN vxlan = deserializer.deserialize(BYTE_PACKET_VXLAN, 0, BYTE_PACKET_VXLAN.length); |
| 94 | |
| 95 | assertThat(vxlan.getFlag(), is((byte) TEST_FLAGS)); |
| 96 | assertThat(vxlan.getVni(), is(TEST_VNI1)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests comparator. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testEqual() { |
| 104 | VXLAN vxlan1 = new VXLAN(); |
| 105 | vxlan1.setFlag((byte) TEST_FLAGS); |
| 106 | vxlan1.setVni(TEST_VNI1); |
| 107 | |
| 108 | VXLAN vxlan2 = new VXLAN(); |
| 109 | vxlan2.setFlag((byte) TEST_FLAGS); |
| 110 | vxlan2.setVni(TEST_VNI2); |
| 111 | |
| 112 | new EqualsTester() |
| 113 | .addEqualityGroup(vxlan1, vxlan1) |
| 114 | .addEqualityGroup(vxlan2).testEquals(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Tests toString. |
| 119 | */ |
| 120 | @Test |
| 121 | public void testToStringVXLAN() throws Exception { |
| 122 | VXLAN vxlan = deserializer.deserialize(BYTE_PACKET_VXLAN, 0, BYTE_PACKET_VXLAN.length); |
| 123 | String str = vxlan.toString(); |
| 124 | |
| 125 | assertTrue(StringUtils.contains(str, "flags=" + TEST_FLAGS)); |
| 126 | assertTrue(StringUtils.contains(str, "vni=" + TEST_VNI1)); |
| 127 | } |
| 128 | } |