writer_test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 writer. In
  32. * practice BinaryWriter is used to drive the Decoder and Reader test cases,
  33. * so only writer-specific tests are here.
  34. *
  35. * Test suite is written using Jasmine -- see http://jasmine.github.io/
  36. *
  37. * @author aappleby@google.com (Austin Appleby)
  38. */
  39. goog.require('goog.crypt');
  40. goog.require('goog.testing.asserts');
  41. goog.require('jspb.BinaryReader');
  42. goog.require('jspb.BinaryWriter');
  43. /**
  44. * @param {function()} func This function should throw an error when run.
  45. */
  46. function assertFails(func) {
  47. assertThrows(func);
  48. }
  49. describe('binaryWriterTest', function() {
  50. /**
  51. * Verifies that misuse of the writer class triggers assertions.
  52. */
  53. it('testWriteErrors', function() {
  54. // Submessages with invalid field indices should assert.
  55. var writer = new jspb.BinaryWriter();
  56. var dummyMessage = /** @type {!jspb.BinaryMessage} */({});
  57. assertFails(function() {
  58. writer.writeMessage(-1, dummyMessage, goog.nullFunction);
  59. });
  60. // Writing invalid field indices should assert.
  61. writer = new jspb.BinaryWriter();
  62. assertFails(function() {writer.writeUint64(-1, 1);});
  63. // Writing out-of-range field values should assert.
  64. writer = new jspb.BinaryWriter();
  65. assertFails(function() {writer.writeInt32(1, -Infinity);});
  66. assertFails(function() {writer.writeInt32(1, Infinity);});
  67. assertFails(function() {writer.writeInt64(1, -Infinity);});
  68. assertFails(function() {writer.writeInt64(1, Infinity);});
  69. assertFails(function() {writer.writeUint32(1, -1);});
  70. assertFails(function() {writer.writeUint32(1, Infinity);});
  71. assertFails(function() {writer.writeUint64(1, -1);});
  72. assertFails(function() {writer.writeUint64(1, Infinity);});
  73. assertFails(function() {writer.writeSint32(1, -Infinity);});
  74. assertFails(function() {writer.writeSint32(1, Infinity);});
  75. assertFails(function() {writer.writeSint64(1, -Infinity);});
  76. assertFails(function() {writer.writeSint64(1, Infinity);});
  77. assertFails(function() {writer.writeFixed32(1, -1);});
  78. assertFails(function() {writer.writeFixed32(1, Infinity);});
  79. assertFails(function() {writer.writeFixed64(1, -1);});
  80. assertFails(function() {writer.writeFixed64(1, Infinity);});
  81. assertFails(function() {writer.writeSfixed32(1, -Infinity);});
  82. assertFails(function() {writer.writeSfixed32(1, Infinity);});
  83. assertFails(function() {writer.writeSfixed64(1, -Infinity);});
  84. assertFails(function() {writer.writeSfixed64(1, Infinity);});
  85. });
  86. /**
  87. * Basic test of retrieving the result as a Uint8Array buffer
  88. */
  89. it('testGetResultBuffer', function() {
  90. var expected = '0864120b48656c6c6f20776f726c641a0301020320c801';
  91. var writer = new jspb.BinaryWriter();
  92. writer.writeUint32(1, 100);
  93. writer.writeString(2, 'Hello world');
  94. writer.writeBytes(3, new Uint8Array([1, 2, 3]));
  95. writer.writeUint32(4, 200);
  96. var buffer = writer.getResultBuffer();
  97. assertEquals(expected, goog.crypt.byteArrayToHex(buffer));
  98. });
  99. /**
  100. * Tests websafe encodings for base64 strings.
  101. */
  102. it('testWebSafeOption', function() {
  103. var writer = new jspb.BinaryWriter();
  104. writer.writeBytes(1, new Uint8Array([127]));
  105. assertEquals('CgF/', writer.getResultBase64String());
  106. assertEquals('CgF/', writer.getResultBase64String(false));
  107. assertEquals('CgF_', writer.getResultBase64String(true));
  108. });
  109. it('writes split 64 fields', function() {
  110. var writer = new jspb.BinaryWriter();
  111. writer.writeSplitVarint64(1, 0x1, 0x2);
  112. writer.writeSplitVarint64(1, 0xFFFFFFFF, 0xFFFFFFFF);
  113. writer.writeSplitFixed64(2, 0x1, 0x2);
  114. writer.writeSplitFixed64(2, 0xFFFFFFF0, 0xFFFFFFFF);
  115. function lo(i) {
  116. return i + 1;
  117. }
  118. function hi(i) {
  119. return i + 2;
  120. }
  121. writer.writeRepeatedSplitVarint64(3, [0, 1, 2], lo, hi);
  122. writer.writeRepeatedSplitFixed64(4, [0, 1, 2], lo, hi);
  123. writer.writePackedSplitVarint64(5, [0, 1, 2], lo, hi);
  124. writer.writePackedSplitFixed64(6, [0, 1, 2], lo, hi);
  125. function bitsAsArray(lowBits, highBits) {
  126. return [lowBits >>> 0, highBits >>> 0];
  127. }
  128. var reader = jspb.BinaryReader.alloc(writer.getResultBuffer());
  129. reader.nextField();
  130. expect(reader.getFieldNumber()).toEqual(1);
  131. expect(reader.readSplitVarint64(bitsAsArray)).toEqual([0x1, 0x2]);
  132. reader.nextField();
  133. expect(reader.getFieldNumber()).toEqual(1);
  134. expect(reader.readSplitVarint64(bitsAsArray)).toEqual([
  135. 0xFFFFFFFF, 0xFFFFFFFF
  136. ]);
  137. reader.nextField();
  138. expect(reader.getFieldNumber()).toEqual(2);
  139. expect(reader.readSplitFixed64(bitsAsArray)).toEqual([0x1, 0x2]);
  140. reader.nextField();
  141. expect(reader.getFieldNumber()).toEqual(2);
  142. expect(reader.readSplitFixed64(bitsAsArray)).toEqual([
  143. 0xFFFFFFF0, 0xFFFFFFFF
  144. ]);
  145. for (let i = 0; i < 3; i++) {
  146. reader.nextField();
  147. expect(reader.getFieldNumber()).toEqual(3);
  148. expect(reader.readSplitVarint64(bitsAsArray)).toEqual([i + 1, i + 2]);
  149. }
  150. for (let i = 0; i < 3; i++) {
  151. reader.nextField();
  152. expect(reader.getFieldNumber()).toEqual(4);
  153. expect(reader.readSplitFixed64(bitsAsArray)).toEqual([i + 1, i + 2]);
  154. }
  155. reader.nextField();
  156. expect(reader.getFieldNumber()).toEqual(5);
  157. expect(reader.readPackedInt64String()).toEqual([
  158. String(2 * 2 ** 32 + 1),
  159. String(3 * 2 ** 32 + 2),
  160. String(4 * 2 ** 32 + 3),
  161. ]);
  162. reader.nextField();
  163. expect(reader.getFieldNumber()).toEqual(6);
  164. expect(reader.readPackedFixed64String()).toEqual([
  165. String(2 * 2 ** 32 + 1),
  166. String(3 * 2 ** 32 + 2),
  167. String(4 * 2 ** 32 + 3),
  168. ]);
  169. });
  170. });