42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
'use strict'
|
|
|
|
const test = require('brittle')
|
|
const {
|
|
forceLoopbackHost,
|
|
isLoopbackAddress,
|
|
assertListeningLoopback,
|
|
withForcedLoopback
|
|
} = require('../lib/bind-guard')
|
|
|
|
test('forceLoopbackHost accepts loopback', (t) => {
|
|
t.is(forceLoopbackHost('127.0.0.1'), '127.0.0.1')
|
|
t.is(forceLoopbackHost('localhost'), '127.0.0.1')
|
|
t.is(forceLoopbackHost(null), '127.0.0.1')
|
|
t.is(forceLoopbackHost(''), '127.0.0.1')
|
|
})
|
|
|
|
test('forceLoopbackHost rejects public and any', (t) => {
|
|
t.exception(() => forceLoopbackHost('0.0.0.0'))
|
|
t.exception(() => forceLoopbackHost('1.2.3.4'))
|
|
t.exception(() => forceLoopbackHost('::'))
|
|
})
|
|
|
|
test('isLoopbackAddress', (t) => {
|
|
t.ok(isLoopbackAddress('127.0.0.1'))
|
|
t.ok(isLoopbackAddress('127.0.0.2'))
|
|
t.ok(isLoopbackAddress('::1'))
|
|
t.absent(isLoopbackAddress('0.0.0.0'))
|
|
t.absent(isLoopbackAddress('192.168.1.1'))
|
|
})
|
|
|
|
test('assertListeningLoopback', (t) => {
|
|
t.ok(assertListeningLoopback({ address: '127.0.0.1', port: 25565 }))
|
|
t.exception(() => assertListeningLoopback({ address: '0.0.0.0', port: 25565 }))
|
|
})
|
|
|
|
test('withForcedLoopback overwrites host', (t) => {
|
|
const o = withForcedLoopback({ host: 'localhost', port: 1 })
|
|
t.is(o.host, '127.0.0.1')
|
|
t.is(o.port, 1)
|
|
})
|