Files
harbor-net/test/shared/routing-rules.test.js
T
dokril 97791443c0
Build and Deploy Gateway / build-and-push (push) Successful in 40s
Build and Deploy Gateway / deploy (push) Successful in 14s
Add IP routing rules and fix gRPC buffer retention
2026-10-30 22:57:46 +03:00

87 lines
3.6 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { canAppendRouteRule, normalizeRouteRules } from '../../dist/shared/routingRules.js';
test('local route rules normalize URLs, suffixes and duplicates', () => {
assert.deepEqual(normalizeRouteRules([
{ type: 'domain', value: 'https://Example.com/news?id=1', outbound: 'vpn' },
{ type: 'domain_suffix', value: '*.Example.org', outbound: 'direct' },
{ type: 'domain_keyword', value: ' CDN ', outbound: 'vpn' },
{ type: 'domain', value: 'example.com', outbound: 'direct' },
{ type: 'domain_suffix', value: '.ru', enabled: false, outbound: 'direct' },
], { strict: true }), [
{ type: 'domain', value: 'example.com', enabled: true, outbound: 'vpn' },
{ type: 'domain_suffix', value: 'example.org', enabled: true, outbound: 'direct' },
{ type: 'domain_keyword', value: 'cdn', enabled: true, outbound: 'vpn' },
{ type: 'domain_suffix', value: 'ru', enabled: false, outbound: 'direct' },
]);
assert.equal(canAppendRouteRule([{ value: 'filled' }]), true);
assert.equal(canAppendRouteRule([{ value: '' }]), false);
});
test('invalid local route rules fail at the strict boundary', () => {
assert.throws(
() => normalizeRouteRules([{ type: 'domain_regex', value: '.*', outbound: 'direct' }], { strict: true }),
/Invalid domain rule type/,
);
assert.throws(
() => normalizeRouteRules([{ type: 'domain_keyword', value: 'bad/path', outbound: 'direct' }], { strict: true }),
/Invalid domain rule value/,
);
assert.throws(
() => normalizeRouteRules([{ type: 'domain', value: 'example.com' }], { strict: true }),
/outbound is required/,
);
assert.throws(
() => normalizeRouteRules([{ type: 'domain', value: 'example.com', outbound: 'block' }], { strict: true }),
/Invalid route rule outbound/,
);
});
test('legacy rules default to direct while order and first duplicate stay canonical', () => {
assert.deepEqual(normalizeRouteRules([
{ type: 'domain_suffix', value: 'example.com' },
{ type: 'domain', value: 'api.example.com', outbound: 'vpn' },
{ type: 'domain_suffix', value: 'example.com', outbound: 'vpn' },
]), [
{ type: 'domain_suffix', value: 'example.com', enabled: true, outbound: 'direct' },
{ type: 'domain', value: 'api.example.com', enabled: true, outbound: 'vpn' },
]);
});
test('IP rules normalize exact IPv4 and IPv6 destinations and deduplicate equivalent IPv6', () => {
const rule = (value, outbound = 'direct') => ({ type: 'ip_cidr', value, enabled: true, outbound });
assert.deepEqual(normalizeRouteRules([
rule(' 203.0.113.25 '),
rule('2001:0DB8:0:0:0:0:0:1', 'vpn'),
rule('2001:db8::1'),
rule('::ffff:192.0.2.128'),
rule('0.0.0.0'),
rule('255.255.255.255'),
rule('::'),
], { strict: true }), [
rule('203.0.113.25'),
rule('2001:db8::1', 'vpn'),
rule('::ffff:c000:280'),
rule('0.0.0.0'),
rule('255.255.255.255'),
rule('::'),
]);
});
test('IP rules reject domains, URLs, ports, networks and ambiguous or invalid addresses', () => {
for (const value of [
'', 'example.com', 'https://203.0.113.25', '203.0.113.25:443',
'203.0.113.25/32', '2001:db8::/64', '203.0.113', '127.1', '2130706433',
'0x7f.0.0.1', '203.0.113.025', '256.0.0.1', '203.0.113.-1', '1.2.3.4.5',
'1.2.3. 4', '1.2.3.4,5.6.7.8', '1.2.3.4\n5.6.7.8',
'[2001:db8::1]', '[2001:db8::1]:443', 'fe80::1%en0', '2001:db8:::1',
'1:2:3:4:5:6:7', '1:2:3:4:5:6:7:8:9', '2001:db8::gg', '::ffff:256.0.0.1',
]) {
assert.throws(() => normalizeRouteRules([
{ type: 'ip_cidr', value, outbound: 'direct' },
], { strict: true }), TypeError, value);
}
});