| 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.tracecompass.pcap.core.tests.file; |
| 14 | |
| 15 | import static org.junit.Assert.assertEquals; |
| 16 | import static org.junit.Assert.fail; |
| 17 | import static org.junit.Assume.assumeTrue; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | |
| 21 | import org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException; |
| 22 | import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile; |
| 23 | import org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace; |
| 24 | import org.junit.Test; |
| 25 | |
| 26 | /** |
| 27 | * JUnit Class that tests the opening of non-valid pcap files. |
| 28 | * |
| 29 | * @author Vincent Perot |
| 30 | */ |
| 31 | public class PcapFileOpenFailTest { |
| 32 | |
| 33 | /** |
| 34 | * Test that tries to open a pcap with a bad magic number |
| 35 | * |
| 36 | * @throws IOException |
| 37 | * Thrown when an IO error occurs. Fails the test. |
| 38 | */ |
| 39 | @Test |
| 40 | public void FileOpenBadPcapTest() throws IOException { |
| 41 | PcapTestTrace trace = PcapTestTrace.BAD_PCAPFILE; |
| 42 | assumeTrue(trace.exists()); |
| 43 | |
| 44 | try (PcapFile file = new PcapFile(trace.getPath());) { |
| 45 | fail("The pcap was accepted even though the magic number is invalid!"); |
| 46 | } catch (BadPcapFileException e) { |
| 47 | assertEquals("c3d4a1b2 is not a known magic number.", e.getMessage()); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Test that tries to open a non-pcap binary file |
| 53 | * |
| 54 | * @throws IOException |
| 55 | * Thrown when an IO error occurs. Fails the test. |
| 56 | */ |
| 57 | @Test |
| 58 | public void FileOpenBinaryFile() throws IOException { |
| 59 | PcapTestTrace trace = PcapTestTrace.KERNEL_TRACE; |
| 60 | assumeTrue(trace.exists()); |
| 61 | |
| 62 | try (PcapFile file = new PcapFile(trace.getPath());) { |
| 63 | fail("The file was accepted even though it is not a pcap file!"); |
| 64 | } catch (BadPcapFileException e) { |
| 65 | assertEquals("c11ffcc1 is not a known magic number.", e.getMessage()); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Test that tries to open a directory |
| 71 | * |
| 72 | * @throws IOException |
| 73 | * Thrown when an IO error occurs. Fails the test. |
| 74 | */ |
| 75 | @Test |
| 76 | public void FileOpenDirectory() throws IOException { |
| 77 | PcapTestTrace trace = PcapTestTrace.KERNEL_DIRECTORY; |
| 78 | assumeTrue(trace.exists()); |
| 79 | |
| 80 | try (PcapFile file = new PcapFile(trace.getPath());) { |
| 81 | fail("The file was accepted even though it is not a pcap file!"); |
| 82 | } catch (BadPcapFileException e) { |
| 83 | assertEquals("Bad Pcap File.", e.getMessage()); |
| 84 | } |
| 85 | } |
| 86 | } |