| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Vincent Perot - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.pcap.core.tests.protocol.udp; |
| 14 | |
| 15 | import static org.junit.Assert.assertEquals; |
| 16 | import static org.junit.Assert.assertFalse; |
| 17 | import static org.junit.Assert.assertTrue; |
| 18 | import static org.junit.Assert.fail; |
| 19 | import static org.junit.Assume.assumeTrue; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.nio.ByteBuffer; |
| 23 | import java.nio.ByteOrder; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import org.eclipse.linuxtools.internal.pcap.core.packet.BadPacketException; |
| 27 | import org.eclipse.linuxtools.internal.pcap.core.protocol.PcapProtocol; |
| 28 | import org.eclipse.linuxtools.internal.pcap.core.protocol.udp.UDPEndpoint; |
| 29 | import org.eclipse.linuxtools.internal.pcap.core.protocol.udp.UDPPacket; |
| 30 | import org.eclipse.linuxtools.internal.pcap.core.trace.BadPcapFileException; |
| 31 | import org.eclipse.linuxtools.internal.pcap.core.trace.PcapFile; |
| 32 | import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace; |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | |
| 36 | import com.google.common.collect.ImmutableMap; |
| 37 | |
| 38 | /** |
| 39 | * JUnit Class that tests the UDPPacket class and its method. |
| 40 | * |
| 41 | * @author Vincent Perot |
| 42 | */ |
| 43 | public class UDPPacketTest { |
| 44 | |
| 45 | private static final Map<String, String> EXPECTED_FIELDS = ImmutableMap.of( |
| 46 | "Source Port", "18057", |
| 47 | "Destination Port", "39611", |
| 48 | "Length", "41452 bytes", |
| 49 | "Checksum", "0xfaaf" |
| 50 | ); |
| 51 | |
| 52 | private static final String EXPTECTED_TOSTRING; |
| 53 | static { |
| 54 | StringBuilder sb = new StringBuilder(); |
| 55 | sb.append("User Datagram Protocol, Source Port: 18057, Destination Port: 39611, Length: 41452, Checksum: 64175\n"); |
| 56 | sb.append("Payload: 99 88 77 66"); |
| 57 | |
| 58 | EXPTECTED_TOSTRING = sb.toString(); |
| 59 | } |
| 60 | |
| 61 | private ByteBuffer fPacket; |
| 62 | |
| 63 | /** |
| 64 | * Initialize the packet. |
| 65 | */ |
| 66 | @Before |
| 67 | public void initialize() { |
| 68 | fPacket = ByteBuffer.allocate(12); |
| 69 | fPacket.order(ByteOrder.BIG_ENDIAN); |
| 70 | |
| 71 | // Source Port |
| 72 | fPacket.put((byte) 0x46); |
| 73 | fPacket.put((byte) 0x89); |
| 74 | |
| 75 | // Destination Port |
| 76 | fPacket.put((byte) 0x9A); |
| 77 | fPacket.put((byte) 0xBB); |
| 78 | |
| 79 | // Total length - this is randomly chosen so that we verify that the |
| 80 | // packet handles wrong total length. |
| 81 | fPacket.put((byte) 0xA1); |
| 82 | fPacket.put((byte) 0xEC); |
| 83 | |
| 84 | // Checksum |
| 85 | fPacket.put((byte) 0xFA); |
| 86 | fPacket.put((byte) 0xAF); |
| 87 | |
| 88 | // Payload - 4 bytes |
| 89 | fPacket.put((byte) 0x99); |
| 90 | fPacket.put((byte) 0x88); |
| 91 | fPacket.put((byte) 0x77); |
| 92 | fPacket.put((byte) 0x66); |
| 93 | |
| 94 | fPacket.flip(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Test that verify the correctness of the UDPPacket's methods. |
| 99 | * @throws BadPcapFileException |
| 100 | * Thrown when the file is erroneous. Fails the test. |
| 101 | * @throws IOException |
| 102 | * Thrown when an IO error occurs. Fails the test. |
| 103 | * @throws BadPacketException |
| 104 | * Thrown when a packet is erroneous. Fails the test. |
| 105 | */ |
| 106 | @Test |
| 107 | public void CompleteUDPPacketTest() throws IOException, BadPcapFileException, BadPacketException { |
| 108 | PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP; |
| 109 | assumeTrue(trace.exists()); |
| 110 | try (PcapFile dummy = new PcapFile(trace.getPath())) { |
| 111 | ByteBuffer byteBuffer = fPacket; |
| 112 | if (byteBuffer == null) { |
| 113 | fail("CompleteUDPPacketTest has failed!"); |
| 114 | return; |
| 115 | } |
| 116 | UDPPacket packet = new UDPPacket(dummy, null, byteBuffer); |
| 117 | |
| 118 | // Protocol Testing |
| 119 | assertEquals(PcapProtocol.UDP, packet.getProtocol()); |
| 120 | assertTrue(packet.hasProtocol(PcapProtocol.UDP)); |
| 121 | assertTrue(packet.hasProtocol(PcapProtocol.UNKNOWN)); |
| 122 | assertFalse(packet.hasProtocol(PcapProtocol.ETHERNET_II)); |
| 123 | |
| 124 | // Abstract methods Testing |
| 125 | assertTrue(packet.validate()); |
| 126 | assertEquals(473000225, packet.hashCode()); |
| 127 | assertFalse(packet.equals(null)); |
| 128 | assertEquals(new UDPPacket(dummy, null, byteBuffer), packet); |
| 129 | |
| 130 | assertEquals(EXPECTED_FIELDS, packet.getFields()); |
| 131 | assertEquals(EXPTECTED_TOSTRING, packet.toString()); |
| 132 | assertEquals("Src Port: 18057, Dst Port: 39611", packet.getLocalSummaryString()); |
| 133 | assertEquals("Source Port: 18057, Destination Port: 39611", packet.getGlobalSummaryString()); |
| 134 | |
| 135 | assertEquals(new UDPEndpoint(packet, true), packet.getSourceEndpoint()); |
| 136 | assertEquals(new UDPEndpoint(packet, false), packet.getDestinationEndpoint()); |
| 137 | |
| 138 | fPacket.position(8); |
| 139 | byte[] payload = new byte[4]; |
| 140 | fPacket.get(payload); |
| 141 | assertEquals(ByteBuffer.wrap(payload), packet.getPayload()); |
| 142 | |
| 143 | // Packet-specific methods Testing |
| 144 | assertEquals(0x4689, packet.getSourcePort()); |
| 145 | assertEquals(0x9ABB, packet.getDestinationPort()); |
| 146 | assertEquals(0xA1EC, packet.getTotalLength()); |
| 147 | assertEquals(0xFAAF, packet.getChecksum()); |
| 148 | |
| 149 | } |
| 150 | } |
| 151 | } |