ss: Protect against special characters in state value strings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / VariantDefinitionTest.java
CommitLineData
4bd7f2db 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2013, 2014 Ericsson
4bd7f2db
AM
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
866e5b51
FC
12package org.eclipse.linuxtools.ctf.core.tests.types;
13
419f09a8 14import static org.junit.Assert.assertEquals;
866e5b51
FC
15import static org.junit.Assert.assertNotNull;
16import static org.junit.Assert.assertNull;
17
a4fa4e36 18import java.nio.ByteBuffer;
866e5b51 19import java.nio.ByteOrder;
866e5b51 20
a4fa4e36
MK
21import org.eclipse.jdt.annotation.NonNull;
22import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
23import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
024373d7 24import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
866e5b51
FC
25import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
26import org.eclipse.linuxtools.ctf.core.event.types.Definition;
27import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
28import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
29import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
024373d7 30import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
866e5b51
FC
31import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
32import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
33import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
024373d7 34import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
866e5b51
FC
35import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
36import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
37import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
38import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
39import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
a4fa4e36 40import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
866e5b51
FC
41import org.junit.Before;
42import org.junit.Test;
43
a4fa4e36
MK
44import com.google.common.collect.ImmutableList;
45
866e5b51
FC
46/**
47 * The class <code>VariantDefinitionTest</code> contains tests for the class
48 * <code>{@link VariantDefinition}</code>.
284fdee8 49 *
866e5b51
FC
50 * @author ematkho
51 * @version $Revision: 1.0 $
52 */
53public class VariantDefinitionTest {
54
55 private VariantDefinition fixture;
56
a4fa4e36 57 StructDefinition fStructDefinition;
163354ef
MK
58 private static final @NonNull String TEST_STRUCT_ID = "testStruct";
59 private static final @NonNull String ENUM_7 = "g";
60 private static final @NonNull String ENUM_6 = "f";
61 private static final @NonNull String ENUM_5 = "e";
62 private static final @NonNull String ENUM_4 = "d";
63 private static final @NonNull String ENUM_3 = "c";
64 private static final @NonNull String ENUM_2 = "b";
65 private static final @NonNull String ENUM_1 = "a";
66 private static final @NonNull String TAG_ID = "a";
67 private static final @NonNull String LENGTH_SEQ = "_len";
68 private static final @NonNull String VAR_FIELD_NAME = "var";
69 private static final @NonNull String ENUM_8 = "bbq ribs";
a34d8eee 70
866e5b51
FC
71 /**
72 * Perform pre-test initialization.
284fdee8 73 *
866e5b51 74 * Not sure it needs to be that complicated, oh well...
a4fa4e36
MK
75 *
76 * @throws CTFReaderException
77 * won't happen
866e5b51
FC
78 */
79 @Before
a4fa4e36 80 public void setUp() throws CTFReaderException {
024373d7
MK
81 StructDeclaration sDec = new StructDeclaration(12);
82 StructDeclaration smallStruct = new StructDeclaration(8);
a4fa4e36
MK
83 IntegerDeclaration iDec = IntegerDeclaration.createDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
84 IntegerDeclaration lenDec = IntegerDeclaration.createDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
024373d7
MK
85 StringDeclaration strDec = new StringDeclaration();
86 EnumDeclaration enDec = new EnumDeclaration(iDec);
024373d7
MK
87 VariantDeclaration varDec = new VariantDeclaration();
88 EnumDeclaration tagDec = new EnumDeclaration(iDec);
89 ArrayDeclaration arrDec = new ArrayDeclaration(2, iDec);
07002e0a 90 FloatDeclaration fDec = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 8);
024373d7
MK
91 tagDec.add(0, 1, ENUM_1);
92 tagDec.add(2, 3, ENUM_2);
93 tagDec.add(4, 5, ENUM_3);
024373d7
MK
94 tagDec.add(8, 9, ENUM_5);
95 tagDec.add(10, 11, ENUM_6);
96 tagDec.add(12, 13, ENUM_7);
97 varDec.addField(ENUM_4, lenDec);
98 varDec.addField(ENUM_7, fDec);
99 varDec.addField(ENUM_6, smallStruct);
100 varDec.addField(ENUM_5, enDec);
024373d7
MK
101 varDec.addField(ENUM_3, arrDec);
102 varDec.addField(ENUM_2, iDec);
103 varDec.addField(ENUM_1, strDec);
104
105 sDec.addField(TAG_ID, tagDec);
106 sDec.addField(LENGTH_SEQ, lenDec);
024373d7
MK
107
108 sDec.addField(VAR_FIELD_NAME, varDec);
109 varDec.setTag(TAG_ID);
110
a4fa4e36
MK
111 ByteBuffer byteBuffer = ByteBuffer.allocate(100);
112 BitBuffer bb = new BitBuffer(byteBuffer);
113 byteBuffer.mark();
114 byteBuffer.putInt(1);
115 byteBuffer.putInt(2);
116 byteBuffer.putInt(3);
117 byteBuffer.reset();
118 fStructDefinition = sDec.createDefinition(null, TEST_STRUCT_ID, bb);
119 fixture = (VariantDefinition) fStructDefinition.getDefinition(VAR_FIELD_NAME);
866e5b51
FC
120 }
121
866e5b51
FC
122 /**
123 * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String)
a4fa4e36
MK
124 *
125 * @throws CTFReaderException
126 * should not happen
866e5b51
FC
127 */
128 @Test
a4fa4e36 129 public void testVariantDefinition() throws CTFReaderException {
866e5b51 130 VariantDeclaration declaration = new VariantDeclaration();
4a9c1f07 131 declaration.setTag("");
866e5b51 132 VariantDeclaration variantDeclaration = new VariantDeclaration();
a4fa4e36
MK
133 variantDeclaration.addField("", new EnumDeclaration(IntegerDeclaration.INT_32B_DECL));
134 variantDeclaration.addField("a", IntegerDeclaration.INT_64B_DECL);
135 declaration.addField(ENUM_3, new StringDeclaration());
136 variantDeclaration.setTag("a");
137
138 byte[] bytes = new byte[128];
139 ByteBuffer byb = ByteBuffer.wrap(bytes);
140 byb.mark();
141 byb.putInt(0);
142 byb.putShort((short) 2);
143 byb.put(new String("hello").getBytes());
144 byb.reset();
145 BitBuffer bb = new BitBuffer(byb);
146 VariantDefinition variantDefinition = variantDeclaration.createDefinition(fStructDefinition, "field", bb);
147 EnumDeclaration declaration2 = new EnumDeclaration(IntegerDeclaration.INT_8_DECL);
148 declaration2.add(0, 2, ENUM_3);
149 EnumDefinition enumDefinition = new EnumDefinition(
150 declaration2,
151 null,
152 "a",
153 new IntegerDefinition(
154 IntegerDeclaration.INT_8_DECL,
155 null,
156 "A",
157 1
158 ));
866e5b51 159 IDefinitionScope definitionScope = new StructDefinition(
a4fa4e36
MK
160 new StructDeclaration(1L),
161 variantDefinition,
162 "",
163 ImmutableList.<String> of("", "variant"),
164 new Definition[] { enumDefinition, variantDefinition }
165 );
4a9c1f07 166 String fieldName = "";
a4fa4e36
MK
167 declaration.setTag("");
168 VariantDefinition result = declaration.createDefinition(definitionScope, fieldName, bb);
866e5b51
FC
169 assertNotNull(result);
170 }
171
172 /**
173 * Run the Definition getCurrentField() method test.
174 */
175 @Test
176 public void testGetCurrentField() {
177 Definition result = fixture.getCurrentField();
024373d7 178 assertNotNull(result);
866e5b51
FC
179 }
180
181 /**
182 * Run the String getCurrentFieldName() method test.
183 */
184 @Test
185 public void testGetCurrentFieldName() {
186 String result = fixture.getCurrentFieldName();
187 assertNotNull(result);
188 }
189
190 /**
191 * Run the VariantDeclaration getDeclaration() method test.
192 */
193 @Test
194 public void testGetDeclaration() {
195 VariantDeclaration result = fixture.getDeclaration();
196 assertNotNull(result);
197 }
198
199 /**
200 * Run the HashMap<String, Definition> getDefinitions() method test.
201 */
202 @Test
203 public void testGetDefinitions() {
a4fa4e36 204 Definition result = fixture.getCurrentField();
866e5b51
FC
205 assertNotNull(result);
206 }
207
208 /**
209 * Run the String getPath() method test.
210 */
211 @Test
212 public void testGetPath() {
a4fa4e36 213 String result = fixture.getScopePath().toString();
866e5b51
FC
214 assertNotNull(result);
215 }
216
217 /**
218 * Run the ArrayDefinition lookupArray(String) method test.
219 */
220 @Test
221 public void testLookupArray() {
024373d7 222 ArrayDefinition result = fixture.lookupArray(ENUM_3);
a4fa4e36 223 assertNull(result);
866e5b51
FC
224 }
225
226 /**
227 * Run the Definition lookupDefinition(String) method test.
228 */
229 @Test
230 public void testLookupDefinition() {
024373d7
MK
231 Definition result = fixture.lookupDefinition(ENUM_1);
232 assertNotNull(result);
a4fa4e36 233 assertEquals("a", ((EnumDefinition) result).getStringValue());
866e5b51
FC
234 }
235
236 /**
237 * Run the EnumDefinition lookupEnum(String) method test.
238 */
239 @Test
240 public void testLookupEnum() {
024373d7 241 EnumDefinition result = fixture.lookupEnum(ENUM_5);
a4fa4e36 242 assertNull(result);
866e5b51
FC
243 }
244
245 /**
246 * Run the IntegerDefinition lookupInteger(String) method test.
247 */
248 @Test
249 public void testLookupInteger() {
024373d7 250 IntegerDefinition result = fixture.lookupInteger(ENUM_2);
a4fa4e36 251 assertNull(result);
866e5b51
FC
252 }
253
254 /**
255 * Run the SequenceDefinition lookupSequence(String) method test.
256 */
257 @Test
79cb3749 258 public void testLookupSequence_1() {
024373d7 259 SequenceDefinition result = fixture.lookupSequence(ENUM_4);
866e5b51
FC
260 assertNull(result);
261 }
262
263 /**
264 * Run the StringDefinition lookupString(String) method test.
265 */
266 @Test
267 public void testLookupString() {
024373d7 268 StringDefinition result = fixture.lookupString(ENUM_1);
a4fa4e36 269 assertNull(result);
866e5b51
FC
270 }
271
272 /**
273 * Run the StructDefinition lookupStruct(String) method test.
274 */
275 @Test
276 public void testLookupStruct() {
024373d7 277 StructDefinition result = fixture.lookupStruct(ENUM_6);
a4fa4e36 278 assertNull(result);
866e5b51
FC
279 }
280
281 /**
282 * Run the VariantDefinition lookupVariant(String) method test.
283 */
284 @Test
285 public void testLookupVariant() {
024373d7 286 VariantDefinition result = fixture.lookupVariant(ENUM_8);
866e5b51
FC
287 assertNull(result);
288 }
289
419f09a8
SM
290 /**
291 * Run the String toString() method test.
292 */
293 @Test
294 public void testToString() {
295 String result = fixture.toString();
a4fa4e36 296 assertEquals("{ a = \"\" }", result);
419f09a8 297 }
866e5b51 298}
This page took 0.055902 seconds and 5 git commands to generate.