Add a binary compare test

This commit is contained in:
Casper Warden
2017-12-20 17:35:40 +00:00
parent 94d5c70259
commit 4f02dd6a2a
4 changed files with 36 additions and 8 deletions

View File

@@ -113,20 +113,23 @@ describe('Packets', () =>
done();
}
});
let buf = Buffer.allocUnsafe(0);
let extra = 0;
it('should encode back to binary', (done) =>
{
try
{
let buf = Buffer.alloc(packet.getSize());
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)
{
bl += 4 * acksReceived.length;
bl ++;
extra += 4 * acksReceived.length;
extra++;
}
bl += extra;
if (data.length !== bl)
{
@@ -144,6 +147,19 @@ describe('Packets', () =>
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();
}
});
});
}
}