You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.2 KiB
JavaScript
66 lines
2.2 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
|
|
const inputUrl = new URL('../public/cloud/firewall-input.js', import.meta.url);
|
|
const { firewallPresetRules, validFirewallCidr } = await import(
|
|
`${inputUrl.href}?test=${Date.now()}`
|
|
);
|
|
|
|
for (const value of [
|
|
'0.0.0.0/0', '203.0.113.8', '203.0.113.9/24',
|
|
'::', '::/0', '2001:db8::1', '2001:DB8:1:2:3:4:5:6/64',
|
|
'::ffff:192.0.2.1/128', '2001:db8:1:2:3:4:192.0.2.1/96',
|
|
'::192.0.2.1/128', '::1:0/112', '64:ff9b::192.0.2.1/96',
|
|
'ffff::/1', '2001:db8:abcd:ef01:ffff:ffff:ffff:ffff/73',
|
|
'2001:db8::3/127',
|
|
]) {
|
|
assert.equal(validFirewallCidr(value), true, `${value} should be valid`);
|
|
}
|
|
|
|
const maximumCanonicalCidr = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128';
|
|
assert.equal(maximumCanonicalCidr.length, 43);
|
|
assert.equal(validFirewallCidr(maximumCanonicalCidr), true);
|
|
assert.equal(validFirewallCidr(`${maximumCanonicalCidr}0`), false,
|
|
'Firewall CIDR input must remain bounded to 43 characters.');
|
|
|
|
for (const value of [
|
|
'', ' ', '01.2.3.4', '256.0.0.1', '192.0.2.1/33',
|
|
'192.0.2.1/024', '192.0.2.1/24/1',
|
|
':', ':::', '1::2::3', '2001:db8::g', '2001:db8::/129',
|
|
'2001:db8::/064', 'fe80::1%eth0/64', '::ffff:999.0.0.1/128',
|
|
'2001:db8:1:2:3:4:5:6:7',
|
|
]) {
|
|
assert.equal(validFirewallCidr(value), false, `${value} should be invalid`);
|
|
}
|
|
|
|
for (const [name, port, description] of [
|
|
['ssh', 22, 'SSH'], ['http', 80, 'HTTP'], ['https', 443, 'HTTPS'],
|
|
]) {
|
|
assert.deepEqual(firewallPresetRules(name, 0), [
|
|
{
|
|
protocol: 'TCP', source_cidr: '0.0.0.0/0',
|
|
port_from: port, port_to: port, description,
|
|
},
|
|
{
|
|
protocol: 'TCP', source_cidr: '::/0',
|
|
port_from: port, port_to: port, description,
|
|
},
|
|
]);
|
|
}
|
|
|
|
assert.deepEqual(firewallPresetRules('ssh', 48), [
|
|
{
|
|
protocol: 'TCP', source_cidr: '0.0.0.0/0',
|
|
port_from: 22, port_to: 22, description: 'SSH',
|
|
},
|
|
{
|
|
protocol: 'TCP', source_cidr: '::/0',
|
|
port_from: 22, port_to: 22, description: 'SSH',
|
|
},
|
|
]);
|
|
assert.deepEqual(firewallPresetRules('ssh', 49), [],
|
|
'A preset must not add only one address family.');
|
|
assert.deepEqual(firewallPresetRules('unknown', 0), []);
|
|
assert.deepEqual(firewallPresetRules('ssh', -1), []);
|
|
|
|
console.log('Cloud firewall dual-stack input behavior passed.');
|