From bf355706bb393a2eff33249f26873ff737cda754 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Tue, 15 Oct 2024 21:55:51 -0500 Subject: [PATCH] more bundle tests --- packages/osc/tests/bundle-decode.test.js | 109 ++++++++++++++++++++++- 1 file changed, 105 insertions(+), 4 deletions(-) diff --git a/packages/osc/tests/bundle-decode.test.js b/packages/osc/tests/bundle-decode.test.js index 2f6e53c..3c4777c 100644 --- a/packages/osc/tests/bundle-decode.test.js +++ b/packages/osc/tests/bundle-decode.test.js @@ -1,8 +1,8 @@ -const { deepEqual, equal } = require('assert'); +const { deepEqual, equal, throws } = require('assert'); const { describe, it } = require('node:test'); const osc = require('../dist/index'); -const tests = [ +const goodTests = [ { description: 'simple contents single message', bytes: new Uint8Array([ @@ -20,6 +20,34 @@ const tests = [ contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }], }, }, + { + description: 'simple contents single bundle', + bytes: new Uint8Array([ + ...new TextEncoder().encode('#bundle'), + ...new Uint8Array([0x00]), + ...new Uint8Array([0, 0, 0, 32, 0, 0, 0, 0]), + ...new Uint8Array([0x00, 0x00, 0x00, 0x34]), + ...new Uint8Array([ + ...new TextEncoder().encode('#bundle'), + ...new Uint8Array([0x00]), + ...new Uint8Array([0, 0, 0, 32, 0, 0, 0, 0]), + ...new Uint8Array([0x00, 0x00, 0x00, 0x20]), + ...new Uint8Array([ + 0x2f, 0x6f, 0x73, 0x63, 0x69, 0x6c, 0x6c, 0x61, 0x74, 0x6f, 0x72, 0x2f, 0x34, 0x2f, 0x66, 0x72, 0x65, 0x71, + 0x75, 0x65, 0x6e, 0x63, 0x79, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x43, 0xdc, 0x00, 0x00, + ]), + ]), + ]), + expected: { + timeTag: [32, 0], + contents: [ + { + timeTag: [32, 0], + contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }], + }, + ], + }, + }, { description: 'resolume bundle example', bytes: new Uint8Array([ @@ -88,8 +116,71 @@ const tests = [ }, ]; -describe('OSC Bundle Decoding', () => { - tests.forEach((bundleTest) => { +const badTests = [ + { + description: 'bad bundle header', + bytes: new Uint8Array([ + ...new TextEncoder().encode('bundle'), + ...new Uint8Array([0x00]), + ...new Uint8Array([0, 0, 0, 32, 0, 0, 0, 0]), + ...new Uint8Array([0x00, 0x00, 0x00, 0x20]), + ...new Uint8Array([ + 0x2f, 0x6f, 0x73, 0x63, 0x69, 0x6c, 0x6c, 0x61, 0x74, 0x6f, 0x72, 0x2f, 0x34, 0x2f, 0x66, 0x72, 0x65, 0x71, + 0x75, 0x65, 0x6e, 0x63, 0x79, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x43, 0xdc, 0x00, 0x00, + ]), + ]), + throwsMessage: { + name: /^Error$/, + message: 'bundle must start with #bundle', + }, + }, + { + description: 'incomplete bundle', + bytes: new Uint8Array([ + ...new TextEncoder().encode('#bundle'), + ...new Uint8Array([0x00]), + ...new Uint8Array([0, 0, 0, 32, 0, 0, 0, 0]), + ]), + throwsMessage: { + name: /^Error$/, + message: 'bundle has to be at least 20 bytes', + }, + }, + { + description: 'bundle not enough bytes', + bytes: new Uint8Array([ + ...new TextEncoder().encode('#bundle'), + ...new Uint8Array([0x00]), + ...new Uint8Array([0, 0, 0, 32, 0, 0, 0, 0]), + ...new Uint8Array([0x00, 0x00, 0x00, 0x20]), + ...new Uint8Array([0x2f, 0x6f, 0x73, 0x63, 0x69, 0x6c, 0x6c, 0x61]), + ]), + throwsMessage: { + name: /^Error$/, + message: 'bundle does not contain enough data', + }, + }, + { + description: 'bad bundle contents', + bytes: new Uint8Array([ + ...new TextEncoder().encode('#bundle'), + ...new Uint8Array([0x00]), + ...new Uint8Array([0, 0, 0, 32, 0, 0, 0, 0]), + ...new Uint8Array([0x00, 0x00, 0x00, 0x20]), + ...new Uint8Array([ + 0x2a, 0x6f, 0x73, 0x63, 0x69, 0x6c, 0x6c, 0x61, 0x74, 0x6f, 0x72, 0x2f, 0x34, 0x2f, 0x66, 0x72, 0x65, 0x71, + 0x75, 0x65, 0x6e, 0x63, 0x79, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x43, 0xdc, 0x00, 0x00, + ]), + ]), + throwsMessage: { + name: /^Error$/, + message: 'bundle contents does not look like a OSC message or bundle', + }, + }, +]; + +describe('OSC Bundle Decoding Pass', () => { + goodTests.forEach((bundleTest) => { it(bundleTest.description, () => { const [encoded, remainingBytes] = osc.bundleFromBuffer(bundleTest.bytes); equal(remainingBytes.length, 0); @@ -97,3 +188,13 @@ describe('OSC Bundle Decoding', () => { }); }); }); + +describe('OSC Bundle Decoding Throws', () => { + badTests.forEach((bundleTest) => { + it(bundleTest.description, () => { + throws(() => { + osc.bundleFromBuffer(bundleTest.bytes); + }, bundleTest.throwsMessage); + }); + }); +});