|
| 1 | +/* Copyright (C) 2016 NooBaa */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const os = require('os'); |
| 5 | +const net_utils = require('../../../util/net_utils'); |
| 6 | + |
| 7 | +describe('IP Utils', () => { |
| 8 | + it('is_ip should correctly identify IP addresses', () => { |
| 9 | + expect(net_utils.is_ip('192.168.1.1')).toBe(true); |
| 10 | + expect(net_utils.is_ip('::1')).toBe(true); |
| 11 | + expect(net_utils.is_ip('not_an_ip')).toBe(false); |
| 12 | + expect(net_utils.is_ip('256.256.256.256')).toBe(false); |
| 13 | + }); |
| 14 | + |
| 15 | + it('is_fqdn should correctly identify FQDNs', () => { |
| 16 | + expect(net_utils.is_fqdn('example.com')).toBe(true); |
| 17 | + expect(net_utils.is_fqdn('sub.example.com')).toBe(true); |
| 18 | + expect(net_utils.is_fqdn('localhost')).toBe(false); |
| 19 | + expect(net_utils.is_fqdn('invalid_domain-.com')).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it('is_cidr should correctly identify CIDR notation', () => { |
| 23 | + expect(net_utils.is_cidr('192.168.1.0/24')).toBe(true); |
| 24 | + expect(net_utils.is_cidr('10.0.0.0/8')).toBe(true); |
| 25 | + expect(net_utils.is_cidr('2001:db8::/32')).toBe(true); |
| 26 | + expect(net_utils.is_cidr('192.168.1.300/24')).toBe(false); |
| 27 | + expect(net_utils.is_cidr('invalid_cidr')).toBe(false); |
| 28 | + expect(net_utils.is_cidr('192.168.1.1')).toBe(false); |
| 29 | + expect(net_utils.is_cidr('282.150.0.0/12')).toBe(false); |
| 30 | + expect(net_utils.is_cidr('192.168.0.0/35')).toBe(false); |
| 31 | + }); |
| 32 | + |
| 33 | + it('is_localhost should correctly identify localhost addresses', () => { |
| 34 | + expect(net_utils.is_localhost('127.0.0.1')).toBe(true); |
| 35 | + expect(net_utils.is_localhost('::1')).toBe(true); |
| 36 | + expect(net_utils.is_localhost('localhost')).toBe(true); |
| 37 | + expect(net_utils.is_localhost('192.168.1.1')).toBe(false); |
| 38 | + }); |
| 39 | + |
| 40 | + it('unwrap_ipv6 should remove IPv6 prefix', () => { |
| 41 | + expect(net_utils.unwrap_ipv6('::ffff:192.168.1.1')).toBe('192.168.1.1'); |
| 42 | + expect(net_utils.unwrap_ipv6('::1')).toBe('::1'); |
| 43 | + expect(net_utils.unwrap_ipv6('2001:db8::ff00:42:8329')).toBe('2001:db8::ff00:42:8329'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('ip_toLong should convert IPv4 to long', () => { |
| 47 | + expect(net_utils.ip_toLong('192.168.1.1')).toBe(3232235777); |
| 48 | + expect(net_utils.ip_toLong('0.0.0.0')).toBe(0); |
| 49 | + expect(net_utils.ip_toLong('255.255.255.255')).toBe(4294967295); |
| 50 | + }); |
| 51 | + |
| 52 | + it('ip_to_long should handle both IPv4 and IPv6-mapped IPv4', () => { |
| 53 | + expect(net_utils.ip_to_long('192.168.1.1')).toBe(3232235777); |
| 54 | + expect(net_utils.ip_to_long('::ffff:192.168.1.1')).toBe(3232235777); |
| 55 | + }); |
| 56 | + |
| 57 | + it('find_ifc_containing_address should find interface containing the address', () => { |
| 58 | + const networkInterfacesMock = { |
| 59 | + eth0: [ |
| 60 | + { family: 'IPv4', cidr: '192.168.1.0/24', address: '192.168.1.2' }, |
| 61 | + { family: 'IPv6', cidr: 'fe80::/64', address: 'fe80::1' }, |
| 62 | + ], |
| 63 | + lo: [{ family: 'IPv4', cidr: '127.0.0.0/8', address: '127.0.0.1' }], |
| 64 | + }; |
| 65 | + jest.spyOn(os, 'networkInterfaces').mockReturnValue(networkInterfacesMock); |
| 66 | + |
| 67 | + expect(net_utils.find_ifc_containing_address('192.168.1.5')).toEqual({ ifc: 'eth0', info: networkInterfacesMock.eth0[0] }); |
| 68 | + expect(net_utils.find_ifc_containing_address('127.0.0.1')).toEqual({ ifc: 'lo', info: networkInterfacesMock.lo[0] }); |
| 69 | + expect(net_utils.find_ifc_containing_address('8.8.8.8')).toBeUndefined(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('ip_toString should convert buffers to IP strings', () => { |
| 73 | + expect(net_utils.ip_toString(Buffer.from([192, 168, 1, 1]), 0, 4)).toBe('192.168.1.1'); |
| 74 | + expect(net_utils.ip_toString(Buffer.from([0, 0, 0, 0]), 0, 4)).toBe('0.0.0.0'); |
| 75 | + expect(net_utils.ip_toString(Buffer.from([255, 255, 255, 255]), 0, 4)).toBe('255.255.255.255'); |
| 76 | + expect(net_utils.ip_toString(Buffer.from([32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), 0, 16)).toBe('2001:db8::1'); |
| 77 | + expect(() => net_utils.ip_toString(Buffer.from([192, 168, 1, 1]), undefined, 4)).toThrow('Offset is required'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('ipv4_to_buffer should convert IPv4 string to buffer', () => { |
| 81 | + const buff = Buffer.alloc(4); |
| 82 | + expect(net_utils.ipv4_to_buffer('192.168.1.1', buff, 0)).toEqual(Buffer.from([192, 168, 1, 1])); |
| 83 | + }); |
| 84 | + |
| 85 | + it('ipv6_to_buffer should convert expanded IPv6 string to buffer', () => { |
| 86 | + const buff = Buffer.alloc(16); |
| 87 | + expect(net_utils.ipv6_to_buffer('2001:0db8:0000:0000:0000:0000:0000:0001', buff, 0)).toEqual( |
| 88 | + Buffer.from([32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('expend_ipv6 should expand IPv6 shorthand notation', () => { |
| 93 | + expect(net_utils.expend_ipv6('::')).toEqual(['0', '0', '0', '0', '0', '0', '0', '0']); |
| 94 | + expect(net_utils.expend_ipv6('2001:db8::1')).toEqual(['2001', 'db8', '0', '0', '0', '0', '0', '1']); |
| 95 | + expect(net_utils.expend_ipv6('2001:db8::ff00:42:8329')).toEqual(['2001', 'db8', '0', '0', '0', 'ff00', '42', '8329']); |
| 96 | + expect(net_utils.expend_ipv6('::1')).toEqual(['0', '0', '0', '0', '0', '0', '0', '1']); |
| 97 | + expect(net_utils.expend_ipv6('2001:0db8:85a3::8a2e:370:7334')).toEqual(['2001', '0db8', '85a3', '0', '0', '8a2e', '370', '7334']); |
| 98 | + expect(net_utils.expend_ipv6('::ffff:192.168.1.1')).toEqual(['0', '0', '0', '0', '0', 'ffff', 'c0a8', '0101']); |
| 99 | + }); |
| 100 | + |
| 101 | + it('ip_toBuffer should convert IP strings to buffer', () => { |
| 102 | + expect(net_utils.ip_toBuffer('10.0.0.1', Buffer.alloc(4), 0)).toEqual(Buffer.from([10, 0, 0, 1])); |
| 103 | + expect(net_utils.ip_toBuffer('2001:db8::1', Buffer.alloc(16), 0)).toEqual( |
| 104 | + Buffer.from([32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) |
| 105 | + ); |
| 106 | + expect(() => net_utils.ip_toBuffer('invalid_ip', Buffer.alloc(16), 0)).toThrow('Invalid IP address: invalid_ip'); |
| 107 | + expect(() => net_utils.ip_toBuffer('10.0.0.1')).toThrow('Offset is required'); |
| 108 | + }); |
| 109 | +}); |
0 commit comments