7cb539d894903d0eb6ae55d0b3b79fb39fe9b098
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / trace / MetadataPrevalidationTests.java
1 /*******************************************************************************
2 * Copyright (c) 2015 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 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.tracecompass.ctf.core.tests.trace;
12
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.File;
17 import java.io.FileWriter;
18 import java.io.IOException;
19 import java.io.PrintWriter;
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.util.UUID;
25
26 import org.eclipse.tracecompass.ctf.core.CTFException;
27 import org.eclipse.tracecompass.ctf.core.trace.Metadata;
28 import org.junit.Test;
29
30 /**
31 * There are so many it makes sense to move them to their own file
32 */
33 public class MetadataPrevalidationTests {
34
35 private static final String GOOD_TSDL = "/* CTF 1.8 */\ntrace {\n major = 1 ;\n minor = 8 ;\n byte_order = le ; \n};";
36
37 /**
38 * Test a null should return false
39 *
40 * @throws CTFException
41 * if an exception occurs, shouldn't happen
42 */
43 @Test
44 public void testTraceNull() throws CTFException {
45 assertFalse(Metadata.preValidate(null));
46 }
47
48 /**
49 * Test a non-existing file should return false
50 *
51 * @throws CTFException
52 * if an exception occurs, shouldn't happen
53 */
54 @Test
55 public void testTraceFileDoesNotExist() throws CTFException {
56 assertFalse(Metadata.preValidate("abcdefghijklmnopqrstuvwxyz"));
57 }
58
59 /**
60 * Test a trace file should return false
61 *
62 * @throws IOException
63 * A file error occurs, shouldn't happen
64 * @throws CTFException
65 * if an exception occurs, shouldn't happen
66 */
67 @Test
68 public void testTraceFile() throws CTFException, IOException {
69 File f = File.createTempFile("test", ".log");
70 try (PrintWriter pw = new PrintWriter(f)) {
71 pw.println("2 hello world");
72 }
73 assertFalse(Metadata.preValidate(f.getAbsolutePath()));
74 }
75
76 /**
77 * Test an empty directory should return false
78 *
79 * @throws IOException
80 * A file error occurs, shouldn't happen
81 * @throws CTFException
82 * if an exception occurs, shouldn't happen
83 */
84 @Test
85 public void testTraceDirectoryWithNoFiles() throws IOException, CTFException {
86 Path dir = Files.createTempDirectory("trace");
87 assertFalse(Metadata.preValidate(dir.toAbsolutePath().toString()));
88 }
89
90 /**
91 * Test a directory with no metadata file should return false
92 *
93 * @throws IOException
94 * A file error occurs, shouldn't happen
95 * @throws CTFException
96 * if an exception occurs, shouldn't happen
97 */
98 @Test
99 public void testTraceDirectoryWithNoMetadataButFiles() throws CTFException, IOException {
100 Path dir = Files.createTempDirectory("trace");
101 Path f = Files.createFile(dir.resolve("metadata"));
102 try (PrintWriter pw = new PrintWriter(f.toFile())) {
103 pw.println("2 hello world");
104 }
105 assertFalse(Metadata.preValidate(dir.toAbsolutePath().toString()));
106 }
107
108 /**
109 * Test a valid trace with packetized little endian metadata should return
110 * true
111 *
112 * @throws IOException
113 * A file error occurs, shouldn't happen
114 * @throws CTFException
115 * if an exception occurs, shouldn't happen
116 */
117 @Test
118 public void testTraceDirectoryWithLittleEndianMetadata() throws CTFException, IOException {
119 Path dir = Files.createTempDirectory("trace");
120 Path f = Files.createFile(dir.resolve("metadata"));
121 Files.write(f, packetize(GOOD_TSDL, ByteOrder.BIG_ENDIAN));
122 assertTrue(Metadata.preValidate(dir.toAbsolutePath().toString()));
123 }
124
125 /**
126 * Test a valid trace with packetized big endian metadata should return true
127 *
128 * @throws IOException
129 * A file error occurs, shouldn't happen
130 * @throws CTFException
131 * if an exception occurs, shouldn't happen
132 */
133 @Test
134 public void testTraceDirectoryWithBigEndianMetadata() throws CTFException, IOException {
135 Path dir = Files.createTempDirectory("trace");
136 Path f = Files.createFile(dir.resolve("metadata"));
137 Files.write(f, packetize(GOOD_TSDL, ByteOrder.BIG_ENDIAN));
138 assertTrue(Metadata.preValidate(dir.toAbsolutePath().toString()));
139 }
140
141 /**
142 * Test a valid trace with text metadata should return true
143 *
144 * @throws IOException
145 * A file error occurs, shouldn't happen
146 * @throws CTFException
147 * if an exception occurs, shouldn't happen
148 */
149 @Test
150 public void testTraceDirectoryWithTextMetadata() throws IOException, CTFException {
151 Path dir = Files.createTempDirectory("trace");
152 Path f = Files.createFile(dir.resolve("metadata"));
153 try (PrintWriter pw = new PrintWriter(f.toFile())) {
154 pw.println(GOOD_TSDL);
155 }
156 assertTrue(Metadata.preValidate(dir.toAbsolutePath().toString()));
157 }
158
159 /**
160 * Test a valid trace with text invalid metadata should return false
161 *
162 * @throws IOException
163 * A file error occurs, shouldn't happen
164 * @throws CTFException
165 * if an exception occurs, shouldn't happen
166 */
167 @Test
168 public void testTraceDirectoryWithInvalidMetadata() throws IOException, CTFException {
169 Path dir = Files.createTempDirectory("trace");
170 Path f = Files.createFile(dir.resolve("metadata"));
171 try (PrintWriter pw = new PrintWriter(f.toFile())) {
172 // no header
173 pw.println("trace { major =1 ; minor = 8 ; byte_order = le;};");
174 }
175 assertFalse(Metadata.preValidate(dir.toAbsolutePath().toString()));
176 }
177
178 /**
179 * Test a valid trace with an empty metadata should return false
180 *
181 * @throws IOException
182 * A file error occurs, shouldn't happen
183 * @throws CTFException
184 * if an exception occurs, shouldn't happen
185 */
186 @Test
187 public void testTraceDirectoryWithEmptyMetadata() throws IOException, CTFException {
188 Path dir = Files.createTempDirectory("trace");
189 Files.createFile(dir.resolve("metadata"));
190 assertFalse(Metadata.preValidate(dir.toAbsolutePath().toString()));
191 }
192
193 /**
194 * Test a valid trace with 1 byte long metadata should return false
195 *
196 * @throws IOException
197 * A file error occurs, shouldn't happen
198 * @throws CTFException
199 * if an exception occurs, shouldn't happen
200 */
201 @Test
202 public void testTraceDirectoryWith1ByteMetadata() throws IOException, CTFException {
203 Path dir = Files.createTempDirectory("trace");
204 Path f = Files.createFile(dir.resolve("metadata"));
205 try (FileWriter pw = new FileWriter(f.toFile())) {
206 pw.append('x');
207 }
208 assertFalse(Metadata.preValidate(dir.toAbsolutePath().toString()));
209 }
210
211 private static byte[] packetize(String body, ByteOrder bo) {
212 byte[] retVal = new byte[40 + body.length()];
213 ByteBuffer bb = ByteBuffer.wrap(retVal);
214 bb.order(bo);
215 generateMetadataPacketHeader(bb, body);
216 return retVal;
217 }
218
219 private static void generateMetadataPacketHeader(ByteBuffer headerByteBuffer, String body) {
220 /* Read from the ByteBuffer */
221 headerByteBuffer.putInt(0x75D11D57);
222 final UUID randomUUID = UUID.randomUUID();
223 headerByteBuffer.putLong(randomUUID.getMostSignificantBits());
224 headerByteBuffer.putLong(randomUUID.getLeastSignificantBits());
225 headerByteBuffer.putInt(0); // checksum
226 headerByteBuffer.putInt(body.length());
227 headerByteBuffer.putInt(body.length());
228 headerByteBuffer.put((byte) 0);
229 headerByteBuffer.put((byte) 0);
230 headerByteBuffer.putInt(0);
231 headerByteBuffer.put((byte) 1);
232 headerByteBuffer.put((byte) 8);
233 headerByteBuffer.put(body.getBytes());
234 }
235 }
This page took 0.041179 seconds and 4 git commands to generate.