Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 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 | |
| 17 | package org.onlab.packet.ipv6; |
| 18 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 19 | import org.apache.commons.lang3.StringUtils; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 21 | import org.junit.BeforeClass; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Data; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 24 | import org.onlab.packet.DeserializationException; |
| 25 | import org.onlab.packet.Deserializer; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 26 | import org.onlab.packet.UDP; |
| 27 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 28 | import java.util.Arrays; |
| 29 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 30 | import static org.hamcrest.Matchers.is; |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 31 | import static org.junit.Assert.*; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Tests for class {@link Routing}. |
| 35 | */ |
| 36 | public 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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 48 | private Deserializer<Routing> deserializer; |
| 49 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 50 | @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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 71 | @Before |
| 72 | public void setUp() { |
| 73 | deserializer = Routing.deserializer(); |
| 74 | } |
| 75 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 76 | /** |
| 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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 96 | public void testDeserialize() throws DeserializationException { |
| 97 | Routing routing = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 98 | |
| 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 Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 128 | |
| 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. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 143 | } |