blob: b1b555d5374efcaeeaa28a7a6b54d27df6d8a88e [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;
Pier Ventre78e73f62016-12-02 19:59:28 -080027import java.util.Arrays;
Jonathan Hart2a655752015-04-07 16:46:33 -070028
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080029import static org.hamcrest.Matchers.is;
Pier Ventre78e73f62016-12-02 19:59:28 -080030import static org.junit.Assert.*;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080031
32/**
33 * Tests for class {@link IPv6}.
34 */
35public class IPv6Test {
36 private static final byte[] SOURCE_ADDRESS = {
37 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
38 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
39 };
40 private static final byte[] DESTINATION_ADDRESS = {
41 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
42 (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
43 };
Pier Ventre78e73f62016-12-02 19:59:28 -080044 private static final byte[] SOLICITATION_NODE_ADDRESS = {
45 (byte) 0xff, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
46 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xff, (byte) 0x54, (byte) 0x37, (byte) 0xc8
47 };
48 private static final byte[] MULTICAST_ADDRESS = {
49 (byte) 0x33, (byte) 0x33, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
50 };
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080051 private static Data data;
52 private static UDP udp;
53 private static byte[] bytePacket;
54
Jonathan Hart2a655752015-04-07 16:46:33 -070055 private Deserializer<IPv6> deserializer;
56
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080057 @BeforeClass
58 public static void setUpBeforeClass() throws Exception {
59 data = new Data();
60 data.setData("testSerialize".getBytes());
61 udp = new UDP();
62 udp.setPayload(data);
63
64 byte[] bytePayload = udp.serialize();
65 byte[] byteHeader = {
66 (byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79,
67 (byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff),
68 (byte) 0x11, (byte) 0x20,
69 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
70 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
71 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
72 (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8,
73 };
74 bytePacket = new byte[byteHeader.length + bytePayload.length];
75 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
76 System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
77 }
78
Jonathan Hart2a655752015-04-07 16:46:33 -070079 @Before
80 public void setUp() {
81 deserializer = IPv6.deserializer();
82 }
83
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080084 /**
85 * Tests serialize and setters.
86 */
87 @Test
88 public void testSerialize() {
89 IPv6 ipv6 = new IPv6();
90 ipv6.setPayload(udp);
91 ipv6.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080092 ipv6.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080093 ipv6.setFlowLabel(0x13579);
94 ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
95 ipv6.setHopLimit((byte) 32);
96 ipv6.setSourceAddress(SOURCE_ADDRESS);
97 ipv6.setDestinationAddress(DESTINATION_ADDRESS);
98
99 assertArrayEquals(ipv6.serialize(), bytePacket);
100 }
101
Jonathan Hart2a655752015-04-07 16:46:33 -0700102 @Test
103 public void testDeserializeBadInput() throws Exception {
104 PacketTestUtils.testDeserializeBadInput(deserializer);
105 }
106
107 @Test
108 public void testDeserializeTruncated() throws Exception {
109 // Run the truncation test only on the IPv6 header
110 byte[] ipv6Header = new byte[IPv6.FIXED_HEADER_LENGTH];
111 ByteBuffer.wrap(bytePacket).get(ipv6Header);
112
113 PacketTestUtils.testDeserializeTruncated(deserializer, ipv6Header);
114 }
115
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800116 /**
117 * Tests deserialize and getters.
118 */
119 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700120 public void testDeserialize() throws DeserializationException {
121 IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800122
123 assertThat(ipv6.getVersion(), is((byte) 6));
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800124 assertThat(ipv6.getTrafficClass(), is((byte) 0x93));
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800125 assertThat(ipv6.getFlowLabel(), is(0x13579));
126 assertThat(ipv6.getNextHeader(), is(IPv6.PROTOCOL_UDP));
127 assertThat(ipv6.getHopLimit(), is((byte) 32));
128 assertArrayEquals(ipv6.getSourceAddress(), SOURCE_ADDRESS);
129 assertArrayEquals(ipv6.getDestinationAddress(), DESTINATION_ADDRESS);
130 }
131
132 /**
133 * Tests comparator.
134 */
135 @Test
136 public void testEqual() {
137 IPv6 packet1 = new IPv6();
138 packet1.setPayload(udp);
139 packet1.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800140 packet1.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800141 packet1.setFlowLabel(0x13579);
142 packet1.setNextHeader(IPv6.PROTOCOL_UDP);
143 packet1.setHopLimit((byte) 32);
144 packet1.setSourceAddress(SOURCE_ADDRESS);
145 packet1.setDestinationAddress(DESTINATION_ADDRESS);
146
147 IPv6 packet2 = new IPv6();
148 packet2.setPayload(udp);
149 packet2.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800150 packet2.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800151 packet2.setFlowLabel(0x13579);
152 packet2.setNextHeader(IPv6.PROTOCOL_UDP);
153 packet2.setHopLimit((byte) 32);
154 packet2.setSourceAddress(DESTINATION_ADDRESS);
155 packet2.setDestinationAddress(SOURCE_ADDRESS);
156
157 assertTrue(packet1.equals(packet1));
158 assertFalse(packet1.equals(packet2));
159 }
Jian Li5fc14292015-12-04 11:30:46 -0800160
161 /**
162 * Tests toString.
163 */
164 @Test
165 public void testToStringIPv6() throws Exception {
166 IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length);
167 String str = ipv6.toString();
168
169 assertTrue(StringUtils.contains(str, "version=" + (byte) 6));
170 assertTrue(StringUtils.contains(str, "trafficClass=" + (byte) 0x93));
171 assertTrue(StringUtils.contains(str, "flowLabel=" + 0x13579));
172 assertTrue(StringUtils.contains(str, "nextHeader=" + IPv6.PROTOCOL_UDP));
173 assertTrue(StringUtils.contains(str, "hopLimit=" + (byte) 32));
174 // TODO: test IPv6 source and destination address
175 }
Pier Ventre78e73f62016-12-02 19:59:28 -0800176
177 /**
178 * Tests the proper operation of the solicitationNodeAddress function.
179 */
180 @Test
181 public void testSolicitationNodeAddress() {
182 assertTrue(Arrays.equals(SOLICITATION_NODE_ADDRESS, IPv6.solicitationNodeAddress(DESTINATION_ADDRESS)));
183 }
184
185 /**
186 * Tests the proper operation of the multicastAddress function.
187 */
188 @Test
189 public void testMulticastAddress() {
190 assertTrue(Arrays.equals(MULTICAST_ADDRESS, IPv6.multicastMacAddress(DESTINATION_ADDRESS)));
191 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800192}