decoder_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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.BinaryEncoder');
  46. /**
  47. * Tests 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 encoder = new jspb.BinaryEncoder();
  58. // Encode zero and limits.
  59. writeValue.call(encoder, filter(0));
  60. writeValue.call(encoder, filter(epsilon));
  61. writeValue.call(encoder, filter(upperLimit));
  62. // Encode positive values.
  63. for (var cursor = epsilon; cursor < upperLimit; cursor *= 1.1) {
  64. writeValue.call(encoder, filter(cursor));
  65. }
  66. var decoder = jspb.BinaryDecoder.alloc(encoder.end());
  67. // Check zero and limits.
  68. assertEquals(filter(0), readValue.call(decoder));
  69. assertEquals(filter(epsilon), readValue.call(decoder));
  70. assertEquals(filter(upperLimit), readValue.call(decoder));
  71. // Check positive values.
  72. for (var cursor = epsilon; cursor < upperLimit; cursor *= 1.1) {
  73. if (filter(cursor) != readValue.call(decoder)) throw 'fail!';
  74. }
  75. // Encoding values outside the valid range should assert.
  76. assertThrows(function() {writeValue.call(encoder, -1);});
  77. assertThrows(function() {writeValue.call(encoder, upperLimit * 1.1);});
  78. }
  79. /**
  80. * Tests encoding and decoding of signed types.
  81. * @param {Function} readValue
  82. * @param {Function} writeValue
  83. * @param {number} epsilon
  84. * @param {number} lowerLimit
  85. * @param {number} upperLimit
  86. * @param {Function} filter
  87. * @suppress {missingProperties}
  88. */
  89. function doTestSignedValue(readValue,
  90. writeValue, epsilon, lowerLimit, upperLimit, filter) {
  91. var encoder = new jspb.BinaryEncoder();
  92. // Encode zero and limits.
  93. writeValue.call(encoder, filter(lowerLimit));
  94. writeValue.call(encoder, filter(-epsilon));
  95. writeValue.call(encoder, filter(0));
  96. writeValue.call(encoder, filter(epsilon));
  97. writeValue.call(encoder, filter(upperLimit));
  98. var inputValues = [];
  99. // Encode negative values.
  100. for (var cursor = lowerLimit; cursor < -epsilon; cursor /= 1.1) {
  101. var val = filter(cursor);
  102. writeValue.call(encoder, val);
  103. inputValues.push(val);
  104. }
  105. // Encode positive values.
  106. for (var cursor = epsilon; cursor < upperLimit; cursor *= 1.1) {
  107. var val = filter(cursor);
  108. writeValue.call(encoder, val);
  109. inputValues.push(val);
  110. }
  111. var decoder = jspb.BinaryDecoder.alloc(encoder.end());
  112. // Check zero and limits.
  113. assertEquals(filter(lowerLimit), readValue.call(decoder));
  114. assertEquals(filter(-epsilon), readValue.call(decoder));
  115. assertEquals(filter(0), readValue.call(decoder));
  116. assertEquals(filter(epsilon), readValue.call(decoder));
  117. assertEquals(filter(upperLimit), readValue.call(decoder));
  118. // Verify decoded values.
  119. for (var i = 0; i < inputValues.length; i++) {
  120. assertEquals(inputValues[i], readValue.call(decoder));
  121. }
  122. // Encoding values outside the valid range should assert.
  123. assertThrows(function() {writeValue.call(encoder, lowerLimit * 1.1);});
  124. assertThrows(function() {writeValue.call(encoder, upperLimit * 1.1);});
  125. }
  126. describe('binaryDecoderTest', function() {
  127. /**
  128. * Tests the decoder instance cache.
  129. */
  130. it('testInstanceCache', /** @suppress {visibility} */ function() {
  131. // Empty the instance caches.
  132. jspb.BinaryDecoder.instanceCache_ = [];
  133. // Allocating and then freeing a decoder should put it in the instance
  134. // cache.
  135. jspb.BinaryDecoder.alloc().free();
  136. assertEquals(1, jspb.BinaryDecoder.instanceCache_.length);
  137. // Allocating and then freeing three decoders should leave us with three in
  138. // the cache.
  139. var decoder1 = jspb.BinaryDecoder.alloc();
  140. var decoder2 = jspb.BinaryDecoder.alloc();
  141. var decoder3 = jspb.BinaryDecoder.alloc();
  142. decoder1.free();
  143. decoder2.free();
  144. decoder3.free();
  145. assertEquals(3, jspb.BinaryDecoder.instanceCache_.length);
  146. });
  147. /**
  148. * Tests reading 64-bit integers as hash strings.
  149. */
  150. it('testHashStrings', function() {
  151. var encoder = new jspb.BinaryEncoder();
  152. var hashA = String.fromCharCode(0x00, 0x00, 0x00, 0x00,
  153. 0x00, 0x00, 0x00, 0x00);
  154. var hashB = String.fromCharCode(0x12, 0x34, 0x00, 0x00,
  155. 0x00, 0x00, 0x00, 0x00);
  156. var hashC = String.fromCharCode(0x12, 0x34, 0x56, 0x78,
  157. 0x87, 0x65, 0x43, 0x21);
  158. var hashD = String.fromCharCode(0xFF, 0xFF, 0xFF, 0xFF,
  159. 0xFF, 0xFF, 0xFF, 0xFF);
  160. encoder.writeVarintHash64(hashA);
  161. encoder.writeVarintHash64(hashB);
  162. encoder.writeVarintHash64(hashC);
  163. encoder.writeVarintHash64(hashD);
  164. encoder.writeFixedHash64(hashA);
  165. encoder.writeFixedHash64(hashB);
  166. encoder.writeFixedHash64(hashC);
  167. encoder.writeFixedHash64(hashD);
  168. var decoder = jspb.BinaryDecoder.alloc(encoder.end());
  169. assertEquals(hashA, decoder.readVarintHash64());
  170. assertEquals(hashB, decoder.readVarintHash64());
  171. assertEquals(hashC, decoder.readVarintHash64());
  172. assertEquals(hashD, decoder.readVarintHash64());
  173. assertEquals(hashA, decoder.readFixedHash64());
  174. assertEquals(hashB, decoder.readFixedHash64());
  175. assertEquals(hashC, decoder.readFixedHash64());
  176. assertEquals(hashD, decoder.readFixedHash64());
  177. });
  178. /**
  179. * Tests reading and writing large strings
  180. */
  181. it('testLargeStrings', function() {
  182. var encoder = new jspb.BinaryEncoder();
  183. var len = 150000;
  184. var long_string = '';
  185. for (var i = 0; i < len; i++) {
  186. long_string += 'a';
  187. }
  188. encoder.writeString(long_string);
  189. var decoder = jspb.BinaryDecoder.alloc(encoder.end());
  190. assertEquals(long_string, decoder.readString(len));
  191. });
  192. /**
  193. * Test encoding and decoding utf-8.
  194. */
  195. it('testUtf8', function() {
  196. var encoder = new jspb.BinaryEncoder();
  197. var ascii = "ASCII should work in 3, 2, 1...";
  198. var utf8_two_bytes = "©";
  199. var utf8_three_bytes = "❄";
  200. var utf8_four_bytes = "😁";
  201. encoder.writeString(ascii);
  202. encoder.writeString(utf8_two_bytes);
  203. encoder.writeString(utf8_three_bytes);
  204. encoder.writeString(utf8_four_bytes);
  205. var decoder = jspb.BinaryDecoder.alloc(encoder.end());
  206. assertEquals(ascii, decoder.readString(ascii.length));
  207. assertEquals(utf8_two_bytes, decoder.readString(utf8_two_bytes.length));
  208. assertEquals(utf8_three_bytes, decoder.readString(utf8_three_bytes.length));
  209. assertEquals(utf8_four_bytes, decoder.readString(utf8_four_bytes.length));
  210. });
  211. /**
  212. * Verifies that misuse of the decoder class triggers assertions.
  213. * @suppress {checkTypes|visibility}
  214. */
  215. it('testDecodeErrors', function() {
  216. // Reading a value past the end of the stream should trigger an assertion.
  217. var decoder = jspb.BinaryDecoder.alloc([0, 1, 2]);
  218. assertThrows(function() {decoder.readUint64()});
  219. // Overlong varints should trigger assertions.
  220. decoder.setBlock([255, 255, 255, 255, 255, 255,
  221. 255, 255, 255, 255, 255, 0]);
  222. assertThrows(function() {decoder.readUnsignedVarint64()});
  223. decoder.reset();
  224. assertThrows(function() {decoder.readSignedVarint64()});
  225. decoder.reset();
  226. assertThrows(function() {decoder.readZigzagVarint64()});
  227. // Positive 32-bit varints encoded with 1 bits in positions 33 through 35
  228. // should trigger assertions.
  229. decoder.setBlock([255, 255, 255, 255, 0x1F]);
  230. assertThrows(function() {decoder.readUnsignedVarint32()});
  231. decoder.setBlock([255, 255, 255, 255, 0x2F]);
  232. assertThrows(function() {decoder.readUnsignedVarint32()});
  233. decoder.setBlock([255, 255, 255, 255, 0x4F]);
  234. assertThrows(function() {decoder.readUnsignedVarint32()});
  235. // Negative 32-bit varints encoded with non-1 bits in the high dword should
  236. // trigger assertions.
  237. decoder.setBlock([255, 255, 255, 255, 255, 255, 0, 255, 255, 1]);
  238. assertThrows(function() {decoder.readUnsignedVarint32()});
  239. decoder.setBlock([255, 255, 255, 255, 255, 255, 255, 255, 255, 0]);
  240. assertThrows(function() {decoder.readUnsignedVarint32()});
  241. });
  242. /**
  243. * Tests encoding and decoding of unsigned integers.
  244. */
  245. it('testUnsignedIntegers', function() {
  246. doTestUnsignedValue(
  247. jspb.BinaryDecoder.prototype.readUint8,
  248. jspb.BinaryEncoder.prototype.writeUint8,
  249. 1, 0xFF, Math.round);
  250. doTestUnsignedValue(
  251. jspb.BinaryDecoder.prototype.readUint16,
  252. jspb.BinaryEncoder.prototype.writeUint16,
  253. 1, 0xFFFF, Math.round);
  254. doTestUnsignedValue(
  255. jspb.BinaryDecoder.prototype.readUint32,
  256. jspb.BinaryEncoder.prototype.writeUint32,
  257. 1, 0xFFFFFFFF, Math.round);
  258. doTestUnsignedValue(
  259. jspb.BinaryDecoder.prototype.readUint64,
  260. jspb.BinaryEncoder.prototype.writeUint64,
  261. 1, Math.pow(2, 64) - 1025, Math.round);
  262. });
  263. /**
  264. * Tests encoding and decoding of signed integers.
  265. */
  266. it('testSignedIntegers', function() {
  267. doTestSignedValue(
  268. jspb.BinaryDecoder.prototype.readInt8,
  269. jspb.BinaryEncoder.prototype.writeInt8,
  270. 1, -0x80, 0x7F, Math.round);
  271. doTestSignedValue(
  272. jspb.BinaryDecoder.prototype.readInt16,
  273. jspb.BinaryEncoder.prototype.writeInt16,
  274. 1, -0x8000, 0x7FFF, Math.round);
  275. doTestSignedValue(
  276. jspb.BinaryDecoder.prototype.readInt32,
  277. jspb.BinaryEncoder.prototype.writeInt32,
  278. 1, -0x80000000, 0x7FFFFFFF, Math.round);
  279. doTestSignedValue(
  280. jspb.BinaryDecoder.prototype.readInt64,
  281. jspb.BinaryEncoder.prototype.writeInt64,
  282. 1, -Math.pow(2, 63), Math.pow(2, 63) - 513, Math.round);
  283. });
  284. /**
  285. * Tests encoding and decoding of floats.
  286. */
  287. it('testFloats', function() {
  288. /**
  289. * @param {number} x
  290. * @return {number}
  291. */
  292. function truncate(x) {
  293. var temp = new Float32Array(1);
  294. temp[0] = x;
  295. return temp[0];
  296. }
  297. doTestSignedValue(
  298. jspb.BinaryDecoder.prototype.readFloat,
  299. jspb.BinaryEncoder.prototype.writeFloat,
  300. jspb.BinaryConstants.FLOAT32_EPS,
  301. -jspb.BinaryConstants.FLOAT32_MAX,
  302. jspb.BinaryConstants.FLOAT32_MAX,
  303. truncate);
  304. doTestSignedValue(
  305. jspb.BinaryDecoder.prototype.readDouble,
  306. jspb.BinaryEncoder.prototype.writeDouble,
  307. jspb.BinaryConstants.FLOAT64_EPS * 10,
  308. -jspb.BinaryConstants.FLOAT64_MAX,
  309. jspb.BinaryConstants.FLOAT64_MAX,
  310. function(x) { return x; });
  311. });
  312. });