blob: 8c786923a47d222f4ec3729dfa7f2f7c9e3d2b1b [file] [log] [blame]
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +01001package eu.mulk.jgvariant.core;
2
Matthias Andreas Benkarde5a5c752021-12-15 19:53:55 +01003import static java.nio.ByteOrder.BIG_ENDIAN;
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +01004import static java.nio.ByteOrder.LITTLE_ENDIAN;
5import static java.nio.charset.StandardCharsets.UTF_8;
Matthias Andreas Benkard55c34812021-12-14 21:51:10 +01006import static org.junit.jupiter.api.Assertions.assertAll;
7import static org.junit.jupiter.api.Assertions.assertArrayEquals;
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +01008import static org.junit.jupiter.api.Assertions.assertEquals;
Matthias Andreas Benkarde5a5c752021-12-15 19:53:55 +01009import static org.junit.jupiter.api.Assertions.assertThrows;
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010010
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010011import java.nio.ByteBuffer;
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +010012import java.text.ParseException;
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010013import java.util.List;
Matthias Andreas Benkardcd924f62021-12-28 00:46:06 +010014import java.util.Map;
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010015import java.util.Optional;
16import org.junit.jupiter.api.Test;
17
18/**
19 * Tests based on the examples given in <a
20 * href="https://people.gnome.org/~desrt/gvariant-serialisation.pdf">~desrt/gvariant-serialisation.pdf</a>.
21 */
22class DecoderTest {
23
24 @Test
25 void testString() {
26 var data = new byte[] {0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x00};
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +010027 var decoder = Decoder.ofString(UTF_8);
28 assertEquals("hello world", decoder.decode(ByteBuffer.wrap(data)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010029 }
30
31 @Test
32 void testMaybe() {
33 var data =
34 new byte[] {0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x00, 0x00};
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +010035 var decoder = Decoder.ofMaybe(Decoder.ofString(UTF_8));
36 assertEquals(Optional.of("hello world"), decoder.decode(ByteBuffer.wrap(data)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010037 }
38
39 @Test
40 void testBooleanArray() {
41 var data = new byte[] {0x01, 0x00, 0x00, 0x01, 0x01};
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +010042 var decoder = Decoder.ofArray(Decoder.ofBoolean());
43 assertEquals(List.of(true, false, false, true, true), decoder.decode(ByteBuffer.wrap(data)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010044 }
45
46 @Test
47 void testStructure() {
48 var data =
49 new byte[] {
50 0x66, 0x6F, 0x6F, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x04
51 };
52
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +010053 record TestRecord(String s, int i) {}
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010054
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +010055 var decoder = Decoder.ofStructure(TestRecord.class, Decoder.ofString(UTF_8), Decoder.ofInt());
56 assertEquals(new TestRecord("foo", -1), decoder.decode(ByteBuffer.wrap(data)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010057 }
58
59 @Test
60 void testComplexStructureArray() {
61 var data =
62 new byte[] {
63 0x68,
64 0x69,
65 0x00,
66 0x00,
67 (byte) 0xfe,
68 (byte) 0xff,
69 (byte) 0xff,
70 (byte) 0xff,
71 0x03,
72 0x00,
73 0x00,
74 0x00,
75 0x62,
76 0x79,
77 0x65,
78 0x00,
79 (byte) 0xff,
80 (byte) 0xff,
81 (byte) 0xff,
82 (byte) 0xff,
83 0x04,
84 0x09,
85 0x15
86 };
87
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +010088 record TestRecord(String s, int i) {}
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010089
90 var decoder =
91 Decoder.ofArray(
92 Decoder.ofStructure(
93 TestRecord.class,
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +010094 Decoder.ofString(UTF_8),
95 Decoder.ofInt().withByteOrder(LITTLE_ENDIAN)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010096 assertEquals(
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +010097 List.of(new TestRecord("hi", -2), new TestRecord("bye", -1)),
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010098 decoder.decode(ByteBuffer.wrap(data)));
99 }
100
101 @Test
Matthias Andreas Benkard9a6c8ed2021-12-28 01:00:22 +0100102 void testDictionary() {
103 var data =
104 new byte[] {
105 0x68,
106 0x69,
107 0x00,
108 0x00,
109 (byte) 0xfe,
110 (byte) 0xff,
111 (byte) 0xff,
112 (byte) 0xff,
113 0x03,
114 0x00,
115 0x00,
116 0x00,
117 0x62,
118 0x79,
119 0x65,
120 0x00,
121 (byte) 0xff,
122 (byte) 0xff,
123 (byte) 0xff,
124 (byte) 0xff,
125 0x04,
126 0x09,
127 0x15
128 };
129
130 var decoder =
131 Decoder.ofDictionary(Decoder.ofString(UTF_8), Decoder.ofInt().withByteOrder(LITTLE_ENDIAN));
132 assertEquals(Map.of("hi", -2, "bye", -1), decoder.decode(ByteBuffer.wrap(data)));
133 }
134
135 @Test
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100136 void testStringArray() {
137 var data =
138 new byte[] {
139 0x69, 0x00, 0x63, 0x61, 0x6E, 0x00, 0x68, 0x61, 0x73, 0x00, 0x73, 0x74, 0x72, 0x69, 0x6E,
140 0x67, 0x73, 0x3F, 0x00, 0x02, 0x06, 0x0a, 0x13
141 };
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100142 var decoder = Decoder.ofArray(Decoder.ofString(UTF_8));
143 assertEquals(List.of("i", "can", "has", "strings?"), decoder.decode(ByteBuffer.wrap(data)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100144 }
145
146 @Test
147 void testNestedStructure() {
148 var data =
149 new byte[] {
150 0x69, 0x63, 0x61, 0x6E, 0x00, 0x68, 0x61, 0x73, 0x00, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67,
151 0x73, 0x3F, 0x00, 0x04, 0x0d, 0x05
152 };
153
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100154 record TestChild(byte b, String s) {}
155 record TestParent(TestChild tc, List<String> as) {}
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100156
157 var decoder =
158 Decoder.ofStructure(
159 TestParent.class,
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100160 Decoder.ofStructure(TestChild.class, Decoder.ofByte(), Decoder.ofString(UTF_8)),
161 Decoder.ofArray(Decoder.ofString(UTF_8)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100162
163 assertEquals(
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100164 new TestParent(new TestChild((byte) 0x69, "can"), List.of("has", "strings?")),
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100165 decoder.decode(ByteBuffer.wrap(data)));
166 }
167
168 @Test
Matthias Andreas Benkard55c34812021-12-14 21:51:10 +0100169 void testNestedStructureVariant() {
170 var data =
171 new byte[] {
172 0x69, 0x63, 0x61, 0x6E, 0x00, 0x68, 0x61, 0x73, 0x00, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67,
173 0x73, 0x3F, 0x00, 0x04, 0x0d, 0x05, 0x00, 0x28, 0x28, 0x79, 0x73, 0x29, 0x61, 0x73, 0x29
174 };
175
176 var decoder = Decoder.ofVariant();
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +0100177 var variant = decoder.decode(ByteBuffer.wrap(data));
178 var result = (Object[]) variant.value();
Matthias Andreas Benkard55c34812021-12-14 21:51:10 +0100179
180 assertAll(
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +0100181 () -> assertEquals(Signature.parse("((ys)as)"), variant.signature()),
Matthias Andreas Benkard55c34812021-12-14 21:51:10 +0100182 () -> assertEquals(2, result.length),
183 () -> assertArrayEquals(new Object[] {(byte) 0x69, "can"}, (Object[]) result[0]),
184 () -> assertEquals(List.of("has", "strings?"), result[1]));
185 }
186
187 @Test
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100188 void testSimpleStructure() {
189 var data = new byte[] {0x60, 0x70};
190
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100191 record TestRecord(byte b1, byte b2) {}
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100192
193 var decoder =
194 Decoder.ofStructure(
195 TestRecord.class,
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100196 Decoder.ofByte().withByteOrder(LITTLE_ENDIAN),
197 Decoder.ofByte().withByteOrder(LITTLE_ENDIAN));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100198
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100199 assertEquals(new TestRecord((byte) 0x60, (byte) 0x70), decoder.decode(ByteBuffer.wrap(data)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100200 }
201
202 @Test
203 void testPaddedStructureRight() {
204 var data = new byte[] {0x60, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00};
205
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100206 record TestRecord(int b1, byte b2) {}
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100207
208 var decoder =
209 Decoder.ofStructure(
210 TestRecord.class,
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100211 Decoder.ofInt().withByteOrder(LITTLE_ENDIAN),
212 Decoder.ofByte().withByteOrder(LITTLE_ENDIAN));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100213
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100214 assertEquals(new TestRecord(0x60, (byte) 0x70), decoder.decode(ByteBuffer.wrap(data)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100215 }
216
217 @Test
218 void testPaddedStructureLeft() {
219 var data = new byte[] {0x60, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00};
220
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100221 record TestRecord(byte b1, int b2) {}
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100222
223 var decoder =
224 Decoder.ofStructure(
225 TestRecord.class,
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100226 Decoder.ofByte().withByteOrder(LITTLE_ENDIAN),
227 Decoder.ofInt().withByteOrder(LITTLE_ENDIAN));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100228
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100229 assertEquals(new TestRecord((byte) 0x60, 0x70), decoder.decode(ByteBuffer.wrap(data)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100230 }
231
232 @Test
233 void testSimpleStructureArray() {
234 var data =
235 new byte[] {
236 0x60,
237 0x00,
238 0x00,
239 0x00,
240 0x70,
241 0x00,
242 0x00,
243 0x00,
244 (byte) 0x88,
245 0x02,
246 0x00,
247 0x00,
248 (byte) 0xF7,
249 0x00,
250 0x00,
251 0x00
252 };
253
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100254 record TestRecord(int b1, byte b2) {}
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100255
256 var decoder =
257 Decoder.ofArray(
258 Decoder.ofStructure(
259 TestRecord.class,
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100260 Decoder.ofInt().withByteOrder(LITTLE_ENDIAN),
261 Decoder.ofByte().withByteOrder(LITTLE_ENDIAN)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100262
263 assertEquals(
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100264 List.of(new TestRecord(96, (byte) 0x70), new TestRecord(648, (byte) 0xf7)),
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100265 decoder.decode(ByteBuffer.wrap(data)));
266 }
267
268 @Test
269 void testByteArray() {
270 var data = new byte[] {0x04, 0x05, 0x06, 0x07};
271
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100272 var decoder = Decoder.ofArray(Decoder.ofByte());
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100273
274 assertEquals(
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100275 List.of((byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07),
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100276 decoder.decode(ByteBuffer.wrap(data)));
277 }
278
279 @Test
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +0100280 void testPrimitiveByteArray() {
281 var data = new byte[] {0x04, 0x05, 0x06, 0x07};
282
283 var decoder = Decoder.ofByteArray();
284
285 assertArrayEquals(data, decoder.decode(ByteBuffer.wrap(data)));
286 }
287
288 @Test
289 void testPrimitiveByteArrayRecord() {
290 var data = new byte[] {0x04, 0x05, 0x06, 0x07};
291
292 record TestRecord(byte[] bytes) {}
293
294 var decoder = Decoder.ofStructure(TestRecord.class, Decoder.ofByteArray());
295
296 assertArrayEquals(data, decoder.decode(ByteBuffer.wrap(data)).bytes());
297 }
298
299 @Test
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100300 void testIntegerArray() {
301 var data = new byte[] {0x04, 0x00, 0x00, 0x00, 0x02, 0x01, 0x00, 0x00};
302
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100303 var decoder = Decoder.ofArray(Decoder.ofInt().withByteOrder(LITTLE_ENDIAN));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100304
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100305 assertEquals(List.of(4, 258), decoder.decode(ByteBuffer.wrap(data)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100306 }
307
308 @Test
Matthias Andreas Benkardcd924f62021-12-28 00:46:06 +0100309 void testDictionaryEntryAsMapEntry() {
310 var data =
311 new byte[] {0x61, 0x20, 0x6B, 0x65, 0x79, 0x00, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x06};
312
313 var decoder =
314 Decoder.ofDictionaryEntry(
315 Decoder.ofString(UTF_8), Decoder.ofInt().withByteOrder(LITTLE_ENDIAN));
316 assertEquals(Map.entry("a key", 514), decoder.decode(ByteBuffer.wrap(data)));
317 }
318
319 @Test
320 void testDictionaryEntryAsRecord() {
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100321 var data =
322 new byte[] {0x61, 0x20, 0x6B, 0x65, 0x79, 0x00, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x06};
323
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100324 record TestEntry(String key, int value) {}
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100325
326 var decoder =
327 Decoder.ofStructure(
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +0100328 TestEntry.class, Decoder.ofString(UTF_8), Decoder.ofInt().withByteOrder(LITTLE_ENDIAN));
329 assertEquals(new TestEntry("a key", 514), decoder.decode(ByteBuffer.wrap(data)));
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100330 }
Matthias Andreas Benkarde5a5c752021-12-15 19:53:55 +0100331
332 @Test
333 void testPaddedPrimitives() {
334 var data =
335 new byte[] {
336 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
337 0x00, 0x40, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
338 };
339
340 record TestRecord(short s, long l, double d) {}
341
342 var decoder =
343 Decoder.ofStructure(
344 TestRecord.class,
345 Decoder.ofShort().withByteOrder(BIG_ENDIAN),
346 Decoder.ofLong().withByteOrder(LITTLE_ENDIAN),
347 Decoder.ofDouble());
348 assertEquals(new TestRecord((short) 1, 2, 3.25), decoder.decode(ByteBuffer.wrap(data)));
349 }
350
351 @Test
352 void testEmbeddedMaybe() {
353 var data = new byte[] {0x01, 0x01};
354
355 record TestRecord(Optional<Byte> set, Optional<Byte> unset) {}
356
357 var decoder =
358 Decoder.ofStructure(
359 TestRecord.class, Decoder.ofMaybe(Decoder.ofByte()), Decoder.ofMaybe(Decoder.ofByte()));
360 assertEquals(
361 new TestRecord(Optional.of((byte) 1), Optional.empty()),
362 decoder.decode(ByteBuffer.wrap(data)));
363 }
364
365 @Test
366 void testRecordComponentMismatch() {
367 record TestRecord(Optional<Byte> set) {}
368
369 var maybeDecoder = Decoder.ofMaybe(Decoder.ofByte());
370 assertThrows(
371 IllegalArgumentException.class,
372 () -> Decoder.ofStructure(TestRecord.class, maybeDecoder, maybeDecoder));
373 }
374
375 @Test
376 void testTrivialRecord() {
377 var data = new byte[] {0x00};
378
379 record TestRecord() {}
380
381 var decoder = Decoder.ofStructure(TestRecord.class);
382 assertEquals(new TestRecord(), decoder.decode(ByteBuffer.wrap(data)));
383 }
384
385 @Test
386 void testTwoElementTrivialRecordArray() {
387 var data = new byte[] {0x00, 0x00};
388
389 record TestRecord() {}
390
391 var decoder = Decoder.ofArray(Decoder.ofStructure(TestRecord.class));
392 assertEquals(
393 List.of(new TestRecord(), new TestRecord()), decoder.decode(ByteBuffer.wrap(data)));
394 }
395
396 @Test
397 void testSingletonTrivialRecordArray() {
398 var data = new byte[] {0x00};
399
400 record TestRecord() {}
401
402 var decoder = Decoder.ofArray(Decoder.ofStructure(TestRecord.class));
403 assertEquals(List.of(new TestRecord()), decoder.decode(ByteBuffer.wrap(data)));
404 }
405
406 @Test
407 void testEmptyTrivialRecordArray() {
408 var data = new byte[] {};
409
410 record TestRecord() {}
411
412 var decoder = Decoder.ofArray(Decoder.ofStructure(TestRecord.class));
413 assertEquals(List.of(), decoder.decode(ByteBuffer.wrap(data)));
414 }
415
416 @Test
417 void testVariantArray() {
418 var data = new byte[] {};
419
420 record TestRecord() {}
421
422 var decoder = Decoder.ofArray(Decoder.ofStructure(TestRecord.class));
423 assertEquals(List.of(), decoder.decode(ByteBuffer.wrap(data)));
424 }
425
426 @Test
427 void testInvalidVariantSignature() {
428 var data = new byte[] {0x00, 0x00, 0x2E};
429
430 var decoder = Decoder.ofVariant();
431 assertThrows(IllegalArgumentException.class, () -> decoder.decode(ByteBuffer.wrap(data)));
432 }
433
434 @Test
435 void testMissingVariantSignature() {
436 var data = new byte[] {0x01};
437
438 var decoder = Decoder.ofVariant();
439 assertThrows(IllegalArgumentException.class, () -> decoder.decode(ByteBuffer.wrap(data)));
440 }
441
442 @Test
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +0100443 void testSimpleVariantRecord() throws ParseException {
Matthias Andreas Benkarde5a5c752021-12-15 19:53:55 +0100444 // signature: "(bynqiuxtdsogvmiai)"
445 var data =
446 new byte[] {
447 0x01, // b
448 0x02, // y
449 0x00, 0x03, // n
450 0x00, 0x04, // q
451 0x00, 0x00, // (padding)
452 0x00, 0x00, 0x00, 0x05, // i
453 0x00, 0x00, 0x00, 0x06, // u
454 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, // x
455 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, // t
456 0x40, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // d
457 0x68, 0x69, 0x00, // s
458 0x68, 0x69, 0x00, // o
459 0x68, 0x69, 0x00, // g
460 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (padding)
461 0x00, 0x00, 0x00, 0x09, 0x00, 0x69, // v
462 0x00, 0x00, // (padding)
463 0x00, 0x00, 0x00, 0x0a, // mi
464 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x0c, // ai
465 68, 62, 49, 46, 43, // framing offsets
466 0x00, 0x28, 0x62, 0x79, 0x6E, 0x71, 0x69, 0x75, 0x78, 0x74, 0x64, 0x73, 0x6F, 0x67, 0x76,
467 0x6D, 0x69, 0x61, 0x69, 0x29
468 };
469
470 var decoder = Decoder.ofVariant();
471 assertArrayEquals(
472 new Object[] {
473 true,
474 (byte) 2,
475 (short) 3,
476 (short) 4,
477 (int) 5,
478 (int) 6,
479 (long) 7,
480 (long) 8,
481 (double) 3.25,
482 "hi",
483 "hi",
484 "hi",
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +0100485 new Variant(Signature.parse("i"), 9),
Matthias Andreas Benkarde5a5c752021-12-15 19:53:55 +0100486 Optional.of(10),
487 List.of(11, 12)
488 },
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +0100489 (Object[]) decoder.decode(ByteBuffer.wrap(data)).value());
490 }
491
492 @Test
493 void testSignatureString() throws ParseException {
494 var data =
495 new byte[] {
496 0x28, 0x62, 0x79, 0x6E, 0x71, 0x69, 0x75, 0x78, 0x74, 0x64, 0x73, 0x6F, 0x67, 0x76, 0x6D,
497 0x69, 0x61, 0x69, 0x29
498 };
499
500 var signature = Signature.parse(ByteBuffer.wrap(data));
501 assertEquals("(bynqiuxtdsogvmiai)", signature.toString());
Matthias Andreas Benkarde5a5c752021-12-15 19:53:55 +0100502 }
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +0100503
504 @Test
505 void testMap() {
506 var data = new byte[] {0x0A, 0x0B, 0x0C};
507 var decoder = Decoder.ofByteArray().map(bytes -> bytes.length);
508 assertEquals(3, decoder.decode(ByteBuffer.wrap(data)));
509 }
Matthias Andreas Benkard44df94e2021-12-30 18:43:33 +0100510
511 @Test
512 void testContramap() {
513 var data = new byte[] {0x0A, 0x0B, 0x0C};
514 var decoder = Decoder.ofByteArray().contramap(bytes -> bytes.slice(1, 1));
515 assertArrayEquals(new byte[] {0x0B}, decoder.decode(ByteBuffer.wrap(data)));
516 }
517
518 @Test
519 void testPredicateTrue() {
520 var data = new byte[] {0x00, 0x01, 0x00};
521 var innerDecoder = Decoder.ofShort().contramap(bytes -> bytes.slice(1, 2).order(bytes.order()));
522 var decoder =
523 Decoder.ofPredicate(
524 byteBuffer -> byteBuffer.get(0) == 0,
525 innerDecoder.withByteOrder(LITTLE_ENDIAN),
526 innerDecoder.withByteOrder(BIG_ENDIAN));
527 assertEquals((short) 1, decoder.decode(ByteBuffer.wrap(data)));
528 }
529
530 @Test
531 void testPredicateFalse() {
532 var data = new byte[] {0x01, 0x01, 0x00};
533 var innerDecoder = Decoder.ofShort().contramap(bytes -> bytes.slice(1, 2).order(bytes.order()));
534 var decoder =
535 Decoder.ofPredicate(
536 byteBuffer -> byteBuffer.get(0) == 0,
537 innerDecoder.withByteOrder(LITTLE_ENDIAN),
538 innerDecoder.withByteOrder(BIG_ENDIAN));
539 assertEquals((short) 256, decoder.decode(ByteBuffer.wrap(data)));
540 }
541
542 @Test
543 void testByteOrder() {
544 var data =
545 new byte[] {
546 0x01, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00, 0x04, 0x05, 0x00, 0x00, 0x06, 0x00, 0x07, 0x08,
547 0x00
548 };
549
550 record TestChild(short s1, short s2) {}
551 record TestParent(TestChild tc1, TestChild tc2, TestChild tc3, TestChild tc4) {}
552
553 var decoder =
554 Decoder.ofStructure(
555 TestParent.class,
556 Decoder.ofStructure(TestChild.class, Decoder.ofShort(), Decoder.ofShort())
557 .withByteOrder(LITTLE_ENDIAN),
558 Decoder.ofStructure(TestChild.class, Decoder.ofShort(), Decoder.ofShort())
559 .withByteOrder(BIG_ENDIAN),
560 Decoder.ofStructure(
561 TestChild.class,
562 Decoder.ofShort().withByteOrder(LITTLE_ENDIAN),
563 Decoder.ofShort())
564 .withByteOrder(BIG_ENDIAN),
565 Decoder.ofStructure(
566 TestChild.class, Decoder.ofShort().withByteOrder(BIG_ENDIAN), Decoder.ofShort())
567 .withByteOrder(LITTLE_ENDIAN));
568
569 assertEquals(
570 new TestParent(
571 new TestChild((short) 1, (short) 2),
572 new TestChild((short) 3, (short) 4),
573 new TestChild((short) 5, (short) 6),
574 new TestChild((short) 7, (short) 8)),
575 decoder.decode(ByteBuffer.wrap(data)));
576 }
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100577}