Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [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 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 21 | import org.junit.Before; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 22 | import org.junit.BeforeClass; |
| 23 | import org.junit.Test; |
| 24 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 25 | import java.nio.ByteBuffer; |
| 26 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 27 | import static org.hamcrest.Matchers.is; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 28 | import static org.junit.Assert.assertArrayEquals; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 29 | import static org.junit.Assert.assertFalse; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 30 | import static org.junit.Assert.assertThat; |
| 31 | import static org.junit.Assert.assertTrue; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Tests for class {@link IPv6}. |
| 35 | */ |
| 36 | public class IPv6Test { |
| 37 | private static final byte[] SOURCE_ADDRESS = { |
| 38 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 39 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce |
| 40 | }; |
| 41 | private static final byte[] DESTINATION_ADDRESS = { |
| 42 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 43 | (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8 |
| 44 | }; |
| 45 | private static Data data; |
| 46 | private static UDP udp; |
| 47 | private static byte[] bytePacket; |
| 48 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 49 | private Deserializer<IPv6> deserializer; |
| 50 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 51 | @BeforeClass |
| 52 | public static void setUpBeforeClass() throws Exception { |
| 53 | data = new Data(); |
| 54 | data.setData("testSerialize".getBytes()); |
| 55 | udp = new UDP(); |
| 56 | udp.setPayload(data); |
| 57 | |
| 58 | byte[] bytePayload = udp.serialize(); |
| 59 | byte[] byteHeader = { |
| 60 | (byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79, |
| 61 | (byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff), |
| 62 | (byte) 0x11, (byte) 0x20, |
| 63 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 64 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, |
| 65 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 66 | (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8, |
| 67 | }; |
| 68 | bytePacket = new byte[byteHeader.length + bytePayload.length]; |
| 69 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
| 70 | System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); |
| 71 | } |
| 72 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 73 | @Before |
| 74 | public void setUp() { |
| 75 | deserializer = IPv6.deserializer(); |
| 76 | } |
| 77 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 78 | /** |
| 79 | * Tests serialize and setters. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testSerialize() { |
| 83 | IPv6 ipv6 = new IPv6(); |
| 84 | ipv6.setPayload(udp); |
| 85 | ipv6.setVersion((byte) 6); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 86 | ipv6.setTrafficClass((byte) 0x93); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 87 | ipv6.setFlowLabel(0x13579); |
| 88 | ipv6.setNextHeader(IPv6.PROTOCOL_UDP); |
| 89 | ipv6.setHopLimit((byte) 32); |
| 90 | ipv6.setSourceAddress(SOURCE_ADDRESS); |
| 91 | ipv6.setDestinationAddress(DESTINATION_ADDRESS); |
| 92 | |
| 93 | assertArrayEquals(ipv6.serialize(), bytePacket); |
| 94 | } |
| 95 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 96 | @Test |
| 97 | public void testDeserializeBadInput() throws Exception { |
| 98 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testDeserializeTruncated() throws Exception { |
| 103 | // Run the truncation test only on the IPv6 header |
| 104 | byte[] ipv6Header = new byte[IPv6.FIXED_HEADER_LENGTH]; |
| 105 | ByteBuffer.wrap(bytePacket).get(ipv6Header); |
| 106 | |
| 107 | PacketTestUtils.testDeserializeTruncated(deserializer, ipv6Header); |
| 108 | } |
| 109 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 110 | /** |
| 111 | * Tests deserialize and getters. |
| 112 | */ |
| 113 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 114 | public void testDeserialize() throws DeserializationException { |
| 115 | IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 116 | |
| 117 | assertThat(ipv6.getVersion(), is((byte) 6)); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 118 | assertThat(ipv6.getTrafficClass(), is((byte) 0x93)); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 119 | assertThat(ipv6.getFlowLabel(), is(0x13579)); |
| 120 | assertThat(ipv6.getNextHeader(), is(IPv6.PROTOCOL_UDP)); |
| 121 | assertThat(ipv6.getHopLimit(), is((byte) 32)); |
| 122 | assertArrayEquals(ipv6.getSourceAddress(), SOURCE_ADDRESS); |
| 123 | assertArrayEquals(ipv6.getDestinationAddress(), DESTINATION_ADDRESS); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Tests comparator. |
| 128 | */ |
| 129 | @Test |
| 130 | public void testEqual() { |
| 131 | IPv6 packet1 = new IPv6(); |
| 132 | packet1.setPayload(udp); |
| 133 | packet1.setVersion((byte) 6); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 134 | packet1.setTrafficClass((byte) 0x93); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 135 | packet1.setFlowLabel(0x13579); |
| 136 | packet1.setNextHeader(IPv6.PROTOCOL_UDP); |
| 137 | packet1.setHopLimit((byte) 32); |
| 138 | packet1.setSourceAddress(SOURCE_ADDRESS); |
| 139 | packet1.setDestinationAddress(DESTINATION_ADDRESS); |
| 140 | |
| 141 | IPv6 packet2 = new IPv6(); |
| 142 | packet2.setPayload(udp); |
| 143 | packet2.setVersion((byte) 6); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 144 | packet2.setTrafficClass((byte) 0x93); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 145 | packet2.setFlowLabel(0x13579); |
| 146 | packet2.setNextHeader(IPv6.PROTOCOL_UDP); |
| 147 | packet2.setHopLimit((byte) 32); |
| 148 | packet2.setSourceAddress(DESTINATION_ADDRESS); |
| 149 | packet2.setDestinationAddress(SOURCE_ADDRESS); |
| 150 | |
| 151 | assertTrue(packet1.equals(packet1)); |
| 152 | assertFalse(packet1.equals(packet2)); |
| 153 | } |
| 154 | } |