From 0784a88434002559b3295d1e9c9e2f9f9bc70695 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Tue, 30 Apr 2024 08:28:51 -0700 Subject: [PATCH] refactor bigger tests into files --- test/get_ip_any.js | 628 +++++++++++++++++++++++ test/get_mx.js | 181 +++++++ test/get_public_ip.js | 81 +++ test/harakaMx.js | 198 ++++++++ test/net_utils.js | 1095 +---------------------------------------- 5 files changed, 1091 insertions(+), 1092 deletions(-) create mode 100644 test/get_ip_any.js create mode 100644 test/get_mx.js create mode 100644 test/get_public_ip.js create mode 100644 test/harakaMx.js diff --git a/test/get_ip_any.js b/test/get_ip_any.js new file mode 100644 index 0000000..c1d61cb --- /dev/null +++ b/test/get_ip_any.js @@ -0,0 +1,628 @@ +const assert = require('node:assert') +const net = require('node:net') + + +const ip_fixtures = [ + [false, ' 2001:0000:1234:0000:0000:C1C0:ABCD:0876 '], + [false, ' 2001:0000:1234:0000:0000:C1C0:ABCD:0876 0'], + [false, ' 2001:0000:1234:0000:0000:C1C0:ABCD:0876'], + [false, ' 2001:0:1234::C1C0:ABCD:876 '], + [false, ' 2001:0:1234::C1C0:ABCD:876'], + [false, ''], + [false, "':10.0.0.1"], + [false, '---'], + [false, '02001:0000:1234:0000:0000:C1C0:ABCD:0876'], + [false, '1.2.3.4:1111:2222:3333:4444::5555'], + [false, '1.2.3.4:1111:2222:3333::5555'], + [false, '1.2.3.4:1111:2222::5555'], + [false, '1.2.3.4:1111::5555'], + [false, '1.2.3.4::'], + [false, '1.2.3.4::5555'], + [false, '1111'], + [false, '11112222:3333:4444:5555:6666:1.2.3.4'], + [false, '11112222:3333:4444:5555:6666:7777:8888'], + [false, '1111:'], + [false, '1111:1.2.3.4'], + [false, '1111:2222'], + [false, '1111:22223333:4444:5555:6666:1.2.3.4'], + [false, '1111:22223333:4444:5555:6666:7777:8888'], + [false, '1111:2222:'], + [false, '1111:2222:1.2.3.4'], + [false, '1111:2222:3333'], + [false, '1111:2222:33334444:5555:6666:1.2.3.4'], + [false, '1111:2222:33334444:5555:6666:7777:8888'], + [false, '1111:2222:3333:'], + [false, '1111:2222:3333:1.2.3.4'], + [false, '1111:2222:3333:4444'], + [false, '1111:2222:3333:44445555:6666:1.2.3.4'], + [false, '1111:2222:3333:44445555:6666:7777:8888'], + [false, '1111:2222:3333:4444:'], + [false, '1111:2222:3333:4444:1.2.3.4'], + [false, '1111:2222:3333:4444:5555'], + [false, '1111:2222:3333:4444:55556666:1.2.3.4'], + [false, '1111:2222:3333:4444:55556666:7777:8888'], + [false, '1111:2222:3333:4444:5555:'], + [false, '1111:2222:3333:4444:5555:1.2.3.4'], + [false, '1111:2222:3333:4444:5555:6666'], + [false, '1111:2222:3333:4444:5555:66661.2.3.4'], + [false, '1111:2222:3333:4444:5555:66667777:8888'], + [false, '1111:2222:3333:4444:5555:6666:'], + [false, '1111:2222:3333:4444:5555:6666:00.00.00.00'], + [false, '1111:2222:3333:4444:5555:6666:000.000.000.000'], + [false, '1111:2222:3333:4444:5555:6666:1.2.3.4.5'], + [false, '1111:2222:3333:4444:5555:6666:255.255.255255'], + [false, '1111:2222:3333:4444:5555:6666:255.255255.255'], + [false, '1111:2222:3333:4444:5555:6666:255255.255.255'], + [false, '1111:2222:3333:4444:5555:6666:256.256.256.256'], + [false, '1111:2222:3333:4444:5555:6666:7777'], + [false, '1111:2222:3333:4444:5555:6666:77778888'], + [false, '1111:2222:3333:4444:5555:6666:7777:'], + [false, '1111:2222:3333:4444:5555:6666:7777:1.2.3.4'], + [false, '1111:2222:3333:4444:5555:6666:7777:8888:'], + [false, '1111:2222:3333:4444:5555:6666:7777:8888:1.2.3.4'], + [false, '1111:2222:3333:4444:5555:6666:7777:8888:9999'], + [false, '1111:2222:3333:4444:5555:6666:7777:8888::'], + [false, '1111:2222:3333:4444:5555:6666:7777:::'], + [false, '1111:2222:3333:4444:5555:6666::1.2.3.4'], + [false, '1111:2222:3333:4444:5555:6666::8888:'], + [false, '1111:2222:3333:4444:5555:6666:::'], + [false, '1111:2222:3333:4444:5555:6666:::8888'], + [false, '1111:2222:3333:4444:5555::7777:8888:'], + [false, '1111:2222:3333:4444:5555::7777::'], + [false, '1111:2222:3333:4444:5555::8888:'], + [false, '1111:2222:3333:4444:5555:::'], + [false, '1111:2222:3333:4444:5555:::1.2.3.4'], + [false, '1111:2222:3333:4444:5555:::7777:8888'], + [false, '1111:2222:3333:4444::5555:'], + [false, '1111:2222:3333:4444::6666:7777:8888:'], + [false, '1111:2222:3333:4444::6666:7777::'], + [false, '1111:2222:3333:4444::6666::8888'], + [false, '1111:2222:3333:4444::7777:8888:'], + [false, '1111:2222:3333:4444::8888:'], + [false, '1111:2222:3333:4444:::'], + [false, '1111:2222:3333:4444:::6666:1.2.3.4'], + [false, '1111:2222:3333:4444:::6666:7777:8888'], + [false, '1111:2222:3333::5555:'], + [false, '1111:2222:3333::5555:6666:7777:8888:'], + [false, '1111:2222:3333::5555:6666:7777::'], + [false, '1111:2222:3333::5555:6666::8888'], + [false, '1111:2222:3333::5555::1.2.3.4'], + [false, '1111:2222:3333::5555::7777:8888'], + [false, '1111:2222:3333::6666:7777:8888:'], + [false, '1111:2222:3333::7777:8888:'], + [false, '1111:2222:3333::8888:'], + [false, '1111:2222:3333:::'], + [false, '1111:2222:3333:::5555:6666:1.2.3.4'], + [false, '1111:2222:3333:::5555:6666:7777:8888'], + [false, '1111:2222::4444:5555:6666:7777:8888:'], + [false, '1111:2222::4444:5555:6666:7777::'], + [false, '1111:2222::4444:5555:6666::8888'], + [false, '1111:2222::4444:5555::1.2.3.4'], + [false, '1111:2222::4444:5555::7777:8888'], + [false, '1111:2222::4444::6666:1.2.3.4'], + [false, '1111:2222::4444::6666:7777:8888'], + [false, '1111:2222::5555:'], + [false, '1111:2222::5555:6666:7777:8888:'], + [false, '1111:2222::6666:7777:8888:'], + [false, '1111:2222::7777:8888:'], + [false, '1111:2222::8888:'], + [false, '1111:2222:::'], + [false, '1111:2222:::4444:5555:6666:1.2.3.4'], + [false, '1111:2222:::4444:5555:6666:7777:8888'], + [false, '1111::3333:4444:5555:6666:7777:8888:'], + [false, '1111::3333:4444:5555:6666:7777::'], + [false, '1111::3333:4444:5555:6666::8888'], + [false, '1111::3333:4444:5555::1.2.3.4'], + [false, '1111::3333:4444:5555::7777:8888'], + [false, '1111::3333:4444::6666:1.2.3.4'], + [false, '1111::3333:4444::6666:7777:8888'], + [false, '1111::3333::5555:6666:1.2.3.4'], + [false, '1111::3333::5555:6666:7777:8888'], + [false, '1111::4444:5555:6666:7777:8888:'], + [false, '1111::5555:'], + [false, '1111::5555:6666:7777:8888:'], + [false, '1111::6666:7777:8888:'], + [false, '1111::7777:8888:'], + [false, '1111::8888:'], + [false, '1111:::'], + [false, '1111:::3333:4444:5555:6666:1.2.3.4'], + [false, '1111:::3333:4444:5555:6666:7777:8888'], + [false, '123'], + [false, '12345::6:7:8'], + [false, '192.168.0.256'], + [false, '192.168.256.0'], + [false, '1:2:3:4:5:6:7:8:9'], + [false, '1:2:3::4:5:6:7:8:9'], + [false, '1:2:3::4:5::7:8'], + [false, '1::1.2.256.4'], + [false, '1::1.2.3.256'], + [false, '1::1.2.3.300'], + [false, '1::1.2.3.900'], + [false, '1::1.2.300.4'], + [false, '1::1.2.900.4'], + [false, '1::1.256.3.4'], + [false, '1::1.300.3.4'], + [false, '1::1.900.3.4'], + [false, '1::256.2.3.4'], + [false, '1::260.2.3.4'], + [false, '1::2::3'], + [false, '1::300.2.3.4'], + [false, '1::300.300.300.300'], + [false, '1::3000.30.30.30'], + [false, '1::400.2.3.4'], + [false, '1::5:1.2.256.4'], + [false, '1::5:1.2.3.256'], + [false, '1::5:1.2.3.300'], + [false, '1::5:1.2.3.900'], + [false, '1::5:1.2.300.4'], + [false, '1::5:1.2.900.4'], + [false, '1::5:1.256.3.4'], + [false, '1::5:1.300.3.4'], + [false, '1::5:1.900.3.4'], + [false, '1::5:256.2.3.4'], + [false, '1::5:260.2.3.4'], + [false, '1::5:300.2.3.4'], + [false, '1::5:300.300.300.300'], + [false, '1::5:3000.30.30.30'], + [false, '1::5:400.2.3.4'], + [false, '1::5:900.2.3.4'], + [false, '1::900.2.3.4'], + [false, '1:::3:4:5'], + [false, '2001:0000:1234: 0000:0000:C1C0:ABCD:0876'], + [false, '2001:0000:1234:0000:00001:C1C0:ABCD:0876'], + [false, '2001:0000:1234:0000:0000:C1C0:ABCD:0876 0'], + [false, '2001:1:1:1:1:1:255Z255X255Y255'], + [false, '2001::FFD3::57ab'], + [false, '2001:DB8:0:0:8:800:200C:417A:221'], + [false, '2001:db8:85a3::8a2e:37023:7334'], + [false, '2001:db8:85a3::8a2e:370k:7334'], + [false, '255.256.255.255'], + [false, '256.255.255.255'], + [false, '3ffe:0b00:0000:0001:0000:0000:000a'], + [false, '3ffe:b00::1::a'], + [false, ':'], + [false, ':1.2.3.4'], + [false, ':1111:2222:3333:4444:5555:6666:1.2.3.4'], + [false, ':1111:2222:3333:4444:5555:6666:7777:8888'], + [false, ':1111:2222:3333:4444:5555:6666:7777::'], + [false, ':1111:2222:3333:4444:5555:6666::'], + [false, ':1111:2222:3333:4444:5555:6666::8888'], + [false, ':1111:2222:3333:4444:5555::'], + [false, ':1111:2222:3333:4444:5555::1.2.3.4'], + [false, ':1111:2222:3333:4444:5555::7777:8888'], + [false, ':1111:2222:3333:4444:5555::8888'], + [false, ':1111:2222:3333:4444::'], + [false, ':1111:2222:3333:4444::1.2.3.4'], + [false, ':1111:2222:3333:4444::5555'], + [false, ':1111:2222:3333:4444::6666:1.2.3.4'], + [false, ':1111:2222:3333:4444::6666:7777:8888'], + [false, ':1111:2222:3333:4444::7777:8888'], + [false, ':1111:2222:3333:4444::8888'], + [false, ':1111:2222:3333::'], + [false, ':1111:2222:3333::1.2.3.4'], + [false, ':1111:2222:3333::5555'], + [false, ':1111:2222:3333::5555:6666:1.2.3.4'], + [false, ':1111:2222:3333::5555:6666:7777:8888'], + [false, ':1111:2222:3333::6666:1.2.3.4'], + [false, ':1111:2222:3333::6666:7777:8888'], + [false, ':1111:2222:3333::7777:8888'], + [false, ':1111:2222:3333::8888'], + [false, ':1111:2222::'], + [false, ':1111:2222::1.2.3.4'], + [false, ':1111:2222::4444:5555:6666:1.2.3.4'], + [false, ':1111:2222::4444:5555:6666:7777:8888'], + [false, ':1111:2222::5555'], + [false, ':1111:2222::5555:6666:1.2.3.4'], + [false, ':1111:2222::5555:6666:7777:8888'], + [false, ':1111:2222::6666:1.2.3.4'], + [false, ':1111:2222::6666:7777:8888'], + [false, ':1111:2222::7777:8888'], + [false, ':1111:2222::8888'], + [false, ':1111::'], + [false, ':1111::1.2.3.4'], + [false, ':1111::3333:4444:5555:6666:1.2.3.4'], + [false, ':1111::3333:4444:5555:6666:7777:8888'], + [false, ':1111::4444:5555:6666:1.2.3.4'], + [false, ':1111::4444:5555:6666:7777:8888'], + [false, ':1111::5555'], + [false, ':1111::5555:6666:1.2.3.4'], + [false, ':1111::5555:6666:7777:8888'], + [false, ':1111::6666:1.2.3.4'], + [false, ':1111::6666:7777:8888'], + [false, ':1111::7777:8888'], + [false, ':1111::8888'], + [false, ':2222:3333:4444:5555:6666:1.2.3.4'], + [false, ':2222:3333:4444:5555:6666:7777:8888'], + [false, ':3333:4444:5555:6666:1.2.3.4'], + [false, ':3333:4444:5555:6666:7777:8888'], + [false, ':4444:5555:6666:1.2.3.4'], + [false, ':4444:5555:6666:7777:8888'], + [false, ':5555:6666:1.2.3.4'], + [false, ':5555:6666:7777:8888'], + [false, ':6666:1.2.3.4'], + [false, ':6666:7777:8888'], + [false, ':7777:8888'], + [false, ':8888'], + [false, '::.'], + [false, '::..'], + [false, '::...'], + [false, '::...4'], + [false, '::..3.'], + [false, '::..3.4'], + [false, '::.2..'], + [false, '::.2.3.'], + [false, '::.2.3.4'], + [false, '::1...'], + [false, '::1.2..'], + [false, '::1.2.256.4'], + [false, '::1.2.3.'], + [false, '::1.2.3.256'], + [false, '::1.2.3.300'], + [false, '::1.2.3.900'], + [false, '::1.2.300.4'], + [false, '::1.2.900.4'], + [false, '::1.256.3.4'], + [false, '::1.300.3.4'], + [false, '::1.900.3.4'], + [false, '::1111:2222:3333:4444:5555:6666::'], + [false, '::2222:3333:4444:5555:6666:7777:1.2.3.4'], + [false, '::2222:3333:4444:5555:6666:7777:8888:'], + [false, '::2222:3333:4444:5555:6666:7777:8888:9999'], + [false, '::2222:3333:4444:5555:7777:8888::'], + [false, '::2222:3333:4444:5555:7777::8888'], + [false, '::2222:3333:4444:5555::1.2.3.4'], + [false, '::2222:3333:4444:5555::7777:8888'], + [false, '::2222:3333:4444::6666:1.2.3.4'], + [false, '::2222:3333:4444::6666:7777:8888'], + [false, '::2222:3333::5555:6666:1.2.3.4'], + [false, '::2222:3333::5555:6666:7777:8888'], + [false, '::2222::4444:5555:6666:1.2.3.4'], + [false, '::2222::4444:5555:6666:7777:8888'], + [false, '::256.2.3.4'], + [false, '::260.2.3.4'], + [false, '::300.2.3.4'], + [false, '::300.300.300.300'], + [false, '::3000.30.30.30'], + [false, '::3333:4444:5555:6666:7777:8888:'], + [false, '::400.2.3.4'], + [false, '::4444:5555:6666:7777:8888:'], + [false, '::5555:'], + [false, '::5555:6666:7777:8888:'], + [false, '::6666:7777:8888:'], + [false, '::7777:8888:'], + [false, '::8888:'], + [false, '::900.2.3.4'], + [false, ':::'], + [false, ':::1.2.3.4'], + [false, ':::2222:3333:4444:5555:6666:1.2.3.4'], + [false, ':::2222:3333:4444:5555:6666:7777:8888'], + [false, ':::3333:4444:5555:6666:7777:8888'], + [false, ':::4444:5555:6666:1.2.3.4'], + [false, ':::4444:5555:6666:7777:8888'], + [false, ':::5555'], + [false, ':::5555:6666:1.2.3.4'], + [false, ':::5555:6666:7777:8888'], + [false, ':::6666:1.2.3.4'], + [false, ':::6666:7777:8888'], + [false, ':::7777:8888'], + [false, ':::8888'], + [false, '::ffff:192x168.1.26'], + [false, '::ffff:2.3.4'], + [false, '::ffff:257.1.2.3'], + [false, 'FF01::101::2'], + [false, 'FF02:0000:0000:0000:0000:0000:0000:0000:0001'], + [false, 'XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:1.2.3.4'], + [false, 'XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX'], + [false, 'fe80:0000:0000:0000:0204:61ff:254.157.241.086'], + [false, 'fe80::4413:c8ae:2821:5852%10'], + [false, 'ldkfj'], + [false, 'mydomain.com'], + [false, 'test.mydomain.com'], + [true, '0000:0000:0000:0000:0000:0000:0000:0000'], + [true, '0000:0000:0000:0000:0000:0000:0000:0001'], + [true, '0:0:0:0:0:0:0:0'], + [true, '0:0:0:0:0:0:0:1'], + [true, '0:0:0:0:0:0:0::'], + [true, '0:0:0:0:0:0:13.1.68.3'], + [true, '0:0:0:0:0:0::'], + [true, '0:0:0:0:0::'], + [true, '0:0:0:0:0:FFFF:129.144.52.38'], + [true, '0:0:0:0::'], + [true, '0:0:0::'], + [true, '0:0::'], + [true, '0::'], + [true, '0:a:b:c:d:e:f::'], + [true, '1.2.3.4'], + [true, '1111:2222:3333:4444:5555:6666:123.123.123.123'], + [true, '1111:2222:3333:4444:5555:6666:7777:8888'], + [true, '1111:2222:3333:4444:5555:6666:7777::'], + [true, '1111:2222:3333:4444:5555:6666::'], + [true, '1111:2222:3333:4444:5555:6666::8888'], + [true, '1111:2222:3333:4444:5555::'], + [true, '1111:2222:3333:4444:5555::123.123.123.123'], + [true, '1111:2222:3333:4444:5555::7777:8888'], + [true, '1111:2222:3333:4444:5555::8888'], + [true, '1111:2222:3333:4444::'], + [true, '1111:2222:3333:4444::123.123.123.123'], + [true, '1111:2222:3333:4444::6666:123.123.123.123'], + [true, '1111:2222:3333:4444::6666:7777:8888'], + [true, '1111:2222:3333:4444::7777:8888'], + [true, '1111:2222:3333:4444::8888'], + [true, '1111:2222:3333::'], + [true, '1111:2222:3333::123.123.123.123'], + [true, '1111:2222:3333::5555:6666:123.123.123.123'], + [true, '1111:2222:3333::5555:6666:7777:8888'], + [true, '1111:2222:3333::6666:123.123.123.123'], + [true, '1111:2222:3333::6666:7777:8888'], + [true, '1111:2222:3333::7777:8888'], + [true, '1111:2222:3333::8888'], + [true, '1111:2222::'], + [true, '1111:2222::123.123.123.123'], + [true, '1111:2222::4444:5555:6666:123.123.123.123'], + [true, '1111:2222::4444:5555:6666:7777:8888'], + [true, '1111:2222::5555:6666:123.123.123.123'], + [true, '1111:2222::5555:6666:7777:8888'], + [true, '1111:2222::6666:123.123.123.123'], + [true, '1111:2222::6666:7777:8888'], + [true, '1111:2222::7777:8888'], + [true, '1111:2222::8888'], + [true, '1111::'], + [true, '1111::123.123.123.123'], + [true, '1111::3333:4444:5555:6666:123.123.123.123'], + [true, '1111::3333:4444:5555:6666:7777:8888'], + [true, '1111::4444:5555:6666:123.123.123.123'], + [true, '1111::4444:5555:6666:7777:8888'], + [true, '1111::5555:6666:123.123.123.123'], + [true, '1111::5555:6666:7777:8888'], + [true, '1111::6666:123.123.123.123'], + [true, '1111::6666:7777:8888'], + [true, '1111::7777:8888'], + [true, '1111::8888'], + [true, '123.23.34.2'], + [true, '172.26.168.134'], + [true, '192.168.0.0'], + [true, '192.168.128.255'], + [true, '1:2:3:4:5:6:1.2.3.4'], + [true, '1:2:3:4:5:6:7:8'], + [true, '1:2:3:4:5:6::'], + [true, '1:2:3:4:5:6::8'], + [true, '1:2:3:4:5::'], + [true, '1:2:3:4:5::1.2.3.4'], + [true, '1:2:3:4:5::7:8'], + [true, '1:2:3:4:5::8'], + [true, '1:2:3:4::'], + [true, '1:2:3:4::1.2.3.4'], + [true, '1:2:3:4::5:1.2.3.4'], + [true, '1:2:3:4::7:8'], + [true, '1:2:3:4::8'], + [true, '1:2:3::'], + [true, '1:2:3::1.2.3.4'], + [true, '1:2:3::5:1.2.3.4'], + [true, '1:2:3::7:8'], + [true, '1:2:3::8'], + [true, '1:2::'], + [true, '1:2::1.2.3.4'], + [true, '1:2::5:1.2.3.4'], + [true, '1:2::7:8'], + [true, '1:2::8'], + [true, '1::'], + [true, '1::1.2.3.4'], + [true, '1::2:3'], + [true, '1::2:3:4'], + [true, '1::2:3:4:5'], + [true, '1::2:3:4:5:6'], + [true, '1::2:3:4:5:6:7'], + [true, '1::5:1.2.3.4'], + [true, '1::5:11.22.33.44'], + [true, '1::7:8'], + [true, '1::8'], + [true, '2001:0000:1234:0000:0000:C1C0:ABCD:0876'], + [true, '2001:0:1234::C1C0:ABCD:876'], + [true, '2001:0db8:0000:0000:0000:0000:1428:57ab'], + [true, '2001:0db8:0000:0000:0000::1428:57ab'], + [true, '2001:0db8:0:0:0:0:1428:57ab'], + [true, '2001:0db8:0:0::1428:57ab'], + [true, '2001:0db8:1234:0000:0000:0000:0000:0000'], + [true, '2001:0db8:1234::'], + [true, '2001:0db8:1234:ffff:ffff:ffff:ffff:ffff'], + [true, '2001:0db8:85a3:0000:0000:8a2e:0370:7334'], + [true, '2001:0db8::1428:57ab'], + [true, '2001:2:3:4:5:6:7:134'], + [true, '2001:DB8:0:0:8:800:200C:417A'], + [true, '2001:DB8::8:800:200C:417A'], + [true, '2001:db8:85a3:0:0:8a2e:370:7334'], + [true, '2001:db8:85a3::8a2e:370:7334'], + [true, '2001:db8::'], + [true, '2001:db8::1428:57ab'], + [true, '2001:db8:a::123'], + [true, '2002::'], + [true, '2::10'], + [true, '3ffe:0b00:0000:0000:0001:0000:0000:000a'], + [true, '3ffe:b00::1:0:0:a'], + [true, '::'], + [true, '::0'], + [true, '::0:0'], + [true, '::0:0:0'], + [true, '::0:0:0:0'], + [true, '::0:0:0:0:0'], + [true, '::0:0:0:0:0:0'], + [true, '::0:0:0:0:0:0:0'], + [true, '::0:a:b:c:d:e:f'], + [true, '::1'], + [true, '::123.123.123.123'], + [true, '::13.1.68.3'], + [true, '::2222:3333:4444:5555:6666:123.123.123.123'], + [true, '::2222:3333:4444:5555:6666:7777:8888'], + [true, '::2:3'], + [true, '::2:3:4'], + [true, '::2:3:4:5'], + [true, '::2:3:4:5:6'], + [true, '::2:3:4:5:6:7'], + [true, '::2:3:4:5:6:7:8'], + [true, '::3333:4444:5555:6666:7777:8888'], + [true, '::4444:5555:6666:123.123.123.123'], + [true, '::4444:5555:6666:7777:8888'], + [true, '::5555:6666:123.123.123.123'], + [true, '::5555:6666:7777:8888'], + [true, '::6666:123.123.123.123'], + [true, '::6666:7777:8888'], + [true, '::7777:8888'], + [true, '::8'], + [true, '::8888'], + [true, '::FFFF:129.144.52.38'], + [true, '::ffff:0:0'], + [true, '::ffff:0c22:384e'], + [true, '::ffff:12.34.56.78'], + [true, '::ffff:192.0.2.128'], + [true, '::ffff:192.168.1.1'], + [true, '::ffff:192.168.1.26'], + [true, '::ffff:c000:280'], + [true, 'FF01:0:0:0:0:0:0:101'], + [true, 'FF01::101'], + [true, 'FF02:0000:0000:0000:0000:0000:0000:0001'], + [true, 'FF02::1'], + [true, 'a:b:c:d:e:f:0::'], + [true, 'fe80:0000:0000:0000:0204:61ff:fe9d:f156'], + [true, 'fe80:0:0:0:204:61ff:254.157.241.86'], + [true, 'fe80:0:0:0:204:61ff:fe9d:f156'], + [true, 'fe80::'], + [true, 'fe80::1'], + [true, 'fe80::204:61ff:254.157.241.86'], + [true, 'fe80::204:61ff:fe9d:f156'], + [true, 'fe80::217:f2ff:254.7.237.98'], + [true, 'fe80::217:f2ff:fe07:ed62'], + [true, 'ff02::1'], +] + +describe('get_ipany_re', function () { + const net_utils = require('../index') + + it('IPv6, Prefix', function (done) { + // for x-*-ip headers + // it must fail as of not valide + assert.ok(!net.isIPv6('IPv6:2001:db8:85a3::8a2e:370:7334')) + // must okay! + assert.ok(net.isIPv6('2001:db8:85a3::8a2e:370:7334')) + done() + }) + + it('IP fixtures check', function (done) { + for (const i in ip_fixtures) { + const match = net_utils.get_ipany_re('^', '$').test(ip_fixtures[i][1]) + // console.log('IP:', `'${ip_fixtures[i][1]}'` , 'Expected:', ip_fixtures[i][0] , 'Match:' , match); + assert.ok( + match === ip_fixtures[i][0], + `${ip_fixtures[i][1]} - Expected: ${ip_fixtures[i][0]} - Match: ${match}`, + ) + } + done() + }) + + it('IPv4, bare', function (done) { + // for x-*-ip headers + const match = net_utils.get_ipany_re().exec('127.0.0.1') + assert.equal(match[1], '127.0.0.1') + assert.equal(match.length, 2) + done() + }) + + it('IPv4, Received header, parens', function (done) { + const received_re = net_utils.get_ipany_re( + '^Received:.*?[\\[\\(]', + '[\\]\\)]', + ) + const match = received_re.exec( + 'Received: from unknown (HELO mail.theartfarm.com) (127.0.0.30) by mail.theartfarm.com with SMTP; 5 Sep 2015 14:29:00 -0000', + ) + assert.equal(match[1], '127.0.0.30') + assert.equal(match.length, 2) + done() + }) + + it('IPv4, Received header, bracketed, expedia', function (done) { + const received_header = + 'Received: from mta2.expediamail.com (mta2.expediamail.com [66.231.89.19]) by mail.theartfarm.com (Haraka/2.6.2-toaster) with ESMTPS id C669CF18-1C1C-484C-8A5B-A89088B048CB.1 envelope-from (version=TLSv1/SSLv3 cipher=AES256-SHA verify=NO); Sat, 05 Sep 2015 07:28:57 -0700' + const received_re = net_utils.get_ipany_re( + '^Received:.*?[\\[\\(]', + '[\\]\\)]', + ) + const match = received_re.exec(received_header) + assert.equal(match[1], '66.231.89.19') + assert.equal(match.length, 2) + done() + }) + + it('IPv4, Received header, bracketed, github', function (done) { + const received_re = net_utils.get_ipany_re( + '^Received:.*?[\\[\\(]', + '[\\]\\)]', + ) + const match = received_re.exec( + 'Received: from github-smtp2a-ext-cp1-prd.iad.github.net (github-smtp2-ext5.iad.github.net [192.30.252.196])', + ) + assert.equal(match[1], '192.30.252.196') + assert.equal(match.length, 2) + done() + }) + + it('IPv6, Received header, bracketed', function (done) { + const received_header = + 'Received: from ?IPv6:2601:184:c001:5cf7:a53f:baf7:aaf3:bce7? ([2601:184:c001:5cf7:a53f:baf7:aaf3:bce7])' + const received_re = net_utils.get_ipany_re( + '^Received:.*?[\\[\\(]', + '[\\]\\)]', + ) + const match = received_re.exec(received_header) + assert.equal(match[1], '2601:184:c001:5cf7:a53f:baf7:aaf3:bce7') + assert.equal(match.length, 2) + done() + }) + + it('IPv6, Received header, bracketed, IPv6 prefix', function (done) { + const received_re = net_utils.get_ipany_re( + '^Received:.*?[\\[\\(](?:IPv6:)?', + '[\\]\\)]', + ) + const match = received_re.exec( + 'Received: from hub.freebsd.org (hub.freebsd.org [IPv6:2001:1900:2254:206c::16:88])', + ) + assert.equal(match[1], '2001:1900:2254:206c::16:88') + assert.equal(match.length, 2) + done() + }) + + it('IPv6, folded Received header, bracketed, IPv6 prefix', function (done) { + // note the use of [\s\S], '.' doesn't match newlines in JS regexp + const received_re = net_utils.get_ipany_re( + '^Received:[\\s\\S]*?[\\[\\(](?:IPv6:)?', + '[\\]\\)]', + ) + const match = received_re.exec( + 'Received: from freefall.freebsd.org (freefall.freebsd.org\r\n [IPv6:2001:1900:2254:206c::16:87])', + ) + if (match) { + assert.equal(match[1], '2001:1900:2254:206c::16:87') + assert.equal(match.length, 2) + } + done() + }) + + it('IPv6, Received header, bracketed, IPv6 prefix, localhost compressed', function (done) { + const received_re = net_utils.get_ipany_re( + '^Received:.*?[\\[\\(](?:IPv6:)?', + '[\\]\\)]', + ) + const match = received_re.exec( + 'Received: from ietfa.amsl.com (localhost [IPv6:::1])', + ) + assert.equal(match[1], '::1') + assert.equal(match.length, 2) + done() + }) + + it('IPv6 bogus', function (done) { + const is_bogus = net_utils.ipv6_bogus('::192.41.13.251') // From https://github.com/haraka/Haraka/issues/2763 + assert.equal(is_bogus, true) + done() + }) +}) diff --git a/test/get_mx.js b/test/get_mx.js new file mode 100644 index 0000000..7a0724b --- /dev/null +++ b/test/get_mx.js @@ -0,0 +1,181 @@ +const assert = require('assert') + +describe('get_mx', function () { + this.timeout(12000) + + beforeEach(function (done) { + this.net_utils = require('../index') + done() + }) + + const validCases = { + 'tnpi.net': 'mail.theartfarm.com', + 'matt@tnpi.net': 'mail.theartfarm.com', + 'matt.simerson@gmail.com': /google.com/, + 'example.com': '', + 'no-mx.haraka.tnpi.net': '192.0.99.5', + 'bad-mx.haraka.tnpi.net': /99/, + 'über.haraka.tnpi.net': 'no-mx.haraka.tnpi.net', + } + + function checkValid(c, mxlist) { + try { + if ('string' === typeof c) { + assert.equal(mxlist[0].exchange, c) + } else { + assert.ok(c.test(mxlist[0].exchange)) + } + } catch (err) { + console.error(err) + } + } + + for (const c in validCases) { + it(`gets MX records for ${c}`, function (done) { + this.timeout(12000) + this.net_utils.get_mx(c, (err, mxlist) => { + if (err) console.error(err) + assert.ifError(err) + checkValid(validCases[c], mxlist) + done() + }) + }) + + it(`awaits MX records for ${c}`, async function () { + this.timeout(12000) + const mxlist = await this.net_utils.get_mx(c) + checkValid(validCases[c], mxlist) + }) + } + + // macOS: ENODATA, win: ENOTOUND, ubuntu: ESERVFAIL + const invalidCases = { + invalid: /queryMx (ENODATA|ENOTFOUND|ESERVFAIL) invalid/, + 'gmail.xn--com-0da': /(ENOTFOUND|Cannot convert name to ASCII)/, + 'non-exist.haraka.tnpi.net': /ignore/, + 'haraka.non-exist': /ignore/, + } + + function checkInvalid(expected, actual) { + if ('string' === typeof expected) { + assert.strictEqual(actual, expected) + } else { + assert.equal(expected.test(actual), true) + } + } + + for (const c in invalidCases) { + it(`cb does not crash on invalid name: ${c}`, function () { + this.net_utils.get_mx(c, (err, mxlist) => { + if (err) checkInvalid(invalidCases[c], err.message) + assert.equal(mxlist.length, 0) + }) + }) + + it(`async does not crash on invalid name: ${c}`, async function () { + try { + const mxlist = await this.net_utils.get_mx(c) + assert.equal(mxlist.length, 0) + } catch (err) { + checkInvalid(invalidCases[c], err.message) + } + }) + } + + + describe('resolve_mx_hosts', function () { + this.timeout(12000) + + beforeEach((done) => { + this.net_utils = require('../index') + done() + }) + + const expectedResolvedMx = [ + { + exchange: '2605:ae00:329::14', + priority: 10, + from_dns: 'mail.theartfarm.com', + }, + { + exchange: '66.128.51.165', + priority: 10, + from_dns: 'mail.theartfarm.com', + }, + ] + + it('resolves mx hosts to IPs, tnpi.net', async () => { + const r = await this.net_utils.resolve_mx_hosts([ + { exchange: 'mail.theartfarm.com', priority: 10, from_dns: 'tnpi.net' }, + ]) + assert.deepEqual(r, expectedResolvedMx) + }) + + it('resolves mx hosts to IPs, gmail.com', async () => { + const mxes = await this.net_utils.get_mx('gmail.com') + assert.equal(mxes.length, 5) + const r = await this.net_utils.resolve_mx_hosts(mxes) + assert.equal(r.length, 10) + }) + + it('returns IPs as is', async () => { + const r = await this.net_utils.resolve_mx_hosts(expectedResolvedMx) + assert.deepEqual(r, expectedResolvedMx) + }) + + it('returns sockets as-is', async () => { + const r = await this.net_utils.resolve_mx_hosts([{ path: '/var/run/sock' }]) + assert.deepEqual(r, [{ path: '/var/run/sock' }]) + }) + + it('resolve_mx_hosts, gmail.com', async () => { + const mxes = await this.net_utils.get_mx('gmail.com') + const r = await this.net_utils.resolve_mx_hosts(mxes) + assert.equal(r.length, 10) + }) + + it('resolve_mx_hosts, yahoo.com', async () => { + const mxes = await this.net_utils.get_mx('yahoo.com') + const r = await this.net_utils.resolve_mx_hosts([mxes[0]]) + assert.equal(r.length, 8) + }) + }) + + describe('get_implicit_mx', function () { + this.timeout(5000) + + beforeEach(function (done) { + this.net_utils = require('../index') + done() + }) + + it('harakamail.com', async function () { + const mf = await this.net_utils.get_implicit_mx('harakamail.com') + assert.equal(mf.length, 1) + }) + + it('mx.theartfarm.com', async function () { + const mf = await this.net_utils.get_implicit_mx('mx.theartfarm.com') + assert.equal(mf.length, 0) + }) + + it('resolve-fail-definitive.josef-froehle.de', async function () { + const mf = await this.net_utils.get_implicit_mx( + 'resolve-fail-definitive.josef-froehle.de', + ) + assert.equal(mf.length, 0) + }) + it('resolve-fail-a.josef-froehle.de', async function () { + const mf = await this.net_utils.get_implicit_mx( + 'resolve-fail-a.josef-froehle.de', + ) + assert.equal(mf.length, 1) + }) + it('resolve-fail-aaaa.josef-froehle.de', async function () { + const mf = await this.net_utils.get_implicit_mx( + 'resolve-fail-aaaa.josef-froehle.de', + ) + assert.equal(mf.length, 0) + }) + }) +}) \ No newline at end of file diff --git a/test/get_public_ip.js b/test/get_public_ip.js new file mode 100644 index 0000000..a2a5ea6 --- /dev/null +++ b/test/get_public_ip.js @@ -0,0 +1,81 @@ +const assert = require('node:assert') +const path = require('node:path') + +function has_stun() { + try { + require('@msimerson/stun') + } catch (e) { + return false + } + return true +} + +describe('get_public_ip', function () { + beforeEach(function (done) { + this.net_utils = require('../index') + this.net_utils.config = this.net_utils.config.module_config( + path.resolve('test'), + ) + done() + }) + + it('cached', function (done) { + this.net_utils.public_ip = '1.1.1.1' + this.net_utils.get_public_ip((err, ip) => { + assert.equal(null, err) + assert.equal('1.1.1.1', ip) + done() + }) + }) + + it('normal', function (done) { + this.net_utils.public_ip = undefined + this.net_utils.get_public_ip((err, ip) => { + if (has_stun()) { + if (err) { + console.error(err) + } else { + console.log(`stun success: ${ip}`) + assert.equal(null, err) + assert.ok(ip, ip) + } + } else { + console.log('stun skipped') + } + done() + }) + }) + + describe('get_public_ip_async', function () { + beforeEach((done) => { + this.net_utils = require('../index') + this.net_utils.config = this.net_utils.config.module_config( + path.resolve('test'), + ) + done() + }) + + it('cached', async () => { + this.net_utils.public_ip = '1.1.1.1' + const ip = await this.net_utils.get_public_ip() + assert.equal('1.1.1.1', ip) + }) + + it('normal', async () => { + this.net_utils.public_ip = undefined + + if (!has_stun()) { + console.log('stun skipped') + return + } + + try { + const ip = await this.net_utils.get_public_ip() + console.log(`stun success: ${ip}`) + assert.ok(ip, ip) + } catch (e) { + console.error(e) + } + }) + }) +}) diff --git a/test/harakaMx.js b/test/harakaMx.js new file mode 100644 index 0000000..b91eeee --- /dev/null +++ b/test/harakaMx.js @@ -0,0 +1,198 @@ +const assert = require('assert') + +describe('HarakaMx', () => { + beforeEach(function (done) { + this.nu = require('../index') + done() + }) + + describe('fromObject', () => { + it('accepts an object', function () { + assert.deepEqual( + new this.nu.HarakaMx({ + from_dns: 'example.com', + exchange: '.', + priority: 0, + }), + { from_dns: 'example.com', exchange: '.', priority: 0 }, + ) + }) + + it('sets default priority to 0', function () { + assert.deepEqual(new this.nu.HarakaMx({ exchange: '.' }), { + exchange: '.', + priority: 0, + }) + }) + + it('if optional domain provided, sets from_dns', function () { + assert.deepEqual(new this.nu.HarakaMx({ exchange: '.' }, 'example.com'), { + from_dns: 'example.com', + exchange: '.', + priority: 0, + }) + }) + }) + + describe('fromString', function () { + it('parses a hostname', function () { + assert.deepEqual(new this.nu.HarakaMx('mail.example.com'), { + exchange: 'mail.example.com', + priority: 0, + }) + }) + + it('parses a hostname:port', function () { + assert.deepEqual(new this.nu.HarakaMx('mail.example.com:25'), { + exchange: 'mail.example.com', + port: 25, + priority: 0, + }) + }) + + it('parses an IPv4', function () { + assert.deepEqual(new this.nu.HarakaMx('192.0.2.1'), { + exchange: '192.0.2.1', + priority: 0, + }) + }) + + it('parses an IPv4:port', function () { + assert.deepEqual(new this.nu.HarakaMx('192.0.2.1:25'), { + exchange: '192.0.2.1', + port: 25, + priority: 0, + }) + }) + + it('parses an IPv6', function () { + assert.deepEqual(new this.nu.HarakaMx('2001:db8::1'), { + exchange: '2001:db8::1', + priority: 0, + }) + }) + + it('parses an IPv6:port', function () { + assert.deepEqual(new this.nu.HarakaMx('2001:db8::1:25'), { + exchange: '2001:db8::1', + port: 25, + priority: 0, + }) + }) + + it('parses an [IPv6]:port', function () { + assert.deepEqual(new this.nu.HarakaMx('[2001:db8::1]:25'), { + exchange: '2001:db8::1', + port: 25, + priority: 0, + }) + }) + }) + + describe('fromUri', function () { + it('parses simple URIs', function () { + assert.deepEqual(new this.nu.HarakaMx('smtp://192.0.2.2'), { + exchange: '192.0.2.2', + port: 25, + priority: 0, + }) + + assert.deepEqual(new this.nu.HarakaMx('smtp://[2001:db8::1]:25'), { + exchange: '[2001:db8::1]', + port: 25, + priority: 0, + }) + }) + + it('parses more complex URIs', function () { + assert.deepEqual( + new this.nu.HarakaMx('smtp://authUser:sekretPass@[2001:db8::1]'), + { + exchange: '[2001:db8::1]', + port: 25, + priority: 0, + auth_pass: 'sekretPass', + auth_user: 'authUser', + }, + ) + + assert.deepEqual( + new this.nu.HarakaMx('lmtp://authUser:sekretPass@[2001:db8::1]:25'), + { + exchange: '[2001:db8::1]', + port: 25, + priority: 0, + using_lmtp: true, + auth_pass: 'sekretPass', + auth_user: 'authUser', + }, + ) + }) + }) + + describe('toUrl', function () { + it('has a reasonable toUrl()', function () { + assert.equal( + new this.nu.HarakaMx({ exchange: '.' }).toUrl(), + 'smtp://.:25', + ) + + assert.equal( + new this.nu.HarakaMx({ + from_dns: 'example.com', + exchange: '.', + priority: 10, + }).toUrl(), + 'smtp://.:25', + ) + + assert.equal( + new this.nu.HarakaMx('smtp://au:ap@192.0.2.3:25').toUrl(), + 'smtp://au:****@192.0.2.3:25', + ) + + assert.equal( + new this.nu.HarakaMx('smtp://au:ap@192.0.2.3:465').toUrl(), + 'smtp://au:****@192.0.2.3:465', + ) + + assert.equal( + new this.nu.HarakaMx('smtp://[2001:db8::1]:25').toUrl(), + 'smtp://[2001:db8::1]:25', + ) + }) + }) + + describe('toString', function () { + it('has a reasonable toString()', function () { + assert.equal( + new this.nu.HarakaMx({ exchange: '.' }).toString(), + 'MX 0 smtp://.:25', + ) + + assert.equal( + new this.nu.HarakaMx({ + from_dns: 'example.com', + exchange: '.', + priority: 10, + }).toString(), + 'MX 10 smtp://.:25 (from example.com)', + ) + + assert.equal( + new this.nu.HarakaMx('smtp://au:ap@192.0.2.3:25').toString(), + 'MX 0 smtp://au:****@192.0.2.3:25', + ) + + assert.equal( + new this.nu.HarakaMx('smtp://au:ap@192.0.2.3:465').toString(), + 'MX 0 smtp://au:****@192.0.2.3:465', + ) + + assert.equal( + new this.nu.HarakaMx('smtp://[2001:db8::1]:25').toString(), + 'MX 0 smtp://[2001:db8::1]:25', + ) + }) + }) +}) diff --git a/test/net_utils.js b/test/net_utils.js index 26da194..cb0aea9 100644 --- a/test/net_utils.js +++ b/test/net_utils.js @@ -1,7 +1,6 @@ -const assert = require('assert') -const net = require('net') -const os = require('os') -const path = require('path') +const assert = require('node:assert') +const os = require('node:os') +const path = require('node:path') require('haraka-config').watch_files = false const net_utils = require('../index') @@ -240,95 +239,6 @@ describe('is_private_ip', function () { }) }) -describe('get_public_ip', function () { - beforeEach(function (done) { - this.net_utils = require('../index') - this.net_utils.config = this.net_utils.config.module_config( - path.resolve('test'), - ) - done() - }) - - function has_stun() { - try { - require('stun') - } catch (e) { - return false - } - return true - } - - it('cached', function (done) { - this.net_utils.public_ip = '1.1.1.1' - const cb = function (err, ip) { - assert.equal(null, err) - assert.equal('1.1.1.1', ip) - done() - } - this.net_utils.get_public_ip(cb) - }) - - it('normal', function (done) { - this.net_utils.public_ip = undefined - const cb = function (err, ip) { - if (has_stun()) { - if (err) { - console.error(err) - } else { - console.log(`stun success: ${ip}`) - assert.equal(null, err) - assert.ok(ip, ip) - } - } else { - console.log('stun skipped') - } - done() - } - this.net_utils.get_public_ip(cb) - }) -}) - -describe('get_public_ip_async', function () { - beforeEach(() => { - this.net_utils = require('../index') - this.net_utils.config = this.net_utils.config.module_config( - path.resolve('test'), - ) - }) - - function has_stun() { - try { - require('stun') - } catch (e) { - return false - } - return true - } - - it('cached', async () => { - this.net_utils.public_ip = '1.1.1.1' - const ip = await this.net_utils.get_public_ip() - assert.equal('1.1.1.1', ip) - }) - - it('normal', async () => { - this.net_utils.public_ip = undefined - - if (!has_stun()) { - console.log('stun skipped') - return - } - - try { - const ip = await this.net_utils.get_public_ip() - console.log(`stun success: ${ip}`) - assert.ok(ip, ip) - } catch (e) { - console.error(e) - } - }) -}) - describe('octets_in_string', function () { it('c-24-18-98-14.hsd1.wa.comcast.net', function (done) { const str = 'c-24-18-98-14.hsd1.wa.comcast.net' @@ -469,629 +379,6 @@ describe('is_local_ipv6', function () { }) }) -const ip_fixtures = [ - [false, ' 2001:0000:1234:0000:0000:C1C0:ABCD:0876 '], - [false, ' 2001:0000:1234:0000:0000:C1C0:ABCD:0876 0'], - [false, ' 2001:0000:1234:0000:0000:C1C0:ABCD:0876'], - [false, ' 2001:0:1234::C1C0:ABCD:876 '], - [false, ' 2001:0:1234::C1C0:ABCD:876'], - [false, ''], - [false, "':10.0.0.1"], - [false, '---'], - [false, '02001:0000:1234:0000:0000:C1C0:ABCD:0876'], - [false, '1.2.3.4:1111:2222:3333:4444::5555'], - [false, '1.2.3.4:1111:2222:3333::5555'], - [false, '1.2.3.4:1111:2222::5555'], - [false, '1.2.3.4:1111::5555'], - [false, '1.2.3.4::'], - [false, '1.2.3.4::5555'], - [false, '1111'], - [false, '11112222:3333:4444:5555:6666:1.2.3.4'], - [false, '11112222:3333:4444:5555:6666:7777:8888'], - [false, '1111:'], - [false, '1111:1.2.3.4'], - [false, '1111:2222'], - [false, '1111:22223333:4444:5555:6666:1.2.3.4'], - [false, '1111:22223333:4444:5555:6666:7777:8888'], - [false, '1111:2222:'], - [false, '1111:2222:1.2.3.4'], - [false, '1111:2222:3333'], - [false, '1111:2222:33334444:5555:6666:1.2.3.4'], - [false, '1111:2222:33334444:5555:6666:7777:8888'], - [false, '1111:2222:3333:'], - [false, '1111:2222:3333:1.2.3.4'], - [false, '1111:2222:3333:4444'], - [false, '1111:2222:3333:44445555:6666:1.2.3.4'], - [false, '1111:2222:3333:44445555:6666:7777:8888'], - [false, '1111:2222:3333:4444:'], - [false, '1111:2222:3333:4444:1.2.3.4'], - [false, '1111:2222:3333:4444:5555'], - [false, '1111:2222:3333:4444:55556666:1.2.3.4'], - [false, '1111:2222:3333:4444:55556666:7777:8888'], - [false, '1111:2222:3333:4444:5555:'], - [false, '1111:2222:3333:4444:5555:1.2.3.4'], - [false, '1111:2222:3333:4444:5555:6666'], - [false, '1111:2222:3333:4444:5555:66661.2.3.4'], - [false, '1111:2222:3333:4444:5555:66667777:8888'], - [false, '1111:2222:3333:4444:5555:6666:'], - [false, '1111:2222:3333:4444:5555:6666:00.00.00.00'], - [false, '1111:2222:3333:4444:5555:6666:000.000.000.000'], - [false, '1111:2222:3333:4444:5555:6666:1.2.3.4.5'], - [false, '1111:2222:3333:4444:5555:6666:255.255.255255'], - [false, '1111:2222:3333:4444:5555:6666:255.255255.255'], - [false, '1111:2222:3333:4444:5555:6666:255255.255.255'], - [false, '1111:2222:3333:4444:5555:6666:256.256.256.256'], - [false, '1111:2222:3333:4444:5555:6666:7777'], - [false, '1111:2222:3333:4444:5555:6666:77778888'], - [false, '1111:2222:3333:4444:5555:6666:7777:'], - [false, '1111:2222:3333:4444:5555:6666:7777:1.2.3.4'], - [false, '1111:2222:3333:4444:5555:6666:7777:8888:'], - [false, '1111:2222:3333:4444:5555:6666:7777:8888:1.2.3.4'], - [false, '1111:2222:3333:4444:5555:6666:7777:8888:9999'], - [false, '1111:2222:3333:4444:5555:6666:7777:8888::'], - [false, '1111:2222:3333:4444:5555:6666:7777:::'], - [false, '1111:2222:3333:4444:5555:6666::1.2.3.4'], - [false, '1111:2222:3333:4444:5555:6666::8888:'], - [false, '1111:2222:3333:4444:5555:6666:::'], - [false, '1111:2222:3333:4444:5555:6666:::8888'], - [false, '1111:2222:3333:4444:5555::7777:8888:'], - [false, '1111:2222:3333:4444:5555::7777::'], - [false, '1111:2222:3333:4444:5555::8888:'], - [false, '1111:2222:3333:4444:5555:::'], - [false, '1111:2222:3333:4444:5555:::1.2.3.4'], - [false, '1111:2222:3333:4444:5555:::7777:8888'], - [false, '1111:2222:3333:4444::5555:'], - [false, '1111:2222:3333:4444::6666:7777:8888:'], - [false, '1111:2222:3333:4444::6666:7777::'], - [false, '1111:2222:3333:4444::6666::8888'], - [false, '1111:2222:3333:4444::7777:8888:'], - [false, '1111:2222:3333:4444::8888:'], - [false, '1111:2222:3333:4444:::'], - [false, '1111:2222:3333:4444:::6666:1.2.3.4'], - [false, '1111:2222:3333:4444:::6666:7777:8888'], - [false, '1111:2222:3333::5555:'], - [false, '1111:2222:3333::5555:6666:7777:8888:'], - [false, '1111:2222:3333::5555:6666:7777::'], - [false, '1111:2222:3333::5555:6666::8888'], - [false, '1111:2222:3333::5555::1.2.3.4'], - [false, '1111:2222:3333::5555::7777:8888'], - [false, '1111:2222:3333::6666:7777:8888:'], - [false, '1111:2222:3333::7777:8888:'], - [false, '1111:2222:3333::8888:'], - [false, '1111:2222:3333:::'], - [false, '1111:2222:3333:::5555:6666:1.2.3.4'], - [false, '1111:2222:3333:::5555:6666:7777:8888'], - [false, '1111:2222::4444:5555:6666:7777:8888:'], - [false, '1111:2222::4444:5555:6666:7777::'], - [false, '1111:2222::4444:5555:6666::8888'], - [false, '1111:2222::4444:5555::1.2.3.4'], - [false, '1111:2222::4444:5555::7777:8888'], - [false, '1111:2222::4444::6666:1.2.3.4'], - [false, '1111:2222::4444::6666:7777:8888'], - [false, '1111:2222::5555:'], - [false, '1111:2222::5555:6666:7777:8888:'], - [false, '1111:2222::6666:7777:8888:'], - [false, '1111:2222::7777:8888:'], - [false, '1111:2222::8888:'], - [false, '1111:2222:::'], - [false, '1111:2222:::4444:5555:6666:1.2.3.4'], - [false, '1111:2222:::4444:5555:6666:7777:8888'], - [false, '1111::3333:4444:5555:6666:7777:8888:'], - [false, '1111::3333:4444:5555:6666:7777::'], - [false, '1111::3333:4444:5555:6666::8888'], - [false, '1111::3333:4444:5555::1.2.3.4'], - [false, '1111::3333:4444:5555::7777:8888'], - [false, '1111::3333:4444::6666:1.2.3.4'], - [false, '1111::3333:4444::6666:7777:8888'], - [false, '1111::3333::5555:6666:1.2.3.4'], - [false, '1111::3333::5555:6666:7777:8888'], - [false, '1111::4444:5555:6666:7777:8888:'], - [false, '1111::5555:'], - [false, '1111::5555:6666:7777:8888:'], - [false, '1111::6666:7777:8888:'], - [false, '1111::7777:8888:'], - [false, '1111::8888:'], - [false, '1111:::'], - [false, '1111:::3333:4444:5555:6666:1.2.3.4'], - [false, '1111:::3333:4444:5555:6666:7777:8888'], - [false, '123'], - [false, '12345::6:7:8'], - [false, '192.168.0.256'], - [false, '192.168.256.0'], - [false, '1:2:3:4:5:6:7:8:9'], - [false, '1:2:3::4:5:6:7:8:9'], - [false, '1:2:3::4:5::7:8'], - [false, '1::1.2.256.4'], - [false, '1::1.2.3.256'], - [false, '1::1.2.3.300'], - [false, '1::1.2.3.900'], - [false, '1::1.2.300.4'], - [false, '1::1.2.900.4'], - [false, '1::1.256.3.4'], - [false, '1::1.300.3.4'], - [false, '1::1.900.3.4'], - [false, '1::256.2.3.4'], - [false, '1::260.2.3.4'], - [false, '1::2::3'], - [false, '1::300.2.3.4'], - [false, '1::300.300.300.300'], - [false, '1::3000.30.30.30'], - [false, '1::400.2.3.4'], - [false, '1::5:1.2.256.4'], - [false, '1::5:1.2.3.256'], - [false, '1::5:1.2.3.300'], - [false, '1::5:1.2.3.900'], - [false, '1::5:1.2.300.4'], - [false, '1::5:1.2.900.4'], - [false, '1::5:1.256.3.4'], - [false, '1::5:1.300.3.4'], - [false, '1::5:1.900.3.4'], - [false, '1::5:256.2.3.4'], - [false, '1::5:260.2.3.4'], - [false, '1::5:300.2.3.4'], - [false, '1::5:300.300.300.300'], - [false, '1::5:3000.30.30.30'], - [false, '1::5:400.2.3.4'], - [false, '1::5:900.2.3.4'], - [false, '1::900.2.3.4'], - [false, '1:::3:4:5'], - [false, '2001:0000:1234: 0000:0000:C1C0:ABCD:0876'], - [false, '2001:0000:1234:0000:00001:C1C0:ABCD:0876'], - [false, '2001:0000:1234:0000:0000:C1C0:ABCD:0876 0'], - [false, '2001:1:1:1:1:1:255Z255X255Y255'], - [false, '2001::FFD3::57ab'], - [false, '2001:DB8:0:0:8:800:200C:417A:221'], - [false, '2001:db8:85a3::8a2e:37023:7334'], - [false, '2001:db8:85a3::8a2e:370k:7334'], - [false, '255.256.255.255'], - [false, '256.255.255.255'], - [false, '3ffe:0b00:0000:0001:0000:0000:000a'], - [false, '3ffe:b00::1::a'], - [false, ':'], - [false, ':1.2.3.4'], - [false, ':1111:2222:3333:4444:5555:6666:1.2.3.4'], - [false, ':1111:2222:3333:4444:5555:6666:7777:8888'], - [false, ':1111:2222:3333:4444:5555:6666:7777::'], - [false, ':1111:2222:3333:4444:5555:6666::'], - [false, ':1111:2222:3333:4444:5555:6666::8888'], - [false, ':1111:2222:3333:4444:5555::'], - [false, ':1111:2222:3333:4444:5555::1.2.3.4'], - [false, ':1111:2222:3333:4444:5555::7777:8888'], - [false, ':1111:2222:3333:4444:5555::8888'], - [false, ':1111:2222:3333:4444::'], - [false, ':1111:2222:3333:4444::1.2.3.4'], - [false, ':1111:2222:3333:4444::5555'], - [false, ':1111:2222:3333:4444::6666:1.2.3.4'], - [false, ':1111:2222:3333:4444::6666:7777:8888'], - [false, ':1111:2222:3333:4444::7777:8888'], - [false, ':1111:2222:3333:4444::8888'], - [false, ':1111:2222:3333::'], - [false, ':1111:2222:3333::1.2.3.4'], - [false, ':1111:2222:3333::5555'], - [false, ':1111:2222:3333::5555:6666:1.2.3.4'], - [false, ':1111:2222:3333::5555:6666:7777:8888'], - [false, ':1111:2222:3333::6666:1.2.3.4'], - [false, ':1111:2222:3333::6666:7777:8888'], - [false, ':1111:2222:3333::7777:8888'], - [false, ':1111:2222:3333::8888'], - [false, ':1111:2222::'], - [false, ':1111:2222::1.2.3.4'], - [false, ':1111:2222::4444:5555:6666:1.2.3.4'], - [false, ':1111:2222::4444:5555:6666:7777:8888'], - [false, ':1111:2222::5555'], - [false, ':1111:2222::5555:6666:1.2.3.4'], - [false, ':1111:2222::5555:6666:7777:8888'], - [false, ':1111:2222::6666:1.2.3.4'], - [false, ':1111:2222::6666:7777:8888'], - [false, ':1111:2222::7777:8888'], - [false, ':1111:2222::8888'], - [false, ':1111::'], - [false, ':1111::1.2.3.4'], - [false, ':1111::3333:4444:5555:6666:1.2.3.4'], - [false, ':1111::3333:4444:5555:6666:7777:8888'], - [false, ':1111::4444:5555:6666:1.2.3.4'], - [false, ':1111::4444:5555:6666:7777:8888'], - [false, ':1111::5555'], - [false, ':1111::5555:6666:1.2.3.4'], - [false, ':1111::5555:6666:7777:8888'], - [false, ':1111::6666:1.2.3.4'], - [false, ':1111::6666:7777:8888'], - [false, ':1111::7777:8888'], - [false, ':1111::8888'], - [false, ':2222:3333:4444:5555:6666:1.2.3.4'], - [false, ':2222:3333:4444:5555:6666:7777:8888'], - [false, ':3333:4444:5555:6666:1.2.3.4'], - [false, ':3333:4444:5555:6666:7777:8888'], - [false, ':4444:5555:6666:1.2.3.4'], - [false, ':4444:5555:6666:7777:8888'], - [false, ':5555:6666:1.2.3.4'], - [false, ':5555:6666:7777:8888'], - [false, ':6666:1.2.3.4'], - [false, ':6666:7777:8888'], - [false, ':7777:8888'], - [false, ':8888'], - [false, '::.'], - [false, '::..'], - [false, '::...'], - [false, '::...4'], - [false, '::..3.'], - [false, '::..3.4'], - [false, '::.2..'], - [false, '::.2.3.'], - [false, '::.2.3.4'], - [false, '::1...'], - [false, '::1.2..'], - [false, '::1.2.256.4'], - [false, '::1.2.3.'], - [false, '::1.2.3.256'], - [false, '::1.2.3.300'], - [false, '::1.2.3.900'], - [false, '::1.2.300.4'], - [false, '::1.2.900.4'], - [false, '::1.256.3.4'], - [false, '::1.300.3.4'], - [false, '::1.900.3.4'], - [false, '::1111:2222:3333:4444:5555:6666::'], - [false, '::2222:3333:4444:5555:6666:7777:1.2.3.4'], - [false, '::2222:3333:4444:5555:6666:7777:8888:'], - [false, '::2222:3333:4444:5555:6666:7777:8888:9999'], - [false, '::2222:3333:4444:5555:7777:8888::'], - [false, '::2222:3333:4444:5555:7777::8888'], - [false, '::2222:3333:4444:5555::1.2.3.4'], - [false, '::2222:3333:4444:5555::7777:8888'], - [false, '::2222:3333:4444::6666:1.2.3.4'], - [false, '::2222:3333:4444::6666:7777:8888'], - [false, '::2222:3333::5555:6666:1.2.3.4'], - [false, '::2222:3333::5555:6666:7777:8888'], - [false, '::2222::4444:5555:6666:1.2.3.4'], - [false, '::2222::4444:5555:6666:7777:8888'], - [false, '::256.2.3.4'], - [false, '::260.2.3.4'], - [false, '::300.2.3.4'], - [false, '::300.300.300.300'], - [false, '::3000.30.30.30'], - [false, '::3333:4444:5555:6666:7777:8888:'], - [false, '::400.2.3.4'], - [false, '::4444:5555:6666:7777:8888:'], - [false, '::5555:'], - [false, '::5555:6666:7777:8888:'], - [false, '::6666:7777:8888:'], - [false, '::7777:8888:'], - [false, '::8888:'], - [false, '::900.2.3.4'], - [false, ':::'], - [false, ':::1.2.3.4'], - [false, ':::2222:3333:4444:5555:6666:1.2.3.4'], - [false, ':::2222:3333:4444:5555:6666:7777:8888'], - [false, ':::3333:4444:5555:6666:7777:8888'], - [false, ':::4444:5555:6666:1.2.3.4'], - [false, ':::4444:5555:6666:7777:8888'], - [false, ':::5555'], - [false, ':::5555:6666:1.2.3.4'], - [false, ':::5555:6666:7777:8888'], - [false, ':::6666:1.2.3.4'], - [false, ':::6666:7777:8888'], - [false, ':::7777:8888'], - [false, ':::8888'], - [false, '::ffff:192x168.1.26'], - [false, '::ffff:2.3.4'], - [false, '::ffff:257.1.2.3'], - [false, 'FF01::101::2'], - [false, 'FF02:0000:0000:0000:0000:0000:0000:0000:0001'], - [false, 'XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:1.2.3.4'], - [false, 'XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX'], - [false, 'fe80:0000:0000:0000:0204:61ff:254.157.241.086'], - [false, 'fe80::4413:c8ae:2821:5852%10'], - [false, 'ldkfj'], - [false, 'mydomain.com'], - [false, 'test.mydomain.com'], - [true, '0000:0000:0000:0000:0000:0000:0000:0000'], - [true, '0000:0000:0000:0000:0000:0000:0000:0001'], - [true, '0:0:0:0:0:0:0:0'], - [true, '0:0:0:0:0:0:0:1'], - [true, '0:0:0:0:0:0:0::'], - [true, '0:0:0:0:0:0:13.1.68.3'], - [true, '0:0:0:0:0:0::'], - [true, '0:0:0:0:0::'], - [true, '0:0:0:0:0:FFFF:129.144.52.38'], - [true, '0:0:0:0::'], - [true, '0:0:0::'], - [true, '0:0::'], - [true, '0::'], - [true, '0:a:b:c:d:e:f::'], - [true, '1.2.3.4'], - [true, '1111:2222:3333:4444:5555:6666:123.123.123.123'], - [true, '1111:2222:3333:4444:5555:6666:7777:8888'], - [true, '1111:2222:3333:4444:5555:6666:7777::'], - [true, '1111:2222:3333:4444:5555:6666::'], - [true, '1111:2222:3333:4444:5555:6666::8888'], - [true, '1111:2222:3333:4444:5555::'], - [true, '1111:2222:3333:4444:5555::123.123.123.123'], - [true, '1111:2222:3333:4444:5555::7777:8888'], - [true, '1111:2222:3333:4444:5555::8888'], - [true, '1111:2222:3333:4444::'], - [true, '1111:2222:3333:4444::123.123.123.123'], - [true, '1111:2222:3333:4444::6666:123.123.123.123'], - [true, '1111:2222:3333:4444::6666:7777:8888'], - [true, '1111:2222:3333:4444::7777:8888'], - [true, '1111:2222:3333:4444::8888'], - [true, '1111:2222:3333::'], - [true, '1111:2222:3333::123.123.123.123'], - [true, '1111:2222:3333::5555:6666:123.123.123.123'], - [true, '1111:2222:3333::5555:6666:7777:8888'], - [true, '1111:2222:3333::6666:123.123.123.123'], - [true, '1111:2222:3333::6666:7777:8888'], - [true, '1111:2222:3333::7777:8888'], - [true, '1111:2222:3333::8888'], - [true, '1111:2222::'], - [true, '1111:2222::123.123.123.123'], - [true, '1111:2222::4444:5555:6666:123.123.123.123'], - [true, '1111:2222::4444:5555:6666:7777:8888'], - [true, '1111:2222::5555:6666:123.123.123.123'], - [true, '1111:2222::5555:6666:7777:8888'], - [true, '1111:2222::6666:123.123.123.123'], - [true, '1111:2222::6666:7777:8888'], - [true, '1111:2222::7777:8888'], - [true, '1111:2222::8888'], - [true, '1111::'], - [true, '1111::123.123.123.123'], - [true, '1111::3333:4444:5555:6666:123.123.123.123'], - [true, '1111::3333:4444:5555:6666:7777:8888'], - [true, '1111::4444:5555:6666:123.123.123.123'], - [true, '1111::4444:5555:6666:7777:8888'], - [true, '1111::5555:6666:123.123.123.123'], - [true, '1111::5555:6666:7777:8888'], - [true, '1111::6666:123.123.123.123'], - [true, '1111::6666:7777:8888'], - [true, '1111::7777:8888'], - [true, '1111::8888'], - [true, '123.23.34.2'], - [true, '172.26.168.134'], - [true, '192.168.0.0'], - [true, '192.168.128.255'], - [true, '1:2:3:4:5:6:1.2.3.4'], - [true, '1:2:3:4:5:6:7:8'], - [true, '1:2:3:4:5:6::'], - [true, '1:2:3:4:5:6::8'], - [true, '1:2:3:4:5::'], - [true, '1:2:3:4:5::1.2.3.4'], - [true, '1:2:3:4:5::7:8'], - [true, '1:2:3:4:5::8'], - [true, '1:2:3:4::'], - [true, '1:2:3:4::1.2.3.4'], - [true, '1:2:3:4::5:1.2.3.4'], - [true, '1:2:3:4::7:8'], - [true, '1:2:3:4::8'], - [true, '1:2:3::'], - [true, '1:2:3::1.2.3.4'], - [true, '1:2:3::5:1.2.3.4'], - [true, '1:2:3::7:8'], - [true, '1:2:3::8'], - [true, '1:2::'], - [true, '1:2::1.2.3.4'], - [true, '1:2::5:1.2.3.4'], - [true, '1:2::7:8'], - [true, '1:2::8'], - [true, '1::'], - [true, '1::1.2.3.4'], - [true, '1::2:3'], - [true, '1::2:3:4'], - [true, '1::2:3:4:5'], - [true, '1::2:3:4:5:6'], - [true, '1::2:3:4:5:6:7'], - [true, '1::5:1.2.3.4'], - [true, '1::5:11.22.33.44'], - [true, '1::7:8'], - [true, '1::8'], - [true, '2001:0000:1234:0000:0000:C1C0:ABCD:0876'], - [true, '2001:0:1234::C1C0:ABCD:876'], - [true, '2001:0db8:0000:0000:0000:0000:1428:57ab'], - [true, '2001:0db8:0000:0000:0000::1428:57ab'], - [true, '2001:0db8:0:0:0:0:1428:57ab'], - [true, '2001:0db8:0:0::1428:57ab'], - [true, '2001:0db8:1234:0000:0000:0000:0000:0000'], - [true, '2001:0db8:1234::'], - [true, '2001:0db8:1234:ffff:ffff:ffff:ffff:ffff'], - [true, '2001:0db8:85a3:0000:0000:8a2e:0370:7334'], - [true, '2001:0db8::1428:57ab'], - [true, '2001:2:3:4:5:6:7:134'], - [true, '2001:DB8:0:0:8:800:200C:417A'], - [true, '2001:DB8::8:800:200C:417A'], - [true, '2001:db8:85a3:0:0:8a2e:370:7334'], - [true, '2001:db8:85a3::8a2e:370:7334'], - [true, '2001:db8::'], - [true, '2001:db8::1428:57ab'], - [true, '2001:db8:a::123'], - [true, '2002::'], - [true, '2::10'], - [true, '3ffe:0b00:0000:0000:0001:0000:0000:000a'], - [true, '3ffe:b00::1:0:0:a'], - [true, '::'], - [true, '::0'], - [true, '::0:0'], - [true, '::0:0:0'], - [true, '::0:0:0:0'], - [true, '::0:0:0:0:0'], - [true, '::0:0:0:0:0:0'], - [true, '::0:0:0:0:0:0:0'], - [true, '::0:a:b:c:d:e:f'], - [true, '::1'], - [true, '::123.123.123.123'], - [true, '::13.1.68.3'], - [true, '::2222:3333:4444:5555:6666:123.123.123.123'], - [true, '::2222:3333:4444:5555:6666:7777:8888'], - [true, '::2:3'], - [true, '::2:3:4'], - [true, '::2:3:4:5'], - [true, '::2:3:4:5:6'], - [true, '::2:3:4:5:6:7'], - [true, '::2:3:4:5:6:7:8'], - [true, '::3333:4444:5555:6666:7777:8888'], - [true, '::4444:5555:6666:123.123.123.123'], - [true, '::4444:5555:6666:7777:8888'], - [true, '::5555:6666:123.123.123.123'], - [true, '::5555:6666:7777:8888'], - [true, '::6666:123.123.123.123'], - [true, '::6666:7777:8888'], - [true, '::7777:8888'], - [true, '::8'], - [true, '::8888'], - [true, '::FFFF:129.144.52.38'], - [true, '::ffff:0:0'], - [true, '::ffff:0c22:384e'], - [true, '::ffff:12.34.56.78'], - [true, '::ffff:192.0.2.128'], - [true, '::ffff:192.168.1.1'], - [true, '::ffff:192.168.1.26'], - [true, '::ffff:c000:280'], - [true, 'FF01:0:0:0:0:0:0:101'], - [true, 'FF01::101'], - [true, 'FF02:0000:0000:0000:0000:0000:0000:0001'], - [true, 'FF02::1'], - [true, 'a:b:c:d:e:f:0::'], - [true, 'fe80:0000:0000:0000:0204:61ff:fe9d:f156'], - [true, 'fe80:0:0:0:204:61ff:254.157.241.86'], - [true, 'fe80:0:0:0:204:61ff:fe9d:f156'], - [true, 'fe80::'], - [true, 'fe80::1'], - [true, 'fe80::204:61ff:254.157.241.86'], - [true, 'fe80::204:61ff:fe9d:f156'], - [true, 'fe80::217:f2ff:254.7.237.98'], - [true, 'fe80::217:f2ff:fe07:ed62'], - [true, 'ff02::1'], -] - -describe('get_ipany_re', function () { - it('IPv6, Prefix', function (done) { - // for x-*-ip headers - // it must fail as of not valide - assert.ok(!net.isIPv6('IPv6:2001:db8:85a3::8a2e:370:7334')) - // must okay! - assert.ok(net.isIPv6('2001:db8:85a3::8a2e:370:7334')) - done() - }) - - it('IP fixtures check', function (done) { - for (const i in ip_fixtures) { - const match = net_utils.get_ipany_re('^', '$').test(ip_fixtures[i][1]) - // console.log('IP:', `'${ip_fixtures[i][1]}'` , 'Expected:', ip_fixtures[i][0] , 'Match:' , match); - assert.ok( - match === ip_fixtures[i][0], - `${ip_fixtures[i][1]} - Expected: ${ip_fixtures[i][0]} - Match: ${match}`, - ) - } - done() - }) - - it('IPv4, bare', function (done) { - // for x-*-ip headers - const match = net_utils.get_ipany_re().exec('127.0.0.1') - assert.equal(match[1], '127.0.0.1') - assert.equal(match.length, 2) - done() - }) - - it('IPv4, Received header, parens', function (done) { - const received_re = net_utils.get_ipany_re( - '^Received:.*?[\\[\\(]', - '[\\]\\)]', - ) - const match = received_re.exec( - 'Received: from unknown (HELO mail.theartfarm.com) (127.0.0.30) by mail.theartfarm.com with SMTP; 5 Sep 2015 14:29:00 -0000', - ) - assert.equal(match[1], '127.0.0.30') - assert.equal(match.length, 2) - done() - }) - - it('IPv4, Received header, bracketed, expedia', function (done) { - const received_header = - 'Received: from mta2.expediamail.com (mta2.expediamail.com [66.231.89.19]) by mail.theartfarm.com (Haraka/2.6.2-toaster) with ESMTPS id C669CF18-1C1C-484C-8A5B-A89088B048CB.1 envelope-from (version=TLSv1/SSLv3 cipher=AES256-SHA verify=NO); Sat, 05 Sep 2015 07:28:57 -0700' - const received_re = net_utils.get_ipany_re( - '^Received:.*?[\\[\\(]', - '[\\]\\)]', - ) - const match = received_re.exec(received_header) - assert.equal(match[1], '66.231.89.19') - assert.equal(match.length, 2) - done() - }) - - it('IPv4, Received header, bracketed, github', function (done) { - const received_re = net_utils.get_ipany_re( - '^Received:.*?[\\[\\(]', - '[\\]\\)]', - ) - const match = received_re.exec( - 'Received: from github-smtp2a-ext-cp1-prd.iad.github.net (github-smtp2-ext5.iad.github.net [192.30.252.196])', - ) - assert.equal(match[1], '192.30.252.196') - assert.equal(match.length, 2) - done() - }) - - it('IPv6, Received header, bracketed', function (done) { - const received_header = - 'Received: from ?IPv6:2601:184:c001:5cf7:a53f:baf7:aaf3:bce7? ([2601:184:c001:5cf7:a53f:baf7:aaf3:bce7])' - const received_re = net_utils.get_ipany_re( - '^Received:.*?[\\[\\(]', - '[\\]\\)]', - ) - const match = received_re.exec(received_header) - assert.equal(match[1], '2601:184:c001:5cf7:a53f:baf7:aaf3:bce7') - assert.equal(match.length, 2) - done() - }) - - it('IPv6, Received header, bracketed, IPv6 prefix', function (done) { - const received_re = net_utils.get_ipany_re( - '^Received:.*?[\\[\\(](?:IPv6:)?', - '[\\]\\)]', - ) - const match = received_re.exec( - 'Received: from hub.freebsd.org (hub.freebsd.org [IPv6:2001:1900:2254:206c::16:88])', - ) - assert.equal(match[1], '2001:1900:2254:206c::16:88') - assert.equal(match.length, 2) - done() - }) - - it('IPv6, folded Received header, bracketed, IPv6 prefix', function (done) { - // note the use of [\s\S], '.' doesn't match newlines in JS regexp - const received_re = net_utils.get_ipany_re( - '^Received:[\\s\\S]*?[\\[\\(](?:IPv6:)?', - '[\\]\\)]', - ) - const match = received_re.exec( - 'Received: from freefall.freebsd.org (freefall.freebsd.org\r\n [IPv6:2001:1900:2254:206c::16:87])', - ) - if (match) { - assert.equal(match[1], '2001:1900:2254:206c::16:87') - assert.equal(match.length, 2) - } - done() - }) - - it('IPv6, Received header, bracketed, IPv6 prefix, localhost compressed', function (done) { - const received_re = net_utils.get_ipany_re( - '^Received:.*?[\\[\\(](?:IPv6:)?', - '[\\]\\)]', - ) - const match = received_re.exec( - 'Received: from ietfa.amsl.com (localhost [IPv6:::1])', - ) - assert.equal(match[1], '::1') - assert.equal(match.length, 2) - done() - }) - - it('IPv6 bogus', function (done) { - const is_bogus = net_utils.ipv6_bogus('::192.41.13.251') // From https://github.com/haraka/Haraka/issues/2763 - assert.equal(is_bogus, true) - done() - }) -}) - describe('get_ips_by_host', function () { const tests = { 'servedby.tnpi.net': [ @@ -1246,379 +533,3 @@ describe('on_local_interface', function () { done() }) }) - -describe('get_mx', function () { - this.timeout(12000) - - beforeEach(function (done) { - this.net_utils = require('../index') - done() - }) - - const validCases = { - 'tnpi.net': 'mail.theartfarm.com', - 'matt@tnpi.net': 'mail.theartfarm.com', - 'matt.simerson@gmail.com': /google.com/, - 'example.com': '', - 'no-mx.haraka.tnpi.net': '192.0.99.5', - 'bad-mx.haraka.tnpi.net': /99/, - 'über.haraka.tnpi.net': 'no-mx.haraka.tnpi.net', - } - - function checkValid(c, mxlist) { - try { - if ('string' === typeof c) { - assert.equal(mxlist[0].exchange, c) - } else { - assert.ok(c.test(mxlist[0].exchange)) - } - } catch (err) { - console.error(err) - } - } - - for (const c in validCases) { - it(`gets MX records for ${c}`, function (done) { - this.timeout(12000) - this.net_utils.get_mx(c, (err, mxlist) => { - if (err) console.error(err) - assert.ifError(err) - checkValid(validCases[c], mxlist) - done() - }) - }) - - it(`awaits MX records for ${c}`, async function () { - this.timeout(12000) - const mxlist = await this.net_utils.get_mx(c) - checkValid(validCases[c], mxlist) - }) - } - - // macOS: ENODATA, win: ENOTOUND, ubuntu: ESERVFAIL - const invalidCases = { - invalid: /queryMx (ENODATA|ENOTFOUND|ESERVFAIL) invalid/, - 'gmail.xn--com-0da': /(ENOTFOUND|Cannot convert name to ASCII)/, - 'non-exist.haraka.tnpi.net': /ignore/, - 'haraka.non-exist': /ignore/, - } - - function checkInvalid(expected, actual) { - if ('string' === typeof expected) { - assert.strictEqual(actual, expected) - } else { - assert.equal(expected.test(actual), true) - } - } - - for (const c in invalidCases) { - it(`cb does not crash on invalid name: ${c}`, function () { - this.net_utils.get_mx(c, (err, mxlist) => { - if (err) checkInvalid(invalidCases[c], err.message) - assert.equal(mxlist.length, 0) - }) - }) - - it(`async does not crash on invalid name: ${c}`, async function () { - try { - const mxlist = await this.net_utils.get_mx(c) - assert.equal(mxlist.length, 0) - } catch (err) { - checkInvalid(invalidCases[c], err.message) - } - }) - } -}) - -describe('resolve_mx_hosts', function () { - this.timeout(12000) - - beforeEach((done) => { - this.net_utils = require('../index') - done() - }) - - const expectedResolvedMx = [ - { - exchange: '2605:ae00:329::14', - priority: 10, - from_dns: 'mail.theartfarm.com', - }, - { - exchange: '66.128.51.165', - priority: 10, - from_dns: 'mail.theartfarm.com', - }, - ] - - it('resolves mx hosts to IPs, tnpi.net', async () => { - const r = await this.net_utils.resolve_mx_hosts([ - { exchange: 'mail.theartfarm.com', priority: 10, from_dns: 'tnpi.net' }, - ]) - assert.deepEqual(r, expectedResolvedMx) - }) - - it('resolves mx hosts to IPs, gmail.com', async () => { - const mxes = await this.net_utils.get_mx('gmail.com') - assert.equal(mxes.length, 5) - const r = await this.net_utils.resolve_mx_hosts(mxes) - assert.equal(r.length, 10) - }) - - it('returns IPs as is', async () => { - const r = await this.net_utils.resolve_mx_hosts(expectedResolvedMx) - assert.deepEqual(r, expectedResolvedMx) - }) - - it('returns sockets as-is', async () => { - const r = await this.net_utils.resolve_mx_hosts([{ path: '/var/run/sock' }]) - assert.deepEqual(r, [{ path: '/var/run/sock' }]) - }) - - it('resolve_mx_hosts, gmail.com', async () => { - const mxes = await this.net_utils.get_mx('gmail.com') - const r = await this.net_utils.resolve_mx_hosts(mxes) - assert.equal(r.length, 10) - }) - - it('resolve_mx_hosts, yahoo.com', async () => { - const mxes = await this.net_utils.get_mx('yahoo.com') - const r = await this.net_utils.resolve_mx_hosts([mxes[0]]) - assert.equal(r.length, 8) - }) -}) - -describe('get_implicit_mx', function () { - this.timeout(5000) - - beforeEach(function (done) { - this.net_utils = require('../index') - done() - }) - - it('harakamail.com', async function () { - const mf = await this.net_utils.get_implicit_mx('harakamail.com') - assert.equal(mf.length, 1) - }) - - it('mx.theartfarm.com', async function () { - const mf = await this.net_utils.get_implicit_mx('mx.theartfarm.com') - assert.equal(mf.length, 0) - }) - - it('resolve-fail-definitive.josef-froehle.de', async function () { - const mf = await this.net_utils.get_implicit_mx( - 'resolve-fail-definitive.josef-froehle.de', - ) - assert.equal(mf.length, 0) - }) - it('resolve-fail-a.josef-froehle.de', async function () { - const mf = await this.net_utils.get_implicit_mx( - 'resolve-fail-a.josef-froehle.de', - ) - assert.equal(mf.length, 1) - }) - it('resolve-fail-aaaa.josef-froehle.de', async function () { - const mf = await this.net_utils.get_implicit_mx( - 'resolve-fail-aaaa.josef-froehle.de', - ) - assert.equal(mf.length, 0) - }) -}) - -describe('HarakaMx', () => { - beforeEach(function (done) { - this.nu = require('../index') - done() - }) - - describe('fromObject', () => { - it('accepts an object', function () { - assert.deepEqual( - new this.nu.HarakaMx({ - from_dns: 'example.com', - exchange: '.', - priority: 0, - }), - { from_dns: 'example.com', exchange: '.', priority: 0 }, - ) - }) - - it('sets default priority to 0', function () { - assert.deepEqual(new this.nu.HarakaMx({ exchange: '.' }), { - exchange: '.', - priority: 0, - }) - }) - - it('if optional domain provided, sets from_dns', function () { - assert.deepEqual(new this.nu.HarakaMx({ exchange: '.' }, 'example.com'), { - from_dns: 'example.com', - exchange: '.', - priority: 0, - }) - }) - }) - - describe('fromString', function () { - it('parses a hostname', function () { - assert.deepEqual(new this.nu.HarakaMx('mail.example.com'), { - exchange: 'mail.example.com', - priority: 0, - }) - }) - - it('parses a hostname:port', function () { - assert.deepEqual(new this.nu.HarakaMx('mail.example.com:25'), { - exchange: 'mail.example.com', - port: 25, - priority: 0, - }) - }) - - it('parses an IPv4', function () { - assert.deepEqual(new this.nu.HarakaMx('192.0.2.1'), { - exchange: '192.0.2.1', - priority: 0, - }) - }) - - it('parses an IPv4:port', function () { - assert.deepEqual(new this.nu.HarakaMx('192.0.2.1:25'), { - exchange: '192.0.2.1', - port: 25, - priority: 0, - }) - }) - - it('parses an IPv6', function () { - assert.deepEqual(new this.nu.HarakaMx('2001:db8::1'), { - exchange: '2001:db8::1', - priority: 0, - }) - }) - - it('parses an IPv6:port', function () { - assert.deepEqual(new this.nu.HarakaMx('2001:db8::1:25'), { - exchange: '2001:db8::1', - port: 25, - priority: 0, - }) - }) - - it('parses an [IPv6]:port', function () { - assert.deepEqual(new this.nu.HarakaMx('[2001:db8::1]:25'), { - exchange: '2001:db8::1', - port: 25, - priority: 0, - }) - }) - }) - - describe('fromUri', function () { - it('parses simple URIs', function () { - assert.deepEqual(new this.nu.HarakaMx('smtp://192.0.2.2'), { - exchange: '192.0.2.2', - port: 25, - priority: 0, - }) - - assert.deepEqual(new this.nu.HarakaMx('smtp://[2001:db8::1]:25'), { - exchange: '[2001:db8::1]', - port: 25, - priority: 0, - }) - }) - - it('parses more complex URIs', function () { - assert.deepEqual( - new this.nu.HarakaMx('smtp://authUser:sekretPass@[2001:db8::1]'), - { - exchange: '[2001:db8::1]', - port: 25, - priority: 0, - auth_pass: 'sekretPass', - auth_user: 'authUser', - }, - ) - - assert.deepEqual( - new this.nu.HarakaMx('lmtp://authUser:sekretPass@[2001:db8::1]:25'), - { - exchange: '[2001:db8::1]', - port: 25, - priority: 0, - using_lmtp: true, - auth_pass: 'sekretPass', - auth_user: 'authUser', - }, - ) - }) - }) - - describe('toUrl', function () { - it('has a reasonable toUrl()', function () { - assert.equal( - new this.nu.HarakaMx({ exchange: '.' }).toUrl(), - 'smtp://.:25', - ) - - assert.equal( - new this.nu.HarakaMx({ - from_dns: 'example.com', - exchange: '.', - priority: 10, - }).toUrl(), - 'smtp://.:25', - ) - - assert.equal( - new this.nu.HarakaMx('smtp://au:ap@192.0.2.3:25').toUrl(), - 'smtp://au:****@192.0.2.3:25', - ) - - assert.equal( - new this.nu.HarakaMx('smtp://au:ap@192.0.2.3:465').toUrl(), - 'smtp://au:****@192.0.2.3:465', - ) - - assert.equal( - new this.nu.HarakaMx('smtp://[2001:db8::1]:25').toUrl(), - 'smtp://[2001:db8::1]:25', - ) - }) - }) - - describe('toString', function () { - it('has a reasonable toString()', function () { - assert.equal( - new this.nu.HarakaMx({ exchange: '.' }).toString(), - 'MX 0 smtp://.:25', - ) - - assert.equal( - new this.nu.HarakaMx({ - from_dns: 'example.com', - exchange: '.', - priority: 10, - }).toString(), - 'MX 10 smtp://.:25 (from example.com)', - ) - - assert.equal( - new this.nu.HarakaMx('smtp://au:ap@192.0.2.3:25').toString(), - 'MX 0 smtp://au:****@192.0.2.3:25', - ) - - assert.equal( - new this.nu.HarakaMx('smtp://au:ap@192.0.2.3:465').toString(), - 'MX 0 smtp://au:****@192.0.2.3:465', - ) - - assert.equal( - new this.nu.HarakaMx('smtp://[2001:db8::1]:25').toString(), - 'MX 0 smtp://[2001:db8::1]:25', - ) - }) - }) -})