index-spec.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. import expect from "unexpected";
  3. import unzip from "../src";
  4. import temp from "temp";
  5. import path from "path";
  6. import fs from "fs";
  7. import mockfs from "mock-fs";
  8. describe("unzip-crx", () => {
  9. let tempDir;
  10. before(() => {
  11. temp.track();
  12. });
  13. beforeEach(() => {
  14. tempDir = temp.mkdirSync("unzip-crx-test-files");
  15. });
  16. it("should unpack the given crx file", (done) => {
  17. const unzipPath = path.resolve(tempDir, "ext");
  18. const readmeFile = path.resolve(tempDir, "ext/README.md");
  19. unzip("./test/fixtures/extension.crx", unzipPath)
  20. .then(() => {
  21. const file = fs.readFileSync(readmeFile, "utf8");
  22. expect(file, "to equal", "# Crazy Readme File");
  23. done();
  24. })
  25. .catch((err) => done(err));
  26. });
  27. it("should unpack the given regular zip file", (done) => {
  28. const expectBinary = fs.readFileSync(
  29. path.join(__dirname, "./fixtures/extension/test.bin")
  30. );
  31. const unzipPath = path.resolve(tempDir, "ext");
  32. const readmeFile = path.resolve(tempDir, "ext/README.md");
  33. const binaryFile = path.resolve(tempDir, "ext/test.bin");
  34. unzip("./test/fixtures/extension-zipped.crx", unzipPath)
  35. .then(() => {
  36. const file = fs.readFileSync(readmeFile, "utf8");
  37. const binaryContent = fs.readFileSync(binaryFile);
  38. expect(file, "to equal", "# Crazy Readme File");
  39. expect(binaryContent, "to equal", expectBinary);
  40. done();
  41. })
  42. .catch((err) => done(err));
  43. });
  44. it("should throw if crx file header malformed", () => {
  45. const unzipPath = path.resolve(tempDir, "ext");
  46. return expect(
  47. unzip("./test/fixtures/extension-malformed.crx", unzipPath),
  48. "to be rejected with",
  49. new Error("Invalid header: Does not start with Cr24")
  50. );
  51. });
  52. it("should throw if crx version number is malformed", () => {
  53. const unzipPath = path.resolve(tempDir, "ext");
  54. return expect(
  55. unzip("./test/fixtures/extension-malformed-v.crx", unzipPath),
  56. "to be rejected with",
  57. new Error("Unexpected crx format version number.")
  58. );
  59. });
  60. describe("- ext dir is not writable", () => {
  61. it("should throw if directory is not writable", () => {
  62. const unzipPath = path.resolve(tempDir);
  63. fs.chmodSync(unzipPath, "644");
  64. return expect(
  65. unzip("./test/fixtures/extension.crx", unzipPath),
  66. "to be rejected with",
  67. /EACCES: permission denied/
  68. );
  69. });
  70. });
  71. afterEach(() => {
  72. temp.cleanupSync();
  73. });
  74. });