blob: 720a4d206b54eb60f5453c206a917a077b2e2775 [file] [log] [blame]
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Jonathan Hart2a655752015-04-07 16:46:33 -070021import org.junit.Before;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080022import org.junit.BeforeClass;
23import org.junit.Test;
24
Jonathan Hart2a655752015-04-07 16:46:33 -070025import java.nio.ByteBuffer;
26
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080027import static org.hamcrest.Matchers.is;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080028import static org.junit.Assert.assertArrayEquals;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080029import static org.junit.Assert.assertFalse;
Jonathan Hart2a655752015-04-07 16:46:33 -070030import static org.junit.Assert.assertThat;
31import static org.junit.Assert.assertTrue;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080032
33/**
34 * Tests for class {@link IPv6}.
35 */
36public 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 Hart2a655752015-04-07 16:46:33 -070049 private Deserializer<IPv6> deserializer;
50
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080051 @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 Hart2a655752015-04-07 16:46:33 -070073 @Before
74 public void setUp() {
75 deserializer = IPv6.deserializer();
76 }
77
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080078 /**
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. Chan7fee36a2014-12-31 00:19:59 +080086 ipv6.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080087 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 Hart2a655752015-04-07 16:46:33 -070096 @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. Chan93b7fb02014-12-28 03:59:36 +0800110 /**
111 * Tests deserialize and getters.
112 */
113 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700114 public void testDeserialize() throws DeserializationException {
115 IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800116
117 assertThat(ipv6.getVersion(), is((byte) 6));
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800118 assertThat(ipv6.getTrafficClass(), is((byte) 0x93));
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800119 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. Chan7fee36a2014-12-31 00:19:59 +0800134 packet1.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800135 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. Chan7fee36a2014-12-31 00:19:59 +0800144 packet2.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800145 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}