macos-network-identity.test.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { readFileSync } from 'node:fs'
  4. import { load } from 'js-yaml'
  5. import { namespaceMachOUUID } from '../scripts/prepare-desktop-macos.mjs'
  6. function binary(cpu = 0x100000c) {
  7. const bytes = Buffer.alloc(96, 0x42)
  8. bytes.writeUInt32LE(0xfeedfacf, 0); bytes.writeUInt32LE(cpu, 4)
  9. bytes.writeUInt32LE(1, 16); bytes.writeUInt32LE(24, 20)
  10. bytes.writeUInt32LE(0x1b, 32); bytes.writeUInt32LE(24, 36)
  11. return bytes
  12. }
  13. test('Electron build UUIDs are separated by app/helper identity without changing other bytes', () => {
  14. const original = binary(), before = Buffer.from(original)
  15. const client = namespaceMachOUUID(original, 'cn.samien.yaoyao.desktop')
  16. const full = namespaceMachOUUID(original, 'cn.samien.yaoyao.desktop.full')
  17. const helper = namespaceMachOUUID(original, 'cn.samien.yaoyao.desktop.helper')
  18. assert.deepEqual(original, before)
  19. assert.deepEqual(client, namespaceMachOUUID(original, 'cn.samien.yaoyao.desktop'))
  20. assert.equal(new Set([original, client, full, helper].map(b => b.subarray(40, 56).toString('hex'))).size, 4)
  21. assert.deepEqual(client.subarray(0, 40), original.subarray(0, 40))
  22. assert.deepEqual(client.subarray(56), original.subarray(56))
  23. const nextElectron = binary(); nextElectron[40]++
  24. assert.notDeepEqual(client, namespaceMachOUUID(nextElectron, 'cn.samien.yaoyao.desktop'))
  25. })
  26. test('universal builds rewrite both architecture slices and preserve their boundaries', () => {
  27. const bytes = Buffer.alloc(512)
  28. bytes.writeUInt32BE(0xcafebabe, 0); bytes.writeUInt32BE(2, 4)
  29. bytes.writeUInt32BE(128, 16); bytes.writeUInt32BE(96, 20)
  30. bytes.writeUInt32BE(256, 36); bytes.writeUInt32BE(96, 40)
  31. binary().copy(bytes, 128); const intel = binary(0x1000007); intel[40]++; intel.copy(bytes, 256)
  32. const result = namespaceMachOUUID(bytes, 'test.app')
  33. assert.deepEqual(result.subarray(128, 224), namespaceMachOUUID(bytes.subarray(128, 224), 'test.app'))
  34. assert.deepEqual(result.subarray(256, 352), namespaceMachOUUID(intel, 'test.app'))
  35. assert.deepEqual(result.subarray(0, 128), bytes.subarray(0, 128))
  36. bytes.writeUInt32BE(128, 36)
  37. assert.throws(() => namespaceMachOUUID(bytes, 'test.app'), /重叠/)
  38. })
  39. test('malformed executables fail before anything is written', () => {
  40. assert.throws(() => namespaceMachOUUID(Buffer.alloc(4), 'test.app'), /Mach-O/)
  41. for (const [offset, value] of [[20, 9999], [36, 0], [36, 16], [32, 0x19], [16, 2]]) {
  42. const bytes = binary(); bytes.writeUInt32LE(value, offset)
  43. assert.throws(() => namespaceMachOUUID(bytes, 'test.app'), /Mach-O/)
  44. }
  45. assert.throws(() => namespaceMachOUUID(binary(), ''), /应用标识/)
  46. })
  47. test('both macOS package variants declare LAN access and run identity preparation before signing', () => {
  48. const root = new URL('../', import.meta.url)
  49. for (const file of ['electron-builder.client.yml', 'electron-builder.yml']) {
  50. const config = load(readFileSync(new URL(file, root), 'utf8'))
  51. assert.match(config.mac.extendInfo.NSLocalNetworkUsageDescription, /局域网服务器/)
  52. const hook = readFileSync(new URL(config.afterPack, root), 'utf8')
  53. assert.match(hook, /prepareMacDesktop/)
  54. }
  55. })