114 lines
3.1 KiB
JavaScript
114 lines
3.1 KiB
JavaScript
/**
|
|
* Unit tests for BARE_OS_POSIX_SOCKET_SCM_RIGHTS parsing helpers (no live TCP/UDP bridge).
|
|
*/
|
|
import test from 'brittle'
|
|
import {
|
|
bareOsEnsureSocketSlotAliasKeys,
|
|
bareOsParseSendmsgScmRights,
|
|
bareOsScmRightsMaxFds,
|
|
wantPosixSocketScmRights
|
|
} from './lib/posix/bare-os-socket-scm-rights.js'
|
|
|
|
test('wantPosixSocketScmRights', (t) => {
|
|
t.absent(wantPosixSocketScmRights({}))
|
|
t.ok(wantPosixSocketScmRights({ BARE_OS_POSIX_SOCKET_SCM_RIGHTS: '1' }))
|
|
t.ok(wantPosixSocketScmRights({ BARE_OS_POSIX_SOCKET_SCM_RIGHTS: 'true' }))
|
|
t.absent(wantPosixSocketScmRights({ BARE_OS_POSIX_SOCKET_SCM_RIGHTS: '0' }))
|
|
})
|
|
|
|
test('bareOsScmRightsMaxFds default and cap', (t) => {
|
|
t.is(bareOsScmRightsMaxFds({}), 4)
|
|
t.is(bareOsScmRightsMaxFds({ BARE_OS_POSIX_SOCKET_SCM_RIGHTS_MAX_FDS: '2' }), 2)
|
|
t.is(bareOsScmRightsMaxFds({ BARE_OS_POSIX_SOCKET_SCM_RIGHTS_MAX_FDS: '99' }), 16)
|
|
})
|
|
|
|
test('bareOsParseSendmsgScmRights: empty / no ancillary', (t) => {
|
|
const r = bareOsParseSendmsgScmRights({}, true, 4)
|
|
t.ok(r.ok)
|
|
t.alike(r.fds, [])
|
|
const r2 = bareOsParseSendmsgScmRights({ buf: new Uint8Array(1) }, true, 4)
|
|
t.ok(r2.ok)
|
|
})
|
|
|
|
test('bareOsParseSendmsgScmRights: rejects binary control when scm on', (t) => {
|
|
const r = bareOsParseSendmsgScmRights(
|
|
{ control: new Uint8Array([1]) },
|
|
true,
|
|
4
|
|
)
|
|
t.absent(r.ok)
|
|
t.is(r.reject, 'control')
|
|
})
|
|
|
|
test('bareOsParseSendmsgScmRights: rejects controllen when scm on', (t) => {
|
|
const r = bareOsParseSendmsgScmRights({ controllen: 3 }, true, 4)
|
|
t.absent(r.ok)
|
|
t.is(r.reject, 'controllen')
|
|
})
|
|
|
|
test('bareOsParseSendmsgScmRights: valid cmsgs fds', (t) => {
|
|
const r = bareOsParseSendmsgScmRights(
|
|
{ cmsgs: [{ fds: [3, 4] }] },
|
|
true,
|
|
4
|
|
)
|
|
t.ok(r.ok)
|
|
t.alike(r.fds, [3, 4])
|
|
})
|
|
|
|
test('bareOsParseSendmsgScmRights: msgHdr.cmsgs merged', (t) => {
|
|
const r = bareOsParseSendmsgScmRights(
|
|
{ msgHdr: { cmsgs: [{ fds: [5] }] } },
|
|
true,
|
|
4
|
|
)
|
|
t.ok(r.ok)
|
|
t.alike(r.fds, [5])
|
|
})
|
|
|
|
test('bareOsParseSendmsgScmRights: cap exceeded', (t) => {
|
|
const r = bareOsParseSendmsgScmRights(
|
|
{ cmsgs: [{ fds: [1, 2, 3, 4, 5] }] },
|
|
true,
|
|
4
|
|
)
|
|
t.absent(r.ok)
|
|
t.is(r.reject, 'cmsgs_cap')
|
|
})
|
|
|
|
test('bareOsParseSendmsgScmRights: cmsgs when scm off', (t) => {
|
|
const r = bareOsParseSendmsgScmRights(
|
|
{ cmsgs: [{ fds: [1] }] },
|
|
false,
|
|
4
|
|
)
|
|
t.absent(r.ok)
|
|
t.is(r.reject, 'cmsgs')
|
|
})
|
|
|
|
test('bareOsParseSendmsgScmRights: malformed cmsgs entry', (t) => {
|
|
const r = bareOsParseSendmsgScmRights({ cmsgs: [{ fds: 'nope' }] }, true, 4)
|
|
t.absent(r.ok)
|
|
t.is(r.reject, 'cmsgs')
|
|
})
|
|
|
|
test('bareOsEnsureSocketSlotAliasKeys', (t) => {
|
|
const slot = {}
|
|
bareOsEnsureSocketSlotAliasKeys(slot, '7')
|
|
t.ok(slot.aliasKeys instanceof Set)
|
|
t.ok(slot.aliasKeys.has('7'))
|
|
bareOsEnsureSocketSlotAliasKeys(slot, '8')
|
|
t.ok(slot.aliasKeys.has('8'))
|
|
})
|
|
|
|
test('SCM_RIGHTS: send-side cmsgs parse ok when gate on (logical dup applied in booter sendmsg)', (t) => {
|
|
t.ok(wantPosixSocketScmRights({ BARE_OS_POSIX_SOCKET_SCM_RIGHTS: '1' }))
|
|
const prep = bareOsParseSendmsgScmRights(
|
|
{ cmsgs: [{ fds: [11] }] },
|
|
true,
|
|
bareOsScmRightsMaxFds({})
|
|
)
|
|
t.ok(prep.ok)
|
|
t.alike(prep.fds, [11])
|
|
})
|