Skip to content

Commit

Permalink
pcap: Fix unknown packet character string
Browse files Browse the repository at this point in the history
By converting the byte array to a String, the output terminates at the
first 0x00 byte encountered. Instead, iterate through every byte and
append it as a character to a string builder, replacing non-printable
characters with the middle-dot character.

[Fixed] Fix unknown packet character string

Change-Id: Ibcddb1b414ce585c0208da3a0cab81e25ff5684d
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/c/tracecompass/org.eclipse.tracecompass/+/205588
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
  • Loading branch information
PatrickTasse committed Nov 21, 2023
1 parent ee914eb commit 069221c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2019 Ericsson
* Copyright (c) 2014, 2023 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
Expand Down Expand Up @@ -45,11 +45,11 @@
public class UnknownPacketTest {

private static final Map<String, String> EXPECTED_FIELDS = ImmutableMap.of(
"Binary", "61",
"Character", "a"
"Binary", "00 01 02 31 32 33 41 42 43 61 62 63 fd fe ff",
"Character", "···123ABCabc···"
);

private static final String fToString = "Payload: 61";
private static final String fToString = "Payload: 00 01 02 31 32 33 41 42 43 61 62 63 fd fe ff";

private ByteBuffer fPacket;

Expand All @@ -58,11 +58,25 @@ public class UnknownPacketTest {
*/
@Before
public void initialize() {
fPacket = ByteBuffer.allocate(1);
fPacket = ByteBuffer.allocate(15);
fPacket.order(ByteOrder.BIG_ENDIAN);

// Payload - 1 byte
fPacket.put((byte) 97);
// Payload - 15 bytes
fPacket.put((byte) 0x00);
fPacket.put((byte) 0x01);
fPacket.put((byte) 0x02);
fPacket.put((byte) 0x31);
fPacket.put((byte) 0x32);
fPacket.put((byte) 0x33);
fPacket.put((byte) 0x41);
fPacket.put((byte) 0x42);
fPacket.put((byte) 0x43);
fPacket.put((byte) 0x61);
fPacket.put((byte) 0x62);
fPacket.put((byte) 0x63);
fPacket.put((byte) 0xfd);
fPacket.put((byte) 0xfe);
fPacket.put((byte) 0xff);

fPacket.flip();
}
Expand Down Expand Up @@ -94,22 +108,22 @@ public void CompleteUnknownPacketTest() throws IOException, BadPcapFileException

// Abstract methods Testing
assertTrue(packet.validate());
assertEquals(1089, packet.hashCode());
assertFalse(packet.equals(null));
assertEquals(new UnknownPacket(dummy, null, byteBuffer), packet);
assertEquals(new UnknownPacket(dummy, null, byteBuffer).hashCode(), packet.hashCode());

assertEquals(EXPECTED_FIELDS, packet.getFields());
assertEquals(fToString, packet.toString());
assertEquals("Len: 1 bytes", packet.getLocalSummaryString());
assertEquals("Data: 1 bytes", packet.getGlobalSummaryString());
assertEquals("Len: 15 bytes", packet.getLocalSummaryString());
assertEquals("Data: 15 bytes", packet.getGlobalSummaryString());
// TODO take care of plural form.

// Unknown Endpoints are never equal!
assertFalse(packet.getSourceEndpoint().equals(new UnknownEndpoint(packet, true)));
assertFalse(packet.getDestinationEndpoint().equals(new UnknownEndpoint(packet, false)));

fPacket.position(0);
byte[] payload = new byte[1];
byte[] payload = new byte[15];
fPacket.get(payload);
ByteBuffer payloadBB = ByteBuffer.wrap(payload);
payloadBB.flip();
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2019 Ericsson
* Copyright (c) 2014, 2023 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
Expand All @@ -14,7 +14,6 @@

package org.eclipse.tracecompass.internal.pcap.core.protocol.unknown;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Map;
Expand Down Expand Up @@ -138,12 +137,16 @@ public Map<String, String> getFields() {

Builder<String, String> builder = ImmutableMap.<@NonNull String, @NonNull String> builder()
.put("Binary", ConversionHelper.bytesToHex(array, true)); //$NON-NLS-1$
try {
String s = new String(array, "UTF-8"); //$NON-NLS-1$
builder.put("Character", s); //$NON-NLS-1$
} catch (UnsupportedEncodingException e) {
// Do nothing. The string won't be added to the map anyway.
StringBuilder sb = new StringBuilder();
for (byte b : array) {
char ch = (char) b;
if (ch >= 32 && ch < 127) {
sb.append(ch);
} else {
sb.append('·');
}
}
builder.put("Character", sb.toString()); //$NON-NLS-1$
fFields = builder.build();
return fFields;
}
Expand Down

0 comments on commit 069221c

Please sign in to comment.