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
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.tracecompass
.ctf
.core
.tests
.trace
;
13 import static org
.junit
.Assert
.assertFalse
;
14 import static org
.junit
.Assert
.assertTrue
;
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
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.Metadata
;
28 import org
.junit
.Test
;
31 * There are so many it makes sense to move them to their own file
33 public class MetadataPrevalidationTests
{
35 private static final String GOOD_TSDL
= "/* CTF 1.8 */\ntrace {\n major = 1 ;\n minor = 8 ;\n byte_order = le ; \n};";
38 * Test a null should return false
40 * @throws CTFException
41 * if an exception occurs, shouldn't happen
44 public void testTraceNull() throws CTFException
{
45 assertFalse(Metadata
.preValidate(null));
49 * Test a non-existing file should return false
51 * @throws CTFException
52 * if an exception occurs, shouldn't happen
55 public void testTraceFileDoesNotExist() throws CTFException
{
56 assertFalse(Metadata
.preValidate("abcdefghijklmnopqrstuvwxyz"));
60 * Test a trace file should return false
63 * A file error occurs, shouldn't happen
64 * @throws CTFException
65 * if an exception occurs, shouldn't happen
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");
73 assertFalse(Metadata
.preValidate(f
.getAbsolutePath()));
77 * Test an empty directory should return false
80 * A file error occurs, shouldn't happen
81 * @throws CTFException
82 * if an exception occurs, shouldn't happen
85 public void testTraceDirectoryWithNoFiles() throws IOException
, CTFException
{
86 Path dir
= Files
.createTempDirectory("trace");
87 assertFalse(Metadata
.preValidate(dir
.toAbsolutePath().toString()));
91 * Test a directory with no metadata file should return false
94 * A file error occurs, shouldn't happen
95 * @throws CTFException
96 * if an exception occurs, shouldn't happen
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");
105 assertFalse(Metadata
.preValidate(dir
.toAbsolutePath().toString()));
109 * Test a valid trace with packetized little endian metadata should return
112 * @throws IOException
113 * A file error occurs, shouldn't happen
114 * @throws CTFException
115 * if an exception occurs, shouldn't happen
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()));
126 * Test a valid trace with packetized big endian metadata should return true
128 * @throws IOException
129 * A file error occurs, shouldn't happen
130 * @throws CTFException
131 * if an exception occurs, shouldn't happen
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()));
142 * Test a valid trace with text metadata should return true
144 * @throws IOException
145 * A file error occurs, shouldn't happen
146 * @throws CTFException
147 * if an exception occurs, shouldn't happen
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
);
156 assertTrue(Metadata
.preValidate(dir
.toAbsolutePath().toString()));
160 * Test a valid trace with text invalid metadata should return false
162 * @throws IOException
163 * A file error occurs, shouldn't happen
164 * @throws CTFException
165 * if an exception occurs, shouldn't happen
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())) {
173 pw
.println("trace { major =1 ; minor = 8 ; byte_order = le;};");
175 assertFalse(Metadata
.preValidate(dir
.toAbsolutePath().toString()));
179 * Test a valid trace with an empty metadata should return false
181 * @throws IOException
182 * A file error occurs, shouldn't happen
183 * @throws CTFException
184 * if an exception occurs, shouldn't happen
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()));
194 * Test a valid trace with 1 byte long metadata should return false
196 * @throws IOException
197 * A file error occurs, shouldn't happen
198 * @throws CTFException
199 * if an exception occurs, shouldn't happen
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())) {
208 assertFalse(Metadata
.preValidate(dir
.toAbsolutePath().toString()));
211 private static byte[] packetize(String body
, ByteOrder bo
) {
212 byte[] retVal
= new byte[40 + body
.length()];
213 ByteBuffer bb
= ByteBuffer
.wrap(retVal
);
215 generateMetadataPacketHeader(bb
, body
);
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());