Skip to content

Commit

Permalink
invalid emsg boxes and test
Browse files Browse the repository at this point in the history
  • Loading branch information
adrums86 committed Feb 7, 2023
1 parent 1d621d4 commit 54d9645
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/mp4/emsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ var parseEmsgBox = function(boxData) {
offset += scheme_id_uri.length;
value = uint8ToCString(boxData.subarray(offset));
offset += value.length;
} else {
// Invalid EMSG box.
return;
}

var message_data = new Uint8Array(boxData.subarray(offset, boxData.byteLength));
Expand Down
6 changes: 6 additions & 0 deletions test/mp4-emsg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ QUnit.test('Can parse a v1 emsg box', function(assert) {
assert.equal(parsedBox.id, 2, 'v1 box has expected id');
assert.deepEqual(parsedBox.message_data, messageData, 'v1 box has expected data');
});

QUnit.test('Will return undefined if the emsg box is invalid', function(assert) {
var badBoxData = generateEmsgBoxData(3, messageData);
var parsedBox = emsg.parseEmsgBox(badBoxData);
assert.equal(parsedBox, undefined, 'parsed box is undefined');
});
15 changes: 14 additions & 1 deletion test/utils/mp4-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ var sampleMoov =

/**
* Generates generic emsg box data for both v0 and v1 boxes concats the messageData
* and returns the result as a Uint8Array
* and returns the result as a Uint8Array. Passing any version other than 0 or 1 will
* return an invalid EMSG box.
*/
var generateEmsgBoxData = function(version, messageData) {
var emsgProps;
Expand All @@ -339,6 +340,18 @@ var generateEmsgBoxData = function(version, messageData) {
0x75, 0x72, 0x6E, 0x3A, 0x66, 0x6F, 0x6F, 0x3A, 0x62, 0x61, 0x72, 0x3A, 0x32, 0x30, 0x32, 0x33, 0x00, // urn:foo:bar:2023\0
0x66, 0x6F, 0x6F, 0x2E, 0x62, 0x61, 0x72, 0x2E, 0x76, 0x61, 0x6C, 0x75, 0x65, 0x00 // foo.bar.value\0
]);
} else {
// Invalid EMSG box
emsgProps = new Uint8Array([
0x02, // version
0x00, 0x00, 0x00, //flags
0x00, 0x00, 0x00, 0x64, // timescale = 100
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x10, // presentation_time = 10000
0x00, 0x00, 0x00, 0x01, // event_duration = 1
0x00, 0x00, 0x00, 0x02, // id = 2
0x75, 0x72, 0x6E, 0x3A, 0x66, 0x6F, 0x6F, 0x3A, 0x62, 0x61, 0x72, 0x3A, 0x32, 0x30, 0x32, 0x33, 0x00, // urn:foo:bar:2023\0
0x66, 0x6F, 0x6F, 0x2E, 0x62, 0x61, 0x72, 0x2E, 0x76, 0x61, 0x6C, 0x75, 0x65, 0x00 // foo.bar.value\0
]);
}

// concat the props and messageData
Expand Down

0 comments on commit 54d9645

Please sign in to comment.