badguardhome/client/src/__tests__/helpers.test.js

132 lines
4.2 KiB
JavaScript
Raw Normal View History

- client: Match client IP strictly: Merge pull request #620 in DNS/adguard-home from fix/1687 to master Close #1687 Squashed commit of the following: commit 5287da0b98d154d4243abdb4b9021006499c225f Merge: c6b50c70 83b9b701 Author: ArtemBaskal <a.baskal@adguard.com> Date: Fri May 29 12:47:23 2020 +0300 Merge branch 'master' into fix/1687 commit c6b50c70a5089fcadfd2606b07b3b84769db2760 Author: ArtemBaskal <a.baskal@adguard.com> Date: Fri May 29 12:42:12 2020 +0300 minor commit dab9fa9ee0502838b4e10aef93d037c2fb5bf41b Author: ArtemBaskal <a.baskal@adguard.com> Date: Thu May 28 16:56:08 2020 +0300 Add support for exact matching of long and short ipv6 notations, add tests commit e72e86cda81af2c5e54f93abb2890438fd3648b0 Author: ArtemBaskal <a.baskal@adguard.com> Date: Thu May 28 13:57:22 2020 +0300 Update helper, write tests commit 92f4c34224ab7927b02edde829f2d9653a00a854 Author: ArtemBaskal <a.baskal@adguard.com> Date: Wed May 27 18:35:05 2020 +0300 Make variable names more expressive commit 3d38f21281237e9cccbba26afc1ab641947c5dc0 Author: ArtemBaskal <a.baskal@adguard.com> Date: Wed May 27 17:09:08 2020 +0300 Add ipv6 cidr support commit 7db0a2fb18ccd96d8d1def73f12138e4f4e37f71 Author: ArtemBaskal <a.baskal@adguard.com> Date: Tue May 26 12:48:57 2020 +0300 Minor commit 65e87f3899aab3417cac57bab0a8fa371cafd4ec Author: ArtemBaskal <a.baskal@adguard.com> Date: Tue May 26 12:46:30 2020 +0300 Add breaks between helpers commit 3f38bdfe7bc17e019bf048c79c9e8f1336b6f3d3 Author: ArtemBaskal <a.baskal@adguard.com> Date: Thu May 21 20:17:27 2020 +0300 - client: Match client IP strictly
2020-05-29 09:53:40 +00:00
import { getIpMatchListStatus } from '../helpers/helpers';
import { IP_MATCH_LIST_STATUS } from '../helpers/constants';
describe('getIpMatchListStatus', () => {
describe('IPv4', () => {
test('should return EXACT on find the exact ip match', () => {
const list = `127.0.0.2
2001:db8:11a3:9d7:0:0:0:0
192.168.0.1/8
127.0.0.1
127.0.0.3`;
expect(getIpMatchListStatus('127.0.0.1', list))
.toEqual(IP_MATCH_LIST_STATUS.EXACT);
});
test('should return CIDR on find the cidr match', () => {
const list = `127.0.0.2
2001:db8:11a3:9d7:0:0:0:0
192.168.0.1/8
127.0.0.0/24
127.0.0.3`;
expect(getIpMatchListStatus('127.0.0.1', list))
.toEqual(IP_MATCH_LIST_STATUS.CIDR);
});
test('should return NOT_FOUND if the ip is not in the list', () => {
const list = `127.0.0.1
2001:db8:11a3:9d7:0:0:0:0
192.168.0.1/8
127.0.0.2
127.0.0.3`;
expect(getIpMatchListStatus('127.0.0.4', list))
.toEqual(IP_MATCH_LIST_STATUS.NOT_FOUND);
});
test('should return the first EXACT or CIDR match in the list', () => {
const list1 = `2001:db8:11a3:9d7:0:0:0:0
127.0.0.1
127.0.0.8/24
127.0.0.3`;
expect(getIpMatchListStatus('127.0.0.1', list1))
.toEqual(IP_MATCH_LIST_STATUS.EXACT);
const list2 = `2001:db8:11a3:9d7:ffff:ffff:ffff:ffff
2001:0db8:11a3:09d7:0000:0000:0000:0000/64
127.0.0.0/24
127.0.0.1
127.0.0.8/24
127.0.0.3`;
expect(getIpMatchListStatus('127.0.0.1', list2))
.toEqual(IP_MATCH_LIST_STATUS.CIDR);
});
});
describe('IPv6', () => {
test('should return EXACT on find the exact ip match', () => {
const list = `127.0.0.0
2001:db8:11a3:9d7:0:0:0:0
2001:db8:11a3:9d7:ffff:ffff:ffff:ffff
127.0.0.1`;
expect(getIpMatchListStatus('2001:db8:11a3:9d7:0:0:0:0', list))
.toEqual(IP_MATCH_LIST_STATUS.EXACT);
});
test('should return EXACT on find the exact ip match of short and long notation', () => {
const list = `127.0.0.0
192.168.0.1/8
2001:db8::
127.0.0.2`;
expect(getIpMatchListStatus('2001:db8:0:0:0:0:0:0', list))
.toEqual(IP_MATCH_LIST_STATUS.EXACT);
});
test('should return CIDR on find the cidr match', () => {
const list1 = `2001:0db8:11a3:09d7:0000:0000:0000:0000/64
127.0.0.1
127.0.0.2`;
expect(getIpMatchListStatus('2001:db8:11a3:9d7:0:0:0:0', list1))
.toEqual(IP_MATCH_LIST_STATUS.CIDR);
const list2 = `2001:0db8::/16
127.0.0.0
2001:db8:11a3:9d7:0:0:0:0
2001:db8::
2001:db8:11a3:9d7:ffff:ffff:ffff:ffff
127.0.0.1`;
expect(getIpMatchListStatus('2001:db1::', list2))
.toEqual(IP_MATCH_LIST_STATUS.CIDR);
});
test('should return NOT_FOUND if the ip is not in the list', () => {
const list = `2001:db8:11a3:9d7:0:0:0:0
2001:0db8:11a3:09d7:0000:0000:0000:0000/64
127.0.0.1
127.0.0.2`;
expect(getIpMatchListStatus('::', list))
.toEqual(IP_MATCH_LIST_STATUS.NOT_FOUND);
});
test('should return the first EXACT or CIDR match in the list', () => {
const list1 = `2001:db8:11a3:9d7:0:0:0:0
2001:0db8:11a3:09d7:0000:0000:0000:0000/64
127.0.0.3`;
expect(getIpMatchListStatus('2001:db8:11a3:9d7:0:0:0:0', list1))
.toEqual(IP_MATCH_LIST_STATUS.EXACT);
const list2 = `2001:0db8:11a3:09d7:0000:0000:0000:0000/64
2001:db8:11a3:9d7:0:0:0:0
127.0.0.3`;
expect(getIpMatchListStatus('2001:db8:11a3:9d7:0:0:0:0', list2))
.toEqual(IP_MATCH_LIST_STATUS.CIDR);
});
});
describe('Empty list or IP', () => {
test('should return NOT_FOUND on empty ip', () => {
const list = `127.0.0.0
2001:db8:11a3:9d7:0:0:0:0
2001:db8:11a3:9d7:ffff:ffff:ffff:ffff
127.0.0.1`;
expect(getIpMatchListStatus('', list))
.toEqual(IP_MATCH_LIST_STATUS.NOT_FOUND);
});
test('should return NOT_FOUND on empty list', () => {
const list = '';
expect(getIpMatchListStatus('127.0.0.1', list))
.toEqual(IP_MATCH_LIST_STATUS.NOT_FOUND);
});
});
});