buffer_decoder_test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @fileoverview Tests for BufferDecoder.
  3. */
  4. goog.module('protobuf.binary.varintsTest');
  5. const BufferDecoder = goog.require('protobuf.binary.BufferDecoder');
  6. const {CHECK_CRITICAL_STATE, CHECK_STATE} = goog.require('protobuf.internal.checks');
  7. goog.setTestOnly();
  8. /**
  9. * @param {...number} bytes
  10. * @return {!ArrayBuffer}
  11. */
  12. function createArrayBuffer(...bytes) {
  13. return new Uint8Array(bytes).buffer;
  14. }
  15. describe('setCursor does', () => {
  16. it('set the cursor at the position specified', () => {
  17. const bufferDecoder =
  18. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x0, 0x1));
  19. expect(bufferDecoder.cursor()).toBe(0);
  20. bufferDecoder.setCursor(1);
  21. expect(bufferDecoder.cursor()).toBe(1);
  22. });
  23. });
  24. describe('skip does', () => {
  25. it('advance the cursor', () => {
  26. const bufferDecoder =
  27. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x0, 0x1, 0x2));
  28. bufferDecoder.setCursor(1);
  29. bufferDecoder.skip(1);
  30. expect(bufferDecoder.cursor()).toBe(2);
  31. });
  32. });
  33. describe('Skip varint does', () => {
  34. it('skip a varint', () => {
  35. const bufferDecoder =
  36. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x01));
  37. bufferDecoder.skipVarint();
  38. expect(bufferDecoder.cursor()).toBe(1);
  39. });
  40. it('fail when varint is larger than 10 bytes', () => {
  41. const bufferDecoder = BufferDecoder.fromArrayBuffer(createArrayBuffer(
  42. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
  43. if (CHECK_CRITICAL_STATE) {
  44. expect(() => bufferDecoder.skipVarint()).toThrow();
  45. } else {
  46. // Note in unchecked mode we produce invalid output for invalid inputs.
  47. // This test just documents our behavior in those cases.
  48. // These values might change at any point and are not considered
  49. // what the implementation should be doing here.
  50. bufferDecoder.skipVarint();
  51. expect(bufferDecoder.cursor()).toBe(11);
  52. }
  53. });
  54. it('fail when varint is beyond end of underlying array', () => {
  55. const bufferDecoder =
  56. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x80, 0x80));
  57. expect(() => bufferDecoder.skipVarint()).toThrow();
  58. });
  59. });
  60. describe('readVarint64 does', () => {
  61. it('read zero', () => {
  62. const bufferDecoder =
  63. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x00));
  64. const {lowBits, highBits} = bufferDecoder.getVarint(0);
  65. expect(lowBits).toBe(0);
  66. expect(highBits).toBe(0);
  67. expect(bufferDecoder.cursor()).toBe(1);
  68. });
  69. it('read one', () => {
  70. const bufferDecoder =
  71. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x01));
  72. const {lowBits, highBits} = bufferDecoder.getVarint(0);
  73. expect(lowBits).toBe(1);
  74. expect(highBits).toBe(0);
  75. expect(bufferDecoder.cursor()).toBe(1);
  76. });
  77. it('read max value', () => {
  78. const bufferDecoder = BufferDecoder.fromArrayBuffer(createArrayBuffer(
  79. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01));
  80. const {lowBits, highBits} = bufferDecoder.getVarint(0);
  81. expect(lowBits).toBe(-1);
  82. expect(highBits).toBe(-1);
  83. expect(bufferDecoder.cursor()).toBe(10);
  84. });
  85. });
  86. describe('readUnsignedVarint32 does', () => {
  87. it('read zero', () => {
  88. const bufferDecoder =
  89. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x00));
  90. const result = bufferDecoder.getUnsignedVarint32();
  91. expect(result).toBe(0);
  92. expect(bufferDecoder.cursor()).toBe(1);
  93. });
  94. it('read one', () => {
  95. const bufferDecoder =
  96. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x01));
  97. const result = bufferDecoder.getUnsignedVarint32();
  98. expect(result).toBe(1);
  99. expect(bufferDecoder.cursor()).toBe(1);
  100. });
  101. it('read max int32', () => {
  102. const bufferDecoder = BufferDecoder.fromArrayBuffer(
  103. createArrayBuffer(0xFF, 0xFF, 0xFF, 0xFF, 0x0F));
  104. const result = bufferDecoder.getUnsignedVarint32();
  105. expect(result).toBe(4294967295);
  106. expect(bufferDecoder.cursor()).toBe(5);
  107. });
  108. it('read max value', () => {
  109. const bufferDecoder = BufferDecoder.fromArrayBuffer(createArrayBuffer(
  110. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01));
  111. const result = bufferDecoder.getUnsignedVarint32();
  112. expect(result).toBe(4294967295);
  113. expect(bufferDecoder.cursor()).toBe(10);
  114. });
  115. it('fail if data is longer than 10 bytes', () => {
  116. const bufferDecoder = BufferDecoder.fromArrayBuffer(createArrayBuffer(
  117. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01));
  118. if (CHECK_CRITICAL_STATE) {
  119. expect(() => bufferDecoder.getUnsignedVarint32()).toThrow();
  120. } else {
  121. // Note in unchecked mode we produce invalid output for invalid inputs.
  122. // This test just documents our behavior in those cases.
  123. // These values might change at any point and are not considered
  124. // what the implementation should be doing here.
  125. const result = bufferDecoder.getUnsignedVarint32();
  126. expect(result).toBe(4294967295);
  127. expect(bufferDecoder.cursor()).toBe(10);
  128. }
  129. });
  130. });
  131. describe('readUnsignedVarint32At does', () => {
  132. it('reads from a specific index', () => {
  133. const bufferDecoder =
  134. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x1, 0x2));
  135. const result = bufferDecoder.getUnsignedVarint32At(1);
  136. expect(result).toBe(2);
  137. expect(bufferDecoder.cursor()).toBe(2);
  138. });
  139. });
  140. describe('getFloat32 does', () => {
  141. it('read one', () => {
  142. const bufferDecoder = BufferDecoder.fromArrayBuffer(
  143. createArrayBuffer(0x00, 0x00, 0x80, 0x3F));
  144. const result = bufferDecoder.getFloat32(0);
  145. expect(result).toBe(1);
  146. expect(bufferDecoder.cursor()).toBe(4);
  147. });
  148. });
  149. describe('getFloat64 does', () => {
  150. it('read one', () => {
  151. const bufferDecoder = BufferDecoder.fromArrayBuffer(
  152. createArrayBuffer(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F));
  153. const result = bufferDecoder.getFloat64(0);
  154. expect(result).toBe(1);
  155. expect(bufferDecoder.cursor()).toBe(8);
  156. });
  157. });
  158. describe('getInt32 does', () => {
  159. it('read one', () => {
  160. const bufferDecoder = BufferDecoder.fromArrayBuffer(
  161. createArrayBuffer(0x01, 0x00, 0x00, 0x00));
  162. const result = bufferDecoder.getInt32(0);
  163. expect(result).toBe(1);
  164. expect(bufferDecoder.cursor()).toBe(4);
  165. });
  166. it('read minus one', () => {
  167. const bufferDecoder = BufferDecoder.fromArrayBuffer(
  168. createArrayBuffer(0xFF, 0xFF, 0xFF, 0xFF));
  169. const result = bufferDecoder.getInt32(0);
  170. expect(result).toBe(-1);
  171. expect(bufferDecoder.cursor()).toBe(4);
  172. });
  173. });
  174. describe('getUint32 does', () => {
  175. it('read one', () => {
  176. const bufferDecoder =
  177. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x01, 0x00, 0x00, 0x0));
  178. const result = bufferDecoder.getUint32(0);
  179. expect(result).toBe(1);
  180. expect(bufferDecoder.cursor()).toBe(4);
  181. });
  182. it('read max uint32', () => {
  183. const bufferDecoder = BufferDecoder.fromArrayBuffer(
  184. createArrayBuffer(0xFF, 0xFF, 0xFF, 0xFF));
  185. const result = bufferDecoder.getUint32(0);
  186. expect(result).toBe(4294967295);
  187. expect(bufferDecoder.cursor()).toBe(4);
  188. });
  189. });
  190. describe('subBufferDecoder does', () => {
  191. it('can create valid sub buffers', () => {
  192. const bufferDecoder =
  193. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x00, 0x01, 0x02));
  194. expect(bufferDecoder.subBufferDecoder(0, 0))
  195. .toEqual(BufferDecoder.fromArrayBuffer(createArrayBuffer()));
  196. expect(bufferDecoder.subBufferDecoder(0, 1))
  197. .toEqual(BufferDecoder.fromArrayBuffer(createArrayBuffer(0x00)));
  198. expect(bufferDecoder.subBufferDecoder(1, 0))
  199. .toEqual(BufferDecoder.fromArrayBuffer(createArrayBuffer()));
  200. expect(bufferDecoder.subBufferDecoder(1, 1))
  201. .toEqual(BufferDecoder.fromArrayBuffer(createArrayBuffer(0x01)));
  202. expect(bufferDecoder.subBufferDecoder(1, 2))
  203. .toEqual(BufferDecoder.fromArrayBuffer(createArrayBuffer(0x01, 0x02)));
  204. });
  205. it('can not create invalid', () => {
  206. const bufferDecoder =
  207. BufferDecoder.fromArrayBuffer(createArrayBuffer(0x00, 0x01, 0x02));
  208. if (CHECK_STATE) {
  209. expect(() => bufferDecoder.subBufferDecoder(-1, 1)).toThrow();
  210. expect(() => bufferDecoder.subBufferDecoder(0, -4)).toThrow();
  211. expect(() => bufferDecoder.subBufferDecoder(0, 4)).toThrow();
  212. }
  213. });
  214. });