internal-spec.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { expect } from 'chai';
  2. import { BrowserWindow } from 'electron/main';
  3. import { closeAllWindows } from './window-helpers';
  4. describe('feature-string parsing', () => {
  5. it('is indifferent to whitespace around keys and values', () => {
  6. const { parseCommaSeparatedKeyValue } = require('../lib/browser/parse-features-string');
  7. const checkParse = (string: string, parsed: Record<string, string | boolean>) => {
  8. const features = parseCommaSeparatedKeyValue(string);
  9. expect(features).to.deep.equal(parsed);
  10. };
  11. checkParse('a=yes,c=d', { a: true, c: 'd' });
  12. checkParse('a=yes ,c=d', { a: true, c: 'd' });
  13. checkParse('a=yes, c=d', { a: true, c: 'd' });
  14. checkParse('a=yes , c=d', { a: true, c: 'd' });
  15. checkParse(' a=yes , c=d', { a: true, c: 'd' });
  16. checkParse(' a= yes , c=d', { a: true, c: 'd' });
  17. checkParse(' a = yes , c=d', { a: true, c: 'd' });
  18. checkParse(' a = yes , c =d', { a: true, c: 'd' });
  19. checkParse(' a = yes , c = d', { a: true, c: 'd' });
  20. checkParse(' a = yes , c = d ', { a: true, c: 'd' });
  21. });
  22. });
  23. describe('process._linkedBinding', () => {
  24. describe('in the main process', () => {
  25. it('can access electron_browser bindings', () => {
  26. process._linkedBinding('electron_browser_app');
  27. });
  28. it('can access electron_common bindings', () => {
  29. process._linkedBinding('electron_common_v8_util');
  30. });
  31. it('cannot access electron_renderer bindings', () => {
  32. expect(() => {
  33. process._linkedBinding('electron_renderer_ipc');
  34. }).to.throw(/No such module was linked: electron_renderer_ipc/);
  35. });
  36. });
  37. describe('in the renderer process', () => {
  38. afterEach(closeAllWindows);
  39. it('cannot access electron_browser bindings', async () => {
  40. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  41. w.loadURL('about:blank');
  42. await expect(w.webContents.executeJavaScript('void process._linkedBinding(\'electron_browser_app\')'))
  43. .to.eventually.be.rejectedWith(/Script failed to execute/);
  44. });
  45. it('can access electron_common bindings', async () => {
  46. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  47. w.loadURL('about:blank');
  48. await w.webContents.executeJavaScript('void process._linkedBinding(\'electron_common_v8_util\')');
  49. });
  50. it('can access electron_renderer bindings', async () => {
  51. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  52. w.loadURL('about:blank');
  53. await w.webContents.executeJavaScript('void process._linkedBinding(\'electron_renderer_ipc\')');
  54. });
  55. });
  56. });