blob: 18f532e2b3299a241fa725d6945c1d44ac5119bd [file] [log] [blame]
Charles M.C. Chan197a0122015-04-08 18:15:34 +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
17
18
19package org.onlab.packet;
20
21import org.junit.BeforeClass;
22import org.junit.Test;
23
24import static org.hamcrest.Matchers.is;
25import static org.junit.Assert.assertArrayEquals;
26import static org.junit.Assert.assertFalse;
27import static org.junit.Assert.assertThat;
28import static org.junit.Assert.assertTrue;
29
30/**
31 * Tests for class {@link TCP}.
32 */
33public class TCPTest {
34 private static final byte[] IPV4_SOURCE_ADDRESS = {
35 (byte) 192, (byte) 168, (byte) 1, (byte) 1
36 };
37 private static final byte[] IPV4_DESTINATION_ADDRESS = {
38 (byte) 192, (byte) 168, (byte) 1, (byte) 2
39 };
40 private static final byte[] IPV6_SOURCE_ADDRESS = {
41 (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
42 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
43 };
44 private static final byte[] IPV6_DESTINATION_ADDRESS = {
45 (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
46 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02
47 };
48
49 private static IPv4 ipv4 = new IPv4();
50 private static IPv6 ipv6 = new IPv6();
51 private static byte[] bytePacketTCP4 = {
52 (byte) 0x00, (byte) 0x50, (byte) 0x00, (byte) 0x60, // src,dst port
53 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10, // seq
54 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, // ack
55 (byte) 0x50, (byte) 0x02, // offset,flag
56 (byte) 0x10, (byte) 0x00, // window
57 (byte) 0x1b, (byte) 0xae, // checksum
58 (byte) 0x00, (byte) 0x01 // urgent
59 };
60 private static byte[] bytePacketTCP6 = {
61 (byte) 0x00, (byte) 0x50, (byte) 0x00, (byte) 0x60, // src,dst port
62 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10, // seq
63 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, // ack
64 (byte) 0x50, (byte) 0x02, // offset,flag
65 (byte) 0x10, (byte) 0x00, // window
66 (byte) 0xa1, (byte) 0xfd, // checksum
67 (byte) 0x00, (byte) 0x01 // urgent
68 };
69
Jonathan Hart2a655752015-04-07 16:46:33 -070070 private static Deserializer<TCP> deserializer;
71
Charles M.C. Chan197a0122015-04-08 18:15:34 +080072 @BeforeClass
73 public static void setUpBeforeClass() throws Exception {
Jonathan Hart2a655752015-04-07 16:46:33 -070074 deserializer = TCP.deserializer();
75
Charles M.C. Chan197a0122015-04-08 18:15:34 +080076 ipv4.setSourceAddress(IPv4.toIPv4Address(IPV4_SOURCE_ADDRESS));
77 ipv4.setDestinationAddress(IPv4.toIPv4Address(IPV4_DESTINATION_ADDRESS));
78 ipv4.setProtocol(IPv4.PROTOCOL_TCP);
79
80 ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
81 ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
82 ipv6.setNextHeader(IPv6.PROTOCOL_TCP);
83 }
84
85 /**
86 * Tests serialize and setters.
87 */
88 @Test
89 public void testSerialize() {
90 TCP tcp = new TCP();
91 tcp.setSourcePort((short) 0x50);
92 tcp.setDestinationPort((short) 0x60);
93 tcp.setSequence(0x10);
94 tcp.setAcknowledge(0x20);
95 tcp.setDataOffset((byte) 0x5);
96 tcp.setFlags((short) 0x2);
97 tcp.setWindowSize((short) 0x1000);
98 tcp.setUrgentPointer((short) 0x1);
99
100 tcp.setParent(ipv4);
101 assertArrayEquals(bytePacketTCP4, tcp.serialize());
102 tcp.resetChecksum();
103 tcp.setParent(ipv6);
104 assertArrayEquals(bytePacketTCP6, tcp.serialize());
105 }
106
Jonathan Hart2a655752015-04-07 16:46:33 -0700107 @Test
108 public void testDeserializeBadInput() throws Exception {
109 PacketTestUtils.testDeserializeBadInput(deserializer);
110 }
111
112 @Test
113 public void testDeserializeTruncated() throws Exception {
114 PacketTestUtils.testDeserializeTruncated(deserializer, bytePacketTCP4);
115 }
116
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800117 /**
118 * Tests deserialize and getters.
119 */
120 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700121 public void testDeserialize() throws Exception {
122 TCP tcp = deserializer.deserialize(bytePacketTCP4, 0, bytePacketTCP4.length);
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800123
124 assertThat(tcp.getSourcePort(), is((short) 0x50));
125 assertThat(tcp.getDestinationPort(), is((short) 0x60));
126 assertThat(tcp.getSequence(), is(0x10));
127 assertThat(tcp.getAcknowledge(), is(0x20));
128 assertThat(tcp.getDataOffset(), is((byte) 0x5));
129 assertThat(tcp.getFlags(), is((short) 0x2));
130 assertThat(tcp.getWindowSize(), is((short) 0x1000));
131 assertThat(tcp.getUrgentPointer(), is((short) 0x1));
132 assertThat(tcp.getChecksum(), is((short) 0x1bae));
133 }
134
135 /**
136 * Tests comparator.
137 */
138 @Test
139 public void testEqual() {
140 TCP tcp1 = new TCP();
141 tcp1.setSourcePort((short) 0x50);
142 tcp1.setDestinationPort((short) 0x60);
143 tcp1.setSequence(0x10);
144 tcp1.setAcknowledge(0x20);
145 tcp1.setDataOffset((byte) 0x5);
146 tcp1.setFlags((short) 0x2);
147 tcp1.setWindowSize((short) 0x1000);
148 tcp1.setUrgentPointer((short) 0x1);
149
150 TCP tcp2 = new TCP();
151 tcp2.setSourcePort((short) 0x70);
152 tcp2.setDestinationPort((short) 0x60);
153 tcp2.setSequence(0x10);
154 tcp2.setAcknowledge(0x20);
155 tcp2.setDataOffset((byte) 0x5);
156 tcp2.setFlags((short) 0x2);
157 tcp2.setWindowSize((short) 0x1000);
158 tcp2.setUrgentPointer((short) 0x1);
159
160 assertTrue(tcp1.equals(tcp1));
161 assertFalse(tcp1.equals(tcp2));
162 }
163}