Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 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 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 21 | import org.apache.commons.lang3.StringUtils; |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 22 | import org.junit.BeforeClass; |
| 23 | import org.junit.Test; |
| 24 | |
| 25 | import static org.hamcrest.Matchers.is; |
| 26 | import static org.junit.Assert.assertArrayEquals; |
| 27 | import static org.junit.Assert.assertFalse; |
| 28 | import static org.junit.Assert.assertThat; |
| 29 | import static org.junit.Assert.assertTrue; |
| 30 | |
| 31 | /** |
| 32 | * Tests for class {@link UDP}. |
| 33 | */ |
| 34 | public 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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 65 | private static Deserializer<UDP> deserializer; |
| 66 | |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 67 | @BeforeClass |
| 68 | public static void setUpBeforeClass() throws Exception { |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 69 | deserializer = UDP.deserializer(); |
| 70 | |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 71 | 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 Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 86 | udp.setSourcePort(0x50); |
| 87 | udp.setDestinationPort(0x60); |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 88 | |
| 89 | udp.setParent(ipv4); |
| 90 | assertArrayEquals(bytePacketUDP4, udp.serialize()); |
| 91 | udp.resetChecksum(); |
| 92 | udp.setParent(ipv6); |
| 93 | assertArrayEquals(bytePacketUDP6, udp.serialize()); |
| 94 | } |
| 95 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 96 | @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. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 106 | /** |
| 107 | * Tests deserialize and getters. |
| 108 | */ |
| 109 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 110 | public void testDeserialize() throws Exception { |
| 111 | UDP udp = deserializer.deserialize(bytePacketUDP4, 0, bytePacketUDP4.length); |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 112 | |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 113 | assertThat(udp.getSourcePort(), is(0x50)); |
| 114 | assertThat(udp.getDestinationPort(), is(0x60)); |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 115 | 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 Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 125 | udp1.setSourcePort(0x50); |
| 126 | udp1.setDestinationPort(0x60); |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 127 | |
| 128 | UDP udp2 = new UDP(); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 129 | udp2.setSourcePort(0x70); |
| 130 | udp2.setDestinationPort(0x60); |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 131 | |
| 132 | assertTrue(udp1.equals(udp1)); |
| 133 | assertFalse(udp1.equals(udp2)); |
| 134 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 135 | |
| 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. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 149 | } |