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 | |
| 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 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 64 | private static Deserializer<UDP> deserializer; |
| 65 | |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 66 | @BeforeClass |
| 67 | public static void setUpBeforeClass() throws Exception { |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 68 | deserializer = UDP.deserializer(); |
| 69 | |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 70 | 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 Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 85 | udp.setSourcePort(0x50); |
| 86 | udp.setDestinationPort(0x60); |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 87 | |
| 88 | udp.setParent(ipv4); |
| 89 | assertArrayEquals(bytePacketUDP4, udp.serialize()); |
| 90 | udp.resetChecksum(); |
| 91 | udp.setParent(ipv6); |
| 92 | assertArrayEquals(bytePacketUDP6, udp.serialize()); |
| 93 | } |
| 94 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 95 | @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. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 105 | /** |
| 106 | * Tests deserialize and getters. |
| 107 | */ |
| 108 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 109 | public void testDeserialize() throws Exception { |
| 110 | UDP udp = deserializer.deserialize(bytePacketUDP4, 0, bytePacketUDP4.length); |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 111 | |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 112 | assertThat(udp.getSourcePort(), is(0x50)); |
| 113 | assertThat(udp.getDestinationPort(), is(0x60)); |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 114 | 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 Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 124 | udp1.setSourcePort(0x50); |
| 125 | udp1.setDestinationPort(0x60); |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 126 | |
| 127 | UDP udp2 = new UDP(); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 128 | udp2.setSourcePort(0x70); |
| 129 | udp2.setDestinationPort(0x60); |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 130 | |
| 131 | assertTrue(udp1.equals(udp1)); |
| 132 | assertFalse(udp1.equals(udp2)); |
| 133 | } |
| 134 | } |