indexer_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * @fileoverview Tests for indexer.js.
  3. */
  4. goog.module('protobuf.binary.IndexerTest');
  5. goog.setTestOnly();
  6. // Note to the reader:
  7. // Since the index behavior changes with the checking level some of the tests
  8. // in this file have to know which checking level is enabled to make correct
  9. // assertions.
  10. // Test are run in all checking levels.
  11. const BinaryStorage = goog.require('protobuf.runtime.BinaryStorage');
  12. const BufferDecoder = goog.require('protobuf.binary.BufferDecoder');
  13. const WireType = goog.require('protobuf.binary.WireType');
  14. const {CHECK_CRITICAL_STATE} = goog.require('protobuf.internal.checks');
  15. const {Field, IndexEntry} = goog.require('protobuf.binary.field');
  16. const {buildIndex} = goog.require('protobuf.binary.indexer');
  17. const {createBufferDecoder} = goog.require('protobuf.binary.bufferDecoderHelper');
  18. /**
  19. * Returns the number of fields stored.
  20. *
  21. * @param {!BinaryStorage} storage
  22. * @return {number}
  23. */
  24. function getStorageSize(storage) {
  25. let size = 0;
  26. storage.forEach(() => void size++);
  27. return size;
  28. }
  29. /**
  30. * @type {number}
  31. */
  32. const PIVOT = 1;
  33. /**
  34. * Asserts a single IndexEntry at a given field number.
  35. * @param {!BinaryStorage} storage
  36. * @param {number} fieldNumber
  37. * @param {...!IndexEntry} expectedEntries
  38. */
  39. function assertStorageEntries(storage, fieldNumber, ...expectedEntries) {
  40. expect(getStorageSize(storage)).toBe(1);
  41. const entryArray = storage.get(fieldNumber).getIndexArray();
  42. expect(entryArray).not.toBeUndefined();
  43. expect(entryArray.length).toBe(expectedEntries.length);
  44. for (let i = 0; i < entryArray.length; i++) {
  45. const storageEntry = entryArray[i];
  46. const expectedEntry = expectedEntries[i];
  47. expect(storageEntry).toBe(expectedEntry);
  48. }
  49. }
  50. describe('Indexer does', () => {
  51. it('return empty storage for empty array', () => {
  52. const storage = buildIndex(createBufferDecoder(), PIVOT);
  53. expect(storage).not.toBeNull();
  54. expect(getStorageSize(storage)).toBe(0);
  55. });
  56. it('throw for null array', () => {
  57. expect(
  58. () => buildIndex(
  59. /** @type {!BufferDecoder} */ (/** @type {*} */ (null)), PIVOT))
  60. .toThrow();
  61. });
  62. it('fail for invalid wire type (6)', () => {
  63. expect(() => buildIndex(createBufferDecoder(0x0E, 0x01), PIVOT))
  64. .toThrowError('Unexpected wire type: 6');
  65. });
  66. it('fail for invalid wire type (7)', () => {
  67. expect(() => buildIndex(createBufferDecoder(0x0F, 0x01), PIVOT))
  68. .toThrowError('Unexpected wire type: 7');
  69. });
  70. it('index varint', () => {
  71. const data = createBufferDecoder(0x08, 0x01, 0x08, 0x01);
  72. const storage = buildIndex(data, PIVOT);
  73. assertStorageEntries(
  74. storage, /* fieldNumber= */ 1,
  75. Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 1),
  76. Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 3));
  77. });
  78. it('index varint with two bytes field number', () => {
  79. const data = createBufferDecoder(0xF8, 0x01, 0x01);
  80. const storage = buildIndex(data, PIVOT);
  81. assertStorageEntries(
  82. storage, /* fieldNumber= */ 31,
  83. Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 2));
  84. });
  85. it('fail for varints that are longer than 10 bytes', () => {
  86. const data = createBufferDecoder(
  87. 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00);
  88. if (CHECK_CRITICAL_STATE) {
  89. expect(() => buildIndex(data, PIVOT))
  90. .toThrowError('Index out of bounds: index: 12 size: 11');
  91. } else {
  92. // Note in unchecked mode we produce invalid output for invalid inputs.
  93. // This test just documents our behavior in those cases.
  94. // These values might change at any point and are not considered
  95. // what the implementation should be doing here.
  96. const storage = buildIndex(data, PIVOT);
  97. assertStorageEntries(
  98. storage, /* fieldNumber= */ 1,
  99. Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 1));
  100. }
  101. });
  102. it('fail for varints with no data', () => {
  103. const data = createBufferDecoder(0x08);
  104. expect(() => buildIndex(data, PIVOT)).toThrow();
  105. });
  106. it('index fixed64', () => {
  107. const data = createBufferDecoder(
  108. /* first= */ 0x09, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  109. /* second= */ 0x09, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08);
  110. const storage = buildIndex(data, PIVOT);
  111. assertStorageEntries(
  112. storage, /* fieldNumber= */ 1,
  113. Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 1),
  114. Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 10));
  115. });
  116. it('fail for fixed64 data missing in input', () => {
  117. const data =
  118. createBufferDecoder(0x09, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07);
  119. if (CHECK_CRITICAL_STATE) {
  120. expect(() => buildIndex(data, PIVOT))
  121. .toThrowError('Index out of bounds: index: 9 size: 8');
  122. } else {
  123. // Note in unchecked mode we produce invalid output for invalid inputs.
  124. // This test just documents our behavior in those cases.
  125. // These values might change at any point and are not considered
  126. // what the implementation should be doing here.
  127. const storage = buildIndex(data, PIVOT);
  128. assertStorageEntries(
  129. storage, /* fieldNumber= */ 1,
  130. Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 1));
  131. }
  132. });
  133. it('fail for fixed64 tag that has no data after it', () => {
  134. if (CHECK_CRITICAL_STATE) {
  135. const data = createBufferDecoder(0x09);
  136. expect(() => buildIndex(data, PIVOT))
  137. .toThrowError('Index out of bounds: index: 9 size: 1');
  138. } else {
  139. // Note in unchecked mode we produce invalid output for invalid inputs.
  140. // This test just documents our behavior in those cases.
  141. // These values might change at any point and are not considered
  142. // what the implementation should be doing here.
  143. const data = createBufferDecoder(0x09);
  144. const storage = buildIndex(data, PIVOT);
  145. assertStorageEntries(
  146. storage, /* fieldNumber= */ 1,
  147. Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 1));
  148. }
  149. });
  150. it('index delimited', () => {
  151. const data = createBufferDecoder(
  152. /* first= */ 0x0A, 0x02, 0x00, 0x01, /* second= */ 0x0A, 0x02, 0x00,
  153. 0x01);
  154. const storage = buildIndex(data, PIVOT);
  155. assertStorageEntries(
  156. storage, /* fieldNumber= */ 1,
  157. Field.encodeIndexEntry(WireType.DELIMITED, /* startIndex= */ 1),
  158. Field.encodeIndexEntry(WireType.DELIMITED, /* startIndex= */ 5));
  159. });
  160. it('fail for length deliimted field data missing in input', () => {
  161. const data = createBufferDecoder(0x0A, 0x04, 0x00, 0x01);
  162. if (CHECK_CRITICAL_STATE) {
  163. expect(() => buildIndex(data, PIVOT))
  164. .toThrowError('Index out of bounds: index: 6 size: 4');
  165. } else {
  166. // Note in unchecked mode we produce invalid output for invalid inputs.
  167. // This test just documents our behavior in those cases.
  168. // These values might change at any point and are not considered
  169. // what the implementation should be doing here.
  170. const storage = buildIndex(data, PIVOT);
  171. assertStorageEntries(
  172. storage, /* fieldNumber= */ 1,
  173. Field.encodeIndexEntry(WireType.DELIMITED, /* startIndex= */ 1));
  174. }
  175. });
  176. it('fail for delimited tag that has no data after it', () => {
  177. const data = createBufferDecoder(0x0A);
  178. expect(() => buildIndex(data, PIVOT)).toThrow();
  179. });
  180. it('index fixed32', () => {
  181. const data = createBufferDecoder(
  182. /* first= */ 0x0D, 0x01, 0x02, 0x03, 0x04, /* second= */ 0x0D, 0x01,
  183. 0x02, 0x03, 0x04);
  184. const storage = buildIndex(data, PIVOT);
  185. assertStorageEntries(
  186. storage, /* fieldNumber= */ 1,
  187. Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 1),
  188. Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 6));
  189. });
  190. it('fail for fixed32 data missing in input', () => {
  191. const data = createBufferDecoder(0x0D, 0x01, 0x02, 0x03);
  192. if (CHECK_CRITICAL_STATE) {
  193. expect(() => buildIndex(data, PIVOT))
  194. .toThrowError('Index out of bounds: index: 5 size: 4');
  195. } else {
  196. // Note in unchecked mode we produce invalid output for invalid inputs.
  197. // This test just documents our behavior in those cases.
  198. // These values might change at any point and are not considered
  199. // what the implementation should be doing here.
  200. const storage = buildIndex(data, PIVOT);
  201. assertStorageEntries(
  202. storage, /* fieldNumber= */ 1,
  203. Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 1));
  204. }
  205. });
  206. it('fail for fixed32 tag that has no data after it', () => {
  207. if (CHECK_CRITICAL_STATE) {
  208. const data = createBufferDecoder(0x0D);
  209. expect(() => buildIndex(data, PIVOT))
  210. .toThrowError('Index out of bounds: index: 5 size: 1');
  211. } else {
  212. // Note in unchecked mode we produce invalid output for invalid inputs.
  213. // This test just documents our behavior in those cases.
  214. // These values might change at any point and are not considered
  215. // what the implementation should be doing here.
  216. const data = createBufferDecoder(0x0D);
  217. const storage = buildIndex(data, PIVOT);
  218. assertStorageEntries(
  219. storage, /* fieldNumber= */ 1,
  220. Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 1));
  221. }
  222. });
  223. it('index group', () => {
  224. const data = createBufferDecoder(
  225. /* first= */ 0x0B, 0x08, 0x01, 0x0C, /* second= */ 0x0B, 0x08, 0x01,
  226. 0x0C);
  227. const storage = buildIndex(data, PIVOT);
  228. assertStorageEntries(
  229. storage, /* fieldNumber= */ 1,
  230. Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1),
  231. Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 5));
  232. });
  233. it('index group and skips inner group', () => {
  234. const data =
  235. createBufferDecoder(0x0B, 0x0B, 0x08, 0x01, 0x0C, 0x08, 0x01, 0x0C);
  236. const storage = buildIndex(data, PIVOT);
  237. assertStorageEntries(
  238. storage, /* fieldNumber= */ 1,
  239. Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));
  240. });
  241. it('fail on unmatched stop group', () => {
  242. const data = createBufferDecoder(0x0C, 0x01);
  243. expect(() => buildIndex(data, PIVOT))
  244. .toThrowError('Unexpected wire type: 4');
  245. });
  246. it('fail for groups without matching stop group', () => {
  247. const data = createBufferDecoder(0x0B, 0x08, 0x01, 0x1C);
  248. if (CHECK_CRITICAL_STATE) {
  249. expect(() => buildIndex(data, PIVOT))
  250. .toThrowError('Expected stop group for fieldnumber 1 not found.');
  251. } else {
  252. // Note in unchecked mode we produce invalid output for invalid inputs.
  253. // This test just documents our behavior in those cases.
  254. // These values might change at any point and are not considered
  255. // what the implementation should be doing here.
  256. const storage = buildIndex(data, PIVOT);
  257. assertStorageEntries(
  258. storage, /* fieldNumber= */ 1,
  259. Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));
  260. }
  261. });
  262. it('fail for groups without stop group', () => {
  263. const data = createBufferDecoder(0x0B, 0x08, 0x01);
  264. if (CHECK_CRITICAL_STATE) {
  265. expect(() => buildIndex(data, PIVOT)).toThrowError('No end group found.');
  266. } else {
  267. // Note in unchecked mode we produce invalid output for invalid inputs.
  268. // This test just documents our behavior in those cases.
  269. // These values might change at any point and are not considered
  270. // what the implementation should be doing here.
  271. const storage = buildIndex(data, PIVOT);
  272. assertStorageEntries(
  273. storage, /* fieldNumber= */ 1,
  274. Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));
  275. }
  276. });
  277. it('fail for group tag that has no data after it', () => {
  278. const data = createBufferDecoder(0x0B);
  279. if (CHECK_CRITICAL_STATE) {
  280. expect(() => buildIndex(data, PIVOT)).toThrowError('No end group found.');
  281. } else {
  282. // Note in unchecked mode we produce invalid output for invalid inputs.
  283. // This test just documents our behavior in those cases.
  284. // These values might change at any point and are not considered
  285. // what the implementation should be doing here.
  286. const storage = buildIndex(data, PIVOT);
  287. assertStorageEntries(
  288. storage, /* fieldNumber= */ 1,
  289. Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));
  290. }
  291. });
  292. it('index too large tag', () => {
  293. const data = createBufferDecoder(0xF8, 0xFF, 0xFF, 0xFF, 0xFF);
  294. expect(() => buildIndex(data, PIVOT)).toThrow();
  295. });
  296. it('fail for varint tag that has no data after it', () => {
  297. const data = createBufferDecoder(0x08);
  298. expect(() => buildIndex(data, PIVOT)).toThrow();
  299. });
  300. });