blob: 13f822b337abd606005e8506a00ea4936f2bacf1 [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 UDP}.
33 */
34public class UDPTest {
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[] bytePacketUDP4 = {
53 (byte) 0x00, (byte) 0x50, // src port
54 (byte) 0x00, (byte) 0x60, // dst port
55 (byte) 0x00, (byte) 0x08, // length
56 (byte) 0x7b, (byte) 0xda, // checksum
57 };
58 private static byte[] bytePacketUDP6 = {
59 (byte) 0x00, (byte) 0x50, // src port
60 (byte) 0x00, (byte) 0x60, // dst port
61 (byte) 0x00, (byte) 0x08, // length
62 (byte) 0x02, (byte) 0x2a, // checksum
63 };
64
Jonathan Hart2a655752015-04-07 16:46:33 -070065 private static Deserializer<UDP> deserializer;
66
Charles M.C. Chan197a0122015-04-08 18:15:34 +080067 @BeforeClass
68 public static void setUpBeforeClass() throws Exception {
Jonathan Hart2a655752015-04-07 16:46:33 -070069 deserializer = UDP.deserializer();
70
Charles M.C. Chan197a0122015-04-08 18:15:34 +080071 ipv4.setSourceAddress(IPv4.toIPv4Address(IPV4_SOURCE_ADDRESS));
72 ipv4.setDestinationAddress(IPv4.toIPv4Address(IPV4_DESTINATION_ADDRESS));
73 ipv4.setProtocol(IPv4.PROTOCOL_UDP);
74
75 ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
76 ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
77 ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
78 }
79
80 /**
81 * Tests serialize and setters.
82 */
83 @Test
84 public void testSerialize() {
85 UDP udp = new UDP();
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070086 udp.setSourcePort(0x50);
87 udp.setDestinationPort(0x60);
Charles M.C. Chan197a0122015-04-08 18:15:34 +080088
89 udp.setParent(ipv4);
90 assertArrayEquals(bytePacketUDP4, udp.serialize());
91 udp.resetChecksum();
92 udp.setParent(ipv6);
93 assertArrayEquals(bytePacketUDP6, udp.serialize());
94 }
95
Jonathan Hart2a655752015-04-07 16:46:33 -070096 @Test
97 public void testDeserializeBadInput() throws Exception {
98 PacketTestUtils.testDeserializeBadInput(deserializer);
99 }
100
101 @Test
102 public void testDeserializeTruncated() throws Exception {
103 PacketTestUtils.testDeserializeTruncated(deserializer, bytePacketUDP4);
104 }
105
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800106 /**
107 * Tests deserialize and getters.
108 */
109 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700110 public void testDeserialize() throws Exception {
111 UDP udp = deserializer.deserialize(bytePacketUDP4, 0, bytePacketUDP4.length);
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800112
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700113 assertThat(udp.getSourcePort(), is(0x50));
114 assertThat(udp.getDestinationPort(), is(0x60));
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800115 assertThat(udp.getLength(), is((short) 8));
116 assertThat(udp.getChecksum(), is((short) 0x7bda));
117 }
118
119 /**
120 * Tests comparator.
121 */
122 @Test
123 public void testEqual() {
124 UDP udp1 = new UDP();
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700125 udp1.setSourcePort(0x50);
126 udp1.setDestinationPort(0x60);
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800127
128 UDP udp2 = new UDP();
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700129 udp2.setSourcePort(0x70);
130 udp2.setDestinationPort(0x60);
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800131
132 assertTrue(udp1.equals(udp1));
133 assertFalse(udp1.equals(udp2));
134 }
Jian Li5fc14292015-12-04 11:30:46 -0800135
136 /**
137 * Tests toString.
138 */
139 @Test
140 public void testToStringUdp() throws Exception {
141 UDP udp = deserializer.deserialize(bytePacketUDP4, 0, bytePacketUDP4.length);
142 String str = udp.toString();
143
144 assertTrue(StringUtils.contains(str, "sourcePort=" + 0x50));
145 assertTrue(StringUtils.contains(str, "destinationPort=" + 0x60));
146 assertTrue(StringUtils.contains(str, "length=" + (short) 8));
147 assertTrue(StringUtils.contains(str, "checksum=" + (short) 0x7bda));
148 }
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800149}