decoder_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. /**
  31. * @fileoverview Test cases for jspb's binary protocol buffer decoder.
  32. *
  33. * There are two particular magic numbers that need to be pointed out -
  34. * 2^64-1025 is the largest number representable as both a double and an
  35. * unsigned 64-bit integer, and 2^63-513 is the largest number representable as
  36. * both a double and a signed 64-bit integer.
  37. *
  38. * Test suite is written using Jasmine -- see http://jasmine.github.io/
  39. *
  40. * @author aappleby@google.com (Austin Appleby)
  41. */
  42. goog.require('goog.testing.asserts');
  43. goog.require('jspb.BinaryConstants');
  44. goog.require('jspb.BinaryDecoder');
  45. goog.require('jspb.BinaryWriter');
  46. /**
  47. * Tests raw encoding and decoding of unsigned types.
  48. * @param {Function} readValue
  49. * @param {Function} writeValue
  50. * @param {number} epsilon
  51. * @param {number} upperLimit
  52. * @param {Function} filter
  53. * @suppress {missingProperties|visibility}
  54. */
  55. function doTestUnsignedValue(readValue,
  56. writeValue, epsilon, upperLimit, filter) {
  57. var writer = new jspb.BinaryWriter();
  58. // Encode zero and limits.
  59. writeValue.call(writer, filter(0));
  60. writeValue.call(writer, filter(epsilon));
  61. writeValue.call(writer, filter(upperLimit));
  62. // Encode positive values.
  63. for (var cursor = epsilon; cursor < upperLimit; cursor *= 1.1) {
  64. writeValue.call(writer, filter(cursor));
  65. }
  66. var reader = jspb.BinaryDecoder.alloc(writer.getResultBuffer());
  67. // Check zero and limits.
  68. assertEquals(filter(0), readValue.call(reader));
  69. assertEquals(filter(epsilon), readValue.call(reader));
  70. assertEquals(filter(upperLimit), readValue.call(reader));
  71. // Check positive values.
  72. for (var cursor = epsilon; cursor < upperLimit; cursor *= 1.1) {
  73. if (filter(cursor) != readValue.call(reader)) throw 'fail!';
  74. }
  75. }
  76. /**
  77. * Tests raw encoding and decoding of signed types.
  78. * @param {Function} readValue
  79. * @param {Function} writeValue
  80. * @param {number} epsilon
  81. * @param {number} lowerLimit
  82. * @param {number} upperLimit
  83. * @param {Function} filter
  84. * @suppress {missingProperties}
  85. */
  86. function doTestSignedValue(readValue,
  87. writeValue, epsilon, lowerLimit, upperLimit, filter) {
  88. var writer = new jspb.BinaryWriter();
  89. // Encode zero and limits.
  90. writeValue.call(writer, filter(lowerLimit));
  91. writeValue.call(writer, filter(-epsilon));
  92. writeValue.call(writer, filter(0));
  93. writeValue.call(writer, filter(epsilon));
  94. writeValue.call(writer, filter(upperLimit));
  95. var inputValues = [];
  96. // Encode negative values.
  97. for (var cursor = lowerLimit; cursor < -epsilon; cursor /= 1.1) {
  98. var val = filter(cursor);
  99. writeValue.call(writer, val);
  100. inputValues.push(val);
  101. }
  102. // Encode positive values.
  103. for (var cursor = epsilon; cursor < upperLimit; cursor *= 1.1) {
  104. var val = filter(cursor);
  105. writeValue.call(writer, val);
  106. inputValues.push(val);
  107. }
  108. var reader = jspb.BinaryDecoder.alloc(writer.getResultBuffer());
  109. // Check zero and limits.
  110. assertEquals(filter(lowerLimit), readValue.call(reader));
  111. assertEquals(filter(-epsilon), readValue.call(reader));
  112. assertEquals(filter(0), readValue.call(reader));
  113. assertEquals(filter(epsilon), readValue.call(reader));
  114. assertEquals(filter(upperLimit), readValue.call(reader));
  115. // Verify decoded values.
  116. for (var i = 0; i < inputValues.length; i++) {
  117. assertEquals(inputValues[i], readValue.call(reader));
  118. }
  119. }
  120. describe('binaryDecoderTest', function() {
  121. /**
  122. * Tests the decoder instance cache.
  123. * @suppress {visibility}
  124. */
  125. it('testInstanceCache', function() {
  126. // Empty the instance caches.
  127. jspb.BinaryDecoder.instanceCache_ = [];
  128. // Allocating and then freeing a decoder should put it in the instance
  129. // cache.
  130. jspb.BinaryDecoder.alloc().free();
  131. assertEquals(1, jspb.BinaryDecoder.instanceCache_.length);
  132. // Allocating and then freeing three decoders should leave us with three in
  133. // the cache.
  134. var decoder1 = jspb.BinaryDecoder.alloc();
  135. var decoder2 = jspb.BinaryDecoder.alloc();
  136. var decoder3 = jspb.BinaryDecoder.alloc();
  137. decoder1.free();
  138. decoder2.free();
  139. decoder3.free();
  140. assertEquals(3, jspb.BinaryDecoder.instanceCache_.length);
  141. });
  142. /**
  143. * Tests reading 64-bit integers as hash strings.
  144. */
  145. it('testHashStrings', function() {
  146. var writer = new jspb.BinaryWriter();
  147. var hashA = String.fromCharCode(0x00, 0x00, 0x00, 0x00,
  148. 0x00, 0x00, 0x00, 0x00);
  149. var hashB = String.fromCharCode(0x12, 0x34, 0x00, 0x00,
  150. 0x00, 0x00, 0x00, 0x00);
  151. var hashC = String.fromCharCode(0x12, 0x34, 0x56, 0x78,
  152. 0x87, 0x65, 0x43, 0x21);
  153. var hashD = String.fromCharCode(0xFF, 0xFF, 0xFF, 0xFF,
  154. 0xFF, 0xFF, 0xFF, 0xFF);
  155. writer.rawWriteVarintHash64(hashA);
  156. writer.rawWriteVarintHash64(hashB);
  157. writer.rawWriteVarintHash64(hashC);
  158. writer.rawWriteVarintHash64(hashD);
  159. writer.rawWriteFixedHash64(hashA);
  160. writer.rawWriteFixedHash64(hashB);
  161. writer.rawWriteFixedHash64(hashC);
  162. writer.rawWriteFixedHash64(hashD);
  163. var decoder = jspb.BinaryDecoder.alloc(writer.getResultBuffer());
  164. assertEquals(hashA, decoder.readVarintHash64());
  165. assertEquals(hashB, decoder.readVarintHash64());
  166. assertEquals(hashC, decoder.readVarintHash64());
  167. assertEquals(hashD, decoder.readVarintHash64());
  168. assertEquals(hashA, decoder.readFixedHash64());
  169. assertEquals(hashB, decoder.readFixedHash64());
  170. assertEquals(hashC, decoder.readFixedHash64());
  171. assertEquals(hashD, decoder.readFixedHash64());
  172. });
  173. /**
  174. * Verifies that misuse of the decoder class triggers assertions.
  175. * @suppress {checkTypes|visibility}
  176. */
  177. it('testDecodeErrors', function() {
  178. // Reading a value past the end of the stream should trigger an assertion.
  179. var decoder = jspb.BinaryDecoder.alloc([0, 1, 2]);
  180. assertThrows(function() {decoder.readUint64()});
  181. // Overlong varints should trigger assertions.
  182. decoder.setBlock(
  183. [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0]);
  184. assertThrows(function() {decoder.readUnsignedVarint64()});
  185. decoder.reset();
  186. assertThrows(function() {decoder.readSignedVarint64()});
  187. decoder.reset();
  188. assertThrows(function() {decoder.readZigzagVarint64()});
  189. // Positive 32-bit varints encoded with 1 bits in positions 33 through 35
  190. // should trigger assertions.
  191. decoder.setBlock([255, 255, 255, 255, 0x1F]);
  192. assertThrows(function() {decoder.readUnsignedVarint32()});
  193. decoder.setBlock([255, 255, 255, 255, 0x2F]);
  194. assertThrows(function() {decoder.readUnsignedVarint32()});
  195. decoder.setBlock([255, 255, 255, 255, 0x4F]);
  196. assertThrows(function() {decoder.readUnsignedVarint32()});
  197. // Negative 32-bit varints encoded with non-1 bits in the high dword should
  198. // trigger assertions.
  199. decoder.setBlock([255, 255, 255, 255, 255, 255, 0, 255, 255, 1]);
  200. assertThrows(function() {decoder.readUnsignedVarint32()});
  201. decoder.setBlock([255, 255, 255, 255, 255, 255, 255, 255, 255, 0]);
  202. assertThrows(function() {decoder.readUnsignedVarint32()});
  203. });
  204. /**
  205. * Tests raw encoding and decoding of unsigned integers.
  206. */
  207. it('testRawUnsigned', function() {
  208. doTestUnsignedValue(
  209. jspb.BinaryDecoder.prototype.readUint8,
  210. jspb.BinaryWriter.prototype.rawWriteUint8,
  211. 1, 0xFF, Math.round);
  212. doTestUnsignedValue(
  213. jspb.BinaryDecoder.prototype.readUint16,
  214. jspb.BinaryWriter.prototype.rawWriteUint16,
  215. 1, 0xFFFF, Math.round);
  216. doTestUnsignedValue(
  217. jspb.BinaryDecoder.prototype.readUint32,
  218. jspb.BinaryWriter.prototype.rawWriteUint32,
  219. 1, 0xFFFFFFFF, Math.round);
  220. doTestUnsignedValue(
  221. jspb.BinaryDecoder.prototype.readUint64,
  222. jspb.BinaryWriter.prototype.rawWriteUint64,
  223. 1, Math.pow(2, 64) - 1025, Math.round);
  224. });
  225. /**
  226. * Tests raw encoding and decoding of signed integers.
  227. */
  228. it('testRawSigned', function() {
  229. doTestSignedValue(
  230. jspb.BinaryDecoder.prototype.readInt8,
  231. jspb.BinaryWriter.prototype.rawWriteInt8,
  232. 1, -0x80, 0x7F, Math.round);
  233. doTestSignedValue(
  234. jspb.BinaryDecoder.prototype.readInt16,
  235. jspb.BinaryWriter.prototype.rawWriteInt16,
  236. 1, -0x8000, 0x7FFF, Math.round);
  237. doTestSignedValue(
  238. jspb.BinaryDecoder.prototype.readInt32,
  239. jspb.BinaryWriter.prototype.rawWriteInt32,
  240. 1, -0x80000000, 0x7FFFFFFF, Math.round);
  241. doTestSignedValue(
  242. jspb.BinaryDecoder.prototype.readInt64,
  243. jspb.BinaryWriter.prototype.rawWriteInt64,
  244. 1, -Math.pow(2, 63), Math.pow(2, 63) - 513, Math.round);
  245. });
  246. /**
  247. * Tests raw encoding and decoding of floats.
  248. */
  249. it('testRawFloats', function() {
  250. /**
  251. * @param {number} x
  252. * @return {number}
  253. */
  254. function truncate(x) {
  255. var temp = new Float32Array(1);
  256. temp[0] = x;
  257. return temp[0];
  258. }
  259. doTestSignedValue(
  260. jspb.BinaryDecoder.prototype.readFloat,
  261. jspb.BinaryWriter.prototype.rawWriteFloat,
  262. jspb.BinaryConstants.FLOAT32_EPS,
  263. -jspb.BinaryConstants.FLOAT32_MAX,
  264. jspb.BinaryConstants.FLOAT32_MAX,
  265. truncate);
  266. doTestSignedValue(
  267. jspb.BinaryDecoder.prototype.readDouble,
  268. jspb.BinaryWriter.prototype.rawWriteDouble,
  269. jspb.BinaryConstants.FLOAT64_EPS * 10,
  270. -jspb.BinaryConstants.FLOAT64_MAX,
  271. jspb.BinaryConstants.FLOAT64_MAX,
  272. function(x) { return x; });
  273. });
  274. });