blob: d33ec26368c16ae4177fdcdb4d821121779cedc4 [file] [log] [blame]
Charles M.C. Chan94f37372015-01-10 17:53:42 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Charles M.C. Chan94f37372015-01-10 17:53:42 +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
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.DeserializationException;
25import org.onlab.packet.Deserializer;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080026import 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 Routing}.
35 */
36public class RoutingTest {
37 private static Data data;
38 private static UDP udp;
39 private static byte[] routingData = {
40 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
41 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
42 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
43 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
44 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
45 };
46 private static byte[] bytePacket;
47
Jonathan Hart2a655752015-04-07 16:46:33 -070048 private Deserializer<Routing> deserializer;
49
Charles M.C. Chan94f37372015-01-10 17:53:42 +080050 @BeforeClass
51 public static void setUpBeforeClass() throws Exception {
52 data = new Data();
53 data.setData("testSerialize".getBytes());
54 udp = new UDP();
55 udp.setPayload(data);
56
57 byte[] bytePayload = udp.serialize();
58 byte[] byteHeader = {
59 (byte) 0x11, (byte) 0x02, (byte) 0x00, (byte) 0x03,
60 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
61 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
62 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
63 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
64 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
65 };
66 bytePacket = new byte[byteHeader.length + bytePayload.length];
67 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
68 System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
69 }
70
Jonathan Hart2a655752015-04-07 16:46:33 -070071 @Before
72 public void setUp() {
73 deserializer = Routing.deserializer();
74 }
75
Charles M.C. Chan94f37372015-01-10 17:53:42 +080076 /**
77 * Tests serialize and setters.
78 */
79 @Test
80 public void testSerialize() {
81 Routing routing = new Routing();
82 routing.setNextHeader((byte) 0x11);
83 routing.setHeaderExtLength((byte) 0x02);
84 routing.setRoutingType((byte) 0x00);
85 routing.setSegmntsLeft((byte) 0x03);
86 routing.setRoutingData(routingData);
87 routing.setPayload(udp);
88
89 assertArrayEquals(routing.serialize(), bytePacket);
90 }
91
92 /**
93 * Tests deserialize and getters.
94 */
95 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070096 public void testDeserialize() throws DeserializationException {
97 Routing routing = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan94f37372015-01-10 17:53:42 +080098
99 assertThat(routing.getNextHeader(), is((byte) 0x11));
100 assertThat(routing.getHeaderExtLength(), is((byte) 0x02));
101 assertThat(routing.getRoutingType(), is((byte) 0x00));
102 assertThat(routing.getSegmentsLeft(), is((byte) 0x03));
103 assertArrayEquals(routing.getRoutingData(), routingData);
104 }
105
106 /**
107 * Tests comparator.
108 */
109 @Test
110 public void testEqual() {
111 Routing routing1 = new Routing();
112 routing1.setNextHeader((byte) 0x11);
113 routing1.setHeaderExtLength((byte) 0x02);
114 routing1.setRoutingType((byte) 0x00);
115 routing1.setSegmntsLeft((byte) 0x03);
116 routing1.setRoutingData(routingData);
117
118 Routing routing2 = new Routing();
119 routing2.setNextHeader((byte) 0x11);
120 routing2.setHeaderExtLength((byte) 0x02);
121 routing2.setRoutingType((byte) 0x00);
122 routing2.setSegmntsLeft((byte) 0x02);
123 routing2.setRoutingData(routingData);
124
125 assertTrue(routing1.equals(routing1));
126 assertFalse(routing1.equals(routing2));
127 }
Jian Li5fc14292015-12-04 11:30:46 -0800128
129 /**
130 * Tests toString.
131 */
132 @Test
133 public void testToStringRouting() throws Exception {
134 Routing routing = deserializer.deserialize(bytePacket, 0, bytePacket.length);
135 String str = routing.toString();
136
137 assertTrue(StringUtils.contains(str, "nextHeader=" + (byte) 0x11));
138 assertTrue(StringUtils.contains(str, "headerExtLength=" + (byte) 0x02));
139 assertTrue(StringUtils.contains(str, "routingType=" + (byte) 0x00));
140 assertTrue(StringUtils.contains(str, "segmentsLeft=" + (byte) 0x03));
141 assertTrue(StringUtils.contains(str, "routingData=" + Arrays.toString(routingData)));
142 }
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800143}