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

461 lines
14 KiB
JavaScript
Raw Normal View History

Pull request: 2842 DHCP validation ui Closes #2842. Squashed commit of the following: commit 8580db9d3fd6bdd906bf53ca3696fc497f7573b8 Merge: a5d7187b ab85ad5a Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Apr 13 15:29:06 2021 +0300 Merge branch 'master' into 2842-dhcp-validation-ui commit a5d7187bba1ae3595bbc26a362ff27aae81a7048 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 15:08:28 2021 +0300 fix: revert deleted translation commit 50169266111032f6de3bc159ba562ee9580532fb Merge: 46adf2c0 48d702f7 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 14:39:40 2021 +0300 Merge branch 'master' into 2842-dhcp-validation-ui commit 46adf2c05b6bedd55e60475eac060347db6572b7 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 14:13:12 2021 +0300 fix: no-bitwise commit 1afc4030a5ea885545e51748976724959f87fb26 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 13:57:43 2021 +0300 fix: IPv4 in CIDR validation commit 2035a3f6a2d7026b9055bab64a265ac1b56abd74 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 11:58:03 2021 +0300 fix: translations commit 6dd455f7dbf92987663b433b7cb8e21c9d0e5b82 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 11:57:27 2021 +0300 fix: MAC validation commit 281e49a2e2b974e0c7eb89547661aed8238a5d0c Merge: 48b50ce9 65553a29 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 6 18:12:06 2021 +0300 Merge branch 'master' into 2842-dhcp-validation-ui commit 48b50ce9ce84479c43c3d6fc824853dc0b17ac1e Author: Artem Baskal <a.baskal@adguard.com> Date: Mon Apr 5 19:04:35 2021 +0300 Add leases ip validation commit 8630f3bf5f03451c3a49c4ce4ebee3a86d16b6a1 Author: Artem Baskal <a.baskal@adguard.com> Date: Mon Apr 5 13:59:16 2021 +0300 Add helper for subnet to bitmap mask conversion, write test commit 80dc7a8d19b27cecc50e2c610619574374f363d3 Author: Artem Baskal <a.baskal@adguard.com> Date: Fri Apr 2 17:46:27 2021 +0300 2842 Update DHCP range validation in UI
2021-04-13 13:29:28 +01:00
import {
sortIp,
countClientsStatistics,
findAddressType,
subnetMaskToBitMask,
} from '../helpers/helpers';
+ client: Move the client access check to the server-side Squashed commit of the following: commit 1aab0f62e94ce665a1b996552fac41dc4e769b4d Merge: cdf5eb6e c1f5fdae Author: ArtemBaskal <a.baskal@adguard.com> Date: Thu Sep 24 15:36:05 2020 +0300 Merge branch '1920-client-find' into feature/1925 commit cdf5eb6ea67a665d21a3155d8cf89bba9a5a9948 Merge: b6c20b1c 10f67bd3 Author: ArtemBaskal <a.baskal@adguard.com> Date: Wed Sep 23 20:28:51 2020 +0300 Merge branch 'master' into feature/1925 commit b6c20b1c7359a0e5902405b0551712f936848a80 Merge: 97d388ef 96512433 Author: ArtemBaskal <a.baskal@adguard.com> Date: Tue Sep 15 10:44:25 2020 +0300 Merge branch 'master' into feature/1925 commit 97d388ef6571d590f21da00f86d889e881ca0c3d Author: ArtemBaskal <a.baskal@adguard.com> Date: Tue Sep 15 10:30:50 2020 +0300 Extract buttons commit ca45fde11fc2b2812ff2b84dbd67aff0b5341be1 Author: ArtemBaskal <a.baskal@adguard.com> Date: Thu Sep 10 12:46:09 2020 +0300 Handle errors in updateLogs commit f15e03c2e5a7115db984f70f72b0ddd870ece73d Author: ArtemBaskal <a.baskal@adguard.com> Date: Thu Sep 10 12:39:34 2020 +0300 Update mobile block status on click commit 033b28db3b324f6d529ac1a0ef657886cdbe02bd Author: ArtemBaskal <a.baskal@adguard.com> Date: Wed Sep 9 20:53:42 2020 +0300 Fix mobile block buttons, auto open page on web serve start commit 2730937b23309167a066b9154728ac53ffe81a49 Author: ArtemBaskal <a.baskal@adguard.com> Date: Wed Sep 9 13:58:37 2020 +0300 Disable allow this client button when isNotInAllowedList is true commit 818cf869d63654c184762ad2701c4429a3e3011e Author: ArtemBaskal <a.baskal@adguard.com> Date: Wed Sep 9 13:06:01 2020 +0300 Update client block state on option click commit a072b8983757f419645c0207ea78e6e867c440cb Author: ArtemBaskal <a.baskal@adguard.com> Date: Tue Sep 8 20:17:16 2020 +0300 Adapt to api changes commit 28ab2bd8b3f14f60bc822b5a69fa1801db67d816 Author: ArtemBaskal <a.baskal@adguard.com> Date: Tue Sep 8 14:12:20 2020 +0300 Change query log block confirm messages commit 9b0b6f6f9b1ec168fa71dbedd036152da59006e3 Author: ArtemBaskal <a.baskal@adguard.com> Date: Tue Sep 8 12:00:46 2020 +0300 Refactor inner work with disallowed commit 05f76154b8f489738d032fdaa835edb371ce70c7 Author: ArtemBaskal <a.baskal@adguard.com> Date: Mon Sep 7 16:11:37 2020 +0300 + client: Move the client access check to the server-side
2020-09-24 13:48:37 +01:00
import { ADDRESS_TYPES } from '../helpers/constants';
describe('sortIp', () => {
describe('ipv4', () => {
test('one octet differ', () => {
const arr = [
'127.0.2.0',
'127.0.3.0',
'127.0.1.0',
];
const sortedArr = [
'127.0.1.0',
'127.0.2.0',
'127.0.3.0',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
test('few octets differ', () => {
const arr = [
'192.168.11.10',
'192.168.10.0',
'192.168.11.11',
'192.168.10.10',
'192.168.1.10',
'192.168.0.1',
'192.168.1.0',
'192.168.1.1',
'192.168.11.0',
'192.168.0.10',
'192.168.10.11',
'192.168.0.11',
'192.168.1.11',
'192.168.0.0',
'192.168.10.1',
'192.168.11.1',
];
const sortedArr = [
'192.168.0.0',
'192.168.0.1',
'192.168.0.10',
'192.168.0.11',
'192.168.1.0',
'192.168.1.1',
'192.168.1.10',
'192.168.1.11',
'192.168.10.0',
'192.168.10.1',
'192.168.10.10',
'192.168.10.11',
'192.168.11.0',
'192.168.11.1',
'192.168.11.10',
'192.168.11.11',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
// Example from issue https://github.com/AdguardTeam/AdGuardHome/issues/1778#issuecomment-640937599
const arr2 = [
'192.168.2.11',
'192.168.3.1',
'192.168.2.100',
'192.168.2.2',
'192.168.2.1',
'192.168.2.10',
'192.168.2.99',
'192.168.2.200',
'192.168.2.199',
];
const sortedArr2 = [
'192.168.2.1',
'192.168.2.2',
'192.168.2.10',
'192.168.2.11',
'192.168.2.99',
'192.168.2.100',
'192.168.2.199',
'192.168.2.200',
'192.168.3.1',
];
expect(arr2.sort(sortIp)).toStrictEqual(sortedArr2);
});
});
describe('ipv6', () => {
test('only long form', () => {
const arr = [
'2001:db8:11a3:9d7:0:0:0:2',
'2001:db8:11a3:9d7:0:0:0:3',
'2001:db8:11a3:9d7:0:0:0:1',
];
const sortedArr = [
'2001:db8:11a3:9d7:0:0:0:1',
'2001:db8:11a3:9d7:0:0:0:2',
'2001:db8:11a3:9d7:0:0:0:3',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
test('only short form', () => {
const arr = [
'2001:db8::',
'2001:db7::',
'2001:db9::',
];
const sortedArr = [
'2001:db7::',
'2001:db8::',
'2001:db9::',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
test('long and short forms', () => {
const arr = [
'2001:db8::',
'2001:db7:11a3:9d7:0:0:0:2',
'2001:db6:11a3:9d7:0:0:0:1',
'2001:db6::',
'2001:db7:11a3:9d7:0:0:0:1',
'2001:db7::',
];
const sortedArr = [
'2001:db6::',
'2001:db6:11a3:9d7:0:0:0:1',
'2001:db7::',
'2001:db7:11a3:9d7:0:0:0:1',
'2001:db7:11a3:9d7:0:0:0:2',
'2001:db8::',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
});
describe('ipv4 and ipv6', () => {
test('ipv6 long form', () => {
const arr = [
'127.0.0.3',
'2001:db8:11a3:9d7:0:0:0:1',
'2001:db8:11a3:9d7:0:0:0:3',
'127.0.0.1',
'2001:db8:11a3:9d7:0:0:0:2',
'127.0.0.2',
];
const sortedArr = [
'127.0.0.1',
'127.0.0.2',
'127.0.0.3',
'2001:db8:11a3:9d7:0:0:0:1',
'2001:db8:11a3:9d7:0:0:0:2',
'2001:db8:11a3:9d7:0:0:0:3',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
test('ipv6 short form', () => {
const arr = [
'2001:db8:11a3:9d7::1',
'127.0.0.3',
'2001:db8:11a3:9d7::3',
'127.0.0.1',
'2001:db8:11a3:9d7::2',
'127.0.0.2',
];
const sortedArr = [
'127.0.0.1',
'127.0.0.2',
'127.0.0.3',
'2001:db8:11a3:9d7::1',
'2001:db8:11a3:9d7::2',
'2001:db8:11a3:9d7::3',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
test('ipv6 long and short forms', () => {
const arr = [
'2001:db8:11a3:9d7::1',
'127.0.0.3',
'2001:db8:11a3:9d7:0:0:0:2',
'127.0.0.1',
'2001:db8:11a3:9d7::3',
'127.0.0.2',
];
const sortedArr = [
'127.0.0.1',
'127.0.0.2',
'127.0.0.3',
'2001:db8:11a3:9d7::1',
'2001:db8:11a3:9d7:0:0:0:2',
'2001:db8:11a3:9d7::3',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
test('always put ipv4 before ipv6', () => {
const arr = [
'::1',
'0.0.0.2',
'127.0.0.1',
'::2',
'2001:db8:11a3:9d7:0:0:0:2',
'0.0.0.1',
'2001:db8:11a3:9d7::1',
];
const sortedArr = [
'0.0.0.1',
'0.0.0.2',
'127.0.0.1',
'::1',
'::2',
'2001:db8:11a3:9d7::1',
'2001:db8:11a3:9d7:0:0:0:2',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
});
describe('cidr', () => {
test('only ipv4 cidr', () => {
const arr = [
'192.168.0.1/9',
'192.168.0.1/7',
'192.168.0.1/8',
];
const sortedArr = [
'192.168.0.1/7',
'192.168.0.1/8',
'192.168.0.1/9',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
test('ipv4 and cidr ipv4', () => {
const arr = [
'192.168.0.1/9',
'192.168.0.1',
'192.168.0.1/32',
'192.168.0.1/7',
'192.168.0.1/8',
];
const sortedArr = [
'192.168.0.1/7',
'192.168.0.1/8',
'192.168.0.1/9',
'192.168.0.1/32',
'192.168.0.1',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
test('only ipv6 cidr', () => {
const arr = [
'2001:db8:11a3:9d7::1/32',
'2001:db8:11a3:9d7::1/64',
'2001:db8:11a3:9d7::1/128',
'2001:db8:11a3:9d7::1/24',
];
const sortedArr = [
'2001:db8:11a3:9d7::1/24',
'2001:db8:11a3:9d7::1/32',
'2001:db8:11a3:9d7::1/64',
'2001:db8:11a3:9d7::1/128',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
test('ipv6 and cidr ipv6', () => {
const arr = [
'2001:db8:11a3:9d7::1/32',
'2001:db8:11a3:9d7::1',
'2001:db8:11a3:9d7::1/64',
'2001:db8:11a3:9d7::1/128',
'2001:db8:11a3:9d7::1/24',
];
const sortedArr = [
'2001:db8:11a3:9d7::1/24',
'2001:db8:11a3:9d7::1/32',
'2001:db8:11a3:9d7::1/64',
'2001:db8:11a3:9d7::1/128',
'2001:db8:11a3:9d7::1',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
});
describe('invalid input', () => {
const originalWarn = console.warn;
beforeEach(() => {
console.warn = jest.fn();
});
afterEach(() => {
expect(console.warn).toHaveBeenCalled();
console.warn = originalWarn;
});
test('invalid strings', () => {
const arr = ['invalid ip', 'invalid cidr'];
expect(arr.sort(sortIp)).toStrictEqual(arr);
});
test('invalid ip', () => {
const arr = ['127.0.0.2.', '.127.0.0.1.', '.2001:db8:11a3:9d7:0:0:0:0'];
expect(arr.sort(sortIp)).toStrictEqual(arr);
});
test('invalid cidr', () => {
const arr = ['127.0.0.2/33', '2001:db8:11a3:9d7:0:0:0:0/129'];
expect(arr.sort(sortIp)).toStrictEqual(arr);
});
test('valid and invalid ip', () => {
const arr = ['127.0.0.4.', '127.0.0.1', '.127.0.0.3', '127.0.0.2'];
expect(arr.sort(sortIp)).toStrictEqual(arr);
});
});
describe('mixed', () => {
test('ipv4, ipv6 in short and long forms and cidr', () => {
const arr = [
'2001:db8:11a3:9d7:0:0:0:1/32',
'192.168.1.2',
'127.0.0.2',
'2001:db8:11a3:9d7::1/128',
'2001:db8:11a3:9d7:0:0:0:1',
'127.0.0.1/12',
'192.168.1.1',
'2001:db8::/32',
'2001:db8:11a3:9d7::1/24',
'192.168.1.2/12',
'2001:db7::/32',
'127.0.0.1',
'2001:db8:11a3:9d7:0:0:0:2',
'192.168.1.1/24',
'2001:db7::/64',
'2001:db7::',
'2001:db8::',
'2001:db8:11a3:9d7:0:0:0:1/128',
'192.168.1.1/12',
'127.0.0.1/32',
'::1',
];
const sortedArr = [
'127.0.0.1/12',
'127.0.0.1/32',
'127.0.0.1',
'127.0.0.2',
'192.168.1.1/12',
'192.168.1.1/24',
'192.168.1.1',
'192.168.1.2/12',
'192.168.1.2',
'::1',
'2001:db7::/32',
'2001:db7::/64',
'2001:db7::',
'2001:db8::/32',
'2001:db8::',
'2001:db8:11a3:9d7::1/24',
'2001:db8:11a3:9d7:0:0:0:1/32',
'2001:db8:11a3:9d7::1/128',
'2001:db8:11a3:9d7:0:0:0:1/128',
'2001:db8:11a3:9d7:0:0:0:1',
'2001:db8:11a3:9d7:0:0:0:2',
];
expect(arr.sort(sortIp)).toStrictEqual(sortedArr);
});
});
});
describe('findAddressType', () => {
describe('ip', () => {
expect(findAddressType('127.0.0.1')).toStrictEqual(ADDRESS_TYPES.IP);
});
describe('cidr', () => {
expect(findAddressType('127.0.0.1/8')).toStrictEqual(ADDRESS_TYPES.CIDR);
});
describe('mac', () => {
expect(findAddressType('00:1B:44:11:3A:B7')).toStrictEqual(ADDRESS_TYPES.UNKNOWN);
});
});
describe('countClientsStatistics', () => {
test('single ip', () => {
expect(countClientsStatistics(['127.0.0.1'], {
'127.0.0.1': 1,
})).toStrictEqual(1);
});
test('multiple ip', () => {
expect(countClientsStatistics(['127.0.0.1', '127.0.0.2'], {
'127.0.0.1': 1,
'127.0.0.2': 2,
})).toStrictEqual(1 + 2);
});
test('cidr', () => {
expect(countClientsStatistics(['127.0.0.0/8'], {
'127.0.0.1': 1,
'127.0.0.2': 2,
})).toStrictEqual(1 + 2);
});
test('cidr and multiple ip', () => {
expect(countClientsStatistics(['1.1.1.1', '2.2.2.2', '3.3.3.0/24'], {
'1.1.1.1': 1,
'2.2.2.2': 2,
'3.3.3.3': 3,
})).toStrictEqual(1 + 2 + 3);
});
test('mac', () => {
expect(countClientsStatistics(['00:1B:44:11:3A:B7', '2.2.2.2', '3.3.3.0/24'], {
'1.1.1.1': 1,
'2.2.2.2': 2,
'3.3.3.3': 3,
})).toStrictEqual(2 + 3);
});
test('not found', () => {
expect(countClientsStatistics(['4.4.4.4', '5.5.5.5', '6.6.6.6'], {
'1.1.1.1': 1,
'2.2.2.2': 2,
'3.3.3.3': 3,
})).toStrictEqual(0);
});
});
Pull request: 2842 DHCP validation ui Closes #2842. Squashed commit of the following: commit 8580db9d3fd6bdd906bf53ca3696fc497f7573b8 Merge: a5d7187b ab85ad5a Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Apr 13 15:29:06 2021 +0300 Merge branch 'master' into 2842-dhcp-validation-ui commit a5d7187bba1ae3595bbc26a362ff27aae81a7048 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 15:08:28 2021 +0300 fix: revert deleted translation commit 50169266111032f6de3bc159ba562ee9580532fb Merge: 46adf2c0 48d702f7 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 14:39:40 2021 +0300 Merge branch 'master' into 2842-dhcp-validation-ui commit 46adf2c05b6bedd55e60475eac060347db6572b7 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 14:13:12 2021 +0300 fix: no-bitwise commit 1afc4030a5ea885545e51748976724959f87fb26 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 13:57:43 2021 +0300 fix: IPv4 in CIDR validation commit 2035a3f6a2d7026b9055bab64a265ac1b56abd74 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 11:58:03 2021 +0300 fix: translations commit 6dd455f7dbf92987663b433b7cb8e21c9d0e5b82 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 13 11:57:27 2021 +0300 fix: MAC validation commit 281e49a2e2b974e0c7eb89547661aed8238a5d0c Merge: 48b50ce9 65553a29 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Apr 6 18:12:06 2021 +0300 Merge branch 'master' into 2842-dhcp-validation-ui commit 48b50ce9ce84479c43c3d6fc824853dc0b17ac1e Author: Artem Baskal <a.baskal@adguard.com> Date: Mon Apr 5 19:04:35 2021 +0300 Add leases ip validation commit 8630f3bf5f03451c3a49c4ce4ebee3a86d16b6a1 Author: Artem Baskal <a.baskal@adguard.com> Date: Mon Apr 5 13:59:16 2021 +0300 Add helper for subnet to bitmap mask conversion, write test commit 80dc7a8d19b27cecc50e2c610619574374f363d3 Author: Artem Baskal <a.baskal@adguard.com> Date: Fri Apr 2 17:46:27 2021 +0300 2842 Update DHCP range validation in UI
2021-04-13 13:29:28 +01:00
describe('subnetMaskToBitMask', () => {
const subnetMasks = [
'0.0.0.0',
'128.0.0.0',
'192.0.0.0',
'224.0.0.0',
'240.0.0.0',
'248.0.0.0',
'252.0.0.0',
'254.0.0.0',
'255.0.0.0',
'255.128.0.0',
'255.192.0.0',
'255.224.0.0',
'255.240.0.0',
'255.248.0.0',
'255.252.0.0',
'255.254.0.0',
'255.255.0.0',
'255.255.128.0',
'255.255.192.0',
'255.255.224.0',
'255.255.240.0',
'255.255.248.0',
'255.255.252.0',
'255.255.254.0',
'255.255.255.0',
'255.255.255.128',
'255.255.255.192',
'255.255.255.224',
'255.255.255.240',
'255.255.255.248',
'255.255.255.252',
'255.255.255.254',
'255.255.255.255',
];
test('correct for all subnetMasks', () => {
expect(
subnetMasks.map((subnetMask) => {
const bitmask = subnetMaskToBitMask(subnetMask);
return subnetMasks[bitmask] === subnetMask;
}).every((res) => res === true),
).toEqual(true);
});
});