Skip to content

Commit

Permalink
Test co64 and stsz atoms
Browse files Browse the repository at this point in the history
Add unit tests to test `co64` and `stsz` atoms.
  • Loading branch information
gkozlenko committed Apr 28, 2024
1 parent 580d434 commit 194a5e4
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
55 changes: 55 additions & 0 deletions test/mp4-atoms/atom-co64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

const Atom = require('../../lib/mp4/atoms/atom-co64');
const BufferUtils = require('../../lib/buffer-utils');

const chai = require('chai');
const expect = chai.expect;

const Utils = require('../lib/utils');

describe('co64', function () {
describe('#parse()', function () {
it('should parse a well-built buffer', function () {
let entries = [
Utils.randInt(111111111111, 999999999999999),
Utils.randInt(111111111111, 999999999999999),
Utils.randInt(111111111111, 999999999999999),
];
let buffer = Buffer.alloc(8 + 8 * entries.length);
buffer.writeUInt32BE(entries.length, 4);
for (let i = 0; i < entries.length; i++) {
BufferUtils.writeUInt64BE(buffer, entries[i], 8 + 8 * i);
}

let atom = new Atom();
atom.parse(buffer);

expect(atom.entries).to.deep.equal(entries);
});
});

describe('#build()', function () {
it('should build a correct buffer', function () {
let entries = [
Utils.randInt(111111111111, 999999999999999),
Utils.randInt(111111111111, 999999999999999),
Utils.randInt(111111111111, 999999999999999),
];

let atom = new Atom();
atom.entries = entries;

let buffer = Buffer.alloc(16 + 8 * entries.length);
atom.build(buffer, 0);

expect(buffer.readUInt32BE(0)).to.be.equal(buffer.length);
expect(buffer.toString('ascii', 4, 8)).to.be.equal('co64');

atom = new Atom();
atom.parse(buffer.subarray(8));

expect(atom.entries).to.deep.equal(entries);
});
});
});
72 changes: 72 additions & 0 deletions test/mp4-atoms/atom-stsz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

const Atom = require('../../lib/mp4/atoms/atom-stsz');

const chai = require('chai');
const expect = chai.expect;

const Utils = require('../lib/utils');

describe('stsz', function () {
describe('#parse()', function () {
it('should parse a well-built buffer with different entries', function () {
let entries = [
Utils.randInt(),
Utils.randInt(),
Utils.randInt(),
];
let buffer = Buffer.alloc(12 + 4 * entries.length);
buffer.writeUInt32BE(0, 4);
buffer.writeUInt32BE(entries.length, 8);
for (let i = 0; i < entries.length; i++) {
buffer.writeUInt32BE(entries[i], 12 + 4 * i);
}

let atom = new Atom();
atom.parse(buffer);

expect(atom.entries).to.deep.equal(entries);
});

it('should parse a well-built buffer with identical entries', function () {
let value = Utils.randInt();
let entries = [
value,
value,
value,
];
let buffer = Buffer.alloc(12);
buffer.writeUInt32BE(value, 4);
buffer.writeUInt32BE(entries.length, 8);

let atom = new Atom();
atom.parse(buffer);

expect(atom.entries).to.deep.equal(entries);
});
});

describe('#build()', function () {
it('should build a correct buffer', function () {
let entries = [
Utils.randInt(),
Utils.randInt(),
Utils.randInt(),
];

let atom = new Atom();
atom.entries = entries;

let buffer = Buffer.alloc(20 + 4 * entries.length);
atom.build(buffer, 0);

expect(buffer.readUInt32BE(0)).to.be.equal(buffer.length);
expect(buffer.toString('ascii', 4, 8)).to.be.equal('stsz');

atom = new Atom();
atom.parse(buffer.subarray(8));

expect(atom.entries).to.deep.equal(entries);
});
});
});
2 changes: 2 additions & 0 deletions test/mp4-atoms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

describe('MP4Atoms', function () {

require('./atom-co64');
require('./atom-hdlr');
require('./atom-mdhd');
require('./atom-stsz');

});

0 comments on commit 194a5e4

Please sign in to comment.