runtime-tree.spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { cpSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { afterEach, expect, it } from 'vitest'
  5. import { DESKTOP_HOST_PACKAGE, DESKTOP_HOST_RUNTIME_FILES } from '../src/core-package-set.ts'
  6. import { DESKTOP_RUNTIME_FILE, desktopRuntimeId, readDesktopRuntime, runtimePath, verifyDesktopRuntime } from '../src/runtime-tree.ts'
  7. import { runtimeFixture } from './runtime-fixture.ts'
  8. const roots: string[] = []
  9. function fixture(): string {
  10. const root = mkdtempSync(join(tmpdir(), 'desktop-runtime-'))
  11. roots.push(root)
  12. runtimeFixture(join(root, 'dsh'))
  13. return root
  14. }
  15. afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }) })
  16. it('verifies a runtime after relocation without depending on build paths', async () => {
  17. const root = fixture()
  18. const before = await verifyDesktopRuntime(join(root, 'dsh'), '1.0.0')
  19. cpSync(join(root, 'dsh'), join(root, 'moved'), { recursive: true })
  20. expect(desktopRuntimeId(await verifyDesktopRuntime(join(root, 'moved'), '1.0.0'))).toBe(desktopRuntimeId(before))
  21. })
  22. it.each(['changed', 'same-size', 'extra', 'missing'])('checks %s runtime bytes only during build verification', async (operation) => {
  23. const dsh = join(fixture(), 'dsh')
  24. const before = readDesktopRuntime(dsh)
  25. if (operation === 'changed') writeFileSync(join(dsh, 'package.json'), '{}')
  26. if (operation === 'same-size') writeFileSync(join(dsh, 'package.json'), '{"type":"Module"}\n')
  27. if (operation === 'extra') writeFileSync(join(dsh, 'extra'), '')
  28. if (operation === 'missing') rmSync(join(dsh, 'package.json'))
  29. expect(readDesktopRuntime(dsh)).toEqual(before)
  30. await expect(verifyDesktopRuntime(dsh, '1.0.0')).rejects.toThrow(/integrity/u)
  31. })
  32. it('rejects filesystem links and incompatible targets', async () => {
  33. const dsh = join(fixture(), 'dsh')
  34. await expect(verifyDesktopRuntime(dsh, '1.0.0', { platform: process.platform, arch: 'wrong' })).rejects.toThrow(/incompatible/u)
  35. symlinkSync(join(dsh, 'node_modules'), join(dsh, 'outside'), process.platform === 'win32' ? 'junction' : 'dir')
  36. expect(readDesktopRuntime(dsh).release.version).toBe('1.0.0')
  37. await expect(verifyDesktopRuntime(dsh, '1.0.0')).rejects.toThrow(/unsupported filesystem/u)
  38. })
  39. it('reads file inventory records unchanged during startup', () => {
  40. const dsh = join(fixture(), 'dsh')
  41. const path = join(dsh, DESKTOP_RUNTIME_FILE)
  42. const descriptor = JSON.parse(readFileSync(path, 'utf8')) as { files: unknown[] }
  43. descriptor.files.unshift({ path: '../outside', bytes: -1.5, sha256: 'unchecked', executable: 'unchecked' })
  44. writeFileSync(path, JSON.stringify(descriptor))
  45. expect(readDesktopRuntime(dsh).files).toEqual(descriptor.files)
  46. })
  47. it.each(['missing', 'directory'])('checks a %s Host entry only during build verification', async (operation) => {
  48. const dsh = join(fixture(), 'dsh')
  49. const path = join(dsh, 'node_modules', DESKTOP_HOST_PACKAGE, DESKTOP_HOST_RUNTIME_FILES[0])
  50. rmSync(path)
  51. if (operation === 'directory') mkdirSync(path)
  52. expect(readDesktopRuntime(dsh).release.version).toBe('1.0.0')
  53. await expect(verifyDesktopRuntime(dsh, '1.0.0')).rejects.toThrow(/integrity/u)
  54. })
  55. it('checks the shell version only during build verification', async () => {
  56. const dsh = join(fixture(), 'dsh')
  57. expect(readDesktopRuntime(dsh).release.version).toBe('1.0.0')
  58. await expect(verifyDesktopRuntime(dsh, '2.0.0')).rejects.toThrow(/does not match Electron/u)
  59. })
  60. it.each([
  61. { schemaVersion: 2 },
  62. { platform: 'other' },
  63. { arch: 'other' },
  64. { release: { schemaVersion: 2 } },
  65. { release: { hostProtocolVersion: 999 } },
  66. { release: { nodeVersion: 'invalid' } },
  67. { release: { pnpmVersion: 'invalid' } },
  68. ])('checks release compatibility only during build verification: %j', async (patch) => {
  69. const dsh = join(fixture(), 'dsh')
  70. const path = join(dsh, DESKTOP_RUNTIME_FILE)
  71. const original = readDesktopRuntime(dsh)
  72. const descriptor = { ...original, ...patch, release: { ...original.release, ...patch.release } }
  73. writeFileSync(path, JSON.stringify(descriptor))
  74. expect(readDesktopRuntime(dsh)).toEqual(descriptor)
  75. await expect(verifyDesktopRuntime(dsh, '1.0.0')).rejects.toThrow(/invalid|incompatible/u)
  76. })
  77. it.each(['missing', 'invalid-json', 'mismatched'])('checks %s shared manifests only during build verification', async (operation) => {
  78. const dsh = join(fixture(), 'dsh')
  79. const before = readDesktopRuntime(dsh)
  80. const path = join(dsh, 'node_modules', DESKTOP_HOST_PACKAGE, 'package.json')
  81. if (operation === 'missing') rmSync(path)
  82. else writeFileSync(path, operation === 'invalid-json' ? '{' : '{}')
  83. expect(readDesktopRuntime(dsh)).toEqual(before)
  84. await expect(verifyDesktopRuntime(dsh, '1.0.0')).rejects.toThrow()
  85. })
  86. it('rejects a descriptor that maps a shared package outside node_modules', async () => {
  87. const dsh = join(fixture(), 'dsh')
  88. const path = join(dsh, DESKTOP_RUNTIME_FILE)
  89. const descriptor = JSON.parse(readFileSync(path, 'utf8')) as { sharedPackages: { path: string }[] }
  90. descriptor.sharedPackages[0]!.path = '../outside'
  91. writeFileSync(path, JSON.stringify(descriptor))
  92. await expect(verifyDesktopRuntime(dsh, '1.0.0')).rejects.toThrow(/shared package record/u)
  93. })
  94. it('verifies recorded executable permissions only on Unix', async () => {
  95. const dsh = join(fixture(), 'dsh')
  96. const path = join(dsh, DESKTOP_RUNTIME_FILE)
  97. const descriptor = JSON.parse(readFileSync(path, 'utf8')) as { files: { executable: boolean }[] }
  98. descriptor.files[0]!.executable = !descriptor.files[0]!.executable
  99. writeFileSync(path, JSON.stringify(descriptor))
  100. if (process.platform === 'win32') await expect(verifyDesktopRuntime(dsh, '1.0.0')).resolves.toMatchObject(descriptor)
  101. else await expect(verifyDesktopRuntime(dsh, '1.0.0')).rejects.toThrow(/integrity/u)
  102. })
  103. it.each(['../outside', '/absolute', 'C:/absolute', 'a\\b', 'a//b', './a'])('rejects nonportable path %s', (path) => {
  104. expect(() => runtimePath('/runtime', path)).toThrow(/invalid relative path/u)
  105. })