Files
orca/src/shared/linux-proc-port-scan-limits.test.ts
T
NeilandOrca 879aad7dd6 oom(foundation): bound shared readers/limits + add BoundedMap primitive (#10299)
* oom(01): A1-shared-readers — reintroduce #10179 subset

Files: 18 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(02): A2-shared-image-media — reintroduce #10179 subset

Files: 7 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(03): A3-shared-fs-listing — reintroduce #10179 subset

Files: 21 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(04): A4-shared-remote-relay — reintroduce #10179 subset

Files: 8 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(05): A5-shared-misc — reintroduce #10179 subset

Files: 28 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(06): B-shared-wiring — reintroduce #10179 subset

Files: 81 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-07-24 21:36:57 -07:00

53 lines
1.8 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import {
createLinuxProcTextReadBudget,
LINUX_PROC_NETWORK_TABLE_MAX_BYTES,
readLinuxProcNetworkTable,
readLinuxProcTextWithinBudget
} from './linux-proc-port-scan-limits'
describe('Linux proc port scan limits', () => {
it('applies the network-table byte cap before retaining content', async () => {
const readFile = vi.fn(async () => Buffer.from('table'))
await expect(readLinuxProcNetworkTable('/proc/net/tcp', readFile)).resolves.toBe('table')
expect(readFile).toHaveBeenCalledWith('/proc/net/tcp', LINUX_PROC_NETWORK_TABLE_MAX_BYTES)
})
it('shares one retained-byte budget across process metadata files', async () => {
const budget = createLinuxProcTextReadBudget(5)
const readFile = vi
.fn<(filePath: string, maxBytes: number) => Promise<Buffer>>()
.mockResolvedValueOnce(Buffer.from('abc'))
.mockResolvedValueOnce(Buffer.from('de'))
await expect(readLinuxProcTextWithinBudget('/proc/1/comm', budget, readFile, 4)).resolves.toBe(
'abc'
)
await expect(
readLinuxProcTextWithinBudget('/proc/1/cmdline', budget, readFile, 4)
).resolves.toBe('de')
await expect(
readLinuxProcTextWithinBudget('/proc/2/cmdline', budget, readFile, 4)
).resolves.toBeUndefined()
expect(readFile.mock.calls).toEqual([
['/proc/1/comm', 4],
['/proc/1/cmdline', 2]
])
expect(budget.remainingBytes).toBe(0)
})
it('does not debit failed metadata reads', async () => {
const budget = createLinuxProcTextReadBudget(4)
const readFile = vi.fn(async () => {
throw new Error('oversized')
})
await expect(
readLinuxProcTextWithinBudget('/proc/1/cmdline', budget, readFile)
).resolves.toBeUndefined()
expect(budget.remainingBytes).toBe(4)
})
})