Charles M.C. Chan
Committed by Ray Milkey

Refactor org.onlab.packet.{TCP,UDP,ICMP6}

- ONOS-1012: Fix TCP checksum when using IPv6
- ONOS-1013: Fix UDP checksum when using IPv6
- ONOS-1593: Remove get/setTcpChecksum
- Remove unnecessary parameter of getUrgentPointer() in TCP
- Complete javadoc for TCP
- Add unit test for {TCP,UDP,ICMP6}

Change-Id: Iad5eeb35812ede6764a9a9a4a57b9837e5ea5dd6
1 /* 1 /*
2 - * Copyright 2014 Open Networking Laboratory 2 + * Copyright 2014-2015 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -34,6 +34,8 @@ import java.util.Map; ...@@ -34,6 +34,8 @@ import java.util.Map;
34 public class ICMP6 extends BasePacket { 34 public class ICMP6 extends BasePacket {
35 public static final byte HEADER_LENGTH = 4; // bytes 35 public static final byte HEADER_LENGTH = 4; // bytes
36 36
37 + public static final byte ECHO_REQUEST = (byte) 0x80;
38 + public static final byte ECHO_REPLY = (byte) 0x81;
37 public static final byte ROUTER_SOLICITATION = (byte) 0x85; 39 public static final byte ROUTER_SOLICITATION = (byte) 0x85;
38 public static final byte ROUTER_ADVERTISEMENT = (byte) 0x86; 40 public static final byte ROUTER_ADVERTISEMENT = (byte) 0x86;
39 public static final byte NEIGHBOR_SOLICITATION = (byte) 0x87; 41 public static final byte NEIGHBOR_SOLICITATION = (byte) 0x87;
...@@ -149,8 +151,8 @@ public class ICMP6 extends BasePacket { ...@@ -149,8 +151,8 @@ public class ICMP6 extends BasePacket {
149 } 151 }
150 } 152 }
151 if (ipv6Parent != null) { 153 if (ipv6Parent != null) {
152 - bbChecksum.put(((IPv6) ipv6Parent).getSourceAddress()); 154 + bbChecksum.put(ipv6Parent.getSourceAddress());
153 - bbChecksum.put(((IPv6) ipv6Parent).getDestinationAddress()); 155 + bbChecksum.put(ipv6Parent.getDestinationAddress());
154 } else { 156 } else {
155 // NOTE: IPv6 source and destination addresses unknown. Use zeroes. 157 // NOTE: IPv6 source and destination addresses unknown. Use zeroes.
156 bbChecksum.put(ZERO_ADDRESS); 158 bbChecksum.put(ZERO_ADDRESS);
......
1 /* 1 /*
2 - * Copyright 2014 Open Networking Laboratory 2 + * Copyright 2014-2015 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -21,7 +21,7 @@ package org.onlab.packet; ...@@ -21,7 +21,7 @@ package org.onlab.packet;
21 import java.nio.ByteBuffer; 21 import java.nio.ByteBuffer;
22 22
23 /** 23 /**
24 - * 24 + * Implements TCP packet format.
25 */ 25 */
26 26
27 public class TCP extends BasePacket { 27 public class TCP extends BasePacket {
...@@ -37,15 +37,18 @@ public class TCP extends BasePacket { ...@@ -37,15 +37,18 @@ public class TCP extends BasePacket {
37 protected byte[] options; 37 protected byte[] options;
38 38
39 /** 39 /**
40 - * @return the sourcePort 40 + * Gets TCP source port.
41 + *
42 + * @return TCP source port
41 */ 43 */
42 public short getSourcePort() { 44 public short getSourcePort() {
43 return this.sourcePort; 45 return this.sourcePort;
44 } 46 }
45 47
46 /** 48 /**
47 - * @param sourcePort 49 + * Sets TCP source port.
48 - * the sourcePort to set 50 + *
51 + * @param sourcePort the sourcePort to set
49 * @return this 52 * @return this
50 */ 53 */
51 public TCP setSourcePort(final short sourcePort) { 54 public TCP setSourcePort(final short sourcePort) {
...@@ -54,6 +57,8 @@ public class TCP extends BasePacket { ...@@ -54,6 +57,8 @@ public class TCP extends BasePacket {
54 } 57 }
55 58
56 /** 59 /**
60 + * Gets TCP destination port.
61 + *
57 * @return the destinationPort 62 * @return the destinationPort
58 */ 63 */
59 public short getDestinationPort() { 64 public short getDestinationPort() {
...@@ -61,8 +66,9 @@ public class TCP extends BasePacket { ...@@ -61,8 +66,9 @@ public class TCP extends BasePacket {
61 } 66 }
62 67
63 /** 68 /**
64 - * @param destinationPort 69 + * Sets TCP destination port.
65 - * the destinationPort to set 70 + *
71 + * @param destinationPort the destinationPort to set
66 * @return this 72 * @return this
67 */ 73 */
68 public TCP setDestinationPort(final short destinationPort) { 74 public TCP setDestinationPort(final short destinationPort) {
...@@ -71,98 +77,169 @@ public class TCP extends BasePacket { ...@@ -71,98 +77,169 @@ public class TCP extends BasePacket {
71 } 77 }
72 78
73 /** 79 /**
80 + * Gets checksum.
81 + *
74 * @return the checksum 82 * @return the checksum
75 */ 83 */
76 public short getChecksum() { 84 public short getChecksum() {
77 return this.checksum; 85 return this.checksum;
78 } 86 }
79 87
88 + /**
89 + * Sets checksum.
90 + *
91 + * @param checksum the checksum to set
92 + * @return this
93 + */
94 + public TCP setChecksum(final short checksum) {
95 + this.checksum = checksum;
96 + return this;
97 + }
98 +
99 + /**
100 + * Gets sequence number.
101 + *
102 + * @return the sequence number
103 + */
80 public int getSequence() { 104 public int getSequence() {
81 return this.sequence; 105 return this.sequence;
82 } 106 }
83 107
108 + /**
109 + * Sets sequence number.
110 + *
111 + * @param seq the sequence number to set
112 + * @return this
113 + */
84 public TCP setSequence(final int seq) { 114 public TCP setSequence(final int seq) {
85 this.sequence = seq; 115 this.sequence = seq;
86 return this; 116 return this;
87 } 117 }
88 118
119 + /**
120 + * Gets acknowledge number.
121 + *
122 + * @return the acknowledge number
123 + */
89 public int getAcknowledge() { 124 public int getAcknowledge() {
90 return this.acknowledge; 125 return this.acknowledge;
91 } 126 }
92 127
128 + /**
129 + * Sets acknowledge number.
130 + *
131 + * @param ack the acknowledge number to set
132 + * @return this
133 + */
93 public TCP setAcknowledge(final int ack) { 134 public TCP setAcknowledge(final int ack) {
94 this.acknowledge = ack; 135 this.acknowledge = ack;
95 return this; 136 return this;
96 } 137 }
97 138
139 + /**
140 + * Gets offset.
141 + *
142 + * @return the offset
143 + */
98 public byte getDataOffset() { 144 public byte getDataOffset() {
99 return this.dataOffset; 145 return this.dataOffset;
100 } 146 }
101 147
148 + /**
149 + * Sets offset.
150 + *
151 + * @param offset the offset to set
152 + * @return this
153 + */
102 public TCP setDataOffset(final byte offset) { 154 public TCP setDataOffset(final byte offset) {
103 this.dataOffset = offset; 155 this.dataOffset = offset;
104 return this; 156 return this;
105 } 157 }
106 158
159 + /**
160 + * Gets TCP flags.
161 + *
162 + * @return the TCP flags
163 + */
107 public short getFlags() { 164 public short getFlags() {
108 return this.flags; 165 return this.flags;
109 } 166 }
110 167
168 + /**
169 + * Sets TCP flags.
170 + *
171 + * @param flags the TCP flags to set
172 + * @return this
173 + */
111 public TCP setFlags(final short flags) { 174 public TCP setFlags(final short flags) {
112 this.flags = flags; 175 this.flags = flags;
113 return this; 176 return this;
114 } 177 }
115 178
179 + /**
180 + * Gets TCP window size.
181 + *
182 + * @return the TCP window size
183 + */
116 public short getWindowSize() { 184 public short getWindowSize() {
117 return this.windowSize; 185 return this.windowSize;
118 } 186 }
119 187
188 + /**
189 + * Sets TCP window size.
190 + *
191 + * @param windowSize the TCP window size to set
192 + * @return this
193 + */
120 public TCP setWindowSize(final short windowSize) { 194 public TCP setWindowSize(final short windowSize) {
121 this.windowSize = windowSize; 195 this.windowSize = windowSize;
122 return this; 196 return this;
123 } 197 }
124 198
125 - public short getTcpChecksum() {
126 - return this.checksum;
127 - }
128 -
129 - public TCP setTcpChecksum(final short checksum) {
130 - this.checksum = checksum;
131 - return this;
132 - }
133 -
134 @Override 199 @Override
135 public void resetChecksum() { 200 public void resetChecksum() {
136 this.checksum = 0; 201 this.checksum = 0;
137 super.resetChecksum(); 202 super.resetChecksum();
138 } 203 }
139 204
140 - public short getUrgentPointer(final short urgentPointer) { 205 + /**
206 + * Gets urgent pointer.
207 + *
208 + * @return the urgent pointer
209 + */
210 + public short getUrgentPointer() {
141 return this.urgentPointer; 211 return this.urgentPointer;
142 } 212 }
143 213
214 + /**
215 + * Sets urgent pointer.
216 + *
217 + * @param urgentPointer the urgent pointer to set
218 + * @return this
219 + */
144 public TCP setUrgentPointer(final short urgentPointer) { 220 public TCP setUrgentPointer(final short urgentPointer) {
145 this.urgentPointer = urgentPointer; 221 this.urgentPointer = urgentPointer;
146 return this; 222 return this;
147 } 223 }
148 224
225 + /**
226 + * Gets TCP options.
227 + *
228 + * @return the TCP options
229 + */
149 public byte[] getOptions() { 230 public byte[] getOptions() {
150 return this.options; 231 return this.options;
151 } 232 }
152 233
153 - public TCP setOptions(final byte[] options) {
154 - this.options = options;
155 - this.dataOffset = (byte) (20 + options.length + 3 >> 2);
156 - return this;
157 - }
158 -
159 /** 234 /**
160 - * @param checksum 235 + * Sets TCP options.
161 - * the checksum to set 236 + *
237 + * @param options the options to set
162 * @return this 238 * @return this
163 */ 239 */
164 - public TCP setChecksum(final short checksum) { 240 + public TCP setOptions(final byte[] options) {
165 - this.checksum = checksum; 241 + this.options = options;
242 + this.dataOffset = (byte) (20 + options.length + 3 >> 2);
166 return this; 243 return this;
167 } 244 }
168 245
...@@ -218,14 +295,32 @@ public class TCP extends BasePacket { ...@@ -218,14 +295,32 @@ public class TCP extends BasePacket {
218 int accumulation = 0; 295 int accumulation = 0;
219 296
220 // compute pseudo header mac 297 // compute pseudo header mac
221 - if (this.parent != null && this.parent instanceof IPv4) { 298 + if (this.parent != null) {
222 - final IPv4 ipv4 = (IPv4) this.parent; 299 + if (this.parent instanceof IPv4) {
223 - accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff) 300 + final IPv4 ipv4 = (IPv4) this.parent;
224 - + (ipv4.getSourceAddress() & 0xffff); 301 + accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff)
225 - accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff) 302 + + (ipv4.getSourceAddress() & 0xffff);
226 - + (ipv4.getDestinationAddress() & 0xffff); 303 + accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff)
227 - accumulation += ipv4.getProtocol() & 0xff; 304 + + (ipv4.getDestinationAddress() & 0xffff);
228 - accumulation += length & 0xffff; 305 + accumulation += ipv4.getProtocol() & 0xff;
306 + accumulation += length & 0xffff;
307 + } else if (this.parent instanceof IPv6) {
308 + final IPv6 ipv6 = (IPv6) this.parent;
309 + final int bbLength =
310 + Ip6Address.BYTE_LENGTH * 2 // IPv6 src, dst
311 + + 2 // nextHeader (with padding)
312 + + 4; // length
313 + final ByteBuffer bbChecksum = ByteBuffer.allocate(bbLength);
314 + bbChecksum.put(ipv6.getSourceAddress());
315 + bbChecksum.put(ipv6.getDestinationAddress());
316 + bbChecksum.put((byte) 0); // padding
317 + bbChecksum.put(ipv6.getNextHeader());
318 + bbChecksum.putInt(length);
319 + bbChecksum.rewind();
320 + for (int i = 0; i < bbLength / 2; ++i) {
321 + accumulation += 0xffff & bbChecksum.getShort();
322 + }
323 + }
229 } 324 }
230 325
231 for (int i = 0; i < length / 2; ++i) { 326 for (int i = 0; i < length / 2; ++i) {
......
1 /* 1 /*
2 - * Copyright 2014 Open Networking Laboratory 2 + * Copyright 2014-2015 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -148,14 +148,32 @@ public class UDP extends BasePacket { ...@@ -148,14 +148,32 @@ public class UDP extends BasePacket {
148 int accumulation = 0; 148 int accumulation = 0;
149 149
150 // compute pseudo header mac 150 // compute pseudo header mac
151 - if (this.parent != null && this.parent instanceof IPv4) { 151 + if (this.parent != null) {
152 - final IPv4 ipv4 = (IPv4) this.parent; 152 + if (this.parent instanceof IPv4) {
153 - accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff) 153 + final IPv4 ipv4 = (IPv4) this.parent;
154 - + (ipv4.getSourceAddress() & 0xffff); 154 + accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff)
155 - accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff) 155 + + (ipv4.getSourceAddress() & 0xffff);
156 - + (ipv4.getDestinationAddress() & 0xffff); 156 + accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff)
157 - accumulation += ipv4.getProtocol() & 0xff; 157 + + (ipv4.getDestinationAddress() & 0xffff);
158 - accumulation += this.length & 0xffff; 158 + accumulation += ipv4.getProtocol() & 0xff;
159 + accumulation += length & 0xffff;
160 + } else if (this.parent instanceof IPv6) {
161 + final IPv6 ipv6 = (IPv6) this.parent;
162 + final int bbLength =
163 + Ip6Address.BYTE_LENGTH * 2 // IPv6 src, dst
164 + + 2 // nextHeader (with padding)
165 + + 4; // length
166 + final ByteBuffer bbChecksum = ByteBuffer.allocate(bbLength);
167 + bbChecksum.put(ipv6.getSourceAddress());
168 + bbChecksum.put(ipv6.getDestinationAddress());
169 + bbChecksum.put((byte) 0); // padding
170 + bbChecksum.put(ipv6.getNextHeader());
171 + bbChecksum.putInt(length);
172 + bbChecksum.rewind();
173 + for (int i = 0; i < bbLength / 2; ++i) {
174 + accumulation += 0xffff & bbChecksum.getShort();
175 + }
176 + }
159 } 177 }
160 178
161 for (int i = 0; i < this.length / 2; ++i) { 179 for (int i = 0; i < this.length / 2; ++i) {
......
1 +/*
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 +
19 +package org.onlab.packet;
20 +
21 +import org.junit.BeforeClass;
22 +import org.junit.Test;
23 +
24 +import static org.hamcrest.Matchers.is;
25 +import static org.junit.Assert.assertArrayEquals;
26 +import static org.junit.Assert.assertFalse;
27 +import static org.junit.Assert.assertThat;
28 +import static org.junit.Assert.assertTrue;
29 +
30 +/**
31 + * Tests for class {@link ICMP6}.
32 + */
33 +public class ICMP6Test {
34 + private static final byte[] IPV6_SOURCE_ADDRESS = {
35 + (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
36 + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
37 + };
38 + private static final byte[] IPV6_DESTINATION_ADDRESS = {
39 + (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
40 + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02
41 + };
42 +
43 + private static IPv6 ipv6 = new IPv6();
44 + private static byte[] bytePacket = {
45 + ICMP6.ECHO_REQUEST, // type
46 + (byte) 0x00, // code
47 + (byte) 0x82, (byte) 0xbc, // checksum
48 + };
49 +
50 + @BeforeClass
51 + public static void setUpBeforeClass() throws Exception {
52 + ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
53 + ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
54 + ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6);
55 + }
56 +
57 + /**
58 + * Tests serialize and setters.
59 + */
60 + @Test
61 + public void testSerialize() {
62 + ICMP6 icmp6 = new ICMP6();
63 + icmp6.setIcmpType(ICMP6.ECHO_REQUEST);
64 + icmp6.setIcmpCode((byte) 0);
65 + icmp6.setParent(ipv6);
66 +
67 + assertArrayEquals(bytePacket, icmp6.serialize());
68 + }
69 +
70 + /**
71 + * Tests deserialize and getters.
72 + */
73 + @Test
74 + public void testDeserialize() {
75 + ICMP6 icmp6 = new ICMP6();
76 + icmp6.deserialize(bytePacket, 0, bytePacket.length);
77 +
78 + assertThat(icmp6.getIcmpType(), is(ICMP6.ECHO_REQUEST));
79 + assertThat(icmp6.getIcmpCode(), is((byte) 0x00));
80 + assertThat(icmp6.getChecksum(), is((short) 0x82bc));
81 + }
82 +
83 + /**
84 + * Tests comparator.
85 + */
86 + @Test
87 + public void testEqual() {
88 + ICMP6 icmp61 = new ICMP6();
89 + icmp61.setIcmpType(ICMP6.ECHO_REQUEST);
90 + icmp61.setIcmpCode((byte) 0);
91 + icmp61.setChecksum((short) 0);
92 +
93 + ICMP6 icmp62 = new ICMP6();
94 + icmp62.setIcmpType(ICMP6.ECHO_REPLY);
95 + icmp62.setIcmpCode((byte) 0);
96 + icmp62.setChecksum((short) 0);
97 +
98 + assertTrue(icmp61.equals(icmp61));
99 + assertFalse(icmp61.equals(icmp62));
100 + }
101 +}
1 +/*
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 +
19 +package org.onlab.packet;
20 +
21 +import org.junit.BeforeClass;
22 +import org.junit.Test;
23 +
24 +import static org.hamcrest.Matchers.is;
25 +import static org.junit.Assert.assertArrayEquals;
26 +import static org.junit.Assert.assertFalse;
27 +import static org.junit.Assert.assertThat;
28 +import static org.junit.Assert.assertTrue;
29 +
30 +/**
31 + * Tests for class {@link TCP}.
32 + */
33 +public 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 +}
1 +/*
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 +
19 +package org.onlab.packet;
20 +
21 +import org.junit.BeforeClass;
22 +import org.junit.Test;
23 +
24 +import static org.hamcrest.Matchers.is;
25 +import static org.junit.Assert.assertArrayEquals;
26 +import static org.junit.Assert.assertFalse;
27 +import static org.junit.Assert.assertThat;
28 +import static org.junit.Assert.assertTrue;
29 +
30 +/**
31 + * Tests for class {@link UDP}.
32 + */
33 +public class UDPTest {
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[] bytePacketUDP4 = {
52 + (byte) 0x00, (byte) 0x50, // src port
53 + (byte) 0x00, (byte) 0x60, // dst port
54 + (byte) 0x00, (byte) 0x08, // length
55 + (byte) 0x7b, (byte) 0xda, // checksum
56 + };
57 + private static byte[] bytePacketUDP6 = {
58 + (byte) 0x00, (byte) 0x50, // src port
59 + (byte) 0x00, (byte) 0x60, // dst port
60 + (byte) 0x00, (byte) 0x08, // length
61 + (byte) 0x02, (byte) 0x2a, // checksum
62 + };
63 +
64 + @BeforeClass
65 + public static void setUpBeforeClass() throws Exception {
66 + ipv4.setSourceAddress(IPv4.toIPv4Address(IPV4_SOURCE_ADDRESS));
67 + ipv4.setDestinationAddress(IPv4.toIPv4Address(IPV4_DESTINATION_ADDRESS));
68 + ipv4.setProtocol(IPv4.PROTOCOL_UDP);
69 +
70 + ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
71 + ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
72 + ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
73 + }
74 +
75 + /**
76 + * Tests serialize and setters.
77 + */
78 + @Test
79 + public void testSerialize() {
80 + UDP udp = new UDP();
81 + udp.setSourcePort((short) 0x50);
82 + udp.setDestinationPort((short) 0x60);
83 +
84 + udp.setParent(ipv4);
85 + assertArrayEquals(bytePacketUDP4, udp.serialize());
86 + udp.resetChecksum();
87 + udp.setParent(ipv6);
88 + assertArrayEquals(bytePacketUDP6, udp.serialize());
89 + }
90 +
91 + /**
92 + * Tests deserialize and getters.
93 + */
94 + @Test
95 + public void testDeserialize() {
96 + UDP udp = new UDP();
97 + udp.deserialize(bytePacketUDP4, 0, bytePacketUDP4.length);
98 +
99 + assertThat(udp.getSourcePort(), is((short) 0x50));
100 + assertThat(udp.getDestinationPort(), is((short) 0x60));
101 + assertThat(udp.getLength(), is((short) 8));
102 + assertThat(udp.getChecksum(), is((short) 0x7bda));
103 + }
104 +
105 + /**
106 + * Tests comparator.
107 + */
108 + @Test
109 + public void testEqual() {
110 + UDP udp1 = new UDP();
111 + udp1.setSourcePort((short) 0x50);
112 + udp1.setDestinationPort((short) 0x60);
113 +
114 + UDP udp2 = new UDP();
115 + udp2.setSourcePort((short) 0x70);
116 + udp2.setDestinationPort((short) 0x60);
117 +
118 + assertTrue(udp1.equals(udp1));
119 + assertFalse(udp1.equals(udp2));
120 + }
121 +}