blob: f1d4d3807f5ae5f347fafde7a33d21c6a99114e3 [file] [log] [blame]
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08003 *
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
19package org.onlab.packet;
20
Jian Li5fc14292015-12-04 11:30:46 -080021import org.apache.commons.lang3.StringUtils;
Jonathan Hart2a655752015-04-07 16:46:33 -070022import org.junit.Before;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080023import org.junit.BeforeClass;
24import org.junit.Test;
25
Jonathan Hart2a655752015-04-07 16:46:33 -070026import java.nio.ByteBuffer;
27
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080028import static org.hamcrest.Matchers.is;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080029import static org.junit.Assert.assertArrayEquals;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080030import static org.junit.Assert.assertFalse;
Jonathan Hart2a655752015-04-07 16:46:33 -070031import static org.junit.Assert.assertThat;
32import static org.junit.Assert.assertTrue;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080033
34/**
35 * Tests for class {@link IPv6}.
36 */
37public class IPv6Test {
38 private static final byte[] SOURCE_ADDRESS = {
39 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
40 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
41 };
42 private static final byte[] DESTINATION_ADDRESS = {
43 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
44 (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
45 };
46 private static Data data;
47 private static UDP udp;
48 private static byte[] bytePacket;
49
Jonathan Hart2a655752015-04-07 16:46:33 -070050 private Deserializer<IPv6> deserializer;
51
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080052 @BeforeClass
53 public static void setUpBeforeClass() throws Exception {
54 data = new Data();
55 data.setData("testSerialize".getBytes());
56 udp = new UDP();
57 udp.setPayload(data);
58
59 byte[] bytePayload = udp.serialize();
60 byte[] byteHeader = {
61 (byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79,
62 (byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff),
63 (byte) 0x11, (byte) 0x20,
64 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
65 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
66 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
67 (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8,
68 };
69 bytePacket = new byte[byteHeader.length + bytePayload.length];
70 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
71 System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
72 }
73
Jonathan Hart2a655752015-04-07 16:46:33 -070074 @Before
75 public void setUp() {
76 deserializer = IPv6.deserializer();
77 }
78
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080079 /**
80 * Tests serialize and setters.
81 */
82 @Test
83 public void testSerialize() {
84 IPv6 ipv6 = new IPv6();
85 ipv6.setPayload(udp);
86 ipv6.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080087 ipv6.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080088 ipv6.setFlowLabel(0x13579);
89 ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
90 ipv6.setHopLimit((byte) 32);
91 ipv6.setSourceAddress(SOURCE_ADDRESS);
92 ipv6.setDestinationAddress(DESTINATION_ADDRESS);
93
94 assertArrayEquals(ipv6.serialize(), bytePacket);
95 }
96
Jonathan Hart2a655752015-04-07 16:46:33 -070097 @Test
98 public void testDeserializeBadInput() throws Exception {
99 PacketTestUtils.testDeserializeBadInput(deserializer);
100 }
101
102 @Test
103 public void testDeserializeTruncated() throws Exception {
104 // Run the truncation test only on the IPv6 header
105 byte[] ipv6Header = new byte[IPv6.FIXED_HEADER_LENGTH];
106 ByteBuffer.wrap(bytePacket).get(ipv6Header);
107
108 PacketTestUtils.testDeserializeTruncated(deserializer, ipv6Header);
109 }
110
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800111 /**
112 * Tests deserialize and getters.
113 */
114 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700115 public void testDeserialize() throws DeserializationException {
116 IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800117
118 assertThat(ipv6.getVersion(), is((byte) 6));
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800119 assertThat(ipv6.getTrafficClass(), is((byte) 0x93));
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800120 assertThat(ipv6.getFlowLabel(), is(0x13579));
121 assertThat(ipv6.getNextHeader(), is(IPv6.PROTOCOL_UDP));
122 assertThat(ipv6.getHopLimit(), is((byte) 32));
123 assertArrayEquals(ipv6.getSourceAddress(), SOURCE_ADDRESS);
124 assertArrayEquals(ipv6.getDestinationAddress(), DESTINATION_ADDRESS);
125 }
126
127 /**
128 * Tests comparator.
129 */
130 @Test
131 public void testEqual() {
132 IPv6 packet1 = new IPv6();
133 packet1.setPayload(udp);
134 packet1.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800135 packet1.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800136 packet1.setFlowLabel(0x13579);
137 packet1.setNextHeader(IPv6.PROTOCOL_UDP);
138 packet1.setHopLimit((byte) 32);
139 packet1.setSourceAddress(SOURCE_ADDRESS);
140 packet1.setDestinationAddress(DESTINATION_ADDRESS);
141
142 IPv6 packet2 = new IPv6();
143 packet2.setPayload(udp);
144 packet2.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800145 packet2.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800146 packet2.setFlowLabel(0x13579);
147 packet2.setNextHeader(IPv6.PROTOCOL_UDP);
148 packet2.setHopLimit((byte) 32);
149 packet2.setSourceAddress(DESTINATION_ADDRESS);
150 packet2.setDestinationAddress(SOURCE_ADDRESS);
151
152 assertTrue(packet1.equals(packet1));
153 assertFalse(packet1.equals(packet2));
154 }
Jian Li5fc14292015-12-04 11:30:46 -0800155
156 /**
157 * Tests toString.
158 */
159 @Test
160 public void testToStringIPv6() throws Exception {
161 IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length);
162 String str = ipv6.toString();
163
164 assertTrue(StringUtils.contains(str, "version=" + (byte) 6));
165 assertTrue(StringUtils.contains(str, "trafficClass=" + (byte) 0x93));
166 assertTrue(StringUtils.contains(str, "flowLabel=" + 0x13579));
167 assertTrue(StringUtils.contains(str, "nextHeader=" + IPv6.PROTOCOL_UDP));
168 assertTrue(StringUtils.contains(str, "hopLimit=" + (byte) 32));
169 // TODO: test IPv6 source and destination address
170 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800171}