Improve python library, add LLGLTF asset parser, Add tests

This commit is contained in:
Casper Warden
2023-11-10 00:14:59 +00:00
parent 6c8273ea5e
commit 72d4eff2d8
13 changed files with 591 additions and 348 deletions

View File

@@ -1,167 +1,167 @@
import 'mocha';
import * as fs from 'fs';
import * as path from 'path';
import { Packet } from '../classes/Packet';
import { DecodeFlags } from '../enums/DecodeFlags';
import { PacketFlags } from '../enums/PacketFlags';
function compareArrays(arr1: any[], arr2: any[]): boolean
{
if (arr1.length === arr2.length
&& arr1.every(function(u, i): boolean
{
return u === arr2[i];
})
)
{
return true;
}
else
{
return false;
}
}
describe('Packets', () =>
{
const p = path.resolve(__dirname + '/../../../testing/packets');
const files = fs.readdirSync(p);
for (const file of files)
{
if (file.substr(file.length - 7) === '.packet')
{
const fullPath = p + '/' + file;
const stats = fs.statSync(fullPath);
if (!stats.isDirectory())
{
describe(file, () =>
{
let pos = 0;
let data: Buffer = Buffer.allocUnsafe(0);
const packet: Packet = new Packet();
const acksReceived: number[] = [];
const acksSent: number[] = [];
it('should decode correctly', (done) =>
{
try
{
data = fs.readFileSync(fullPath);
pos = packet.readFromBuffer(data, 0, (number) =>
{
acksReceived.push(number);
}, (number) =>
{
acksSent.push(number);
});
done();
}
catch (err)
{
done(err);
}
});
it('should have read the entire packet', (done) =>
{
if (pos < data.length)
{
done('Finished reading but we\'re not at the end of the packet (' + pos + ' < ' + data.length + ', seq ' + packet.sequenceNumber + ')');
}
else
{
done();
}
});
const jsonFN = fullPath.replace('.packet', '.json');
const jsFile = fs.readFileSync(jsonFN);
const json = JSON.parse(jsFile.toString('utf8'));
it('should have sent the correct number of packet ACKs', (done) =>
{
if (!compareArrays(json.sentAcks, acksSent))
{
done('Sent acks does not match expected');
}
else
{
done();
}
});
it('should have received the correct number of packet ACKs', (done) =>
{
if (!compareArrays(json.receivedAcks, acksReceived))
{
done('Received acks does not match expected');
}
else
{
done();
}
});
it('should match our expected decoded data', (done) =>
{
let pckt = json['packet'];
pckt = JSON.stringify(pckt);
if (JSON.stringify(packet) !== pckt)
{
done('JSON strings do not match');
}
else
{
done();
}
});
let buf = Buffer.allocUnsafe(0);
let extra = 0;
it('should encode back to binary', (done) =>
{
try
{
buf = Buffer.alloc(packet.getSize());
buf = packet.writeToBuffer(buf, 0, DecodeFlags.DontChangeFlags);
// Account for appended acks
let bl = buf.length;
if (packet.packetFlags & PacketFlags.Ack)
{
extra += 4 * acksReceived.length;
extra++;
}
bl += extra;
if (data.length !== bl)
{
console.log(buf.toString('hex'));
console.log(data.toString('hex'));
done('Packet size ' + bl + ' but expected length was ' + data.length + ' sentAcks: ' + acksSent.length + ', receivedAcks: ' + acksReceived.length + ', getSize: ' + packet.getSize());
}
else
{
done();
}
}
catch (err)
{
done(err);
}
});
it('should match the original packet byte-for-byte', (done) =>
{
// First trim off the extra bytes
const trimmedData = data.slice(0, data.length - extra);
if (trimmedData.compare(buf) !== 0)
{
done('Buffers do not match');
}
else
{
done();
}
});
});
}
}
}
});
import 'mocha';
import * as fs from 'fs';
import * as path from 'path';
import { Packet } from '../classes/Packet';
import { DecodeFlags } from '../enums/DecodeFlags';
import { PacketFlags } from '../enums/PacketFlags';
function compareArrays(arr1: any[], arr2: any[]): boolean
{
if (arr1.length === arr2.length
&& arr1.every(function(u, i): boolean
{
return u === arr2[i];
})
)
{
return true;
}
else
{
return false;
}
}
describe('Packets', () =>
{
const p = path.resolve(__dirname + '/../../../testing/packets');
const files = fs.readdirSync(p);
for (const file of files)
{
if (file.substr(file.length - 7) === '.packet')
{
const fullPath = p + '/' + file;
const stats = fs.statSync(fullPath);
if (!stats.isDirectory())
{
describe(file, () =>
{
let pos = 0;
let data: Buffer = Buffer.allocUnsafe(0);
const packet: Packet = new Packet();
const acksReceived: number[] = [];
const acksSent: number[] = [];
it('should decode correctly', (done) =>
{
try
{
data = fs.readFileSync(fullPath);
pos = packet.readFromBuffer(data, 0, (number) =>
{
acksReceived.push(number);
}, (number) =>
{
acksSent.push(number);
});
done();
}
catch (err)
{
done(err);
}
});
it('should have read the entire packet', (done) =>
{
if (pos < data.length)
{
done('Finished reading but we\'re not at the end of the packet (' + pos + ' < ' + data.length + ', seq ' + packet.sequenceNumber + ')');
}
else
{
done();
}
});
const jsonFN = fullPath.replace('.packet', '.json');
const jsFile = fs.readFileSync(jsonFN);
const json = JSON.parse(jsFile.toString('utf8'));
it('should have sent the correct number of packet ACKs', (done) =>
{
if (!compareArrays(json.sentAcks, acksSent))
{
done('Sent acks does not match expected');
}
else
{
done();
}
});
it('should have received the correct number of packet ACKs', (done) =>
{
if (!compareArrays(json.receivedAcks, acksReceived))
{
done('Received acks does not match expected');
}
else
{
done();
}
});
it('should match our expected decoded data', (done) =>
{
let pckt = json['packet'];
pckt = JSON.stringify(pckt);
if (JSON.stringify(packet) !== pckt)
{
done('JSON strings do not match');
}
else
{
done();
}
});
let buf = Buffer.allocUnsafe(0);
let extra = 0;
it('should encode back to binary', (done) =>
{
try
{
buf = Buffer.alloc(packet.getSize());
buf = packet.writeToBuffer(buf, 0, DecodeFlags.DontChangeFlags);
// Account for appended acks
let bl = buf.length;
if (packet.packetFlags & PacketFlags.Ack)
{
extra += 4 * acksReceived.length;
extra++;
}
bl += extra;
if (data.length !== bl)
{
console.log(buf.toString('hex'));
console.log(data.toString('hex'));
done('Packet size ' + bl + ' but expected length was ' + data.length + ' sentAcks: ' + acksSent.length + ', receivedAcks: ' + acksReceived.length + ', getSize: ' + packet.getSize());
}
else
{
done();
}
}
catch (err)
{
done(err);
}
});
it('should match the original packet byte-for-byte', (done) =>
{
// First trim off the extra bytes
const trimmedData = data.slice(0, data.length - extra);
if (trimmedData.compare(buf) !== 0)
{
done('Buffers do not match');
}
else
{
done();
}
});
});
}
}
}
});

View File

@@ -1,109 +1,109 @@
import 'mocha';
import validator from 'validator';
import * as assert from 'assert';
import { UUID } from '../classes/UUID';
describe('UUID', () =>
{
describe('random', () =>
{
it ('should generate a random, valid v4 UUID', () =>
{
const uuid = UUID.random().toString();
const secondUUID = UUID.random().toString();
if (typeof uuid !== 'string')
{
assert.fail('Returned UUID is not a string');
}
if (!validator.isUUID(uuid))
{
assert.fail('Returned string is not a valid UUID');
}
if (uuid === '00000000-0000-0000-0000-000000000000')
{
assert.fail('Random UUID should not be zero');
}
if (typeof secondUUID !== 'string')
{
assert.fail('Returned second UUID is not a string');
}
if (!validator.isUUID(secondUUID))
{
assert.fail('Second UUID is not a valid UUID');
}
if (secondUUID === '00000000-0000-0000-0000-000000000000')
{
assert.fail('Random UUID should not be zero');
}
if (uuid === secondUUID)
{
assert.fail('Two random UUIDs match! (Not random)');
}
assert.ok(true);
});
});
describe('zero', () =>
{
it ('should generate a zeroed, valid v4 UUID', () =>
{
const uuid = UUID.zero().toString();
if (typeof uuid !== 'string')
{
assert.fail('Returned UUID is not a string');
}
if (!validator.isUUID(uuid))
{
assert.fail('Returned string is not a valid UUID');
}
if (uuid !== '00000000-0000-0000-0000-000000000000')
{
assert.fail('UUID is not zero')
}
assert.ok(true);
});
});
describe('encode/decode', () =>
{
it ('should correctly decode a 16-byte UUID from a buffer', () =>
{
const buf = Buffer.from('00004af668bb6fe34893881408f586c5657c4e1c9910', 'hex');
const uuid = new UUID(buf, 2);
const str = uuid.toString();
if (typeof str !== 'string')
{
assert.fail('Returned UUID is not a string');
}
if (!validator.isUUID(str))
{
assert.fail('Returned string is not a valid UUID');
}
if (str !== '4af668bb-6fe3-4893-8814-08f586c5657c')
{
assert.fail('UUID decoded incorrectly');
}
assert.ok(true);
});
it ('should correct encode a UUID into the correct position in a buffer', () =>
{
const buf = Buffer.alloc(22);
const uuid = new UUID('4af668bb-6fe3-4893-8814-08f586c5657c');
uuid.writeToBuffer(buf, 2);
const bufCmp = Buffer.from('00004af668bb6fe34893881408f586c5657c00000000', 'hex');
if (buf.compare(bufCmp) !== 0)
{
assert.fail('Encoded buffer does not match expected output');
}
const result = uuid.toString();
if (typeof result !== 'string')
{
assert.fail('Returned UUID is not a string');
}
if (!validator.isUUID(result))
{
assert.fail('Returned string is not a valid UUID');
}
assert.ok(true);
});
});
});
import 'mocha';
import validator from 'validator';
import * as assert from 'assert';
import { UUID } from '../classes/UUID';
describe('UUID', () =>
{
describe('random', () =>
{
it ('should generate a random, valid v4 UUID', () =>
{
const uuid = UUID.random().toString();
const secondUUID = UUID.random().toString();
if (typeof uuid !== 'string')
{
assert.fail('Returned UUID is not a string');
}
if (!validator.isUUID(uuid))
{
assert.fail('Returned string is not a valid UUID');
}
if (uuid === '00000000-0000-0000-0000-000000000000')
{
assert.fail('Random UUID should not be zero');
}
if (typeof secondUUID !== 'string')
{
assert.fail('Returned second UUID is not a string');
}
if (!validator.isUUID(secondUUID))
{
assert.fail('Second UUID is not a valid UUID');
}
if (secondUUID === '00000000-0000-0000-0000-000000000000')
{
assert.fail('Random UUID should not be zero');
}
if (uuid === secondUUID)
{
assert.fail('Two random UUIDs match! (Not random)');
}
assert.ok(true);
});
});
describe('zero', () =>
{
it ('should generate a zeroed, valid v4 UUID', () =>
{
const uuid = UUID.zero().toString();
if (typeof uuid !== 'string')
{
assert.fail('Returned UUID is not a string');
}
if (!validator.isUUID(uuid))
{
assert.fail('Returned string is not a valid UUID');
}
if (uuid !== '00000000-0000-0000-0000-000000000000')
{
assert.fail('UUID is not zero')
}
assert.ok(true);
});
});
describe('encode/decode', () =>
{
it ('should correctly decode a 16-byte UUID from a buffer', () =>
{
const buf = Buffer.from('00004af668bb6fe34893881408f586c5657c4e1c9910', 'hex');
const uuid = new UUID(buf, 2);
const str = uuid.toString();
if (typeof str !== 'string')
{
assert.fail('Returned UUID is not a string');
}
if (!validator.isUUID(str))
{
assert.fail('Returned string is not a valid UUID');
}
if (str !== '4af668bb-6fe3-4893-8814-08f586c5657c')
{
assert.fail('UUID decoded incorrectly');
}
assert.ok(true);
});
it ('should correct encode a UUID into the correct position in a buffer', () =>
{
const buf = Buffer.alloc(22);
const uuid = new UUID('4af668bb-6fe3-4893-8814-08f586c5657c');
uuid.writeToBuffer(buf, 2);
const bufCmp = Buffer.from('00004af668bb6fe34893881408f586c5657c00000000', 'hex');
if (buf.compare(bufCmp) !== 0)
{
assert.fail('Encoded buffer does not match expected output');
}
const result = uuid.toString();
if (typeof result !== 'string')
{
assert.fail('Returned UUID is not a string');
}
if (!validator.isUUID(result))
{
assert.fail('Returned string is not a valid UUID');
}
assert.ok(true);
});
});
});