8000 fix(ipv4-range-expander): calculate correct for ip addresses where the first octet is lower than 128 by cgoIT · Pull Request #405 · CorentinTh/it-tools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(ipv4-range-expander): calculate correct for ip addresses where the first octet is lower than 128 #405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/tools/ipv4-range-expander/ipv4-range-expander.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ test.describe('Tool - IPv4 range expander', () => {
expect(await page.getByTestId('cidr.new').textContent()).toEqual('192.168.0.0/21');
});

test('Calculates correct for valid input, where first octet is lower than 128', async ({ page }) => {
await page.getByPlaceholder('Start IPv4 address...').fill('10.0.0.1');
await page.getByPlaceholder('End IPv4 address...').fill('10.0.0.17');

expect(await page.getByTestId('start-address.old').textContent()).toEqual('10.0.0.1');
expect(await page.getByTestId('start-address.new').textContent()).toEqual('10.0.0.0');
expect(await page.getByTestId('end-address.old').textContent()).toEqual('10.0.0.17');
expect(await page.getByTestId('end-address.new').textContent()).toEqual('10.0.0.31');
expect(await page.getByTestId('addresses-in-range.old').textContent()).toEqual('17');
expect(await page.getByTestId('addresses-in-range.new').textContent()).toEqual('32');
expect(await page.getByTestId('cidr.old').textContent()).toEqual('');
expect(await page.getByTestId('cidr.new').textContent()).toEqual('10.0.0.0/27');
});

test('Hides result for invalid input', async ({ page }) => {
await page.getByPlaceholder('Start IPv4 address...').fill('192.168.1.1');
await page.getByPlaceholder('End IPv4 address...').fill('192.168.0.255');
Expand Down
11 changes: 11 additions & 0 deletions src/tools/ipv4-range-expander/ipv4-range-expander.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ describe('ipv4RangeExpander', () => {
expect(result?.newCidr).toEqual('192.168.0.0/21');
});

it('should calculate valid cidr for given addresses, where first octet is lower than 128', () => {
const result = calculateCidr({ startIp: '10.0.0.1', endIp: '10.0.0.17' });

expect(result).toBeDefined();
expect(result?.oldSize).toEqual(17);
expect(result?.newSize).toEqual(32);
expect(result?.newStart).toEqual('10.0.0.0');
expect(result?.newEnd).toEqual('10.0.0.31');
expect(result?.newCidr).toEqual('10.0.0.0/27');
});

it('should return empty result for invalid input', () => {
expect(calculateCidr({ startIp: '192.168.7.1', endIp: '192.168.6.255' })).not.toBeDefined();
});
Expand Down
4 changes: 2 additions & 2 deletions src/tools/ipv4-range-expander/ipv4-range-expander.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ function calculateCidr({ startIp, endIp }: { startIp: string; endIp: string }) {
value: ipv4ToInt({ ip: startIp }).toString(),
fromBase: 10,
toBase: 2,
});
}).padStart(32, '0');
const end = convertBase({
value: ipv4ToInt({ ip: endIp }).toString(),
fromBase: 10,
toBase: 2,
});
}).padStart(32, '0');

const cidr = getCidr(start, end);
if (cidr != null) {
Expand Down
0