blob: 2e12d5b512dcdbe5f0c0a55a1c6ad56f95808ad0 [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
21import org.junit.BeforeClass;
22import org.junit.Test;
23
24import static org.hamcrest.Matchers.is;
25import static org.junit.Assert.assertThat;
26import static org.junit.Assert.assertArrayEquals;
27import static org.junit.Assert.assertTrue;
28import static org.junit.Assert.assertFalse;
29
30/**
31 * Tests for class {@link IPv6}.
32 */
33public class IPv6Test {
34 private static final byte[] SOURCE_ADDRESS = {
35 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
36 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
37 };
38 private static final byte[] DESTINATION_ADDRESS = {
39 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
40 (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
41 };
42 private static Data data;
43 private static UDP udp;
44 private static byte[] bytePacket;
45
46 @BeforeClass
47 public static void setUpBeforeClass() throws Exception {
48 data = new Data();
49 data.setData("testSerialize".getBytes());
50 udp = new UDP();
51 udp.setPayload(data);
52
53 byte[] bytePayload = udp.serialize();
54 byte[] byteHeader = {
55 (byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79,
56 (byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff),
57 (byte) 0x11, (byte) 0x20,
58 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
59 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
60 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
61 (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8,
62 };
63 bytePacket = new byte[byteHeader.length + bytePayload.length];
64 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
65 System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
66 }
67
68 /**
69 * Tests serialize and setters.
70 */
71 @Test
72 public void testSerialize() {
73 IPv6 ipv6 = new IPv6();
74 ipv6.setPayload(udp);
75 ipv6.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080076 ipv6.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080077 ipv6.setFlowLabel(0x13579);
78 ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
79 ipv6.setHopLimit((byte) 32);
80 ipv6.setSourceAddress(SOURCE_ADDRESS);
81 ipv6.setDestinationAddress(DESTINATION_ADDRESS);
82
83 assertArrayEquals(ipv6.serialize(), bytePacket);
84 }
85
86 /**
87 * Tests deserialize and getters.
88 */
89 @Test
90 public void testDeserialize() {
91 IPv6 ipv6 = new IPv6();
92 ipv6.deserialize(bytePacket, 0, bytePacket.length);
93
94 assertThat(ipv6.getVersion(), is((byte) 6));
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080095 assertThat(ipv6.getTrafficClass(), is((byte) 0x93));
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080096 assertThat(ipv6.getFlowLabel(), is(0x13579));
97 assertThat(ipv6.getNextHeader(), is(IPv6.PROTOCOL_UDP));
98 assertThat(ipv6.getHopLimit(), is((byte) 32));
99 assertArrayEquals(ipv6.getSourceAddress(), SOURCE_ADDRESS);
100 assertArrayEquals(ipv6.getDestinationAddress(), DESTINATION_ADDRESS);
101 }
102
103 /**
104 * Tests comparator.
105 */
106 @Test
107 public void testEqual() {
108 IPv6 packet1 = new IPv6();
109 packet1.setPayload(udp);
110 packet1.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800111 packet1.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800112 packet1.setFlowLabel(0x13579);
113 packet1.setNextHeader(IPv6.PROTOCOL_UDP);
114 packet1.setHopLimit((byte) 32);
115 packet1.setSourceAddress(SOURCE_ADDRESS);
116 packet1.setDestinationAddress(DESTINATION_ADDRESS);
117
118 IPv6 packet2 = new IPv6();
119 packet2.setPayload(udp);
120 packet2.setVersion((byte) 6);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800121 packet2.setTrafficClass((byte) 0x93);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800122 packet2.setFlowLabel(0x13579);
123 packet2.setNextHeader(IPv6.PROTOCOL_UDP);
124 packet2.setHopLimit((byte) 32);
125 packet2.setSourceAddress(DESTINATION_ADDRESS);
126 packet2.setDestinationAddress(SOURCE_ADDRESS);
127
128 assertTrue(packet1.equals(packet1));
129 assertFalse(packet1.equals(packet2));
130 }
131}