blob: 1567b2c92d4ff32125eb43be2b07a5be2b77329f [file] [log] [blame]
Charles M.C. Chan197a0122015-04-08 18:15:34 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Charles M.C. Chan197a0122015-04-08 18:15:34 +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
Jian Li5fc14292015-12-04 11:30:46 -080021import org.apache.commons.lang3.StringUtils;
Charles M.C. Chan197a0122015-04-08 18:15:34 +080022import org.junit.BeforeClass;
23import org.junit.Test;
24
25import static org.hamcrest.Matchers.is;
26import static org.junit.Assert.assertArrayEquals;
27import static org.junit.Assert.assertFalse;
28import static org.junit.Assert.assertThat;
29import static org.junit.Assert.assertTrue;
30
31/**
32 * Tests for class {@link TCP}.
33 */
34public class TCPTest {
35 private static final byte[] IPV4_SOURCE_ADDRESS = {
36 (byte) 192, (byte) 168, (byte) 1, (byte) 1
37 };
38 private static final byte[] IPV4_DESTINATION_ADDRESS = {
39 (byte) 192, (byte) 168, (byte) 1, (byte) 2
40 };
41 private static final byte[] IPV6_SOURCE_ADDRESS = {
42 (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
43 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
44 };
45 private static final byte[] IPV6_DESTINATION_ADDRESS = {
46 (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
47 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02
48 };
49
50 private static IPv4 ipv4 = new IPv4();
51 private static IPv6 ipv6 = new IPv6();
52 private static byte[] bytePacketTCP4 = {
53 (byte) 0x00, (byte) 0x50, (byte) 0x00, (byte) 0x60, // src,dst port
54 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10, // seq
55 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, // ack
56 (byte) 0x50, (byte) 0x02, // offset,flag
57 (byte) 0x10, (byte) 0x00, // window
58 (byte) 0x1b, (byte) 0xae, // checksum
59 (byte) 0x00, (byte) 0x01 // urgent
60 };
61 private static byte[] bytePacketTCP6 = {
62 (byte) 0x00, (byte) 0x50, (byte) 0x00, (byte) 0x60, // src,dst port
63 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10, // seq
64 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, // ack
65 (byte) 0x50, (byte) 0x02, // offset,flag
66 (byte) 0x10, (byte) 0x00, // window
67 (byte) 0xa1, (byte) 0xfd, // checksum
68 (byte) 0x00, (byte) 0x01 // urgent
69 };
70
Jonathan Hart2a655752015-04-07 16:46:33 -070071 private static Deserializer<TCP> deserializer;
72
Charles M.C. Chan197a0122015-04-08 18:15:34 +080073 @BeforeClass
74 public static void setUpBeforeClass() throws Exception {
Jonathan Hart2a655752015-04-07 16:46:33 -070075 deserializer = TCP.deserializer();
76
Charles M.C. Chan197a0122015-04-08 18:15:34 +080077 ipv4.setSourceAddress(IPv4.toIPv4Address(IPV4_SOURCE_ADDRESS));
78 ipv4.setDestinationAddress(IPv4.toIPv4Address(IPV4_DESTINATION_ADDRESS));
79 ipv4.setProtocol(IPv4.PROTOCOL_TCP);
80
81 ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
82 ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
83 ipv6.setNextHeader(IPv6.PROTOCOL_TCP);
84 }
85
86 /**
87 * Tests serialize and setters.
88 */
89 @Test
90 public void testSerialize() {
91 TCP tcp = new TCP();
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070092 tcp.setSourcePort(0x50);
93 tcp.setDestinationPort(0x60);
Charles M.C. Chan197a0122015-04-08 18:15:34 +080094 tcp.setSequence(0x10);
95 tcp.setAcknowledge(0x20);
96 tcp.setDataOffset((byte) 0x5);
97 tcp.setFlags((short) 0x2);
98 tcp.setWindowSize((short) 0x1000);
99 tcp.setUrgentPointer((short) 0x1);
100
101 tcp.setParent(ipv4);
102 assertArrayEquals(bytePacketTCP4, tcp.serialize());
103 tcp.resetChecksum();
104 tcp.setParent(ipv6);
105 assertArrayEquals(bytePacketTCP6, tcp.serialize());
106 }
107
Jonathan Hart2a655752015-04-07 16:46:33 -0700108 @Test
109 public void testDeserializeBadInput() throws Exception {
110 PacketTestUtils.testDeserializeBadInput(deserializer);
111 }
112
113 @Test
114 public void testDeserializeTruncated() throws Exception {
115 PacketTestUtils.testDeserializeTruncated(deserializer, bytePacketTCP4);
116 }
117
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800118 /**
119 * Tests deserialize and getters.
120 */
121 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700122 public void testDeserialize() throws Exception {
123 TCP tcp = deserializer.deserialize(bytePacketTCP4, 0, bytePacketTCP4.length);
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800124
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700125 assertThat(tcp.getSourcePort(), is(0x50));
126 assertThat(tcp.getDestinationPort(), is(0x60));
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800127 assertThat(tcp.getSequence(), is(0x10));
128 assertThat(tcp.getAcknowledge(), is(0x20));
129 assertThat(tcp.getDataOffset(), is((byte) 0x5));
130 assertThat(tcp.getFlags(), is((short) 0x2));
131 assertThat(tcp.getWindowSize(), is((short) 0x1000));
132 assertThat(tcp.getUrgentPointer(), is((short) 0x1));
133 assertThat(tcp.getChecksum(), is((short) 0x1bae));
134 }
135
136 /**
137 * Tests comparator.
138 */
139 @Test
140 public void testEqual() {
141 TCP tcp1 = new TCP();
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700142 tcp1.setSourcePort(0x50);
143 tcp1.setDestinationPort(0x60);
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800144 tcp1.setSequence(0x10);
145 tcp1.setAcknowledge(0x20);
146 tcp1.setDataOffset((byte) 0x5);
147 tcp1.setFlags((short) 0x2);
148 tcp1.setWindowSize((short) 0x1000);
149 tcp1.setUrgentPointer((short) 0x1);
150
151 TCP tcp2 = new TCP();
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700152 tcp2.setSourcePort(0x70);
153 tcp2.setDestinationPort(0x60);
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800154 tcp2.setSequence(0x10);
155 tcp2.setAcknowledge(0x20);
156 tcp2.setDataOffset((byte) 0x5);
157 tcp2.setFlags((short) 0x2);
158 tcp2.setWindowSize((short) 0x1000);
159 tcp2.setUrgentPointer((short) 0x1);
160
161 assertTrue(tcp1.equals(tcp1));
162 assertFalse(tcp1.equals(tcp2));
163 }
Jian Li5fc14292015-12-04 11:30:46 -0800164
165 /**
166 * Tests toString.
167 */
168 @Test
169 public void testToStringTcp() throws Exception {
170 TCP tcp = deserializer.deserialize(bytePacketTCP4, 0, bytePacketTCP4.length);
171 String str = tcp.toString();
172
173 assertTrue(StringUtils.contains(str, "sourcePort=" + 0x50));
174 assertTrue(StringUtils.contains(str, "destinationPort=" + 0x60));
175 assertTrue(StringUtils.contains(str, "sequence=" + 0x10));
176 assertTrue(StringUtils.contains(str, "acknowledge=" + 0x20));
177 assertTrue(StringUtils.contains(str, "dataOffset=" + (byte) 0x5));
178 assertTrue(StringUtils.contains(str, "flags=" + (short) 0x2));
179 assertTrue(StringUtils.contains(str, "windowSize=" + (short) 0x1000));
180 assertTrue(StringUtils.contains(str, "checksum=" + (short) 0x1bae));
181 assertTrue(StringUtils.contains(str, "urgentPointer=" + (short) 0x1));
182 }
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800183}