1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.pcap
.core
.tests
.protocol
.udp
;
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
;
21 import java
.io
.IOException
;
22 import java
.nio
.ByteBuffer
;
23 import java
.nio
.ByteOrder
;
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
;
36 import com
.google
.common
.collect
.ImmutableMap
;
39 * JUnit Class that tests the UDPPacket class and its method.
41 * @author Vincent Perot
43 public class UDPPacketTest
{
45 private static final Map
<String
, String
> EXPECTED_FIELDS
= ImmutableMap
.of(
46 "Source Port", "18057",
47 "Destination Port", "39611",
48 "Length", "41452 bytes",
52 private static final String EXPTECTED_TOSTRING
;
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");
58 EXPTECTED_TOSTRING
= sb
.toString();
61 private ByteBuffer fPacket
;
64 * Initialize the packet.
67 public void initialize() {
68 fPacket
= ByteBuffer
.allocate(12);
69 fPacket
.order(ByteOrder
.BIG_ENDIAN
);
72 fPacket
.put((byte) 0x46);
73 fPacket
.put((byte) 0x89);
76 fPacket
.put((byte) 0x9A);
77 fPacket
.put((byte) 0xBB);
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);
85 fPacket
.put((byte) 0xFA);
86 fPacket
.put((byte) 0xAF);
89 fPacket
.put((byte) 0x99);
90 fPacket
.put((byte) 0x88);
91 fPacket
.put((byte) 0x77);
92 fPacket
.put((byte) 0x66);
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.
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!");
116 UDPPacket packet
= new UDPPacket(dummy
, null, byteBuffer
);
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
));
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
);
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());
135 assertEquals(new UDPEndpoint(packet
, true), packet
.getSourceEndpoint());
136 assertEquals(new UDPEndpoint(packet
, false), packet
.getDestinationEndpoint());
139 byte[] payload
= new byte[4];
140 fPacket
.get(payload
);
141 assertEquals(ByteBuffer
.wrap(payload
), packet
.getPayload());
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());