From 1a7a7fca804afa1cf67f8be5e71092898ba40334 Mon Sep 17 00:00:00 2001 From: Jisi Liu Date: Wed, 18 Oct 2017 12:22:18 -0700 Subject: Merge from google internal --- js/binary/arith_test.js | 109 ++++++++++----------- js/binary/constants.js | 3 + js/binary/decoder.js | 27 +++-- js/binary/decoder_test.js | 19 +--- .../v3.0.0/binary/decoder_test.js | 19 +--- .../v3.1.0/binary/decoder_test.js | 19 +--- 6 files changed, 72 insertions(+), 124 deletions(-) (limited to 'js') diff --git a/js/binary/arith_test.js b/js/binary/arith_test.js index 89796bf7..dd5791a7 100644 --- a/js/binary/arith_test.js +++ b/js/binary/arith_test.js @@ -36,7 +36,6 @@ * @author cfallin@google.com (Chris Fallin) */ -goog.require('goog.testing.asserts'); goog.require('jspb.arith.Int64'); goog.require('jspb.arith.UInt64'); @@ -48,30 +47,30 @@ describe('binaryArithTest', function() { it('testCompare', function() { var a = new jspb.arith.UInt64(1234, 5678); var b = new jspb.arith.UInt64(1234, 5678); - assertEquals(a.cmp(b), 0); - assertEquals(b.cmp(a), 0); + expect(a.cmp(b)).toEqual(0); + expect(b.cmp(a)).toEqual(0); b.lo -= 1; - assertEquals(a.cmp(b), 1); - assertEquals(b.cmp(a), -1); + expect(a.cmp(b)).toEqual(1); + expect(b.cmp(a)).toEqual(-1); b.lo += 2; - assertEquals(a.cmp(b), -1); - assertEquals(b.cmp(a), 1); + expect(a.cmp(b)).toEqual(-1); + expect(b.cmp(a)).toEqual(1); b.lo = a.lo; b.hi = a.hi - 1; - assertEquals(a.cmp(b), 1); - assertEquals(b.cmp(a), -1); + expect(a.cmp(b)).toEqual(1); + expect(b.cmp(a)).toEqual(-1); - assertEquals(a.zero(), false); - assertEquals(a.msb(), false); - assertEquals(a.lsb(), false); + expect(a.zero()).toEqual(false); + expect(a.msb()).toEqual(false); + expect(a.lsb()).toEqual(false); a.hi = 0; a.lo = 0; - assertEquals(a.zero(), true); + expect(a.zero()).toEqual(true); a.hi = 0x80000000; - assertEquals(a.zero(), false); - assertEquals(a.msb(), true); + expect(a.zero()).toEqual(false); + expect(a.msb()).toEqual(true); a.lo = 0x00000001; - assertEquals(a.lsb(), true); + expect(a.lsb()).toEqual(true); }); @@ -80,35 +79,35 @@ describe('binaryArithTest', function() { */ it('testShifts', function() { var a = new jspb.arith.UInt64(1, 0); - assertEquals(a.lo, 1); - assertEquals(a.hi, 0); + expect(a.lo).toEqual(1); + expect(a.hi).toEqual(0); var orig = a; a = a.leftShift(); - assertEquals(orig.lo, 1); // original unmodified. - assertEquals(orig.hi, 0); - assertEquals(a.lo, 2); - assertEquals(a.hi, 0); + expect(orig.lo).toEqual(1); // original unmodified. + expect(orig.hi).toEqual(0); + expect(a.lo).toEqual(2); + expect(a.hi).toEqual(0); a = a.leftShift(); - assertEquals(a.lo, 4); - assertEquals(a.hi, 0); + expect(a.lo).toEqual(4); + expect(a.hi).toEqual(0); for (var i = 0; i < 29; i++) { a = a.leftShift(); } - assertEquals(a.lo, 0x80000000); - assertEquals(a.hi, 0); + expect(a.lo).toEqual(0x80000000); + expect(a.hi).toEqual(0); a = a.leftShift(); - assertEquals(a.lo, 0); - assertEquals(a.hi, 1); + expect(a.lo).toEqual(0); + expect(a.hi).toEqual(1); a = a.leftShift(); - assertEquals(a.lo, 0); - assertEquals(a.hi, 2); + expect(a.lo).toEqual(0); + expect(a.hi).toEqual(2); a = a.rightShift(); a = a.rightShift(); - assertEquals(a.lo, 0x80000000); - assertEquals(a.hi, 0); + expect(a.lo).toEqual(0x80000000); + expect(a.hi).toEqual(0); a = a.rightShift(); - assertEquals(a.lo, 0x40000000); - assertEquals(a.hi, 0); + expect(a.lo).toEqual(0x40000000); + expect(a.hi).toEqual(0); }); @@ -122,12 +121,12 @@ describe('binaryArithTest', function() { /* hi = */ 0x92fa2123); // Addition with carry. var c = a.add(b); - assertEquals(a.lo, 0x89abcdef); // originals unmodified. - assertEquals(a.hi, 0x01234567); - assertEquals(b.lo, 0xff52ab91); - assertEquals(b.hi, 0x92fa2123); - assertEquals(c.lo, 0x88fe7980); - assertEquals(c.hi, 0x941d668b); + expect(a.lo).toEqual(0x89abcdef); // originals unmodified. + expect(a.hi).toEqual(0x01234567); + expect(b.lo).toEqual(0xff52ab91); + expect(b.hi).toEqual(0x92fa2123); + expect(c.lo).toEqual(0x88fe7980); + expect(c.hi).toEqual(0x941d668b); // Simple addition without carry. a.lo = 2; @@ -135,8 +134,8 @@ describe('binaryArithTest', function() { b.lo = 3; b.hi = 0; c = a.add(b); - assertEquals(c.lo, 5); - assertEquals(c.hi, 0); + expect(c.lo).toEqual(5); + expect(c.hi).toEqual(0); }); @@ -170,8 +169,8 @@ describe('binaryArithTest', function() { var a = new jspb.arith.UInt64(loValues[i], hiValues[j]); var b = new jspb.arith.UInt64(loValues[j], hiValues[i]); var c = a.add(b).sub(b); - assertEquals(c.hi, a.hi); - assertEquals(c.lo, a.lo); + expect(c.hi).toEqual(a.hi); + expect(c.lo).toEqual(a.lo); } } }); @@ -201,8 +200,8 @@ describe('binaryArithTest', function() { var cLow = testData[i][2] >>> 0; var cHigh = testData[i][3] >>> 0; var c = jspb.arith.UInt64.mul32x32(a, b); - assertEquals(c.lo, cLow); - assertEquals(c.hi, cHigh); + expect(c.lo).toEqual(cLow); + expect(c.hi).toEqual(cHigh); } }); @@ -231,8 +230,8 @@ describe('binaryArithTest', function() { for (var i = 0; i < testData.length; i++) { var a = new jspb.arith.UInt64(testData[i][0], testData[i][1]); var prod = a.mul(testData[i][2]); - assertEquals(prod.lo, testData[i][3]); - assertEquals(prod.hi, testData[i][4]); + expect(prod.lo).toEqual(testData[i][3]); + expect(prod.hi).toEqual(testData[i][4]); } }); @@ -274,9 +273,9 @@ describe('binaryArithTest', function() { var result = a.div(testData[i][2]); var quotient = result[0]; var remainder = result[1]; - assertEquals(quotient.lo, testData[i][3]); - assertEquals(quotient.hi, testData[i][4]); - assertEquals(remainder.lo, testData[i][5]); + expect(quotient.lo).toEqual(testData[i][3]); + expect(quotient.hi).toEqual(testData[i][4]); + expect(remainder.lo).toEqual(testData[i][5]); } }); @@ -311,9 +310,9 @@ describe('binaryArithTest', function() { for (var i = 0; i < testData.length; i++) { var a = new jspb.arith.UInt64(testData[i][0], testData[i][1]); var roundtrip = jspb.arith.UInt64.fromString(a.toString()); - assertEquals(roundtrip.lo, a.lo); - assertEquals(roundtrip.hi, a.hi); - assertEquals(a.toString(), testData[i][2]); + expect(roundtrip.lo).toEqual(a.lo); + expect(roundtrip.hi).toEqual(a.hi); + expect(a.toString()).toEqual(testData[i][2]); } }); @@ -349,7 +348,7 @@ describe('binaryArithTest', function() { for (var i = 0; i < testStrings.length; i++) { var roundtrip = jspb.arith.Int64.fromString(testStrings[i]).toString(); - assertEquals(roundtrip, testStrings[i]); + expect(roundtrip).toEqual(testStrings[i]); } }); }); diff --git a/js/binary/constants.js b/js/binary/constants.js index ef5fecdd..75a8a52c 100644 --- a/js/binary/constants.js +++ b/js/binary/constants.js @@ -51,6 +51,9 @@ goog.provide('jspb.ScalarFieldType'); goog.provide('jspb.WriterFunction'); +goog.forwardDeclare('jspb.BinaryMessage'); +goog.forwardDeclare('jspb.BinaryReader'); +goog.forwardDeclare('jspb.BinaryWriter'); goog.forwardDeclare('jspb.Message'); goog.forwardDeclare('jsproto.BinaryExtension'); diff --git a/js/binary/decoder.js b/js/binary/decoder.js index 313d6f3f..4ec3cada 100644 --- a/js/binary/decoder.js +++ b/js/binary/decoder.js @@ -583,27 +583,24 @@ jspb.BinaryDecoder.prototype.readUnsignedVarint32 = function() { x |= (temp & 0x0F) << 28; if (temp < 128) { // We're reading the high bits of an unsigned varint. The byte we just read - // also contains bits 33 through 35, which we're going to discard. Those - // bits _must_ be zero, or the encoding is invalid. - goog.asserts.assert((temp & 0xF0) == 0); + // also contains bits 33 through 35, which we're going to discard. this.cursor_ += 5; goog.asserts.assert(this.cursor_ <= this.end_); return x >>> 0; } - // If we get here, we're reading the sign extension of a negative 32-bit int. - // We can skip these bytes, as we know in advance that they have to be all - // 1's if the varint is correctly encoded. Since we also know the value is - // negative, we don't have to coerce it to unsigned before we return it. - - goog.asserts.assert((temp & 0xF0) == 0xF0); - goog.asserts.assert(bytes[this.cursor_ + 5] == 0xFF); - goog.asserts.assert(bytes[this.cursor_ + 6] == 0xFF); - goog.asserts.assert(bytes[this.cursor_ + 7] == 0xFF); - goog.asserts.assert(bytes[this.cursor_ + 8] == 0xFF); - goog.asserts.assert(bytes[this.cursor_ + 9] == 0x01); + // If we get here, we need to truncate coming bytes. However we need to make + // sure cursor place is correct. + this.cursor_ += 5; + if (bytes[this.cursor_++] >= 128 && + bytes[this.cursor_++] >= 128 && + bytes[this.cursor_++] >= 128 && + bytes[this.cursor_++] >= 128 && + bytes[this.cursor_++] >= 128) { + // If we get here, the varint is too long. + goog.asserts.assert(false); + } - this.cursor_ += 10; goog.asserts.assert(this.cursor_ <= this.end_); return x; }; diff --git a/js/binary/decoder_test.js b/js/binary/decoder_test.js index d0139e29..b19e1d1b 100644 --- a/js/binary/decoder_test.js +++ b/js/binary/decoder_test.js @@ -270,24 +270,7 @@ describe('binaryDecoderTest', function() { assertThrows(function() {decoder.readSignedVarint64()}); decoder.reset(); assertThrows(function() {decoder.readZigzagVarint64()}); - - // Positive 32-bit varints encoded with 1 bits in positions 33 through 35 - // should trigger assertions. - decoder.setBlock([255, 255, 255, 255, 0x1F]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - decoder.setBlock([255, 255, 255, 255, 0x2F]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - decoder.setBlock([255, 255, 255, 255, 0x4F]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - // Negative 32-bit varints encoded with non-1 bits in the high dword should - // trigger assertions. - decoder.setBlock([255, 255, 255, 255, 255, 255, 0, 255, 255, 1]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - decoder.setBlock([255, 255, 255, 255, 255, 255, 255, 255, 255, 0]); + decoder.reset(); assertThrows(function() {decoder.readUnsignedVarint32()}); }); diff --git a/js/compatibility_tests/v3.0.0/binary/decoder_test.js b/js/compatibility_tests/v3.0.0/binary/decoder_test.js index ac312648..fce2fe18 100644 --- a/js/compatibility_tests/v3.0.0/binary/decoder_test.js +++ b/js/compatibility_tests/v3.0.0/binary/decoder_test.js @@ -228,24 +228,7 @@ describe('binaryDecoderTest', function() { assertThrows(function() {decoder.readSignedVarint64()}); decoder.reset(); assertThrows(function() {decoder.readZigzagVarint64()}); - - // Positive 32-bit varints encoded with 1 bits in positions 33 through 35 - // should trigger assertions. - decoder.setBlock([255, 255, 255, 255, 0x1F]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - decoder.setBlock([255, 255, 255, 255, 0x2F]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - decoder.setBlock([255, 255, 255, 255, 0x4F]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - // Negative 32-bit varints encoded with non-1 bits in the high dword should - // trigger assertions. - decoder.setBlock([255, 255, 255, 255, 255, 255, 0, 255, 255, 1]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - decoder.setBlock([255, 255, 255, 255, 255, 255, 255, 255, 255, 0]); + decoder.reset(); assertThrows(function() {decoder.readUnsignedVarint32()}); }); diff --git a/js/compatibility_tests/v3.1.0/binary/decoder_test.js b/js/compatibility_tests/v3.1.0/binary/decoder_test.js index ac312648..fce2fe18 100644 --- a/js/compatibility_tests/v3.1.0/binary/decoder_test.js +++ b/js/compatibility_tests/v3.1.0/binary/decoder_test.js @@ -228,24 +228,7 @@ describe('binaryDecoderTest', function() { assertThrows(function() {decoder.readSignedVarint64()}); decoder.reset(); assertThrows(function() {decoder.readZigzagVarint64()}); - - // Positive 32-bit varints encoded with 1 bits in positions 33 through 35 - // should trigger assertions. - decoder.setBlock([255, 255, 255, 255, 0x1F]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - decoder.setBlock([255, 255, 255, 255, 0x2F]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - decoder.setBlock([255, 255, 255, 255, 0x4F]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - // Negative 32-bit varints encoded with non-1 bits in the high dword should - // trigger assertions. - decoder.setBlock([255, 255, 255, 255, 255, 255, 0, 255, 255, 1]); - assertThrows(function() {decoder.readUnsignedVarint32()}); - - decoder.setBlock([255, 255, 255, 255, 255, 255, 255, 255, 255, 0]); + decoder.reset(); assertThrows(function() {decoder.readUnsignedVarint32()}); }); -- cgit v1.2.3