| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 | /** * @fileoverview Tests for indexer.js. */goog.module('protobuf.binary.IndexerTest');goog.setTestOnly();// Note to the reader:// Since the index behavior changes with the checking level some of the tests// in this file have to know which checking level is enabled to make correct// assertions.// Test are run in all checking levels.const BinaryStorage = goog.require('protobuf.runtime.BinaryStorage');const BufferDecoder = goog.require('protobuf.binary.BufferDecoder');const WireType = goog.require('protobuf.binary.WireType');const {CHECK_CRITICAL_STATE} = goog.require('protobuf.internal.checks');const {Field, IndexEntry} = goog.require('protobuf.binary.field');const {buildIndex} = goog.require('protobuf.binary.indexer');const {createBufferDecoder} = goog.require('protobuf.binary.bufferDecoderHelper');/** * Returns the number of fields stored. * * @param {!BinaryStorage} storage * @return {number} */function getStorageSize(storage) {  let size = 0;  storage.forEach(() => void size++);  return size;}/** * @type {number} */const PIVOT = 1;/** * Asserts a single IndexEntry at a given field number. * @param {!BinaryStorage} storage * @param {number} fieldNumber * @param {...!IndexEntry} expectedEntries */function assertStorageEntries(storage, fieldNumber, ...expectedEntries) {  expect(getStorageSize(storage)).toBe(1);  const entryArray = storage.get(fieldNumber).getIndexArray();  expect(entryArray).not.toBeUndefined();  expect(entryArray.length).toBe(expectedEntries.length);  for (let i = 0; i < entryArray.length; i++) {    const storageEntry = entryArray[i];    const expectedEntry = expectedEntries[i];    expect(storageEntry).toBe(expectedEntry);  }}describe('Indexer does', () => {  it('return empty storage for empty array', () => {    const storage = buildIndex(createBufferDecoder(), PIVOT);    expect(storage).not.toBeNull();    expect(getStorageSize(storage)).toBe(0);  });  it('throw for null array', () => {    expect(        () => buildIndex(            /** @type {!BufferDecoder} */ (/** @type {*} */ (null)), PIVOT))        .toThrow();  });  it('fail for invalid wire type (6)', () => {    expect(() => buildIndex(createBufferDecoder(0x0E, 0x01), PIVOT))        .toThrowError('Unexpected wire type: 6');  });  it('fail for invalid wire type (7)', () => {    expect(() => buildIndex(createBufferDecoder(0x0F, 0x01), PIVOT))        .toThrowError('Unexpected wire type: 7');  });  it('index varint', () => {    const data = createBufferDecoder(0x08, 0x01, 0x08, 0x01);    const storage = buildIndex(data, PIVOT);    assertStorageEntries(        storage, /* fieldNumber= */ 1,        Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 1),        Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 3));  });  it('index varint with two bytes field number', () => {    const data = createBufferDecoder(0xF8, 0x01, 0x01);    const storage = buildIndex(data, PIVOT);    assertStorageEntries(        storage, /* fieldNumber= */ 31,        Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 2));  });  it('fail for varints that are longer than 10 bytes', () => {    const data = createBufferDecoder(        0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00);    if (CHECK_CRITICAL_STATE) {      expect(() => buildIndex(data, PIVOT))          .toThrowError('Index out of bounds: index: 12 size: 11');    } else {      // Note in unchecked mode we produce invalid output for invalid inputs.      // This test just documents our behavior in those cases.      // These values might change at any point and are not considered      // what the implementation should be doing here.      const storage = buildIndex(data, PIVOT);      assertStorageEntries(          storage, /* fieldNumber= */ 1,          Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 1));    }  });  it('fail for varints with no data', () => {    const data = createBufferDecoder(0x08);    expect(() => buildIndex(data, PIVOT)).toThrow();  });  it('index fixed64', () => {    const data = createBufferDecoder(        /* first= */ 0x09, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,        /* second= */ 0x09, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08);    const storage = buildIndex(data, PIVOT);    assertStorageEntries(        storage, /* fieldNumber= */ 1,        Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 1),        Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 10));  });  it('fail for fixed64 data missing in input', () => {    const data =        createBufferDecoder(0x09, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07);    if (CHECK_CRITICAL_STATE) {      expect(() => buildIndex(data, PIVOT))          .toThrowError('Index out of bounds: index: 9 size: 8');    } else {      // Note in unchecked mode we produce invalid output for invalid inputs.      // This test just documents our behavior in those cases.      // These values might change at any point and are not considered      // what the implementation should be doing here.      const storage = buildIndex(data, PIVOT);      assertStorageEntries(          storage, /* fieldNumber= */ 1,          Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 1));    }  });  it('fail for fixed64 tag that has no data after it', () => {    if (CHECK_CRITICAL_STATE) {      const data = createBufferDecoder(0x09);      expect(() => buildIndex(data, PIVOT))          .toThrowError('Index out of bounds: index: 9 size: 1');    } else {      // Note in unchecked mode we produce invalid output for invalid inputs.      // This test just documents our behavior in those cases.      // These values might change at any point and are not considered      // what the implementation should be doing here.      const data = createBufferDecoder(0x09);      const storage = buildIndex(data, PIVOT);      assertStorageEntries(          storage, /* fieldNumber= */ 1,          Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 1));    }  });  it('index delimited', () => {    const data = createBufferDecoder(        /* first= */ 0x0A, 0x02, 0x00, 0x01, /* second= */ 0x0A, 0x02, 0x00,        0x01);    const storage = buildIndex(data, PIVOT);    assertStorageEntries(        storage, /* fieldNumber= */ 1,        Field.encodeIndexEntry(WireType.DELIMITED, /* startIndex= */ 1),        Field.encodeIndexEntry(WireType.DELIMITED, /* startIndex= */ 5));  });  it('fail for length deliimted field data missing in input', () => {    const data = createBufferDecoder(0x0A, 0x04, 0x00, 0x01);    if (CHECK_CRITICAL_STATE) {      expect(() => buildIndex(data, PIVOT))          .toThrowError('Index out of bounds: index: 6 size: 4');    } else {      // Note in unchecked mode we produce invalid output for invalid inputs.      // This test just documents our behavior in those cases.      // These values might change at any point and are not considered      // what the implementation should be doing here.      const storage = buildIndex(data, PIVOT);      assertStorageEntries(          storage, /* fieldNumber= */ 1,          Field.encodeIndexEntry(WireType.DELIMITED, /* startIndex= */ 1));    }  });  it('fail for delimited tag that has no data after it', () => {    const data = createBufferDecoder(0x0A);    expect(() => buildIndex(data, PIVOT)).toThrow();  });  it('index fixed32', () => {    const data = createBufferDecoder(        /* first= */ 0x0D, 0x01, 0x02, 0x03, 0x04, /* second= */ 0x0D, 0x01,        0x02, 0x03, 0x04);    const storage = buildIndex(data, PIVOT);    assertStorageEntries(        storage, /* fieldNumber= */ 1,        Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 1),        Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 6));  });  it('fail for fixed32 data missing in input', () => {    const data = createBufferDecoder(0x0D, 0x01, 0x02, 0x03);    if (CHECK_CRITICAL_STATE) {      expect(() => buildIndex(data, PIVOT))          .toThrowError('Index out of bounds: index: 5 size: 4');    } else {      // Note in unchecked mode we produce invalid output for invalid inputs.      // This test just documents our behavior in those cases.      // These values might change at any point and are not considered      // what the implementation should be doing here.      const storage = buildIndex(data, PIVOT);      assertStorageEntries(          storage, /* fieldNumber= */ 1,          Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 1));    }  });  it('fail for fixed32 tag that has no data after it', () => {    if (CHECK_CRITICAL_STATE) {      const data = createBufferDecoder(0x0D);      expect(() => buildIndex(data, PIVOT))          .toThrowError('Index out of bounds: index: 5 size: 1');    } else {      // Note in unchecked mode we produce invalid output for invalid inputs.      // This test just documents our behavior in those cases.      // These values might change at any point and are not considered      // what the implementation should be doing here.      const data = createBufferDecoder(0x0D);      const storage = buildIndex(data, PIVOT);      assertStorageEntries(          storage, /* fieldNumber= */ 1,          Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 1));    }  });  it('index group', () => {    const data = createBufferDecoder(        /* first= */ 0x0B, 0x08, 0x01, 0x0C, /* second= */ 0x0B, 0x08, 0x01,        0x0C);    const storage = buildIndex(data, PIVOT);    assertStorageEntries(        storage, /* fieldNumber= */ 1,        Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1),        Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 5));  });  it('index group and skips inner group', () => {    const data =        createBufferDecoder(0x0B, 0x0B, 0x08, 0x01, 0x0C, 0x08, 0x01, 0x0C);    const storage = buildIndex(data, PIVOT);    assertStorageEntries(        storage, /* fieldNumber= */ 1,        Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));  });  it('fail on unmatched stop group', () => {    const data = createBufferDecoder(0x0C, 0x01);    expect(() => buildIndex(data, PIVOT))        .toThrowError('Unexpected wire type: 4');  });  it('fail for groups without matching stop group', () => {    const data = createBufferDecoder(0x0B, 0x08, 0x01, 0x1C);    if (CHECK_CRITICAL_STATE) {      expect(() => buildIndex(data, PIVOT))          .toThrowError('Expected stop group for fieldnumber 1 not found.');    } else {      // Note in unchecked mode we produce invalid output for invalid inputs.      // This test just documents our behavior in those cases.      // These values might change at any point and are not considered      // what the implementation should be doing here.      const storage = buildIndex(data, PIVOT);      assertStorageEntries(          storage, /* fieldNumber= */ 1,          Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));    }  });  it('fail for groups without stop group', () => {    const data = createBufferDecoder(0x0B, 0x08, 0x01);    if (CHECK_CRITICAL_STATE) {      expect(() => buildIndex(data, PIVOT)).toThrowError('No end group found.');    } else {      // Note in unchecked mode we produce invalid output for invalid inputs.      // This test just documents our behavior in those cases.      // These values might change at any point and are not considered      // what the implementation should be doing here.      const storage = buildIndex(data, PIVOT);      assertStorageEntries(          storage, /* fieldNumber= */ 1,          Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));    }  });  it('fail for group tag that has no data after it', () => {    const data = createBufferDecoder(0x0B);    if (CHECK_CRITICAL_STATE) {      expect(() => buildIndex(data, PIVOT)).toThrowError('No end group found.');    } else {      // Note in unchecked mode we produce invalid output for invalid inputs.      // This test just documents our behavior in those cases.      // These values might change at any point and are not considered      // what the implementation should be doing here.      const storage = buildIndex(data, PIVOT);      assertStorageEntries(          storage, /* fieldNumber= */ 1,          Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));    }  });  it('index too large tag', () => {    const data = createBufferDecoder(0xF8, 0xFF, 0xFF, 0xFF, 0xFF);    expect(() => buildIndex(data, PIVOT)).toThrow();  });  it('fail for varint tag that has no data after it', () => {    const data = createBufferDecoder(0x08);    expect(() => buildIndex(data, PIVOT)).toThrow();  });});
 |