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
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.ctf
.core
.tests
.trace
;
14 import static org
.junit
.Assert
.assertEquals
;
15 import static org
.junit
.Assert
.assertNotNull
;
16 import static org
.junit
.Assert
.assertNull
;
19 import java
.io
.FileNotFoundException
;
20 import java
.io
.FileOutputStream
;
21 import java
.io
.IOException
;
22 import java
.io
.PrintWriter
;
23 import java
.nio
.ByteBuffer
;
24 import java
.nio
.ByteOrder
;
25 import java
.nio
.file
.Files
;
26 import java
.nio
.file
.Path
;
27 import java
.nio
.file
.attribute
.FileAttribute
;
28 import java
.util
.UUID
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceReader
;
33 import org
.junit
.Before
;
34 import org
.junit
.Test
;
37 * Tests growing streams
39 * @author Matthew Khouzam
42 public class CTFTraceGrowingStreamTest
{
44 private Path fCtfDirectory
;
45 private File fGrowingStream
;
46 private byte[][] fPackets
;
47 private CTFTrace fFixture
;
51 * Run before every test
53 * @throws IOException won't happen
54 * @throws CTFException won't happen
57 public void init() throws IOException
, CTFException
{
58 fCtfDirectory
= Files
.createTempDirectory("temptrace", new FileAttribute
<?
>[] {});
59 File metadata
= new File(fCtfDirectory
.toString() + "/" + "metadata");
60 fGrowingStream
= new File(fCtfDirectory
.toString() + "/" + "stream");
61 fUUID
= UUID
.randomUUID();
62 fPackets
= new byte[2][];
63 fPackets
[0] = new byte[32];
64 fPackets
[1] = new byte[32];
65 try (PrintWriter pw
= new PrintWriter(metadata
)) {
66 pw
.println("/*CTF 1.8*/");
67 pw
.println("typealias integer { size = 8; align = 8; signed = false; base = 10; } := uint8_t;");
68 pw
.println("typealias integer { size = 32; align = 32; signed = false; base = hex; } := uint32_t;");
70 pw
.println("trace {");
71 pw
.println(" major = 0;");
72 pw
.println(" minor = 1;");
73 pw
.println(" uuid = \"" + fUUID
.toString() + "\";");
74 pw
.println(" byte_order = le;");
75 pw
.println(" packet.header := struct {");
76 pw
.println(" uint32_t magic;");
77 pw
.println(" uint8_t uuid[16];");
81 pw
.println("stream {");
82 pw
.println(" packet.context := struct {");
83 pw
.println(" uint32_t packet_size;");
84 pw
.println(" uint32_t content_size;");
88 pw
.println("event {");
89 pw
.println(" name = thing;");
90 pw
.println(" fields := struct { uint32_t f; };");
95 setupPacket(fPackets
[0], 41);
96 setupPacket(fPackets
[1], 0xbab4face);
98 try (FileOutputStream fos
= new FileOutputStream(fGrowingStream
)) {
99 fos
.write(fPackets
[0]);
101 fFixture
= new CTFTrace(fCtfDirectory
.toString());
104 private void setupPacket(byte data
[], int value
) {
105 ByteBuffer bb
= ByteBuffer
.wrap(data
);
107 bb
.order(ByteOrder
.LITTLE_ENDIAN
);
108 bb
.putInt(0xc1fc1fc1);
109 bb
.order(ByteOrder
.BIG_ENDIAN
);
110 bb
.putLong(fUUID
.getMostSignificantBits());
111 bb
.putLong(fUUID
.getLeastSignificantBits());
112 bb
.order(ByteOrder
.LITTLE_ENDIAN
);
119 * Test a growing stream
121 * @throws CTFException won't happen
122 * @throws IOException won't happen
123 * @throws FileNotFoundException won't happen
126 public void testGrowingLive() throws CTFException
, FileNotFoundException
, IOException
{
127 try (CTFTraceReader reader
= new CTFTraceReader(fFixture
);) {
128 reader
.setLive(true);
129 assertEquals("0x29", reader
.getCurrentEventDef().getFields().getDefinition("f").toString());
131 try (FileOutputStream fos
= new FileOutputStream(fGrowingStream
, true)) {
132 fos
.write(fPackets
[1]);
135 assertNotNull(reader
.getCurrentEventDef());
136 assertEquals("0xbab4face", reader
.getCurrentEventDef().getFields().getDefinition("f").toString());
141 * Test a growing stream
143 * @throws CTFException won't happen
144 * @throws IOException won't happen
145 * @throws FileNotFoundException won't happen
148 public void testGrowingNotLive() throws CTFException
, FileNotFoundException
, IOException
{
149 try (CTFTraceReader reader
= new CTFTraceReader(fFixture
);) {
150 reader
.setLive(false);
151 assertEquals("0x29", reader
.getCurrentEventDef().getFields().getDefinition("f").toString());
153 try (FileOutputStream fos
= new FileOutputStream(fGrowingStream
, true)) {
154 fos
.write(fPackets
[1]);
157 assertNull(reader
.getCurrentEventDef());