codesign-helpers.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { expect } from 'chai';
  2. import * as cp from 'node:child_process';
  3. import * as fs from 'node:fs';
  4. import * as path from 'node:path';
  5. const features = process._linkedBinding('electron_common_features');
  6. const fixturesPath = path.resolve(__dirname, '..', 'fixtures');
  7. export const shouldRunCodesignTests =
  8. process.platform === 'darwin' &&
  9. !(process.env.CI && process.arch === 'arm64') &&
  10. !process.mas &&
  11. !features.isComponentBuild();
  12. let identity: string | null;
  13. export function getCodesignIdentity () {
  14. if (identity === undefined) {
  15. const result = cp.spawnSync(path.resolve(__dirname, '../../script/codesign/get-trusted-identity.sh'));
  16. if (result.status !== 0 || result.stdout.toString().trim().length === 0) {
  17. identity = null;
  18. } else {
  19. identity = result.stdout.toString().trim();
  20. }
  21. }
  22. return identity;
  23. }
  24. export async function copyMacOSFixtureApp (newDir: string, fixture: string | null = 'initial') {
  25. const appBundlePath = path.resolve(process.execPath, '../../..');
  26. const newPath = path.resolve(newDir, 'Electron.app');
  27. cp.spawnSync('cp', ['-R', appBundlePath, path.dirname(newPath)]);
  28. if (fixture) {
  29. const appDir = path.resolve(newPath, 'Contents/Resources/app');
  30. await fs.promises.mkdir(appDir, { recursive: true });
  31. await fs.promises.cp(path.resolve(fixturesPath, 'auto-update', fixture), appDir, { recursive: true });
  32. }
  33. const plistPath = path.resolve(newPath, 'Contents', 'Info.plist');
  34. await fs.promises.writeFile(
  35. plistPath,
  36. (await fs.promises.readFile(plistPath, 'utf8')).replace('<key>BuildMachineOSBuild</key>', `<key>NSAppTransportSecurity</key>
  37. <dict>
  38. <key>NSAllowsArbitraryLoads</key>
  39. <true/>
  40. <key>NSExceptionDomains</key>
  41. <dict>
  42. <key>localhost</key>
  43. <dict>
  44. <key>NSExceptionAllowsInsecureHTTPLoads</key>
  45. <true/>
  46. <key>NSIncludesSubdomains</key>
  47. <true/>
  48. </dict>
  49. </dict>
  50. </dict><key>BuildMachineOSBuild</key>`)
  51. );
  52. return newPath;
  53. };
  54. export function spawn (cmd: string, args: string[], opts: any = {}) {
  55. let out = '';
  56. const child = cp.spawn(cmd, args, opts);
  57. child.stdout.on('data', (chunk: Buffer) => {
  58. out += chunk.toString();
  59. });
  60. child.stderr.on('data', (chunk: Buffer) => {
  61. out += chunk.toString();
  62. });
  63. return new Promise<{ code: number, out: string }>((resolve) => {
  64. child.on('exit', (code, signal) => {
  65. expect(signal).to.equal(null);
  66. resolve({
  67. code: code!,
  68. out
  69. });
  70. });
  71. });
  72. };
  73. export function signApp (appPath: string, identity: string) {
  74. return spawn('codesign', ['-s', identity, '--deep', '--force', appPath]);
  75. };