Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/protocol/osc.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 21 мая 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/common/endian.h>
23 #include <lsp-plug.in/test-fw/utest.h>
24 #include <lsp-plug.in/test-fw/helpers.h>
25 #include <lsp-plug.in/protocol/osc.h>
26 #include <lsp-plug.in/stdlib/math.h>
27 #include <lsp-plug.in/stdlib/string.h>
28
29 namespace lsp
30 {
31 static uint8_t simple_message[] =
32 {
33 '/', 'o', 's', 'c',
34 'i', 'l', 'l', 'a',
35 't', 'o', 'r', '/',
36 '4', '/', 'f', 'r',
37 'e', 'q', 'u', 'e',
38 'n', 'c', 'y', 0,
39 ',', 'f', 0, 0,
40 0x43, 0xdc, 0, 0
41 };
42
43 static uint8_t legacy_message[] =
44 {
45 '/', 't', 'e', 's',
46 't', 0, 0, 0
47 };
48
49 #pragma pack(push, 1)
50 typedef struct parameter_message_t
51 {
52 char address[12];
53 char types[32];
54
55 int32_t int32;
56 float float32;
57 char string[8];
58 uint32_t blob_len;
59 char blob[8];
60 int64_t int64;
61 double double64;
62 uint64_t time_tag;
63 char type[8];
64 uint32_t ascii;
65 uint32_t rgba;
66 uint8_t midi[4];
67 uint8_t raw_midi[4];
68
69 struct {
70 int32_t int32;
71 struct {
72 char string[8];
73 uint32_t blob_size;
74 char blob[8];
75 int32_t int32;
76 } array;
77 float float32;
78 } array;
79 } parameter_message_t;
80
81 #pragma pack(pop)
82
83 #pragma pack(push, 1)
84 typedef struct message_bundle_t
85 {
86 char signature[8];
87 uint64_t time_tag;
88
89 uint32_t bundle1_size;
90 struct
91 {
92 char signature[8];
93 uint64_t time_tag;
94
95 uint32_t message1_size;
96 struct
97 {
98 char address[4];
99 char types[4];
100 int32_t int32;
101 } message1;
102
103 uint32_t message2_size;
104 struct
105 {
106 char address[8];
107 char types[8];
108 int32_t int32;
109 float float32;
110 } message2;
111 } bundle1;
112
113 uint32_t bundle2_size;
114 struct
115 {
116 char signature[8];
117 uint64_t time_tag;
118
119 uint32_t message1_size;
120 struct
121 {
122 char address[4];
123 char types[4];
124 int32_t int32;
125 } message1;
126
127 uint32_t message2_size;
128 struct
129 {
130 char address[4];
131 char types[8];
132 } message2;
133 } bundle2;
134
135 uint32_t message_size;
136 struct
137 {
138 char address[8];
139 char types[8];
140 int32_t int32;
141 } message;
142 } message_bundle_t;
143 #pragma pack(pop)
144
145 #pragma pack(push, 1)
146 typedef struct format_message_t
147 {
148 char address[12];
149 char types[16];
150
151 int32_t int32;
152 float float32;
153 char string[12];
154 int64_t int64;
155 uint64_t time_tag;
156 double double64;
157 char type[12];
158 uint32_t ascii;
159 uint32_t rgba;
160 } format_message_t;
161 #pragma pack(pop)
162
163 #pragma pack(push, 1)
164 typedef struct special_message_t
165 {
166 char address[12];
167 char types[24];
168 } special_message_t;
169 #pragma pack(pop)
170
171 static const char *valid_patterns[] =
172 {
173 "/a",
174 "/1/2/3/4",
175 "/???",
176 "/***",
177 "/[abcde]",
178 "/[a-zA-Z0-9]",
179 "/[!abc0-9]",
180 "/{}",
181 "/{,,,}",
182 "/{foo,bar,}",
183 "/{foo,bar,baz}",
184 "/{foo,bar,}?/test",
185 "/a/?*/{foo,bar,}[0-9]xx?",
186 NULL
187 };
188
189 static const char *invalid_patterns[] =
190 {
191 "",
192 "/",
193 "/#",
194 "//",
195 "///",
196 "/\n",
197 "/[",
198 "/]",
199 "/[a-]",
200 "/[a-zA-Z0-9-#]",
201 "/[!]",
202 "/[?]",
203 "/[*]",
204 "/{",
205 "/}",
206 "/{[}",
207 "/{#}",
208 "/{foo,bar,}}",
209 "/{foo,bar,{}}",
210 "/{foo,bar,}/test#",
211 "/a/?*/{foo,bar,}[0-9]xx?}",
212 NULL
213 };
214
215 typedef struct pattern_match_t
216 {
217 const char *pattern;
218 const char *address;
219 bool match;
220 } pattern_match_t;
221
222 static const pattern_match_t match_pattern[] =
223 {
224 { "/one", "/one", true},
225 { "/one", "/two", false},
226 { "/one", "/one/two", false},
227 { "/one", "//one", false},
228 { "/one/two", "/one/two", true},
229 { "/one/two", "/one/two/", false},
230 { "/one/two", "/one/two/three", false},
231 { "/one/two", "//one/two/three", false},
232 { "/one/two", "/one//two/three", false},
233 { "/*", "/one", true },
234 { "/*", "/two", true },
235 { "/*", "/", false },
236 { "/*", "//", false },
237 { "/*", "", false },
238 { "/*", "/one/two", false },
239 { "/?", "/1", true },
240 { "/?", "/2", true },
241 { "/?", "/", false },
242 { "/?", "/12", false },
243 { "/?", "/1/2", false },
244 { "/??", "/12", true },
245 { "/??", "/123", false },
246 { "/??", "/12/34", false },
247 { "/a", "/a", true },
248 { "/a", "/b", false },
249 { "/a", "/ab", false },
250 { "/a", "/a/b", false },
251 { "/[abc]?", "/a1", true },
252 { "/[abc]?", "/b1", true },
253 { "/[abc]?", "/c1", true },
254 { "/[abc]?", "/d1", false },
255 { "/[abc]?", "/a1/", false },
256 { "/[abc]?", "/a1/b", false },
257 { "/[a-c]?", "/a2", true },
258 { "/[a-c]?", "/b2", true },
259 { "/[a-c]?", "/c2", true },
260 { "/[a-c]?", "/d2", false },
261 { "/[a-c]*", "/c", true },
262 { "/[a-c]*", "/c1", true },
263 { "/[a-c]*", "/c12", true },
264 { "/[a-c]*", "/c123", true },
265 { "/[a-c]*", "/c/", false },
266 { "/[a-c]*", "/c/1", false },
267 { "/{foo,bar,baz}", "/foo", true },
268 { "/{foo,bar,baz}", "/bar", true },
269 { "/{foo,bar,baz}", "/baz", true },
270 { "/{foo,bar,baz}", "/foo/bar", false },
271 { "/{foo,bar,baz}", "//bar", false },
272 { "/{foo,}?", "/x", true },
273 { "/{foo,}", "/", false },
274 { "/{foo,}*", "/", false },
275 { "/{foo,}*", "/bar", true },
276 { "/{foo,}*", "/foo/bar", false },
277 { "/*foo*{bar,baz}/[a-bB-A]one?", "/foobar/AoneX", true },
278 { "/*foo*{bar,baz}/[a-bB-A]one?", "/foobar/Aone?", false },
279 { "/*foo*{bar,baz}/[a-bB-A]one?", "/foobar/Aone#", false },
280 { "/*foo*{bar,baz}/[a-bB-A]one?", "/fooXXXbar/Bone+", true },
281 { "/*foo*{bar,baz}/[a-bB-A]one?", "/XXXfooXXXbar/aone_", true },
282 { "/*foo*{bar,baz}/[a-bB-A]one?", "/XXXfooXXXbarXXX/aone_", false },
283 { "/*foo*{bar,baz}/[a-bB-A]one?", "/XXX/fooXXXbar/aone_", false}
284 };
285 }
286
287 UTEST_BEGIN("runtime.protocol", osc)
288 void test_forge_simple_message()
289 {
290 osc::packet_t packet;
291 osc::forge_t forge;
292 osc::forge_frame_t frame, message, other;
293
294 // Curly braces are used for visualization of nested structures
295 UTEST_ASSERT(osc::forge_begin_dynamic(&frame, &forge) == STATUS_OK);
296 {
297 // Perform some 'dumb' error checks
298 UTEST_ASSERT(osc::forge_close(&packet, &forge) == STATUS_BAD_STATE);
299 UTEST_ASSERT(osc::forge_begin_message(&frame, &frame, "/fail") == STATUS_BAD_ARGUMENTS);
300 UTEST_ASSERT(osc::forge_float32(&frame, 440.0f) == STATUS_BAD_STATE);
301
302 // Start message
303 UTEST_ASSERT(osc::forge_begin_message(&message, &frame, "/oscillator/4/frequency") == STATUS_OK);
304 {
305 // Perform some 'dumb' error checks
306 UTEST_ASSERT(osc::forge_close(&packet, &forge) == STATUS_BAD_STATE);
307 UTEST_ASSERT(osc::forge_end(&frame) == STATUS_BAD_STATE);
308 UTEST_ASSERT(osc::forge_begin_message(&other, &frame, "/fail") == STATUS_BAD_STATE);
309 UTEST_ASSERT(osc::forge_begin_message(&other, &message, "/fail") == STATUS_BAD_STATE);
310 UTEST_ASSERT(osc::forge_begin_bundle(&other, &message, 0) == STATUS_BAD_STATE);
311
312 // Emit parameter
313 UTEST_ASSERT(osc::forge_float32(&message, 440.0f) == STATUS_OK);
314 }
315 UTEST_ASSERT(osc::forge_end(&message) == STATUS_OK);
316
317 // Perform some 'dump' error checks
318 UTEST_ASSERT(osc::forge_end(&message) == STATUS_BAD_STATE);
319 UTEST_ASSERT(osc::forge_begin_message(&message, &frame, "/fail") == STATUS_BAD_STATE);
320 UTEST_ASSERT(osc::forge_begin_bundle(&message, &frame, 0) == STATUS_BAD_STATE);
321 UTEST_ASSERT(osc::forge_float32(&frame, 440.0f) == STATUS_BAD_STATE);
322 }
323 UTEST_ASSERT(osc::forge_end(&frame) == STATUS_OK);
324 UTEST_ASSERT(osc::forge_end(&frame) == STATUS_BAD_STATE); // 'dumb' check
325
326 UTEST_ASSERT(osc::forge_close(&packet, &forge) == STATUS_OK);
327 UTEST_ASSERT(osc::forge_close(&packet, &forge) == STATUS_BAD_STATE); // 'dumb' check
328
329 UTEST_ASSERT(osc::forge_destroy(&forge) == STATUS_OK);
330
331 // Validate output data
332 UTEST_ASSERT(packet.data != NULL);
333 bool matches = (packet.size == sizeof(simple_message)) && (::memcmp(packet.data, simple_message, sizeof(simple_message)) == 0);
334 if (!matches)
335 {
336 dump_bytes("src", simple_message, sizeof(simple_message));
337 dump_bytes("dst", packet.data, packet.size);
338 UTEST_FAIL_MSG("Source and destination messages differ");
339 }
340 osc::forge_free(packet.data);
341 }
342
343 void test_forge_parameters()
344 {
345 // Initialize parametrized message
346 parameter_message_t m;
347 bzero(&m, sizeof(m));
348 strcpy(m.address, "/address/01");
349 strcpy(m.types, ",ifsbhdtScrmm[iT[sbiI]f]TFNI");
350
351 m.int32 = CPU_TO_BE(int32_t(0xdeadbeef));
352 m.float32 = CPU_TO_BE(float(M_PI));
353 strcpy(m.string, "string1");
354 m.blob_len = CPU_TO_BE(uint32_t(5));
355 strcpy(m.blob, "BLOB1");
356 m.int64 = CPU_TO_BE(uint64_t(0x1234567855aacc33ULL));
357 m.double64 = CPU_TO_BE(double(M_E));
358 m.time_tag = CPU_TO_BE(uint64_t(0xff00cc01aa028803ULL));
359 strcpy(m.type, "type01");
360 m.ascii = CPU_TO_BE(uint32_t(uint8_t('\n')));
361 m.rgba = CPU_TO_BE(uint32_t(0x8080ff40));
362 m.midi[0] = 0x94;
363 m.midi[1] = 51;
364 m.midi[2] = 0x7f;
365 m.raw_midi[0] = 0x84;
366 m.raw_midi[1] = 51;
367 m.raw_midi[2] = 0x5f;
368 m.array.int32 = CPU_TO_BE(int32_t(0xfffefcfd));
369 strcpy(m.array.array.string, "string2");
370 m.array.array.blob_size = CPU_TO_BE(uint32_t(6));
371 strcpy(m.array.array.blob, "BLOB02");
372 m.array.array.int32 = CPU_TO_BE(int32_t(0xcafeface));
373 m.array.float32 = CPU_TO_BE(float(440.0f));
374
375 midi::event_t midi;
376 midi.timestamp = 0;
377 midi.type = midi::MIDI_MSG_NOTE_ON;
378 midi.channel = 4;
379 midi.note.pitch = 51;
380 midi.note.velocity = 0x7f;
381
382 uint8_t raw_midi[4];
383 raw_midi[0] = midi::MIDI_MSG_NOTE_OFF | 4;
384 raw_midi[1] = 51;
385 raw_midi[2] = 0x5f;
386
387 // Forge the same message using OSC serializer
388 osc::packet_t packet;
389 osc::forge_t forge;
390 osc::forge_frame_t frame, message, array1, array2;
391
392 UTEST_ASSERT(osc::forge_begin_dynamic(&frame, &forge) == STATUS_OK);
393 UTEST_ASSERT(osc::forge_begin_message(&message, &frame, "/address/01") == STATUS_OK);
394 {
395 UTEST_ASSERT(osc::forge_int32(&message, 0xdeadbeef) == STATUS_OK);
396 UTEST_ASSERT(osc::forge_float32(&message, float(M_PI)) == STATUS_OK);
397 UTEST_ASSERT(osc::forge_string(&message, "string1") == STATUS_OK);
398 UTEST_ASSERT(osc::forge_blob(&message, "BLOB1", 5) == STATUS_OK);
399 UTEST_ASSERT(osc::forge_int64(&message, uint64_t(0x1234567855aacc33ULL)) == STATUS_OK);
400 UTEST_ASSERT(osc::forge_double64(&message, double(M_E)) == STATUS_OK);
401 UTEST_ASSERT(osc::forge_time_tag(&message, uint64_t(0xff00cc01aa028803ULL)) == STATUS_OK);
402 UTEST_ASSERT(osc::forge_type(&message, "type01") == STATUS_OK);
403 UTEST_ASSERT(osc::forge_ascii(&message, '\n') == STATUS_OK);
404 UTEST_ASSERT(osc::forge_rgba(&message, 0x8080ff40) == STATUS_OK);
405 UTEST_ASSERT(osc::forge_midi(&message, &midi) == STATUS_OK);
406 UTEST_ASSERT(osc::forge_midi_raw(&message, raw_midi, 3) == STATUS_OK);
407
408 UTEST_ASSERT(osc::forge_begin_array(&array1, &message) == STATUS_OK);
409 {
410 UTEST_ASSERT(osc::forge_int32(&array1, int32_t(0xfffefcfd)) == STATUS_OK);
411 UTEST_ASSERT(osc::forge_bool(&array1, true) == STATUS_OK);
412 UTEST_ASSERT(osc::forge_begin_array(&array2, &array1) == STATUS_OK);
413 {
414 UTEST_ASSERT(osc::forge_string(&array2, "string2") == STATUS_OK);
415 UTEST_ASSERT(osc::forge_blob(&array2, "BLOB02", 6) == STATUS_OK);
416 UTEST_ASSERT(osc::forge_int32(&array2, int32_t(0xcafeface)) == STATUS_OK);
417 UTEST_ASSERT(osc::forge_inf(&array2) == STATUS_OK);
418 }
419 UTEST_ASSERT(osc::forge_end(&array2) == STATUS_OK);
420 UTEST_ASSERT(osc::forge_float32(&array1, float(440.0f)) == STATUS_OK);
421 }
422 UTEST_ASSERT(osc::forge_end(&array1) == STATUS_OK);
423
424 UTEST_ASSERT(osc::forge_bool(&message, true) == STATUS_OK);
425 UTEST_ASSERT(osc::forge_bool(&message, false) == STATUS_OK);
426 UTEST_ASSERT(osc::forge_null(&message) == STATUS_OK);
427 UTEST_ASSERT(osc::forge_inf(&message) == STATUS_OK);
428 }
429 UTEST_ASSERT(osc::forge_end(&message) == STATUS_OK);
430 UTEST_ASSERT(osc::forge_end(&frame) == STATUS_OK);
431 UTEST_ASSERT(osc::forge_close(&packet, &forge) == STATUS_OK);
432 UTEST_ASSERT(osc::forge_destroy(&forge) == STATUS_OK);
433
434 // Validate output data
435 UTEST_ASSERT(packet.data != NULL);
436 bool matches = (packet.size == sizeof(m)) && (::memcmp(packet.data, &m, sizeof(m)) == 0);
437 if (!matches)
438 {
439 dump_bytes("src", &m, sizeof(m));
440 dump_bytes("dst", packet.data, packet.size);
441 UTEST_FAIL_MSG("Source and destination messages differ");
442 }
443 osc::forge_free(packet.data);
444 }
445
446 void test_forge_message_bundle()
447 {
448 // Initialize message bundle
449 message_bundle_t b;
450 bzero(&b, sizeof(b));
451
452 strcpy(b.signature, "#bundle");
453 b.time_tag = CPU_TO_BE(uint64_t(0x1122334455667788ULL));
454
455 b.bundle1_size = CPU_TO_BE(uint32_t(sizeof(b.bundle1)));
456 {
457 strcpy(b.bundle1.signature, "#bundle");
458 b.bundle1.time_tag = CPU_TO_BE(uint64_t(0x2233445566778899ULL));
459
460 b.bundle1.message1_size = CPU_TO_BE(uint32_t(sizeof(b.bundle1.message1)));
461 {
462 strcpy(b.bundle1.message1.address, "/a0");
463 strcpy(b.bundle1.message1.types, ",i");
464 b.bundle1.message1.int32 = CPU_TO_BE(int32_t(0xdeadbeef));
465 }
466
467 b.bundle1.message2_size = CPU_TO_BE(uint32_t(sizeof(b.bundle1.message2)));
468 {
469 strcpy(b.bundle1.message2.address, "/addr0");
470 strcpy(b.bundle1.message2.types, ",TFif");
471 b.bundle1.message2.int32 = CPU_TO_BE(int32_t(0xcafeface));
472 b.bundle1.message2.float32 = CPU_TO_BE(float(440.0f));
473 }
474 }
475
476 b.bundle2_size = CPU_TO_BE(uint32_t(sizeof(b.bundle2)));
477 {
478 strcpy(b.bundle2.signature, "#bundle");
479 b.bundle2.time_tag = CPU_TO_BE(uint64_t(0x5566778899aabbccULL));
480
481 b.bundle2.message1_size = CPU_TO_BE(uint32_t(sizeof(b.bundle2.message1)));
482 {
483 strcpy(b.bundle2.message1.address, "/b0");
484 strcpy(b.bundle2.message1.types, ",Ii");
485 b.bundle2.message1.int32 = CPU_TO_BE(int32_t(0xf00dfeed));
486 }
487
488 b.bundle2.message2_size = CPU_TO_BE(uint32_t(sizeof(b.bundle2.message2)));
489 {
490 strcpy(b.bundle2.message2.address, "/c0");
491 strcpy(b.bundle2.message2.types, ",TFN");
492 }
493 }
494
495 b.message_size = CPU_TO_BE(uint32_t(sizeof(b.message)));
496 {
497 strcpy(b.message.address, "/addr1");
498 strcpy(b.message.types, ",TFiNI");
499 b.message.int32 = CPU_TO_BE(int32_t(0x55aacc33));
500 }
501
502 // Serialize message
503 osc::packet_t packet;
504 osc::forge_t forge;
505 osc::forge_frame_t frame, bundle1, bundle2, message;
506
507 UTEST_ASSERT(osc::forge_begin_dynamic(&frame, &forge) == STATUS_OK);
508
509 UTEST_ASSERT(osc::forge_begin_bundle(&bundle1, &frame, uint64_t(0x1122334455667788ULL)) == STATUS_OK);
510 {
511 // Sub-bundle 1
512 UTEST_ASSERT(osc::forge_begin_bundle(&bundle2, &bundle1, uint64_t(0x2233445566778899ULL)) == STATUS_OK);
513 {
514 UTEST_ASSERT(osc::forge_begin_message(&message, &bundle2, "/a0") == STATUS_OK);
515 {
516 UTEST_ASSERT(osc::forge_int32(&message, int32_t(0xdeadbeef)) == STATUS_OK);
517 }
518 UTEST_ASSERT(osc::forge_end(&message) == STATUS_OK);
519
520 UTEST_ASSERT(osc::forge_begin_message(&message, &bundle2, "/addr0") == STATUS_OK);
521 {
522 UTEST_ASSERT(osc::forge_bool(&message, true) == STATUS_OK);
523 UTEST_ASSERT(osc::forge_bool(&message, false) == STATUS_OK);
524 UTEST_ASSERT(osc::forge_int32(&message, int32_t(0xcafeface)) == STATUS_OK);
525 UTEST_ASSERT(osc::forge_float32(&message, float(440.0f)) == STATUS_OK);
526 }
527 UTEST_ASSERT(osc::forge_end(&message) == STATUS_OK);
528 }
529 UTEST_ASSERT(osc::forge_end(&bundle2) == STATUS_OK);
530
531 // Sub-bundle 2
532 UTEST_ASSERT(osc::forge_begin_bundle(&bundle2, &bundle1, uint64_t(0x5566778899aabbccULL)) == STATUS_OK);
533 {
534 UTEST_ASSERT(osc::forge_begin_message(&message, &bundle2, "/b0") == STATUS_OK);
535 {
536 UTEST_ASSERT(osc::forge_inf(&message) == STATUS_OK);
537 UTEST_ASSERT(osc::forge_int32(&message, int32_t(0xf00dfeed)) == STATUS_OK);
538 }
539 UTEST_ASSERT(osc::forge_end(&message) == STATUS_OK);
540
541 UTEST_ASSERT(osc::forge_begin_message(&message, &bundle2, "/c0") == STATUS_OK);
542 {
543 UTEST_ASSERT(osc::forge_bool(&message, true) == STATUS_OK);
544 UTEST_ASSERT(osc::forge_bool(&message, false) == STATUS_OK);
545 UTEST_ASSERT(osc::forge_null(&message) == STATUS_OK);
546 }
547 UTEST_ASSERT(osc::forge_end(&message) == STATUS_OK);
548 }
549 UTEST_ASSERT(osc::forge_end(&bundle2) == STATUS_OK);
550
551 // Sub-message
552 UTEST_ASSERT(osc::forge_begin_message(&message, &bundle1, "/addr1") == STATUS_OK);
553 {
554 UTEST_ASSERT(osc::forge_bool(&message, true) == STATUS_OK);
555 UTEST_ASSERT(osc::forge_bool(&message, false) == STATUS_OK);
556 UTEST_ASSERT(osc::forge_int32(&message, int32_t(0x55aacc33)) == STATUS_OK);
557 UTEST_ASSERT(osc::forge_null(&message) == STATUS_OK);
558 UTEST_ASSERT(osc::forge_inf(&message) == STATUS_OK);
559 }
560 UTEST_ASSERT(osc::forge_end(&message) == STATUS_OK);
561 }
562 UTEST_ASSERT(osc::forge_end(&bundle1) == STATUS_OK);
563
564 UTEST_ASSERT(osc::forge_end(&frame) == STATUS_OK);
565 UTEST_ASSERT(osc::forge_close(&packet, &forge) == STATUS_OK);
566 UTEST_ASSERT(osc::forge_destroy(&forge) == STATUS_OK);
567
568 // Validate output data
569 UTEST_ASSERT(packet.data != NULL);
570 bool matches = (packet.size == sizeof(b)) && (::memcmp(packet.data, &b, sizeof(b)) == 0);
571 if (!matches)
572 {
573 dump_bytes("src", &b, sizeof(b));
574 dump_bytes("dst", packet.data, packet.size);
575 UTEST_FAIL_MSG("Source and destination messages differ");
576 }
577 osc::forge_free(packet.data);
578 }
579
580 void test_forge_overflow()
581 {
582 uint8_t buf[22];
583
584 struct
585 {
586 char addr[12];
587 char types[4];
588 int32_t int32;
589 } m;
590
591 bzero(&m, sizeof(m));
592 strcpy(m.addr, "/message");
593 strcpy(m.types, ",i");
594 m.int32 = CPU_TO_BE(uint32_t(0xdeadcafe));
595
596 osc::packet_t packet;
597 osc::forge_t forge;
598 osc::forge_frame_t frame, message;
599
600 UTEST_ASSERT(osc::forge_begin_fixed(&frame, &forge, buf, sizeof(buf)) == STATUS_OK);
601 {
602 UTEST_ASSERT(osc::forge_begin_message(&message, &frame, "/message") == STATUS_OK);
603 {
604 UTEST_ASSERT(osc::forge_int32(&message, 0xdeadcafe) == STATUS_OK);
605 UTEST_ASSERT(osc::forge_int32(&message, 0xdeadbeef) == STATUS_OVERFLOW);
606 }
607 UTEST_ASSERT(osc::forge_end(&message) == STATUS_OK);
608 }
609 UTEST_ASSERT(osc::forge_end(&frame) == STATUS_OK);
610 UTEST_ASSERT(osc::forge_close(&packet, &forge) == STATUS_OK);
611 UTEST_ASSERT(osc::forge_destroy(&forge) == STATUS_OK);
612
613 // Validate output data
614 UTEST_ASSERT(packet.data != NULL);
615 bool matches = (packet.size == sizeof(m)) && (::memcmp(packet.data, &m, sizeof(m)) == 0);
616 if (!matches)
617 {
618 dump_bytes("src", &m, sizeof(m));
619 dump_bytes("dst", packet.data, packet.size);
620 UTEST_FAIL_MSG("Source and destination messages differ");
621 }
622 }
623
624 void test_format()
625 {
626 format_message_t m;
627
628 bzero(&m, sizeof(m));
629 strcpy(m.address, "/test/msg");
630 strcpy(m.types, ",ifshtdScrFNNI");
631 m.int32 = CPU_TO_BE(uint32_t(0xdeadbeef));
632 m.float32 = CPU_TO_BE(float(440.0f));
633 strcpy(m.string, "some_string");
634 m.int64 = CPU_TO_BE(int64_t(0x1122334455667788ULL));
635 m.time_tag = CPU_TO_BE(uint64_t(0xcafebabecafefaceULL));
636 m.double64 = CPU_TO_BE(double(M_PI));
637 strcpy(m.type, "some_type");
638 m.ascii = CPU_TO_BE(uint32_t('\n'));
639 m.rgba = CPU_TO_BE(uint32_t(0xccffee44));
640
641 osc::packet_t packet;
642 osc::forge_t forge;
643 osc::forge_frame_t sframe;
644
645 UTEST_ASSERT(osc::forge_begin_dynamic(&sframe, &forge) == STATUS_OK);
646 {
647 status_t res = osc::forge_message(&sframe, "/test/msg", "ifshtdScrTsSf",
648 uint32_t(0xdeadbeef),
649 float(440.0f),
650 "some_string",
651 int64_t(0x1122334455667788ULL),
652 uint64_t(0xcafebabecafefaceULL),
653 double(M_PI),
654 "some_type",
655 '\n',
656 uint32_t(0xccffee44),
657 false,
658 NULL,
659 NULL,
660 INFINITY
661 );
662
663 UTEST_ASSERT(res == STATUS_OK);
664 }
665 UTEST_ASSERT(osc::forge_end(&sframe) == STATUS_OK);
666 UTEST_ASSERT(osc::forge_close(&packet, &forge) == STATUS_OK);
667 UTEST_ASSERT(osc::forge_destroy(&forge) == STATUS_OK);
668
669 // Validate output data
670 UTEST_ASSERT(packet.data != NULL);
671 bool matches = (packet.size == sizeof(m)) && (::memcmp(packet.data, &m, sizeof(m)) == 0);
672 if (!matches)
673 {
674 dump_bytes("src", &m, sizeof(m));
675 dump_bytes("dst", packet.data, packet.size);
676 UTEST_FAIL_MSG("Source and destination messages differ");
677 }
678
679 // Parse packet data
680 osc::parser_t parser;
681 osc::parse_frame_t dframe;
682 UTEST_ASSERT(osc::parse_begin(&dframe, &parser, packet.data, packet.size) == STATUS_OK);
683 {
684 const char *address = NULL, *string = NULL, *type = NULL;
685 int32_t i32 = 0, ni32 = 0x1770feeb;
686 float f32 = 0.0f, nf32 = 12345.0f;
687 int64_t i64 = 0;
688 uint64_t tag = 0;
689 double d64 = 0.0;
690 char ch = '\0';
691 uint32_t rgba = 0;
692 bool tf = true, ntf = true;
693 // "ifshtdScrFNNI"
694 status_t res = osc::parse_message(&dframe, "ifshtdScrFTif", &address,
695 &i32, &f32, &string, &i64, &tag, &d64, &type, &ch, &rgba, &tf,
696 &ntf, &ni32, &nf32
697 );
698 UTEST_ASSERT(res == STATUS_OK);
699
700 UTEST_ASSERT(strcmp(address, "/test/msg") == 0);
701 UTEST_ASSERT(i32 == int32_t(0xdeadbeef));
702 UTEST_ASSERT(f32 == 440.0f);
703 UTEST_ASSERT(strcmp(string, "some_string") == 0);
704 UTEST_ASSERT(i64 == int64_t(0x1122334455667788ULL));
705 UTEST_ASSERT(tag == uint64_t(0xcafebabecafefaceULL));
706 UTEST_ASSERT(d64 == double(M_PI));
707 UTEST_ASSERT(strcmp(type, "some_type") == 0);
708 UTEST_ASSERT(ch == '\n');
709 UTEST_ASSERT(rgba == uint32_t(0xccffee44));
710 UTEST_ASSERT(tf == false);
711 UTEST_ASSERT(ntf == true);
712 UTEST_ASSERT(ni32 == 0x1770feeb);
713 UTEST_ASSERT(isinf(nf32));
714 }
715 UTEST_ASSERT(osc::parse_end(&dframe) == STATUS_OK);
716 UTEST_ASSERT(osc::parse_destroy(&parser) == STATUS_OK);
717
718 // Free packet data
719 osc::forge_free(packet.data);
720 }
721
722 void test_parse_legacy_message()
723 {
724 osc::parser_t parser;
725 osc::parse_frame_t frame, message;
726 osc::parse_token_t token;
727 uint64_t time_tag;
728 const char *address;
729
730 UTEST_ASSERT(osc::parse_begin(&frame, &parser, legacy_message, sizeof(legacy_message) - 1) != STATUS_OK);
731 UTEST_ASSERT(osc::parse_begin(&frame, &parser, NULL, sizeof(legacy_message) - 1) != STATUS_OK);
732 UTEST_ASSERT(osc::parse_begin(&frame, &parser, legacy_message, sizeof(legacy_message)) == STATUS_OK);
733 {
734 UTEST_ASSERT(osc::parse_token(&frame, &token) == STATUS_OK);
735 UTEST_ASSERT(token == osc::PT_MESSAGE);
736
737 UTEST_ASSERT(osc::parse_begin_bundle(&message, &frame, &time_tag) != STATUS_OK);
738 UTEST_ASSERT(osc::parse_begin_array(&message, &frame) != STATUS_OK);
739 UTEST_ASSERT(osc::parse_begin_message(&message, &frame, &address) == STATUS_OK);
740 {
741 UTEST_ASSERT(strcmp(address, "/test") == 0);
742 UTEST_ASSERT(osc::parse_token(&frame, &token) != STATUS_OK);
743 UTEST_ASSERT(osc::parse_token(&message, &token) == STATUS_OK);
744 UTEST_ASSERT(token == osc::PT_EOR);
745 }
746 UTEST_ASSERT(osc::parse_end(&frame) != STATUS_OK);
747 UTEST_ASSERT(osc::parse_end(&message) == STATUS_OK);
748 UTEST_ASSERT(osc::parse_end(&message) != STATUS_OK);
749
750 UTEST_ASSERT(osc::parse_token(&frame, &token) == STATUS_OK);
751 UTEST_ASSERT(token == osc::PT_EOR);
752 }
753 UTEST_ASSERT(osc::parse_end(&frame) == STATUS_OK);
754 UTEST_ASSERT(osc::parse_end(&frame) != STATUS_OK);
755 UTEST_ASSERT(osc::parse_destroy(&parser) == STATUS_OK);
756 }
757
758 void test_parse_simple_message()
759 {
760 osc::parser_t parser;
761 osc::parse_frame_t frame, message, subframe;
762 osc::parse_token_t token;
763 const char *address;
764 const void *bytes;
765 const uint8_t *midi;
766
767 union
768 {
769 uint32_t u32;
770 float f32;
771 double d64;
772 int32_t i32;
773 uint64_t u64;
774 int64_t i64;
775 size_t sz;
776 char ch;
777 midi::event_t midi;
778 bool tf;
779 } xptr;
780
781 UTEST_ASSERT(osc::parse_begin(&frame, &parser, simple_message, sizeof(simple_message)) == STATUS_OK);
782 {
783 UTEST_ASSERT(osc::parse_raw_message(&frame, &bytes, &xptr.sz, &address) == STATUS_OK);
784 UTEST_ASSERT(strcmp(address, "/oscillator/4/frequency") == 0);
785 UTEST_ASSERT(bytes == simple_message);
786 UTEST_ASSERT(xptr.sz == sizeof(simple_message));
787
788 UTEST_ASSERT(osc::parse_begin_message(&message, &frame, &address) == STATUS_OK);
789 {
790 UTEST_ASSERT(strcmp(address, "/oscillator/4/frequency") == 0);
791 UTEST_ASSERT(osc::parse_token(&message, &token) == STATUS_OK);
792 UTEST_ASSERT(token == osc::PT_FLOAT32);
793
794 UTEST_ASSERT(osc::parse_begin_bundle(&subframe, &message, &xptr.u64) != STATUS_OK);
795 UTEST_ASSERT(osc::parse_begin_message(&subframe, &message, &address) != STATUS_OK);
796 UTEST_ASSERT(osc::parse_begin_array(&subframe, &message) != STATUS_OK);
797
798 UTEST_ASSERT(osc::parse_int32(&message, &xptr.i32) == STATUS_BAD_TYPE);
799 UTEST_ASSERT(osc::parse_string(&message, &address) == STATUS_BAD_TYPE);
800 UTEST_ASSERT(osc::parse_blob(&message, &bytes, &xptr.sz) == STATUS_BAD_TYPE);
801 UTEST_ASSERT(osc::parse_int64(&message, &xptr.i64) == STATUS_BAD_TYPE);
802 UTEST_ASSERT(osc::parse_double64(&message, &xptr.d64) == STATUS_BAD_TYPE);
803 UTEST_ASSERT(osc::parse_time_tag(&message, &xptr.u64) == STATUS_BAD_TYPE);
804 UTEST_ASSERT(osc::parse_type(&message, &address) == STATUS_BAD_TYPE);
805 UTEST_ASSERT(osc::parse_ascii(&message, &xptr.ch) == STATUS_BAD_TYPE);
806 UTEST_ASSERT(osc::parse_rgba(&message, &xptr.u32) == STATUS_BAD_TYPE);
807 UTEST_ASSERT(osc::parse_midi(&message, &xptr.midi) == STATUS_BAD_TYPE);
808 UTEST_ASSERT(osc::parse_midi_raw(&message, &midi, &xptr.sz) == STATUS_BAD_TYPE);
809 UTEST_ASSERT(osc::parse_bool(&message, &xptr.tf) == STATUS_BAD_TYPE);
810 UTEST_ASSERT(osc::parse_null(&message) == STATUS_BAD_TYPE);
811 UTEST_ASSERT(osc::parse_inf(&message) == STATUS_BAD_TYPE);
812
813 UTEST_ASSERT(osc::parse_float32(&message, &xptr.f32) == STATUS_OK);
814 UTEST_ASSERT(xptr.f32 == 440.0f);
815
816 UTEST_ASSERT(osc::parse_token(&message, &token) == STATUS_OK);
817 UTEST_ASSERT(token == osc::PT_EOR);
818
819 UTEST_ASSERT(osc::parse_int32(&message, &xptr.i32) == STATUS_EOF);
820 UTEST_ASSERT(osc::parse_string(&message, &address) == STATUS_EOF);
821 UTEST_ASSERT(osc::parse_blob(&message, &bytes, &xptr.sz) == STATUS_EOF);
822 UTEST_ASSERT(osc::parse_int64(&message, &xptr.i64) == STATUS_EOF);
823 UTEST_ASSERT(osc::parse_double64(&message, &xptr.d64) == STATUS_EOF);
824 UTEST_ASSERT(osc::parse_time_tag(&message, &xptr.u64) == STATUS_EOF);
825 UTEST_ASSERT(osc::parse_type(&message, &address) == STATUS_EOF);
826 UTEST_ASSERT(osc::parse_ascii(&message, &xptr.ch) == STATUS_EOF);
827 UTEST_ASSERT(osc::parse_rgba(&message, &xptr.u32) == STATUS_EOF);
828 UTEST_ASSERT(osc::parse_midi(&message, &xptr.midi) == STATUS_EOF);
829 UTEST_ASSERT(osc::parse_midi_raw(&message, &midi, &xptr.sz) == STATUS_EOF);
830 UTEST_ASSERT(osc::parse_bool(&message, &xptr.tf) == STATUS_EOF);
831 UTEST_ASSERT(osc::parse_null(&message) == STATUS_EOF);
832 UTEST_ASSERT(osc::parse_inf(&message) == STATUS_EOF);
833 UTEST_ASSERT(osc::parse_float32(&message, &xptr.f32) == STATUS_EOF);
834
835 }
836 UTEST_ASSERT(osc::parse_end(&frame) != STATUS_OK);
837 UTEST_ASSERT(osc::parse_end(&message) == STATUS_OK);
838 UTEST_ASSERT(osc::parse_end(&message) != STATUS_OK);
839
840 UTEST_ASSERT(osc::parse_token(&frame, &token) == STATUS_OK);
841 UTEST_ASSERT(token == osc::PT_EOR);
842 }
843 UTEST_ASSERT(osc::parse_end(&frame) == STATUS_OK);
844 UTEST_ASSERT(osc::parse_end(&frame) != STATUS_OK);
845 UTEST_ASSERT(osc::parse_destroy(&parser) == STATUS_OK);
846 }
847
848 void test_parse_specials_message()
849 {
850 special_message_t m;
851 bzero(&m, sizeof(m));
852 strcpy(m.address, "/message001");
853 strcpy(m.types, ",NNNNNNNNNNNNNNNIIII");
854
855 // Parse the message
856 osc::parser_t parser;
857 osc::parse_frame_t frame, message;
858 osc::parse_token_t token;
859 const char *address;
860
861 float f32 = 0.0f;
862 double f64 = 0.0;
863
864 UTEST_ASSERT(osc::parse_begin(&frame, &parser, &m, sizeof(m)) == STATUS_OK);
865 {
866 UTEST_ASSERT(osc::parse_begin_message(&message, &frame, &address) == STATUS_OK);
867 {
868 UTEST_ASSERT(strcmp(address, "/message001") == 0);
869 UTEST_ASSERT(osc::parse_token(&message, &token) == STATUS_OK);
870 UTEST_ASSERT(token == osc::PT_NULL);
871
872 UTEST_ASSERT(osc::parse_int32(&message, NULL) == STATUS_NULL);
873 UTEST_ASSERT(osc::parse_float32(&message, NULL) == STATUS_NULL);
874 UTEST_ASSERT(osc::parse_string(&message, NULL) == STATUS_NULL);
875 UTEST_ASSERT(osc::parse_blob(&message, NULL, NULL) == STATUS_NULL);
876 UTEST_ASSERT(osc::parse_int64(&message, NULL) == STATUS_NULL);
877 UTEST_ASSERT(osc::parse_double64(&message, NULL) == STATUS_NULL);
878 UTEST_ASSERT(osc::parse_time_tag(&message, NULL) == STATUS_NULL);
879 UTEST_ASSERT(osc::parse_type(&message, NULL) == STATUS_NULL);
880 UTEST_ASSERT(osc::parse_ascii(&message, NULL) == STATUS_NULL);
881 UTEST_ASSERT(osc::parse_rgba(&message, NULL) == STATUS_NULL);
882 UTEST_ASSERT(osc::parse_midi(&message, NULL) == STATUS_NULL);
883 UTEST_ASSERT(osc::parse_midi_raw(&message, NULL, 0) == STATUS_NULL);
884 UTEST_ASSERT(osc::parse_bool(&message, NULL) == STATUS_NULL);
885 UTEST_ASSERT(osc::parse_null(&message) == STATUS_OK);
886 UTEST_ASSERT(osc::parse_inf(&message) == STATUS_BAD_TYPE);
887 UTEST_ASSERT(osc::parse_null(&message) == STATUS_OK);
888
889 UTEST_ASSERT(osc::parse_int32(&message, NULL) == STATUS_BAD_TYPE);
890 UTEST_ASSERT(osc::parse_inf(&message) == STATUS_OK);
891 UTEST_ASSERT(osc::parse_null(&message) == STATUS_BAD_TYPE);
892 UTEST_ASSERT(osc::parse_inf(&message) == STATUS_OK);
893
894 UTEST_ASSERT(osc::parse_float32(&message, &f32) == STATUS_OK);
895 UTEST_ASSERT(isinf(f32));
896 UTEST_ASSERT(osc::parse_double64(&message, &f64) == STATUS_OK);
897 UTEST_ASSERT(isinf(f64));
898
899 UTEST_ASSERT(osc::parse_null(&message) == STATUS_EOF);
900 UTEST_ASSERT(osc::parse_token(&message, &token) == STATUS_OK);
901 UTEST_ASSERT(token == osc::PT_EOR);
902 }
903 UTEST_ASSERT(osc::parse_end(&message) == STATUS_OK);
904
905 UTEST_ASSERT(osc::parse_token(&frame, &token) == STATUS_OK);
906 UTEST_ASSERT(token == osc::PT_EOR);
907 }
908 UTEST_ASSERT(osc::parse_end(&frame) == STATUS_OK);
909 UTEST_ASSERT(osc::parse_destroy(&parser) == STATUS_OK);
910 }
911
912 void test_parse_parameters()
913 {
914 // Forge the message using OSC serializer
915 osc::packet_t packet;
916 osc::forge_t forge;
917 osc::forge_frame_t sframe, smessage, sarray1, sarray2;
918
919 midi::event_t src_midi;
920 src_midi.timestamp = 0;
921 src_midi.type = midi::MIDI_MSG_NOTE_ON;
922 src_midi.channel = 4;
923 src_midi.note.pitch = 51;
924 src_midi.note.velocity = 0x7f;
925
926 uint8_t src_raw_midi[4];
927 src_raw_midi[0] = midi::MIDI_MSG_NOTE_OFF | 4;
928 src_raw_midi[1] = 51;
929 src_raw_midi[2] = 0x5f;
930
931 UTEST_ASSERT(osc::forge_begin_dynamic(&sframe, &forge) == STATUS_OK);
932 UTEST_ASSERT(osc::forge_begin_message(&smessage, &sframe, "/address/01") == STATUS_OK);
933 {
934 UTEST_ASSERT(osc::forge_int32(&smessage, 0xdeadbeef) == STATUS_OK);
935 UTEST_ASSERT(osc::forge_float32(&smessage, 440.0f) == STATUS_OK);
936 UTEST_ASSERT(osc::forge_string(&smessage, "string1") == STATUS_OK);
937 UTEST_ASSERT(osc::forge_blob(&smessage, "BLOB1", 5) == STATUS_OK);
938 UTEST_ASSERT(osc::forge_int64(&smessage, uint64_t(0x1234567855aacc33ULL)) == STATUS_OK);
939 UTEST_ASSERT(osc::forge_double64(&smessage, 48000.0) == STATUS_OK);
940 UTEST_ASSERT(osc::forge_time_tag(&smessage, uint64_t(0xff00cc01aa028803ULL)) == STATUS_OK);
941 UTEST_ASSERT(osc::forge_type(&smessage, "type01") == STATUS_OK);
942 UTEST_ASSERT(osc::forge_ascii(&smessage, '\n') == STATUS_OK);
943 UTEST_ASSERT(osc::forge_rgba(&smessage, 0x8080ff40) == STATUS_OK);
944 UTEST_ASSERT(osc::forge_midi(&smessage, &src_midi) == STATUS_OK);
945 UTEST_ASSERT(osc::forge_midi_raw(&smessage, src_raw_midi, 3) == STATUS_OK);
946
947 UTEST_ASSERT(osc::forge_begin_array(&sarray1, &smessage) == STATUS_OK);
948 {
949 UTEST_ASSERT(osc::forge_int32(&sarray1, int32_t(0xfffefcfd)) == STATUS_OK);
950 UTEST_ASSERT(osc::forge_bool(&sarray1, true) == STATUS_OK);
951 UTEST_ASSERT(osc::forge_begin_array(&sarray2, &sarray1) == STATUS_OK);
952 {
953 UTEST_ASSERT(osc::forge_string(&sarray2, "string2") == STATUS_OK);
954 UTEST_ASSERT(osc::forge_blob(&sarray2, "BLOB02", 6) == STATUS_OK);
955 UTEST_ASSERT(osc::forge_int32(&sarray2, int32_t(0xcafeface)) == STATUS_OK);
956 UTEST_ASSERT(osc::forge_inf(&sarray2) == STATUS_OK);
957 }
958 UTEST_ASSERT(osc::forge_end(&sarray2) == STATUS_OK);
959 UTEST_ASSERT(osc::forge_float32(&sarray1, 44100.0f) == STATUS_OK);
960 }
961 UTEST_ASSERT(osc::forge_end(&sarray1) == STATUS_OK);
962
963 UTEST_ASSERT(osc::forge_bool(&smessage, true) == STATUS_OK);
964 UTEST_ASSERT(osc::forge_bool(&smessage, false) == STATUS_OK);
965 UTEST_ASSERT(osc::forge_null(&smessage) == STATUS_OK);
966 UTEST_ASSERT(osc::forge_inf(&smessage) == STATUS_OK);
967 }
968 UTEST_ASSERT(osc::forge_end(&smessage) == STATUS_OK);
969 UTEST_ASSERT(osc::forge_end(&sframe) == STATUS_OK);
970 UTEST_ASSERT(osc::forge_close(&packet, &forge) == STATUS_OK);
971 UTEST_ASSERT(osc::forge_destroy(&forge) == STATUS_OK);
972
973 // Now deserialize and check the packet data
974 osc::parser_t parser;
975 osc::parse_frame_t dframe, dmessage, darray1, darray2;
976 const char *address;
977
978 midi::event_t dst_midi;
979 const uint8_t *dst_raw_midi;
980 const void *blob;
981
982 union
983 {
984 uint32_t u32;
985 float f32;
986 double d64;
987 int32_t i32;
988 uint64_t u64;
989 int64_t i64;
990 size_t sz;
991 char ch;
992 midi::event_t midi;
993 bool tf;
994 } xptr;
995
996 UTEST_ASSERT(osc::parse_begin(&dframe, &parser, packet.data, packet.size) == STATUS_OK);
997 UTEST_ASSERT(osc::parse_begin_message(&dmessage, &dframe, &address) == STATUS_OK);
998 {
999 UTEST_ASSERT(osc::parse_int32(&dmessage, &xptr.i32) == STATUS_OK);
1000 UTEST_ASSERT(xptr.i32 == int32_t(0xdeadbeef));
1001 UTEST_ASSERT(osc::parse_float32(&dmessage, &xptr.f32) == STATUS_OK);
1002 UTEST_ASSERT(xptr.f32 == 440.0f);
1003 UTEST_ASSERT(osc::parse_string(&dmessage, &address) == STATUS_OK);
1004 UTEST_ASSERT(strcmp(address, "string1") == 0);
1005 UTEST_ASSERT(osc::parse_blob(&dmessage, &blob, &xptr.sz) == STATUS_OK);
1006 UTEST_ASSERT(xptr.sz == 5);
1007 UTEST_ASSERT(memcmp(blob, "BLOB1", xptr.sz) == 0);
1008 UTEST_ASSERT(osc::parse_int64(&dmessage, &xptr.i64) == STATUS_OK);
1009 UTEST_ASSERT(xptr.i64 == int64_t(0x1234567855aacc33ULL));
1010 UTEST_ASSERT(osc::parse_double64(&dmessage, &xptr.d64) == STATUS_OK);
1011 UTEST_ASSERT(xptr.d64 == 48000.0);
1012 UTEST_ASSERT(osc::parse_time_tag(&dmessage, &xptr.u64) == STATUS_OK);
1013 UTEST_ASSERT(xptr.u64 == uint64_t(0xff00cc01aa028803ULL));
1014 UTEST_ASSERT(osc::parse_type(&dmessage, &address) == STATUS_OK);
1015 UTEST_ASSERT(strcmp(address, "type01") == 0);
1016 UTEST_ASSERT(osc::parse_ascii(&dmessage, &xptr.ch) == STATUS_OK);
1017 UTEST_ASSERT(xptr.ch == '\n');
1018 UTEST_ASSERT(osc::parse_rgba(&dmessage, &xptr.u32) == STATUS_OK);
1019 UTEST_ASSERT(xptr.u32 == 0x8080ff40);
1020 UTEST_ASSERT(osc::parse_midi(&dmessage, &dst_midi) == STATUS_OK);
1021 UTEST_ASSERT((src_midi.type == dst_midi.type) &&
1022 (src_midi.channel == dst_midi.channel) &&
1023 (src_midi.note.pitch == dst_midi.note.pitch) &&
1024 (src_midi.note.velocity == dst_midi.note.velocity));
1025 UTEST_ASSERT(osc::parse_midi_raw(&dmessage, &dst_raw_midi, &xptr.sz) == STATUS_OK);
1026 UTEST_ASSERT(xptr.sz == 3);
1027 UTEST_ASSERT(memcmp(src_raw_midi, dst_raw_midi, xptr.sz) == 0);
1028
1029 UTEST_ASSERT(osc::parse_begin_array(&darray1, &dmessage) == STATUS_OK);
1030 {
1031 UTEST_ASSERT(osc::parse_int32(&darray1, &xptr.i32) == STATUS_OK);
1032 UTEST_ASSERT(xptr.i32 == int32_t(0xfffefcfd));
1033 UTEST_ASSERT(osc::parse_bool(&darray1, &xptr.tf) == STATUS_OK);
1034 UTEST_ASSERT(xptr.tf == true);
1035
1036 UTEST_ASSERT(osc::parse_begin_array(&darray2, &darray1) == STATUS_OK);
1037 {
1038 UTEST_ASSERT(osc::parse_string(&darray2, &address) == STATUS_OK);
1039 UTEST_ASSERT(strcmp(address, "string2") == 0);
1040 UTEST_ASSERT(osc::parse_blob(&darray2, &blob, &xptr.sz) == STATUS_OK);
1041 UTEST_ASSERT(xptr.sz == 6);
1042 UTEST_ASSERT(memcmp(blob, "BLOB02", xptr.sz) == 0);
1043 UTEST_ASSERT(osc::parse_int32(&darray2, &xptr.i32) == STATUS_OK);
1044 UTEST_ASSERT(xptr.i32 == int32_t(0xcafeface));
1045 UTEST_ASSERT(osc::parse_inf(&darray2) == STATUS_OK);
1046 }
1047 UTEST_ASSERT(osc::parse_end(&darray2) == STATUS_OK);
1048 UTEST_ASSERT(osc::parse_float32(&darray1, &xptr.f32) == STATUS_OK);
1049 UTEST_ASSERT(xptr.f32 == 44100.0f);
1050 }
1051 UTEST_ASSERT(osc::parse_end(&darray1) == STATUS_OK);
1052
1053 UTEST_ASSERT(osc::parse_bool(&dmessage, &xptr.tf) == STATUS_OK);
1054 UTEST_ASSERT(xptr.tf == true);
1055 UTEST_ASSERT(osc::parse_bool(&dmessage, &xptr.tf) == STATUS_OK);
1056 UTEST_ASSERT(xptr.tf == false);
1057 UTEST_ASSERT(osc::parse_null(&dmessage) == STATUS_OK);
1058 UTEST_ASSERT(osc::parse_inf(&dmessage) == STATUS_OK);
1059 }
1060 UTEST_ASSERT(osc::parse_end(&dmessage) == STATUS_OK);
1061 UTEST_ASSERT(osc::parse_end(&dframe) == STATUS_OK);
1062 UTEST_ASSERT(osc::parse_destroy(&parser) == STATUS_OK);
1063
1064 // Deserialize again, test skips
1065 UTEST_ASSERT(osc::parse_begin(&dframe, &parser, packet.data, packet.size) == STATUS_OK);
1066 UTEST_ASSERT(osc::parse_begin_message(&dmessage, &dframe, &address) == STATUS_OK);
1067 {
1068 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1069 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1070 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1071 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1072 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1073 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1074 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1075 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1076 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1077 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1078 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1079 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1080
1081 UTEST_ASSERT(osc::parse_begin_array(&darray1, &dmessage) == STATUS_OK);
1082 {
1083 UTEST_ASSERT(osc::parse_skip(&darray1) == STATUS_OK);
1084 UTEST_ASSERT(osc::parse_skip(&darray1) == STATUS_OK);
1085 UTEST_ASSERT(osc::parse_skip(&darray1) == STATUS_OK);
1086 UTEST_ASSERT(osc::parse_skip(&darray1) == STATUS_OK);
1087 UTEST_ASSERT(osc::parse_skip(&darray1) == STATUS_EOF);
1088 }
1089 UTEST_ASSERT(osc::parse_end(&darray1) == STATUS_OK);
1090
1091 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1092 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1093 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1094 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1095 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_EOF);
1096 }
1097 UTEST_ASSERT(osc::parse_end(&dmessage) == STATUS_OK);
1098 UTEST_ASSERT(osc::parse_end(&dframe) == STATUS_OK);
1099 UTEST_ASSERT(osc::parse_destroy(&parser) == STATUS_OK);
1100
1101 // Free the packet data
1102 osc::forge_free(packet.data);
1103 }
1104
1105 void test_parse_bundles()
1106 {
1107 // Serialize message
1108 osc::packet_t packet;
1109 osc::forge_t forge;
1110 osc::forge_frame_t sframe, sbundle1, sbundle2, smessage;
1111
1112 UTEST_ASSERT(osc::forge_begin_dynamic(&sframe, &forge) == STATUS_OK);
1113
1114 UTEST_ASSERT(osc::forge_begin_bundle(&sbundle1, &sframe, uint64_t(0x1122334455667788ULL)) == STATUS_OK);
1115 {
1116 // Sub-bundle 1
1117 UTEST_ASSERT(osc::forge_begin_bundle(&sbundle2, &sbundle1, uint64_t(0x2233445566778899ULL)) == STATUS_OK);
1118 {
1119 UTEST_ASSERT(osc::forge_begin_message(&smessage, &sbundle2, "/a0") == STATUS_OK);
1120 {
1121 UTEST_ASSERT(osc::forge_int32(&smessage, int32_t(0xdeadbeef)) == STATUS_OK);
1122 }
1123 UTEST_ASSERT(osc::forge_end(&smessage) == STATUS_OK);
1124
1125 UTEST_ASSERT(osc::forge_begin_message(&smessage, &sbundle2, "/addr0") == STATUS_OK);
1126 {
1127 UTEST_ASSERT(osc::forge_bool(&smessage, true) == STATUS_OK);
1128 UTEST_ASSERT(osc::forge_bool(&smessage, false) == STATUS_OK);
1129 UTEST_ASSERT(osc::forge_int32(&smessage, int32_t(0xcafeface)) == STATUS_OK);
1130 UTEST_ASSERT(osc::forge_float32(&smessage, 440.0f) == STATUS_OK);
1131 }
1132 UTEST_ASSERT(osc::forge_end(&smessage) == STATUS_OK);
1133 }
1134 UTEST_ASSERT(osc::forge_end(&sbundle2) == STATUS_OK);
1135
1136 // Sub-bundle 2
1137 UTEST_ASSERT(osc::forge_begin_bundle(&sbundle2, &sbundle1, uint64_t(0x5566778899aabbccULL)) == STATUS_OK);
1138 {
1139 UTEST_ASSERT(osc::forge_begin_message(&smessage, &sbundle2, "/b0") == STATUS_OK);
1140 {
1141 UTEST_ASSERT(osc::forge_inf(&smessage) == STATUS_OK);
1142 UTEST_ASSERT(osc::forge_int32(&smessage, int32_t(0xf00dfeed)) == STATUS_OK);
1143 }
1144 UTEST_ASSERT(osc::forge_end(&smessage) == STATUS_OK);
1145
1146 UTEST_ASSERT(osc::forge_begin_message(&smessage, &sbundle2, "/c0") == STATUS_OK);
1147 {
1148 UTEST_ASSERT(osc::forge_bool(&smessage, true) == STATUS_OK);
1149 UTEST_ASSERT(osc::forge_bool(&smessage, false) == STATUS_OK);
1150 UTEST_ASSERT(osc::forge_null(&smessage) == STATUS_OK);
1151 }
1152 UTEST_ASSERT(osc::forge_end(&smessage) == STATUS_OK);
1153 }
1154 UTEST_ASSERT(osc::forge_end(&sbundle2) == STATUS_OK);
1155
1156 // Sub-message
1157 UTEST_ASSERT(osc::forge_begin_message(&smessage, &sbundle1, "/addr1") == STATUS_OK);
1158 {
1159 UTEST_ASSERT(osc::forge_bool(&smessage, true) == STATUS_OK);
1160 UTEST_ASSERT(osc::forge_bool(&smessage, false) == STATUS_OK);
1161 UTEST_ASSERT(osc::forge_int32(&smessage, int32_t(0x55aacc33)) == STATUS_OK);
1162 UTEST_ASSERT(osc::forge_null(&smessage) == STATUS_OK);
1163 UTEST_ASSERT(osc::forge_inf(&smessage) == STATUS_OK);
1164 }
1165 UTEST_ASSERT(osc::forge_end(&smessage) == STATUS_OK);
1166 }
1167 UTEST_ASSERT(osc::forge_end(&sbundle1) == STATUS_OK);
1168
1169 UTEST_ASSERT(osc::forge_end(&sframe) == STATUS_OK);
1170 UTEST_ASSERT(osc::forge_close(&packet, &forge) == STATUS_OK);
1171 UTEST_ASSERT(osc::forge_destroy(&forge) == STATUS_OK);
1172
1173 // Test reading
1174 union
1175 {
1176 uint32_t u32;
1177 float f32;
1178 double d64;
1179 int32_t i32;
1180 uint64_t u64;
1181 int64_t i64;
1182 size_t sz;
1183 char ch;
1184 midi::event_t midi;
1185 bool tf;
1186 } xptr;
1187
1188 osc::parser_t parser;
1189 osc::parse_frame_t dframe, dbundle1, dbundle2, dmessage;
1190 const char *address;
1191 const void *bytes;
1192 osc::parse_token_t token;
1193
1194 UTEST_ASSERT(osc::parse_begin(&dframe, &parser, packet.data, packet.size) == STATUS_OK);
1195 UTEST_ASSERT(osc::parse_token(&dframe, &token) == STATUS_OK);
1196 UTEST_ASSERT(token == osc::PT_BUNDLE);
1197 UTEST_ASSERT(osc::parse_begin_bundle(&dbundle1, &dframe, &xptr.u64) == STATUS_OK);
1198 UTEST_ASSERT(xptr.u64 == uint64_t(0x1122334455667788ULL));
1199 {
1200 // Sub-bundle 1
1201 UTEST_ASSERT(osc::parse_token(&dbundle1, &token) == STATUS_OK);
1202 UTEST_ASSERT(token == osc::PT_BUNDLE);
1203 UTEST_ASSERT(osc::parse_begin_bundle(&dbundle2, &dbundle1, &xptr.u64) == STATUS_OK);
1204 UTEST_ASSERT(xptr.u64 == uint64_t(0x2233445566778899ULL));
1205 {
1206 UTEST_ASSERT(osc::parse_token(&dbundle2, &token) == STATUS_OK);
1207 UTEST_ASSERT(token == osc::PT_MESSAGE);
1208 UTEST_ASSERT(osc::parse_begin_message(&dmessage, &dbundle2, &address) == STATUS_OK);
1209 UTEST_ASSERT(strcmp(address, "/a0") == 0);
1210 {
1211 UTEST_ASSERT(osc::parse_int32(&dmessage, &xptr.i32) == STATUS_OK);
1212 UTEST_ASSERT(xptr.i32 == int32_t(0xdeadbeef));
1213 }
1214 UTEST_ASSERT(osc::parse_end(&dmessage) == STATUS_OK);
1215
1216 UTEST_ASSERT(osc::parse_token(&dbundle2, &token) == STATUS_OK);
1217 UTEST_ASSERT(token == osc::PT_MESSAGE);
1218 UTEST_ASSERT(osc::parse_begin_message(&dmessage, &dbundle2, &address) == STATUS_OK);
1219 UTEST_ASSERT(strcmp(address, "/addr0") == 0);
1220 {
1221 UTEST_ASSERT(osc::parse_bool(&dmessage, &xptr.tf) == STATUS_OK);
1222 UTEST_ASSERT(xptr.tf == true);
1223 UTEST_ASSERT(osc::parse_bool(&dmessage, &xptr.tf) == STATUS_OK);
1224 UTEST_ASSERT(xptr.tf == false);
1225 UTEST_ASSERT(osc::parse_int32(&dmessage, &xptr.i32) == STATUS_OK);
1226 UTEST_ASSERT(xptr.i32 == int32_t(0xcafeface));
1227 UTEST_ASSERT(osc::parse_float32(&dmessage, &xptr.f32) == STATUS_OK);
1228 UTEST_ASSERT(xptr.f32 == 440.0f);
1229 }
1230 UTEST_ASSERT(osc::parse_end(&dmessage) == STATUS_OK);
1231 UTEST_ASSERT(osc::parse_end(&dmessage) != STATUS_OK);
1232 }
1233 UTEST_ASSERT(osc::parse_end(&dbundle2) == STATUS_OK);
1234 UTEST_ASSERT(osc::parse_end(&dbundle2) != STATUS_OK);
1235
1236 // Sub-bundle 2
1237 UTEST_ASSERT(osc::parse_token(&dbundle1, &token) == STATUS_OK);
1238 UTEST_ASSERT(token == osc::PT_BUNDLE);
1239 UTEST_ASSERT(osc::parse_begin_bundle(&dbundle2, &dbundle1, &xptr.u64) == STATUS_OK);
1240 UTEST_ASSERT(xptr.u64 == uint64_t(0x5566778899aabbccULL));
1241 {
1242 UTEST_ASSERT(osc::parse_token(&dbundle2, &token) == STATUS_OK);
1243 UTEST_ASSERT(token == osc::PT_MESSAGE);
1244
1245 UTEST_ASSERT(osc::parse_raw_message(&dbundle2, &bytes, &xptr.sz, &address) == STATUS_OK);
1246 UTEST_ASSERT(strcmp(address, "/b0") == 0);
1247 UTEST_ASSERT(bytes == address);
1248 UTEST_ASSERT(xptr.sz == 12); // 4 bytes per address, 4 bytes per args, 4 bytes per int32 argument
1249
1250 UTEST_ASSERT(osc::parse_begin_message(&dmessage, &dbundle2, &address) == STATUS_OK);
1251 UTEST_ASSERT(strcmp(address, "/b0") == 0);
1252 {
1253 UTEST_ASSERT(osc::parse_inf(&dmessage) == STATUS_OK);
1254 UTEST_ASSERT(osc::parse_int32(&dmessage, &xptr.i32) == STATUS_OK);
1255 UTEST_ASSERT(xptr.i32 == int32_t(0xf00dfeed));
1256 }
1257 UTEST_ASSERT(osc::parse_end(&dmessage) == STATUS_OK);
1258 UTEST_ASSERT(osc::parse_end(&dmessage) != STATUS_OK);
1259
1260 UTEST_ASSERT(osc::parse_token(&dbundle2, &token) == STATUS_OK);
1261 UTEST_ASSERT(token == osc::PT_MESSAGE);
1262 UTEST_ASSERT(osc::parse_begin_message(&dmessage, &dbundle2, &address) == STATUS_OK);
1263 UTEST_ASSERT(strcmp(address, "/c0") == 0);
1264 {
1265 UTEST_ASSERT(osc::parse_bool(&dmessage, &xptr.tf) == STATUS_OK);
1266 UTEST_ASSERT(xptr.tf == true);
1267 UTEST_ASSERT(osc::parse_bool(&dmessage, &xptr.tf) == STATUS_OK);
1268 UTEST_ASSERT(xptr.tf == false);
1269 UTEST_ASSERT(osc::parse_null(&dmessage) == STATUS_OK);
1270 }
1271 UTEST_ASSERT(osc::parse_end(&dmessage) == STATUS_OK);
1272 }
1273 UTEST_ASSERT(osc::parse_end(&dbundle2) == STATUS_OK);
1274
1275 // Sub-message
1276 UTEST_ASSERT(osc::parse_token(&dbundle1, &token) == STATUS_OK);
1277 UTEST_ASSERT(token == osc::PT_MESSAGE);
1278 UTEST_ASSERT(osc::parse_begin_message(&dmessage, &dbundle1, &address) == STATUS_OK);
1279 UTEST_ASSERT(strcmp(address, "/addr1") == 0);
1280 {
1281 UTEST_ASSERT(osc::parse_bool(&dmessage, &xptr.tf) == STATUS_OK);
1282 UTEST_ASSERT(xptr.tf == true);
1283 UTEST_ASSERT(osc::parse_bool(&dmessage, &xptr.tf) == STATUS_OK);
1284 UTEST_ASSERT(xptr.tf == false);
1285 UTEST_ASSERT(osc::parse_int32(&dmessage, &xptr.i32) == STATUS_OK);
1286 UTEST_ASSERT(xptr.i32 == int32_t(0x55aacc33));
1287 UTEST_ASSERT(osc::parse_null(&dmessage) == STATUS_OK);
1288 UTEST_ASSERT(osc::parse_inf(&dmessage) == STATUS_OK);
1289 }
1290 UTEST_ASSERT(osc::parse_end(&dmessage) == STATUS_OK);
1291 UTEST_ASSERT(osc::parse_end(&dmessage) != STATUS_OK);
1292 }
1293 UTEST_ASSERT(osc::parse_end(&dbundle1) == STATUS_OK);
1294 UTEST_ASSERT(osc::parse_end(&dbundle1) != STATUS_OK);
1295
1296 UTEST_ASSERT(osc::parse_end(&dframe) == STATUS_OK);
1297 UTEST_ASSERT(osc::parse_destroy(&parser) == STATUS_OK);
1298
1299
1300 // Test skipping
1301 UTEST_ASSERT(osc::parse_begin(&dframe, &parser, packet.data, packet.size) == STATUS_OK);
1302 UTEST_ASSERT(osc::parse_begin_bundle(&dbundle1, &dframe, NULL) == STATUS_OK);
1303 {
1304 // Sub-bundle 1
1305 UTEST_ASSERT(osc::parse_begin_bundle(&dbundle2, &dbundle1, NULL) == STATUS_OK);
1306 {
1307 UTEST_ASSERT(osc::parse_skip(&dbundle2) == STATUS_OK);
1308
1309 UTEST_ASSERT(osc::parse_begin_message(&dmessage, &dbundle2, NULL) == STATUS_OK);
1310 {
1311 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1312 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1313 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1314 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_OK);
1315 UTEST_ASSERT(osc::parse_skip(&dmessage) == STATUS_EOF);
1316 }
1317 UTEST_ASSERT(osc::parse_end(&dmessage) == STATUS_OK);
1318
1319 UTEST_ASSERT(osc::parse_skip(&dbundle2) == STATUS_EOF);
1320 }
1321 UTEST_ASSERT(osc::parse_end(&dbundle2) == STATUS_OK);
1322
1323 // Sub-bundle 2
1324 UTEST_ASSERT(osc::parse_skip(&dbundle1) == STATUS_OK);
1325
1326 // Sub-message
1327 UTEST_ASSERT(osc::parse_token(&dbundle1, &token) == STATUS_OK);
1328 UTEST_ASSERT(token == osc::PT_MESSAGE);
1329 UTEST_ASSERT(osc::parse_skip(&dbundle1) == STATUS_OK);
1330
1331 UTEST_ASSERT(osc::parse_skip(&dbundle1) == STATUS_EOF);
1332 }
1333 UTEST_ASSERT(osc::parse_end(&dbundle1) == STATUS_OK);
1334
1335 UTEST_ASSERT(osc::parse_skip(&dframe) == STATUS_EOF);
1336
1337 UTEST_ASSERT(osc::parse_end(&dframe) == STATUS_OK);
1338 UTEST_ASSERT(osc::parse_destroy(&parser) == STATUS_OK);
1339
1340 // Free the packet data
1341 osc::forge_free(packet.data);
1342 }
1343
1344 void test_pattern()
1345 {
1346 osc::pattern_t pattern;
1347
1348 for (const char **p = valid_patterns; *p != NULL; ++p)
1349 {
1350 printf(" testing address pattern: %s\n", *p);
1351 UTEST_ASSERT(osc::pattern_create(&pattern, *p) == STATUS_OK);
1352 UTEST_ASSERT(osc::pattern_destroy(&pattern) == STATUS_OK);
1353 }
1354
1355 for (const char **p = invalid_patterns; *p != NULL; ++p)
1356 {
1357 printf(" testing address pattern: %s\n", *p);
1358 UTEST_ASSERT(osc::pattern_create(&pattern, *p) == STATUS_BAD_FORMAT);
1359 }
1360
1361 for (size_t i=0, n=sizeof(match_pattern)/sizeof(pattern_match_t); i < n; ++i)
1362 {
1363 const pattern_match_t *m = &match_pattern[i];
1364
1365 printf(" matching string \"%s\" to pattern \"%s\"\n", m->address, m->pattern);
1366
1367 UTEST_ASSERT(osc::pattern_create(&pattern, m->pattern) == STATUS_OK);
1368 UTEST_ASSERT(osc::pattern_match(&pattern, m->address) == m->match);
1369 UTEST_ASSERT(osc::pattern_destroy(&pattern) == STATUS_OK);
1370 }
1371 }
1372
1373 UTEST_MAIN
1374 {
1375 #define CALL(v) printf("Executing " #v "...\n"); v();
1376
1377 CALL(test_forge_simple_message);
1378 CALL(test_forge_parameters);
1379 CALL(test_forge_message_bundle);
1380 CALL(test_forge_overflow);
1381
1382 CALL(test_parse_legacy_message);
1383 CALL(test_parse_simple_message);
1384 CALL(test_parse_specials_message);
1385 CALL(test_parse_parameters);
1386 CALL(test_parse_bundles);
1387
1388 CALL(test_format);
1389
1390 CALL(test_pattern);
1391 }
1392 UTEST_END;
1393
1394