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 | |
| 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 | } |