-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.test.ts
113 lines (91 loc) · 3.73 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { getIPRange } from './index';
const successResponsev4 = [
'192.168.1.128',
'192.168.1.129',
'192.168.1.130',
'192.168.1.131',
'192.168.1.132',
'192.168.1.133',
'192.168.1.134',
'192.168.1.135',
];
const successResponsev6 = [
'::ffff:102:304',
'::ffff:102:305',
'::ffff:102:306',
'::ffff:102:307',
]
describe('convert', () => {
describe('for cidr notation', () => {
it('should return an error if the IP address supplied is invalid', () => {
const fn = () => getIPRange('abc');
expect(fn).toThrow();
});
it('should return an error if the IP address is not in CIDR notation', () => {
const fn = () => getIPRange('10.1.128.0');
expect(fn).toThrow();
});
it('should return an error if the IP address uses numbers which are too high', () => {
const fn = () => getIPRange('192.168.1.134/256');
expect(fn).toThrow();
});
it('should return an array of IP addresses within the specified range', () => {
expect(getIPRange('192.168.1.134/29')).toEqual(successResponsev4);
});
it('should support IPv6', () => {
expect(getIPRange('0:0:0:0:0:ffff:102:304/126')).toEqual(successResponsev6);
});
});
});
describe('for two IP addresses', () => {
it('should return an error if one of the IP addresses supplied is invalid', () => {
const fn = () => getIPRange('abc', '192.168.0.1');
expect(fn).toThrow();
});
it('should return an error if one of the IP addresses supplied is invalid', () => {
const fn = () => getIPRange('192.168.0.1', 'abc');
expect(fn).toThrow();
});
it('should return an error if one of the IP addresses is in CIDR notation', () => {
const fn = () => getIPRange('10.1.128.0/29', '10.1.128.0');
expect(fn).toThrow();
});
it('should return an error if one of the IP addresses is in CIDR notation', () => {
const fn = () => getIPRange('10.1.128.0', '10.1.128.0/29');
expect(fn).toThrow();
});
it('should return an error if one IP address has numbers which are too high', () => {
const fn = () => getIPRange('192.168.1.134/256', '192.168.1.134');
expect(fn).toThrow();
});
it('should return an error if one IP address has numbers which are too high', () => {
const fn = () => getIPRange('192.168.1.134', '192.168.1.134/256');
expect(fn).toThrow();
});
it('should return an array of IP addresses within the specified range', () => {
expect(getIPRange('192.168.1.128', '192.168.1.135')).toEqual(successResponsev4);
});
it('should support IPv6', () => {
expect(getIPRange('::ffff:102:304', '::ffff:102:307')).toEqual(successResponsev6);
});
it('should support hyphenated range in IPv4', () => {
expect(getIPRange('192.168.1.128-192.168.1.135')).toEqual(successResponsev4);
});
it('should support hyphenated range in IPv5', () => {
expect(getIPRange('::ffff:102:304-::ffff:102:307')).toEqual(successResponsev6);
});
it('should throw if the range is greater than 10000 default', () => {
const throwFn = () => getIPRange('128.0.0.0/1');
expect(throwFn).toThrow('Too many IPs in range. Total number: 2147483647. Max count is 10000, to increase, set the limit with the MAX_RANGE environment variable');
});
it('should throw if the range is greater than process.env.MAX_RANGE', () => {
process.env.MAX_RANGE = '5000';
const throwFn = () => getIPRange('128.0.0.0/1');
expect(throwFn).toThrow('Too many IPs in range. Total number: 2147483647. Max count is 5000, to increase, set the limit with the MAX_RANGE environment variable');
});
it('should throw if process.env.MAX_RANGE is NaN', () => {
process.env.MAX_RANGE = 'foo';
const throwFn = () => getIPRange('128.0.0.0/1');
expect(throwFn).toThrow('MAX_RANGE must be an integer');
});
});