Skip to content

Commit

Permalink
fix: demskie#6
Browse files Browse the repository at this point in the history
  • Loading branch information
astellingwerf committed Jul 13, 2021
1 parent 25dd058 commit 391a5d8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ export function sortNetworks(networks: Network[]) {
sort.nativeSort(networks);
}

export function summarizeSortedNetworks(sorted: Network[]) {
function increaseSizeByOneBit(network: Network): Network {
const wider = network.setCIDR(network.cidr() - 1);
wider.addr.applySubnetMask(wider.cidr());
return wider;
}

export function summarizeSortedNetworks(sorted: Network[]): Network[] {
const summarized = [] as Network[];
for (let idx = 0; idx < sorted.length; idx++) {
summarized.push(sorted[idx]);
Expand All @@ -31,13 +37,26 @@ export function summarizeSortedNetworks(sorted: Network[]) {
}
if (sorted[idx].cidr() === sorted[i].cidr()) {
if (sorted[idx].adjacent(sorted[i])) {
sorted[idx].setCIDR(sorted[idx].cidr() - 1);
skipped++;
continue;
const wider = increaseSizeByOneBit(sorted[idx].duplicate());
if (wider.contains(sorted[i])) {
increaseSizeByOneBit(sorted[idx]);
skipped++;
continue;
}
}
}
break;
}
while (summarized.length >= 2) {
const a = summarized[summarized.length - 2];
const b = summarized[summarized.length - 1];
if (a.cidr() != b.cidr() || !a.addr.isBaseAddress(a.cidr() - 1) || !a.adjacent(b)) {
break;
}
increaseSizeByOneBit(a);
summarized.pop();
}

idx += skipped;
}
return summarized;
Expand Down
38 changes: 37 additions & 1 deletion src/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import * as index from "../index";

expect.extend({
toContainAddresses(received, addresses) {
const notFound = addresses.filter((i: string) => {
return undefined === received.find((value: string) => index.networkContainsAddress(value, i));
});
if (notFound.length > 0) {
return { message: () => `expected to find ${notFound} in ${received}`, pass: false };
} else {
return { message: () => `expected not to find ${addresses} in ${received}`, pass: true };
}
}
});

test("sanity check baseAddress #1", () => {
const output = index.baseAddress("192.168.200.113/24", true);
expect(output).toEqual("192.168.200.0");
Expand Down Expand Up @@ -397,14 +410,37 @@ test("sanity check summarize #2", () => {

test("sanity check summarize #3", () => {
const output = index.summarize(["192.168.0.0", "192.168.0.2/31", "192.168.0.3", "192.168.0.4/31"], true);
expect(output).toEqual(["192.168.0.0/32", "192.168.0.2/30"]);
expect(output).toEqual(["192.168.0.0/32", "192.168.0.2/31", "192.168.0.4/31"]);
});

test("sanity check summarize #4", () => {
const output = index.summarize(["192.168.0.0/31", "192.168.0.2/31", "192.168.0.3", "192.168.0.5/32"], true);
expect(output).toEqual(["192.168.0.0/30", "192.168.0.5/32"]);
});

test("sanity check summarize #5", () => {
const input = [
"10.9.201.68",
"10.9.201.71",
"10.9.201.70",
"10.9.201.65",
"10.9.201.72",
"10.9.201.67",
"10.9.201.66",
"10.9.201.69"
]; // Shuffled, but effectively ranging from 10.9.201.65 to 10.9.201.72

const output = index.summarize(input, true);
expect(output).toBeDefined();
expect(output).not.toContainAddresses(["192.168.0.0"]);
expect(output).not.toContainAddresses(["10.9.201.64"]);
expect(output).not.toContainAddresses(["10.9.201.73"]);
expect(output).toContainAddresses(input);

const expectedOutput = ["10.9.201.65/32", "10.9.201.66/31", "10.9.201.68/30", "10.9.201.72/32"];
expect(output).toEqual(expectedOutput);
});

// https://tools.ietf.org/html/rfc5952#section-4

test("sanity check IPv6 parsing #1", () => {
Expand Down

0 comments on commit 391a5d8

Please sign in to comment.