blob: 17ada1e3b3af0b887234eec340ff884729f41740 [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
Jian Li5fc14292015-12-04 11:30:46 -080019import org.apache.commons.lang3.StringUtils;
Jonathan Hart2a655752015-04-07 16:46:33 -070020import org.junit.Before;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080021import org.junit.BeforeClass;
22import org.junit.Test;
23import org.onlab.packet.Data;
Jonathan Hart2a655752015-04-07 16:46:33 -070024import org.onlab.packet.Deserializer;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080025import org.onlab.packet.IPv6;
26import org.onlab.packet.UDP;
27
Jian Li5fc14292015-12-04 11:30:46 -080028import java.util.Arrays;
29
Charles M.C. Chan94f37372015-01-10 17:53:42 +080030import static org.hamcrest.Matchers.is;
Jian Li5fc14292015-12-04 11:30:46 -080031import static org.junit.Assert.*;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080032
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 }
Jian Li5fc14292015-12-04 11:30:46 -0800115
116 /**
117 * Tests toString.
118 */
119 @Test
120 public void testToStringBaseOptions() throws Exception {
121 BaseOptions baseopt = deserializer.deserialize(bytePacket, 0, bytePacket.length);
122 String str = baseopt.toString();
123
124 assertTrue(StringUtils.contains(str, "nextHeader=" + (byte) 0x11));
125 assertTrue(StringUtils.contains(str, "headerExtLength=" + (byte) 0x00));
126 assertTrue(StringUtils.contains(str, "options=" + Arrays.toString(options)));
127 }
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800128}