pcap: Add unit tests to pcap.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.pcap.core.tests / src / org / eclipse / linuxtools / pcap / core / tests / file / PcapFileOpenFailTest.java
CommitLineData
a1d21447
VP
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
13package org.eclipse.linuxtools.pcap.core.tests.file;
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.fail;
17import static org.junit.Assume.assumeTrue;
18
19import java.io.IOException;
20
21import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
22import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
23import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
24import org.junit.Test;
25
26/**
27 * JUnit Class that tests the opening of non-valid pcap files.
28 *
29 * @author Vincent Perot
30 */
31public 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 String path = trace.getPath();
45 try (PcapFile file = new PcapFile(path);) {
46 fail("The pcap was accepted even though the magic number is invalid!");
47 } catch (BadPcapFileException e) {
48 assertEquals("c3d4a1b2 is not a known magic number.", e.getMessage());
49 }
50 }
51
52 /**
53 * Test that tries to open a non-pcap binary file
54 *
55 * @throws IOException
56 * Thrown when an IO error occurs. Fails the test.
57 */
58 @Test
59 public void FileOpenBinaryFile() throws IOException {
60 PcapTestTrace trace = PcapTestTrace.KERNEL_TRACE;
61 assumeTrue(trace.exists());
62
63 String path = trace.getPath();
64 try (PcapFile file = new PcapFile(path);) {
65 fail("The file was accepted even though it is not a pcap file!");
66 } catch (BadPcapFileException e) {
67 assertEquals("Bad Pcap File.", e.getMessage());
68 }
69 }
70
71 /**
72 * Test that tries to open a directory
73 *
74 * @throws IOException
75 * Thrown when an IO error occurs. Fails the test.
76 */
77 @Test
78 public void FileOpenDirectory() throws IOException {
79 PcapTestTrace trace = PcapTestTrace.KERNEL_DIRECTORY;
80 assumeTrue(trace.exists());
81
82 String path = trace.getPath();
83 try (PcapFile file = new PcapFile(path);) {
84 fail("The file was accepted even though it is not a pcap file!");
85 } catch (BadPcapFileException e) {
86 assertEquals("Bad Pcap File.", e.getMessage());
87 }
88 }
89}
This page took 0.02772 seconds and 5 git commands to generate.