extension_test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. var assert = require('assert'),
  19. crypto = require('crypto'),
  20. fs = require('fs'),
  21. path = require('path');
  22. var extension = require('../../firefox/extension'),
  23. io = require('../../io'),
  24. zip = require('../../io/zip'),
  25. it = require('../../testing').it;
  26. var JETPACK_EXTENSION = path.join(__dirname,
  27. '../../lib/test/data/firefox/jetpack-sample.xpi');
  28. var NORMAL_EXTENSION = path.join(__dirname,
  29. '../../lib/test/data/firefox/sample.xpi');
  30. var WEBEXTENSION_EXTENSION = path.join(__dirname,
  31. '../../lib/test/data/firefox/webextension.xpi');
  32. var JETPACK_EXTENSION_ID = 'jid1-EaXX7k0wwiZR7w@jetpack';
  33. var NORMAL_EXTENSION_ID = 'sample@seleniumhq.org';
  34. var WEBEXTENSION_EXTENSION_ID = 'webextensions-selenium-example@example.com';
  35. describe('extension', function() {
  36. it('can install a jetpack xpi file', function() {
  37. return io.tmpDir().then(function(dir) {
  38. return extension.install(JETPACK_EXTENSION, dir).then(function(id) {
  39. assert.equal(JETPACK_EXTENSION_ID, id);
  40. var file = path.join(dir, id + '.xpi');
  41. assert.ok(fs.existsSync(file), 'no such file: ' + file);
  42. assert.ok(!fs.statSync(file).isDirectory());
  43. var copiedSha1 = crypto.createHash('sha1')
  44. .update(fs.readFileSync(file))
  45. .digest('hex');
  46. var goldenSha1 = crypto.createHash('sha1')
  47. .update(fs.readFileSync(JETPACK_EXTENSION))
  48. .digest('hex');
  49. assert.equal(copiedSha1, goldenSha1);
  50. });
  51. });
  52. });
  53. it('can install a normal xpi file', function() {
  54. return io.tmpDir().then(function(dir) {
  55. return extension.install(NORMAL_EXTENSION, dir).then(function(id) {
  56. assert.equal(NORMAL_EXTENSION_ID, id);
  57. var file = path.join(dir, NORMAL_EXTENSION_ID);
  58. assert.ok(fs.statSync(file).isDirectory());
  59. assert.ok(fs.existsSync(path.join(file, 'chrome.manifest')));
  60. assert.ok(fs.existsSync(path.join(file, 'content/overlay.xul')));
  61. assert.ok(fs.existsSync(path.join(file, 'content/overlay.js')));
  62. assert.ok(fs.existsSync(path.join(file, 'install.rdf')));
  63. });
  64. });
  65. });
  66. it('can install a webextension xpi file', function() {
  67. return io.tmpDir().then(function(dir) {
  68. return extension.install(WEBEXTENSION_EXTENSION, dir).then(function(id) {
  69. assert.equal(WEBEXTENSION_EXTENSION_ID, id);
  70. var file = path.join(dir, id + '.xpi');
  71. assert.ok(fs.existsSync(file), 'no such file: ' + file);
  72. assert.ok(!fs.statSync(file).isDirectory());
  73. var copiedSha1 = crypto.createHash('sha1')
  74. .update(fs.readFileSync(file))
  75. .digest('hex');
  76. var goldenSha1 = crypto.createHash('sha1')
  77. .update(fs.readFileSync(WEBEXTENSION_EXTENSION))
  78. .digest('hex');
  79. assert.equal(copiedSha1, goldenSha1);
  80. });
  81. });
  82. });
  83. it('can install an extension from a directory', function() {
  84. return io.tmpDir().then(function(srcDir) {
  85. return zip.unzip(NORMAL_EXTENSION, srcDir)
  86. .then(() => io.tmpDir())
  87. .then(dstDir => {
  88. return extension.install(srcDir, dstDir).then(function(id) {
  89. assert.equal(NORMAL_EXTENSION_ID, id);
  90. var dir = path.join(dstDir, NORMAL_EXTENSION_ID);
  91. assert.ok(fs.existsSync(path.join(dir, 'chrome.manifest')));
  92. assert.ok(fs.existsSync(path.join(dir, 'content/overlay.xul')));
  93. assert.ok(fs.existsSync(path.join(dir, 'content/overlay.js')));
  94. assert.ok(fs.existsSync(path.join(dir, 'install.rdf')));
  95. });
  96. });
  97. });
  98. });
  99. });