blob: ba453f61db7eb25b469a7bd91da8fb215fcf802b [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 UDP}.
32 */
33public 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
Jonathan Hart2a655752015-04-07 16:46:33 -070064 private static Deserializer<UDP> deserializer;
65
Charles M.C. Chan197a0122015-04-08 18:15:34 +080066 @BeforeClass
67 public static void setUpBeforeClass() throws Exception {
Jonathan Hart2a655752015-04-07 16:46:33 -070068 deserializer = UDP.deserializer();
69
Charles M.C. Chan197a0122015-04-08 18:15:34 +080070 ipv4.setSourceAddress(IPv4.toIPv4Address(IPV4_SOURCE_ADDRESS));
71 ipv4.setDestinationAddress(IPv4.toIPv4Address(IPV4_DESTINATION_ADDRESS));
72 ipv4.setProtocol(IPv4.PROTOCOL_UDP);
73
74 ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
75 ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
76 ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
77 }
78
79 /**
80 * Tests serialize and setters.
81 */
82 @Test
83 public void testSerialize() {
84 UDP udp = new UDP();
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070085 udp.setSourcePort(0x50);
86 udp.setDestinationPort(0x60);
Charles M.C. Chan197a0122015-04-08 18:15:34 +080087
88 udp.setParent(ipv4);
89 assertArrayEquals(bytePacketUDP4, udp.serialize());
90 udp.resetChecksum();
91 udp.setParent(ipv6);
92 assertArrayEquals(bytePacketUDP6, udp.serialize());
93 }
94
Jonathan Hart2a655752015-04-07 16:46:33 -070095 @Test
96 public void testDeserializeBadInput() throws Exception {
97 PacketTestUtils.testDeserializeBadInput(deserializer);
98 }
99
100 @Test
101 public void testDeserializeTruncated() throws Exception {
102 PacketTestUtils.testDeserializeTruncated(deserializer, bytePacketUDP4);
103 }
104
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800105 /**
106 * Tests deserialize and getters.
107 */
108 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700109 public void testDeserialize() throws Exception {
110 UDP udp = deserializer.deserialize(bytePacketUDP4, 0, bytePacketUDP4.length);
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800111
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700112 assertThat(udp.getSourcePort(), is(0x50));
113 assertThat(udp.getDestinationPort(), is(0x60));
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800114 assertThat(udp.getLength(), is((short) 8));
115 assertThat(udp.getChecksum(), is((short) 0x7bda));
116 }
117
118 /**
119 * Tests comparator.
120 */
121 @Test
122 public void testEqual() {
123 UDP udp1 = new UDP();
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700124 udp1.setSourcePort(0x50);
125 udp1.setDestinationPort(0x60);
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800126
127 UDP udp2 = new UDP();
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700128 udp2.setSourcePort(0x70);
129 udp2.setDestinationPort(0x60);
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800130
131 assertTrue(udp1.equals(udp1));
132 assertFalse(udp1.equals(udp2));
133 }
134}