blob: bb91e9e7d5ee7b273a7f7edfc4f54d8430e376fa [file] [log] [blame]
Charles M.C. Chan94f37372015-01-10 17:53:42 +08001/*
2 * Copyright 2014-2015 Open Networking Laboratory
3 *
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
17package org.onlab.packet.ipv6;
18
Jonathan Hart2a655752015-04-07 16:46:33 -070019import org.junit.Before;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080020import org.junit.BeforeClass;
21import org.junit.Test;
22import org.onlab.packet.Data;
Jonathan Hart2a655752015-04-07 16:46:33 -070023import org.onlab.packet.Deserializer;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080024import org.onlab.packet.IPv6;
25import org.onlab.packet.UDP;
26
27import static org.hamcrest.Matchers.is;
28import static org.junit.Assert.assertArrayEquals;
29import static org.junit.Assert.assertFalse;
30import static org.junit.Assert.assertThat;
31import static org.junit.Assert.assertTrue;
32
33/**
34 * Tests for class {@link BaseOptions}.
35 */
36public class BaseOptionsTest {
37 private static Data data;
38 private static UDP udp;
39 private static byte[] options = {
40 (byte) 0x00, (byte) 0x03,
41 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00
42 };
43 private static byte[] bytePacket;
44
Jonathan Hart2a655752015-04-07 16:46:33 -070045 private Deserializer<BaseOptions> deserializer;
46
Charles M.C. Chan94f37372015-01-10 17:53:42 +080047 @BeforeClass
48 public static void setUpBeforeClass() throws Exception {
49 data = new Data();
50 data.setData("testSerialize".getBytes());
51 udp = new UDP();
52 udp.setPayload(data);
53
54 byte[] bytePayload = udp.serialize();
55 byte[] byteHeader = {
56 (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0x03,
57 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00
58 };
59 bytePacket = new byte[byteHeader.length + bytePayload.length];
60 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
61 System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
62 }
63
Jonathan Hart2a655752015-04-07 16:46:33 -070064 @Before
65 public void setUp() {
66 deserializer = BaseOptions.deserializer();
67 }
68
Charles M.C. Chan94f37372015-01-10 17:53:42 +080069 /**
70 * Tests serialize and setters.
71 */
72 @Test
73 public void testSerialize() {
74 BaseOptions baseopt = new BaseOptions();
75 baseopt.setNextHeader((byte) 0x11);
76 baseopt.setHeaderExtLength((byte) 0x00);
77 baseopt.setOptions(options);
78 baseopt.setPayload(udp);
79
80 assertArrayEquals(baseopt.serialize(), bytePacket);
81 }
82
83 /**
84 * Tests deserialize and getters.
85 */
86 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070087 public void testDeserialize() throws Exception {
88 BaseOptions baseopt = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan94f37372015-01-10 17:53:42 +080089
90 assertThat(baseopt.getNextHeader(), is((byte) 0x11));
91 assertThat(baseopt.getHeaderExtLength(), is((byte) 0x00));
92 assertArrayEquals(baseopt.getOptions(), options);
93 }
94
95 /**
96 * Tests comparator.
97 */
98 @Test
99 public void testEqual() {
100 BaseOptions baseopt1 = new BaseOptions();
101 baseopt1.setNextHeader((byte) 0x11);
102 baseopt1.setHeaderExtLength((byte) 0x00);
103 baseopt1.setOptions(options);
104 baseopt1.setType(IPv6.PROTOCOL_HOPOPT);
105
106 BaseOptions baseopt2 = new BaseOptions();
107 baseopt2.setNextHeader((byte) 0x11);
108 baseopt2.setHeaderExtLength((byte) 0x00);
109 baseopt2.setOptions(options);
110 baseopt1.setType(IPv6.PROTOCOL_DSTOPT);
111
112 assertTrue(baseopt1.equals(baseopt1));
113 assertFalse(baseopt1.equals(baseopt2));
114 }
115}