api-safe-storage-spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import * as cp from 'child_process';
  2. import * as path from 'path';
  3. import { safeStorage } from 'electron/main';
  4. import { expect } from 'chai';
  5. import { emittedOnce } from './events-helpers';
  6. import { ifdescribe } from './spec-helpers';
  7. import * as fs from 'fs-extra';
  8. /* isEncryptionAvailable returns false in Linux when running CI due to a mocked dbus. This stops
  9. * Chrome from reaching the system's keyring or libsecret. When running the tests with config.store
  10. * set to basic-text, a nullptr is returned from chromium, defaulting the available encryption to false.
  11. *
  12. * Because all encryption methods are gated by isEncryptionAvailable, the methods will never return the correct values
  13. * when run on CI and linux.
  14. * Refs: https://github.com/electron/electron/issues/30424.
  15. */
  16. describe('safeStorage module', () => {
  17. it('safeStorage before and after app is ready', async () => {
  18. const appPath = path.join(__dirname, 'fixtures', 'crash-cases', 'safe-storage');
  19. const appProcess = cp.spawn(process.execPath, [appPath]);
  20. let output = '';
  21. appProcess.stdout.on('data', data => { output += data; });
  22. appProcess.stderr.on('data', data => { output += data; });
  23. const code = (await emittedOnce(appProcess, 'exit'))[0] ?? 1;
  24. if (code !== 0 && output) {
  25. console.log(output);
  26. }
  27. expect(code).to.equal(0);
  28. });
  29. });
  30. ifdescribe(process.platform !== 'linux')('safeStorage module', () => {
  31. after(async () => {
  32. const pathToEncryptedString = path.resolve(__dirname, 'fixtures', 'api', 'safe-storage', 'encrypted.txt');
  33. if (await fs.pathExists(pathToEncryptedString)) {
  34. await fs.remove(pathToEncryptedString);
  35. }
  36. });
  37. describe('SafeStorage.isEncryptionAvailable()', () => {
  38. it('should return true when encryption key is available (macOS, Windows)', () => {
  39. expect(safeStorage.isEncryptionAvailable()).to.equal(true);
  40. });
  41. });
  42. describe('SafeStorage.encryptString()', () => {
  43. it('valid input should correctly encrypt string', () => {
  44. const plaintext = 'plaintext';
  45. const encrypted = safeStorage.encryptString(plaintext);
  46. expect(Buffer.isBuffer(encrypted)).to.equal(true);
  47. });
  48. it('UTF-16 characters can be encrypted', () => {
  49. const plaintext = '€ - utf symbol';
  50. const encrypted = safeStorage.encryptString(plaintext);
  51. expect(Buffer.isBuffer(encrypted)).to.equal(true);
  52. });
  53. });
  54. describe('SafeStorage.decryptString()', () => {
  55. it('valid input should correctly decrypt string', () => {
  56. const encrypted = safeStorage.encryptString('plaintext');
  57. expect(safeStorage.decryptString(encrypted)).to.equal('plaintext');
  58. });
  59. it('UTF-16 characters can be decrypted', () => {
  60. const plaintext = '€ - utf symbol';
  61. const encrypted = safeStorage.encryptString(plaintext);
  62. expect(safeStorage.decryptString(encrypted)).to.equal(plaintext);
  63. });
  64. it('unencrypted input should throw', () => {
  65. const plaintextBuffer = Buffer.from('I am unencoded!', 'utf-8');
  66. expect(() => {
  67. safeStorage.decryptString(plaintextBuffer);
  68. }).to.throw(Error);
  69. });
  70. it('non-buffer input should throw', () => {
  71. const notABuffer = {} as any;
  72. expect(() => {
  73. safeStorage.decryptString(notABuffer);
  74. }).to.throw(Error);
  75. });
  76. });
  77. describe('safeStorage persists encryption key across app relaunch', () => {
  78. it('can decrypt after closing and reopening app', async () => {
  79. const fixturesPath = path.resolve(__dirname, 'fixtures');
  80. const encryptAppPath = path.join(fixturesPath, 'api', 'safe-storage', 'encrypt-app');
  81. const encryptAppProcess = cp.spawn(process.execPath, [encryptAppPath]);
  82. let stdout: string = '';
  83. encryptAppProcess.stderr.on('data', data => { stdout += data; });
  84. encryptAppProcess.stderr.on('data', data => { stdout += data; });
  85. try {
  86. await emittedOnce(encryptAppProcess, 'exit');
  87. const appPath = path.join(fixturesPath, 'api', 'safe-storage', 'decrypt-app');
  88. const relaunchedAppProcess = cp.spawn(process.execPath, [appPath]);
  89. let output = '';
  90. relaunchedAppProcess.stdout.on('data', data => { output += data; });
  91. relaunchedAppProcess.stderr.on('data', data => { output += data; });
  92. const [code] = await emittedOnce(relaunchedAppProcess, 'exit');
  93. if (!output.includes('plaintext')) {
  94. console.log(code, output);
  95. }
  96. expect(output).to.include('plaintext');
  97. } catch (e) {
  98. console.log(stdout);
  99. throw e;
  100. }
  101. });
  102. });
  103. });