From 85c134a13a0f93229fb7d1da042d9e65cece7706 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Sun, 10 Mar 2019 20:22:19 +0100 Subject: [PATCH] (decoding) test coverage - reached 100% coverage --- decoding.js | 8 +++++--- encoding.test.js | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/decoding.js b/decoding.js index 791c009..b39bcbc 100644 --- a/decoding.js +++ b/decoding.js @@ -154,11 +154,12 @@ export const peekUint16 = decoder => * @param {Decoder} decoder * @return {number} An unsigned integer. */ -export const peekUint32 = decoder => - (decoder.arr[decoder.pos] + +export const peekUint32 = decoder => ( + decoder.arr[decoder.pos] + (decoder.arr[decoder.pos + 1] << 8) + (decoder.arr[decoder.pos + 2] << 16) + - (decoder.arr[decoder.pos + 3] << 24)) >>> 0 + (decoder.arr[decoder.pos + 3] << 24) +) >>> 0 /** * Read unsigned integer (32bit) with variable length. @@ -180,6 +181,7 @@ export const readVarUint = decoder => { if (r < 1 << 7) { return num >>> 0 // return unsigned number! } + /* istanbul ignore if */ if (len > 35) { throw new Error('Integer out of range!') } diff --git a/encoding.test.js b/encoding.test.js index 555e745..41ebbc6 100644 --- a/encoding.test.js +++ b/encoding.test.js @@ -64,11 +64,13 @@ function test (testname, write, read, val, doLog = true) { } const testVarString = (s) => { - let encoder = encoding.createEncoder() + const encoder = encoding.createEncoder() encoding.writeVarString(encoder, s) - let reader = decoding.createDecoder(encoding.toBuffer(encoder)) - let result = decoding.readVarString(reader) + const decoder = decoding.createDecoder(encoding.toBuffer(encoder)) + const peeked = decoding.peekVarString(decoder) + const result = decoding.readVarString(decoder) t.compareStrings(s, result) + t.compareStrings(s, peeked) } /** @@ -87,6 +89,11 @@ export const testRepeatVarUintEncoding = tc => { test(`varUint of ${n}`, encoding.writeVarUint, decoding.readVarUint, n, false) } +export const testRepeatPeekVarUintEncoding = tc => { + const n = prng.int31(tc.prng, 0, (1 << 28) - 1) + test(`varUint of ${n}`, encoding.writeVarUint, decoding.peekVarUint, n, false) +} + export const testStringEncoding = tc => { testVarString('hello') testVarString('test!') @@ -185,6 +192,20 @@ export const testSetOnOverflow = tc => { t.assert(buffer.createUint8ArrayFromArrayBuffer(buf2)[initialLen + 1] === 7) } +export const testCloneDecoder = tc => { + const encoder = encoding.createEncoder() + encoding.writeUint8(encoder, 12132) + encoding.writeVarUint(encoder, 329840128734) + encoding.writeVarString(encoder, 'dtrnuiaednudiaendturinaedt nduiaen dturinaed ') + const buf = encoding.toBuffer(encoder) + const decoder = decoding.createDecoder(buf) + decoding.skip8(decoder) + const decoder2 = decoding.clone(decoder) + const payload1 = decoding.readTail(decoder) + const payload2 = decoding.readTail(decoder2) + t.compare(payload1, payload2) +} + export const testWriteBinaryEncoder = tc => { const encoder = encoding.createEncoder() encoding.writeUint16(encoder, 4) @@ -196,3 +217,16 @@ export const testWriteBinaryEncoder = tc => { t.assert(decoding.readVarUint(decoder) === 143095) t.assert(decoding.readUint16(decoder) === 4) } + +export const testOverflowStringDecoding = tc => { + const gen = tc.prng + const encoder = encoding.createEncoder() + let longStr = '' + while (longStr.length < 11000) { + longStr += prng.utf16String(gen, 100000) + } + encoding.writeVarString(encoder, longStr) + const buf = encoding.toBuffer(encoder) + const decoder = decoding.createDecoder(buf) + t.assert(longStr === decoding.readVarString(decoder)) +}