index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. const { app, safeStorage } = require('electron');
  2. const { expect } = require('chai');
  3. (async () => {
  4. if (!app.isReady()) {
  5. // isEncryptionAvailable() returns false before the app is ready on
  6. // Linux: https://github.com/electron/electron/issues/32206
  7. // and
  8. // Windows: https://github.com/electron/electron/issues/33640.
  9. expect(safeStorage.isEncryptionAvailable()).to.equal(process.platform === 'darwin');
  10. if (safeStorage.isEncryptionAvailable()) {
  11. const plaintext = 'plaintext';
  12. const ciphertext = safeStorage.encryptString(plaintext);
  13. expect(Buffer.isBuffer(ciphertext)).to.equal(true);
  14. expect(safeStorage.decryptString(ciphertext)).to.equal(plaintext);
  15. } else {
  16. expect(() => safeStorage.encryptString('plaintext')).to.throw(/safeStorage cannot be used before app is ready/);
  17. expect(() => safeStorage.decryptString(Buffer.from(''))).to.throw(/safeStorage cannot be used before app is ready/);
  18. }
  19. }
  20. await app.whenReady();
  21. // isEncryptionAvailable() will always return false on CI due to a mocked
  22. // dbus as mentioned above.
  23. expect(safeStorage.isEncryptionAvailable()).to.equal(process.platform !== 'linux');
  24. if (safeStorage.isEncryptionAvailable()) {
  25. const plaintext = 'plaintext';
  26. const ciphertext = safeStorage.encryptString(plaintext);
  27. expect(Buffer.isBuffer(ciphertext)).to.equal(true);
  28. expect(safeStorage.decryptString(ciphertext)).to.equal(plaintext);
  29. } else {
  30. expect(() => safeStorage.encryptString('plaintext')).to.throw(/Encryption is not available/);
  31. expect(() => safeStorage.decryptString(Buffer.from(''))).to.throw(/Decryption is not available/);
  32. }
  33. })()
  34. .then(app.quit)
  35. .catch((err) => {
  36. console.error(err);
  37. app.exit(1);
  38. });