diff options
author | Grigoriy Kraynov <kraynov@google.com> | 2016-05-12 14:11:20 +0100 |
---|---|---|
committer | Grigoriy Kraynov <kraynov@google.com> | 2016-05-12 14:22:12 +0100 |
commit | 5d54a852ebce67aae5ed5afae203354b90966a84 (patch) | |
tree | 1598de3a01486520844fc5d3dcb0f1a2bd66d27c | |
parent | 371d341c7ef22d38b80235bbcb5a6e230546781c (diff) | |
download | protobuf-5d54a852ebce67aae5ed5afae203354b90966a84.tar.gz protobuf-5d54a852ebce67aae5ed5afae203354b90966a84.tar.bz2 protobuf-5d54a852ebce67aae5ed5afae203354b90966a84.zip |
Test redundant varint fields decoding in JS.
Varints are generally encoded in minimum possible length but need to
ensure that any redundancy does not break fields decoder.
-rw-r--r-- | js/binary/reader_test.js | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/js/binary/reader_test.js b/js/binary/reader_test.js index 6f7e5d45..db674cf8 100644 --- a/js/binary/reader_test.js +++ b/js/binary/reader_test.js @@ -360,6 +360,58 @@ describe('binaryReaderTest', function() { /** + * Tests reading a field from hexadecimal string (format: '08 BE EF'). + * @param {Function} readField + * @param {number} expected + * @param {string} hexString + */ + function doTestHexStringVarint_(readField, expected, hexString) { + var bytesCount = (hexString.length + 1) / 3; + var bytes = new Uint8Array(bytesCount); + for (var i = 0; i < bytesCount; i++) { + byte = parseInt(hexString.substring(i * 3, i * 3 + 2), 16); + bytes[i] = byte; + } + var reader = jspb.BinaryReader.alloc(bytes); + reader.nextField(); + assertEquals(expected, readField.call(reader)); + } + + + /** + * Tests non-canonical redundant varint decoding. + */ + it('testRedundantVarintFields', function() { + assertNotNull(jspb.BinaryReader.prototype.readUint32); + assertNotNull(jspb.BinaryReader.prototype.readUint64); + assertNotNull(jspb.BinaryReader.prototype.readSint32); + assertNotNull(jspb.BinaryReader.prototype.readSint64); + + // uint32 and sint32 take no more than 5 bytes + // 08 - field prefix (type = 0 means varint) + doTestHexStringVarint_( + jspb.BinaryReader.prototype.readUint32, + 12, '08 8C 80 80 80 00'); + + // 11 stands for -6 in zigzag encoding + doTestHexStringVarint_( + jspb.BinaryReader.prototype.readSint32, + -6, '08 8B 80 80 80 00'); + + // uint64 and sint64 take no more than 10 bytes + // 08 - field prefix (type = 0 means varint) + doTestHexStringVarint_( + jspb.BinaryReader.prototype.readUint64, + 12, '08 8C 80 80 80 80 80 80 80 80 00'); + + // 11 stands for -6 in zigzag encoding + doTestHexStringVarint_( + jspb.BinaryReader.prototype.readSint64, + -6, '08 8B 80 80 80 80 80 80 80 80 00'); + }); + + + /** * Tests 64-bit fields that are handled as strings. */ it('testStringInt64Fields', function() { |