|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { buildLinuxBwrapArgs, buildMacOsProfile, detectPlatform } from './profile.js'; |
| 3 | + |
| 4 | +describe('detectPlatform', () => { |
| 5 | + it('returns one of the supported values', () => { |
| 6 | + const p = detectPlatform(); |
| 7 | + expect(['macos', 'linux', 'unsupported']).toContain(p); |
| 8 | + }); |
| 9 | +}); |
| 10 | + |
| 11 | +describe('buildMacOsProfile', () => { |
| 12 | + it('returns empty when disabled', () => { |
| 13 | + expect(buildMacOsProfile({ enabled: false }, '/x')).toBe(''); |
| 14 | + }); |
| 15 | + |
| 16 | + it('starts with deny-default + allows system reads', () => { |
| 17 | + const profile = buildMacOsProfile({ enabled: true }, '/proj'); |
| 18 | + expect(profile).toMatch(/\(deny default\)/); |
| 19 | + expect(profile).toMatch(/file-read\* \(subpath "\/usr"\)/); |
| 20 | + expect(profile).toMatch(/file-write\* \(subpath "\/private\/tmp"\)/); |
| 21 | + }); |
| 22 | + |
| 23 | + it('includes allowRead + allowWrite paths', () => { |
| 24 | + const profile = buildMacOsProfile( |
| 25 | + { |
| 26 | + enabled: true, |
| 27 | + filesystem: { |
| 28 | + allowRead: ['/etc/hosts', '~/.config'], |
| 29 | + allowWrite: ['~/Projects'], |
| 30 | + }, |
| 31 | + }, |
| 32 | + '/proj', |
| 33 | + ); |
| 34 | + expect(profile).toContain('/etc/hosts'); |
| 35 | + expect(profile).toMatch(/file-write\* \(subpath ".*Projects"\)/); |
| 36 | + // ~ should be expanded |
| 37 | + expect(profile).not.toContain('"~/'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('appends deny rules after allows (so deny wins)', () => { |
| 41 | + const profile = buildMacOsProfile( |
| 42 | + { |
| 43 | + enabled: true, |
| 44 | + filesystem: { |
| 45 | + allowRead: ['/etc'], |
| 46 | + denyRead: ['/etc/passwd'], |
| 47 | + }, |
| 48 | + }, |
| 49 | + '/proj', |
| 50 | + ); |
| 51 | + const allowIdx = profile.indexOf('/etc"'); |
| 52 | + const denyIdx = profile.indexOf('/etc/passwd'); |
| 53 | + expect(denyIdx).toBeGreaterThan(allowIdx); |
| 54 | + }); |
| 55 | + |
| 56 | + it('escapes special SBPL chars in paths', () => { |
| 57 | + const profile = buildMacOsProfile( |
| 58 | + { |
| 59 | + enabled: true, |
| 60 | + filesystem: { allowRead: ['/path with "quotes"'] }, |
| 61 | + }, |
| 62 | + '/proj', |
| 63 | + ); |
| 64 | + expect(profile).toContain('\\"quotes\\"'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('unix-socket opt-in', () => { |
| 68 | + const profile = buildMacOsProfile( |
| 69 | + { enabled: true, network: { allowUnixSockets: true } }, |
| 70 | + '/proj', |
| 71 | + ); |
| 72 | + expect(profile).toMatch(/network\* \(local unix-socket\)/); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('buildLinuxBwrapArgs', () => { |
| 77 | + it('returns empty when disabled', () => { |
| 78 | + expect(buildLinuxBwrapArgs({ enabled: false }, '/x')).toEqual([]); |
| 79 | + }); |
| 80 | + |
| 81 | + it('binds system dirs read-only', () => { |
| 82 | + const args = buildLinuxBwrapArgs({ enabled: true }, '/proj'); |
| 83 | + expect(args).toContain('--ro-bind-try'); |
| 84 | + expect(args).toContain('/usr'); |
| 85 | + expect(args).toContain('/lib'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('binds cwd read-write', () => { |
| 89 | + const args = buildLinuxBwrapArgs({ enabled: true }, '/my/project'); |
| 90 | + const idx = args.indexOf('--bind'); |
| 91 | + expect(idx).toBeGreaterThan(-1); |
| 92 | + expect(args[idx + 1]).toBe('/my/project'); |
| 93 | + expect(args[idx + 2]).toBe('/my/project'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('unshares pid/ipc/uts', () => { |
| 97 | + const args = buildLinuxBwrapArgs({ enabled: true }, '/x'); |
| 98 | + expect(args).toContain('--unshare-pid'); |
| 99 | + expect(args).toContain('--unshare-ipc'); |
| 100 | + expect(args).toContain('--unshare-uts'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('unshares net when allowedDomains is empty array', () => { |
| 104 | + const args = buildLinuxBwrapArgs({ enabled: true, network: { allowedDomains: [] } }, '/x'); |
| 105 | + expect(args).toContain('--unshare-net'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('does NOT unshare net when allowedDomains is omitted (default allow)', () => { |
| 109 | + const args = buildLinuxBwrapArgs({ enabled: true }, '/x'); |
| 110 | + expect(args).not.toContain('--unshare-net'); |
| 111 | + }); |
| 112 | +}); |
0 commit comments