-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathlinux-dependency-error.test.ts
More file actions
150 lines (114 loc) · 4.3 KB
/
linux-dependency-error.test.ts
File metadata and controls
150 lines (114 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { describe, test, expect, beforeEach, afterEach, spyOn } from 'bun:test'
import * as which from '../../src/utils/which.js'
import * as seccomp from '../../src/sandbox/generate-seccomp-filter.js'
import {
checkLinuxDependencies,
getLinuxDependencyStatus,
} from '../../src/sandbox/linux-sandbox-utils.js'
// Spies set up in beforeEach, torn down in afterEach. Each test overrides
// just the piece it's exercising. spyOn patches the export binding, so
// linux-sandbox-utils' own imports see the replacement.
let whichSpy: ReturnType<typeof spyOn>
let applySpy: ReturnType<typeof spyOn>
beforeEach(() => {
whichSpy = spyOn(which, 'whichSync').mockImplementation(
(bin: string) => `/usr/bin/${bin}`,
)
applySpy = spyOn(seccomp, 'getApplySeccompBinaryPath').mockReturnValue(
'/path/to/apply-seccomp',
)
})
afterEach(() => {
whichSpy.mockRestore()
applySpy.mockRestore()
})
describe('checkLinuxDependencies', () => {
test('returns no errors or warnings when all dependencies present', () => {
const result = checkLinuxDependencies()
expect(result.errors).toEqual([])
expect(result.warnings).toEqual([])
})
test('returns error when bwrap missing', () => {
whichSpy.mockImplementation((bin: string) =>
bin === 'bwrap' ? null : `/usr/bin/${bin}`,
)
const result = checkLinuxDependencies()
expect(result.errors).toContain('bubblewrap (bwrap) not installed')
expect(result.errors.length).toBe(1)
})
test('returns error when socat missing', () => {
whichSpy.mockImplementation((bin: string) =>
bin === 'socat' ? null : `/usr/bin/${bin}`,
)
const result = checkLinuxDependencies()
expect(result.errors).toContain('socat not installed')
expect(result.errors.length).toBe(1)
})
test('returns multiple errors when both bwrap and socat missing', () => {
whichSpy.mockReturnValue(null)
const result = checkLinuxDependencies()
expect(result.errors).toContain('bubblewrap (bwrap) not installed')
expect(result.errors).toContain('socat not installed')
expect(result.errors.length).toBe(2)
})
test('returns warning when apply-seccomp missing', () => {
applySpy.mockReturnValue(null)
const result = checkLinuxDependencies()
expect(result.warnings).toContain(
'seccomp not available - unix socket access not restricted',
)
})
test('passes custom applyPath through to the resolver', () => {
checkLinuxDependencies({ applyPath: '/custom/apply' })
expect(applySpy).toHaveBeenCalledWith('/custom/apply')
})
test('argv0 mode: no seccomp warning even when binary lookup would fail', () => {
applySpy.mockReturnValue(null)
const result = checkLinuxDependencies({
argv0: 'apply-seccomp',
applyPath: '/proc/self/fd/3',
})
expect(result.warnings).toEqual([])
expect(applySpy).not.toHaveBeenCalled()
})
})
describe('getLinuxDependencyStatus', () => {
test('reports all available when everything installed', () => {
const status = getLinuxDependencyStatus()
expect(status.hasBwrap).toBe(true)
expect(status.hasSocat).toBe(true)
expect(status.hasSeccompApply).toBe(true)
})
test('reports bwrap unavailable when not installed', () => {
whichSpy.mockImplementation((bin: string) =>
bin === 'bwrap' ? null : `/usr/bin/${bin}`,
)
const status = getLinuxDependencyStatus()
expect(status.hasBwrap).toBe(false)
expect(status.hasSocat).toBe(true)
})
test('reports socat unavailable when not installed', () => {
whichSpy.mockImplementation((bin: string) =>
bin === 'socat' ? null : `/usr/bin/${bin}`,
)
const status = getLinuxDependencyStatus()
expect(status.hasSocat).toBe(false)
expect(status.hasBwrap).toBe(true)
})
test('reports seccomp unavailable when apply binary missing', () => {
applySpy.mockReturnValue(null)
const status = getLinuxDependencyStatus()
expect(status.hasSeccompApply).toBe(false)
expect(status.hasBwrap).toBe(true)
expect(status.hasSocat).toBe(true)
})
test('argv0 mode: hasSeccompApply is true without touching disk', () => {
applySpy.mockReturnValue(null)
const status = getLinuxDependencyStatus({
argv0: 'apply-seccomp',
applyPath: '/does/not/exist',
})
expect(status.hasSeccompApply).toBe(true)
expect(applySpy).not.toHaveBeenCalled()
})
})