| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014 Ericsson |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * Vincent Perot - Initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | |
| 12 | package org.eclipse.linuxtools.pcap.core.tests.shared; |
| 13 | |
| 14 | import java.io.IOException; |
| 15 | import java.nio.file.FileSystems; |
| 16 | import java.nio.file.Files; |
| 17 | import java.nio.file.Path; |
| 18 | |
| 19 | import org.eclipse.jdt.annotation.NonNull; |
| 20 | import org.eclipse.linuxtools.internal.pcap.core.trace.BadPcapFileException; |
| 21 | import org.eclipse.linuxtools.internal.pcap.core.trace.PcapFile; |
| 22 | |
| 23 | /** |
| 24 | * Here is the list of the available test traces for the Pcap parser. |
| 25 | * |
| 26 | * @author Vincent Perot |
| 27 | */ |
| 28 | public enum PcapTestTrace { |
| 29 | |
| 30 | /** A bad pcap file. */ |
| 31 | BAD_PCAPFILE("..", "org.eclipse.linuxtools.pcap.core.tests", "rsc", "BadPcapFile.pcap"), |
| 32 | |
| 33 | /** A Valid Pcap that is empty. */ |
| 34 | EMPTY_PCAP("..", "org.eclipse.linuxtools.pcap.core.tests", "rsc", "EmptyPcap.pcap"), |
| 35 | |
| 36 | /** A Pcap that mostly contains TCP packets. */ |
| 37 | MOSTLY_TCP("..", "org.eclipse.linuxtools.pcap.core.tests", "rsc", "mostlyTCP.pcap"), |
| 38 | |
| 39 | /** A Pcap that mostly contains UDP packets. */ |
| 40 | MOSTLY_UDP("..", "org.eclipse.linuxtools.pcap.core.tests", "rsc", "mostlyUDP.pcap"), |
| 41 | |
| 42 | /** A big-endian trace that contains two packets. */ |
| 43 | SHORT_BIG_ENDIAN("..", "org.eclipse.linuxtools.pcap.core.tests", "rsc", "Short_BigEndian.pcap"), |
| 44 | |
| 45 | /** A little-endian trace that contains two packets. */ |
| 46 | SHORT_LITTLE_ENDIAN("..", "org.eclipse.linuxtools.pcap.core.tests", "rsc", "Short_LittleEndian.pcap"), |
| 47 | |
| 48 | /** A large trace for benchmarking. */ |
| 49 | BENCHMARK_TRACE("..", "org.eclipse.linuxtools.pcap.core.tests", "rsc", "benchmarkTrace.pcap"), |
| 50 | |
| 51 | /** A Kernel trace directory. */ |
| 52 | KERNEL_DIRECTORY("..", "org.eclipse.linuxtools.pcap.core.tests", "rsc", "kernel"), |
| 53 | |
| 54 | /** A Kernel trace file. */ |
| 55 | KERNEL_TRACE("..", "org.eclipse.linuxtools.pcap.core.tests", "rsc", "kernel", "channel0_0"); |
| 56 | |
| 57 | private final @NonNull Path fPath; |
| 58 | |
| 59 | private PcapTestTrace(@NonNull String first, String... more) { |
| 60 | @SuppressWarnings("null") |
| 61 | @NonNull Path path = FileSystems.getDefault().getPath(first, more); |
| 62 | fPath = path; |
| 63 | } |
| 64 | |
| 65 | /** @return The path to the test trace */ |
| 66 | public @NonNull Path getPath() { |
| 67 | return fPath; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get a Pcap Trace instance of a test trace. Make sure to call |
| 72 | * {@link #exists()} before calling this! |
| 73 | * |
| 74 | * @return The PcapFile object |
| 75 | * @throws IOException |
| 76 | * Thrown when some IO error occurs. |
| 77 | * @throws BadPcapFileException |
| 78 | * Thrown when the file is not a valid Pcap File. |
| 79 | */ |
| 80 | public PcapFile getTrace() throws BadPcapFileException, IOException { |
| 81 | return new PcapFile(fPath); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Check if this test trace actually exists on disk. |
| 86 | * |
| 87 | * @return If the trace exists |
| 88 | */ |
| 89 | public boolean exists() { |
| 90 | if (Files.notExists(fPath)) { |
| 91 | return false; |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | } |