bundle_test.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright 2017 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. import {BrotliDecode} from "./decode.js";
  6. import {makeTestData} from "./test_data.js";
  7. const CRC_64_POLY = new Uint32Array([0xD7870F42, 0xC96C5795]);
  8. /**
  9. * Calculates binary data footprint.
  10. *
  11. * @param {!Int8Array} data binary data
  12. * @return {string} footprint
  13. */
  14. function calculateCrc64(data) {
  15. const crc = new Uint32Array([0xFFFFFFFF, 0xFFFFFFFF]);
  16. const c = new Uint32Array(2);
  17. for (let i = 0; i < data.length; ++i) {
  18. c[1] = 0;
  19. c[0] = (crc[0] ^ data[i]) & 0xFF;
  20. for (let k = 0; k < 8; ++k) {
  21. const isOdd = c[0] & 1;
  22. c[0] = (c[0] >>> 1) | ((c[1] & 1) << 31);
  23. c[1] = c[1] >>> 1;
  24. if (isOdd) {
  25. c[0] = c[0] ^ CRC_64_POLY[0];
  26. c[1] = c[1] ^ CRC_64_POLY[1];
  27. }
  28. }
  29. crc[0] = ((crc[0] >>> 8) | ((crc[1] & 0xFF) << 24)) ^ c[0];
  30. crc[1] = (crc[1] >>> 8) ^ c[1];
  31. }
  32. crc[0] = ~crc[0];
  33. crc[1] = ~crc[1];
  34. let lo = crc[0].toString(16);
  35. lo = "0".repeat(8 - lo.length) + lo;
  36. let hi = crc[1].toString(16);
  37. hi = "0".repeat(8 - hi.length) + hi;
  38. return hi + lo;
  39. }
  40. /**
  41. * Decompresses data and checks that output footprint is correct.
  42. *
  43. * @param {string} entry filename including footprint prefix
  44. * @param {!Int8Array} data compressed data
  45. */
  46. function checkEntry(entry, data) {
  47. const expectedCrc = entry.substring(0, 16);
  48. const decompressed = BrotliDecode(data);
  49. const crc = calculateCrc64(decompressed);
  50. expect(expectedCrc).toEqual(crc);
  51. }
  52. describe("BundleTest", () => {
  53. const testData = makeTestData();
  54. for (const entry in testData) {
  55. if (!testData.hasOwnProperty(entry)) {
  56. continue;
  57. }
  58. const name = entry.substring(17);
  59. const data = testData[entry];
  60. it('test_' + name, checkEntry.bind(null, entry, data));
  61. }
  62. });