blob: fef26ed27c34d8c94eb5739375ec183e54e83563 [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;
Pier Ventre78e73f62016-12-02 19:59:28 -080029import static org.junit.Assert.*;
Pier Luigi37b687b2017-01-16 22:46:33 -080030import static org.onlab.packet.IPv6.*;
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 };
Pier Luigi37b687b2017-01-16 22:46:33 -080051 private static final byte[] LINK_LOCAL_ADDRESS_1 = {
52 (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
53 (byte) 0x50, (byte) 0x74, (byte) 0xf2, (byte) 0xff, (byte) 0xfe, (byte) 0xb1, (byte) 0xa8, (byte) 0x7f
54 };
55 private static final byte[] MAC_ADDRESS_1 = {
56 (byte) 0x52, (byte) 0x74, (byte) 0xf2, (byte) 0xb1, (byte) 0xa8, (byte) 0x7f
57 };
58 private static final byte[] LINK_LOCAL_ADDRESS_2 = {
59 (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
60 (byte) 0x02, (byte) 0x0c, (byte) 0x29, (byte) 0xff, (byte) 0xfe, (byte) 0x8f, (byte) 0x99, (byte) 0xd2
61 };
62 private static final byte[] MAC_ADDRESS_2 = {
63 (byte) 0x00, (byte) 0x0c, (byte) 0x29, (byte) 0x8f, (byte) 0x99, (byte) 0xd2
64 };
65
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080066 private static Data data;
67 private static UDP udp;
68 private static byte[] bytePacket;
69
Jonathan Hart2a655752015-04-07 16:46:33 -070070 private Deserializer<IPv6> deserializer;
71
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080072 @BeforeClass
73 public static void setUpBeforeClass() throws Exception {
74 data = new Data();
75 data.setData("testSerialize".getBytes());
76 udp = new UDP();
77 udp.setPayload(data);
78
79 byte[] bytePayload = udp.serialize();
80 byte[] byteHeader = {
81 (byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79,
82 (byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff),
83 (byte) 0x11, (byte) 0x20,
84 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
85 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
86 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
87 (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8,
88 };
89 bytePacket = new byte[byteHeader.length + bytePayload.length];
90 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
91 System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
92 }
93
Jonathan Hart2a655752015-04-07 16:46:33 -070094 @Before
95 public void setUp() {
Pier Luigi37b687b2017-01-16 22:46:33 -080096 deserializer = deserializer();
Jonathan Hart2a655752015-04-07 16:46:33 -070097 }
98
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080099 /**
100 * Tests serialize and setters.
101 */
102 @Test
103 public void testSerialize() {
104 IPv6 ipv6 = new IPv6();
105 ipv6.setPayload(udp);
106 ipv6.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800107 ipv6.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800108 ipv6.setFlowLabel(0x13579);
Pier Luigi37b687b2017-01-16 22:46:33 -0800109 ipv6.setNextHeader(PROTOCOL_UDP);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800110 ipv6.setHopLimit((byte) 32);
111 ipv6.setSourceAddress(SOURCE_ADDRESS);
112 ipv6.setDestinationAddress(DESTINATION_ADDRESS);
113
114 assertArrayEquals(ipv6.serialize(), bytePacket);
115 }
116
Jonathan Hart2a655752015-04-07 16:46:33 -0700117 @Test
118 public void testDeserializeBadInput() throws Exception {
119 PacketTestUtils.testDeserializeBadInput(deserializer);
120 }
121
122 @Test
123 public void testDeserializeTruncated() throws Exception {
124 // Run the truncation test only on the IPv6 header
Pier Luigi37b687b2017-01-16 22:46:33 -0800125 byte[] ipv6Header = new byte[FIXED_HEADER_LENGTH];
Jonathan Hart2a655752015-04-07 16:46:33 -0700126 ByteBuffer.wrap(bytePacket).get(ipv6Header);
127
128 PacketTestUtils.testDeserializeTruncated(deserializer, ipv6Header);
129 }
130
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800131 /**
132 * Tests deserialize and getters.
133 */
134 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700135 public void testDeserialize() throws DeserializationException {
136 IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800137
138 assertThat(ipv6.getVersion(), is((byte) 6));
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800139 assertThat(ipv6.getTrafficClass(), is((byte) 0x93));
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800140 assertThat(ipv6.getFlowLabel(), is(0x13579));
Pier Luigi37b687b2017-01-16 22:46:33 -0800141 assertThat(ipv6.getNextHeader(), is(PROTOCOL_UDP));
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800142 assertThat(ipv6.getHopLimit(), is((byte) 32));
143 assertArrayEquals(ipv6.getSourceAddress(), SOURCE_ADDRESS);
144 assertArrayEquals(ipv6.getDestinationAddress(), DESTINATION_ADDRESS);
145 }
146
147 /**
148 * Tests comparator.
149 */
150 @Test
151 public void testEqual() {
152 IPv6 packet1 = new IPv6();
153 packet1.setPayload(udp);
154 packet1.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800155 packet1.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800156 packet1.setFlowLabel(0x13579);
Pier Luigi37b687b2017-01-16 22:46:33 -0800157 packet1.setNextHeader(PROTOCOL_UDP);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800158 packet1.setHopLimit((byte) 32);
159 packet1.setSourceAddress(SOURCE_ADDRESS);
160 packet1.setDestinationAddress(DESTINATION_ADDRESS);
161
162 IPv6 packet2 = new IPv6();
163 packet2.setPayload(udp);
164 packet2.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800165 packet2.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800166 packet2.setFlowLabel(0x13579);
Pier Luigi37b687b2017-01-16 22:46:33 -0800167 packet2.setNextHeader(PROTOCOL_UDP);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800168 packet2.setHopLimit((byte) 32);
169 packet2.setSourceAddress(DESTINATION_ADDRESS);
170 packet2.setDestinationAddress(SOURCE_ADDRESS);
171
172 assertTrue(packet1.equals(packet1));
173 assertFalse(packet1.equals(packet2));
174 }
Jian Li5fc14292015-12-04 11:30:46 -0800175
176 /**
177 * Tests toString.
178 */
179 @Test
180 public void testToStringIPv6() throws Exception {
181 IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length);
182 String str = ipv6.toString();
183
184 assertTrue(StringUtils.contains(str, "version=" + (byte) 6));
185 assertTrue(StringUtils.contains(str, "trafficClass=" + (byte) 0x93));
186 assertTrue(StringUtils.contains(str, "flowLabel=" + 0x13579));
Pier Luigi37b687b2017-01-16 22:46:33 -0800187 assertTrue(StringUtils.contains(str, "nextHeader=" + PROTOCOL_UDP));
Jian Li5fc14292015-12-04 11:30:46 -0800188 assertTrue(StringUtils.contains(str, "hopLimit=" + (byte) 32));
189 // TODO: test IPv6 source and destination address
190 }
Pier Ventre78e73f62016-12-02 19:59:28 -0800191
192 /**
193 * Tests the proper operation of the solicitationNodeAddress function.
194 */
195 @Test
196 public void testSolicitationNodeAddress() {
Pier Luigi37b687b2017-01-16 22:46:33 -0800197 assertArrayEquals(SOLICITATION_NODE_ADDRESS, getSolicitNodeAddress(DESTINATION_ADDRESS));
Pier Ventre78e73f62016-12-02 19:59:28 -0800198 }
199
200 /**
201 * Tests the proper operation of the multicastAddress function.
202 */
203 @Test
204 public void testMulticastAddress() {
Pier Luigi37b687b2017-01-16 22:46:33 -0800205 assertArrayEquals(MULTICAST_ADDRESS, getMCastMacAddress(DESTINATION_ADDRESS));
206 }
207
208 /**
209 * Tests the proper operation of the isLinkLocalAddress function.
210 */
211 @Test
212 public void testIsLinkLocalAddress() {
213 assertFalse(isLinkLocalAddress(SOURCE_ADDRESS));
214 assertFalse(isLinkLocalAddress(DESTINATION_ADDRESS));
215 assertFalse(isLinkLocalAddress(SOLICITATION_NODE_ADDRESS));
216 assertTrue(isLinkLocalAddress(LINK_LOCAL_ADDRESS_1));
217 assertTrue(isLinkLocalAddress(LINK_LOCAL_ADDRESS_2));
218 }
219
220 /**
221 * Tests the proper operation of the linkLocalAddress function.
222 */
223 @Test
224 public void testLinkLocalAddress() {
225 assertArrayEquals(getLinkLocalAddress(MAC_ADDRESS_1), LINK_LOCAL_ADDRESS_1);
226 assertArrayEquals(getLinkLocalAddress(MAC_ADDRESS_2), LINK_LOCAL_ADDRESS_2);
227 }
228
229 /**
230 * Tests the proper operation of the macAddress function.
231 */
232 @Test
233 public void testMacAddress() {
234 assertArrayEquals(getMacAddress(LINK_LOCAL_ADDRESS_1), MAC_ADDRESS_1);
235 assertArrayEquals(getMacAddress(LINK_LOCAL_ADDRESS_2), MAC_ADDRESS_2);
Pier Ventre78e73f62016-12-02 19:59:28 -0800236 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800237}