blob: 8bc3212714065fd37d692378abf5c53acd2fdf5e [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
70 @BeforeClass
71 public static void setUpBeforeClass() throws Exception {
72 ipv4.setSourceAddress(IPv4.toIPv4Address(IPV4_SOURCE_ADDRESS));
73 ipv4.setDestinationAddress(IPv4.toIPv4Address(IPV4_DESTINATION_ADDRESS));
74 ipv4.setProtocol(IPv4.PROTOCOL_TCP);
75
76 ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
77 ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
78 ipv6.setNextHeader(IPv6.PROTOCOL_TCP);
79 }
80
81 /**
82 * Tests serialize and setters.
83 */
84 @Test
85 public void testSerialize() {
86 TCP tcp = new TCP();
87 tcp.setSourcePort((short) 0x50);
88 tcp.setDestinationPort((short) 0x60);
89 tcp.setSequence(0x10);
90 tcp.setAcknowledge(0x20);
91 tcp.setDataOffset((byte) 0x5);
92 tcp.setFlags((short) 0x2);
93 tcp.setWindowSize((short) 0x1000);
94 tcp.setUrgentPointer((short) 0x1);
95
96 tcp.setParent(ipv4);
97 assertArrayEquals(bytePacketTCP4, tcp.serialize());
98 tcp.resetChecksum();
99 tcp.setParent(ipv6);
100 assertArrayEquals(bytePacketTCP6, tcp.serialize());
101 }
102
103 /**
104 * Tests deserialize and getters.
105 */
106 @Test
107 public void testDeserialize() {
108 TCP tcp = new TCP();
109 tcp.deserialize(bytePacketTCP4, 0, bytePacketTCP4.length);
110
111 assertThat(tcp.getSourcePort(), is((short) 0x50));
112 assertThat(tcp.getDestinationPort(), is((short) 0x60));
113 assertThat(tcp.getSequence(), is(0x10));
114 assertThat(tcp.getAcknowledge(), is(0x20));
115 assertThat(tcp.getDataOffset(), is((byte) 0x5));
116 assertThat(tcp.getFlags(), is((short) 0x2));
117 assertThat(tcp.getWindowSize(), is((short) 0x1000));
118 assertThat(tcp.getUrgentPointer(), is((short) 0x1));
119 assertThat(tcp.getChecksum(), is((short) 0x1bae));
120 }
121
122 /**
123 * Tests comparator.
124 */
125 @Test
126 public void testEqual() {
127 TCP tcp1 = new TCP();
128 tcp1.setSourcePort((short) 0x50);
129 tcp1.setDestinationPort((short) 0x60);
130 tcp1.setSequence(0x10);
131 tcp1.setAcknowledge(0x20);
132 tcp1.setDataOffset((byte) 0x5);
133 tcp1.setFlags((short) 0x2);
134 tcp1.setWindowSize((short) 0x1000);
135 tcp1.setUrgentPointer((short) 0x1);
136
137 TCP tcp2 = new TCP();
138 tcp2.setSourcePort((short) 0x70);
139 tcp2.setDestinationPort((short) 0x60);
140 tcp2.setSequence(0x10);
141 tcp2.setAcknowledge(0x20);
142 tcp2.setDataOffset((byte) 0x5);
143 tcp2.setFlags((short) 0x2);
144 tcp2.setWindowSize((short) 0x1000);
145 tcp2.setUrgentPointer((short) 0x1);
146
147 assertTrue(tcp1.equals(tcp1));
148 assertFalse(tcp1.equals(tcp2));
149 }
150}