Skip to content
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

Expose original network on match #9

Closed
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
12 changes: 6 additions & 6 deletions src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ export class Matcher {
*
* @param network - An address or subnet
*
* @returns A boolean
* @returns A Network or null
*/
public has(network: string) {
var net = shared.parseBaseNetwork(network, false, false);
if (!net || !net.isValid()) return false;
if (!net || !net.isValid()) return null;
var idx = sort.binarySearchForInsertionIndex(net, this.sorted);
if (idx < 0) return false;
if (idx < this.sorted.length && this.sorted[idx].contains(net)) return true;
if (idx - 1 >= 0 && this.sorted[idx - 1].contains(net)) return true;
return false;
if (idx < 0) return null;
if (idx < this.sorted.length && this.sorted[idx].contains(net)) return this.sorted[idx];
if (idx - 1 >= 0 && this.sorted[idx - 1].contains(net)) return this.sorted[idx - 1];
return null;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ const AFTER = 1;

export class Network {
public readonly addr = new Address();
public readonly network?: string;
private netbits = -1;

public constructor(network?: string, throwErrors?: boolean) {
this.network = network;

if (network) {
var net = parse.network(network, throwErrors);
if (net) {
Expand Down
44 changes: 34 additions & 10 deletions src/tests/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test("sanity check Matcher #1", () => {
"192.168.0.255/32"
];
const matcher = new match.Matcher(input);
expect(matcher.has("192.168.0.254")).toEqual(false);
expect(matcher.has("192.168.0.254")).toEqual(null);
});

test("sanity check Matcher #2", () => {
Expand All @@ -35,7 +35,11 @@ test("sanity check Matcher #2", () => {
"192.168.0.255/32"
];
const matcher = new match.Matcher(input);
expect(matcher.has("192.168.0.254")).toEqual(true);
expect(matcher.has("192.168.0.254")).toEqual({
addr: { arr: [192, 168, 0, 0] },
netbits: 24,
network: "192.168.0.0/24"
});
});

test("sanity check Matcher #3", () => {
Expand All @@ -54,7 +58,11 @@ test("sanity check Matcher #3", () => {
"192.168.0.255/32"
];
const matcher = new match.Matcher(input);
expect(matcher.has("192.168.0.254")).toEqual(true);
expect(matcher.has("192.168.0.254")).toEqual({
addr: { arr: [192, 168, 0, 0] },
netbits: 24,
network: "192.168.0.0/24"
});
});

test("sanity check Matcher #4", () => {
Expand All @@ -73,7 +81,7 @@ test("sanity check Matcher #4", () => {
"192.168.0.255/32"
];
const matcher = new match.Matcher(input);
expect(matcher.has("foobar")).toEqual(false);
expect(matcher.has("foobar")).toEqual(null);
});

test("sanity check Matcher #5", () => {
Expand All @@ -92,7 +100,11 @@ test("sanity check Matcher #5", () => {
"192.168.0.255/32"
];
const matcher = new match.Matcher(input);
expect(matcher.has("192.168.0.255")).toEqual(true);
expect(matcher.has("192.168.0.255")).toEqual({
addr: { arr: [192, 168, 0, 255] },
netbits: 32,
network: "192.168.0.255/32"
});
});

test("sanity check Matcher #6", () => {
Expand All @@ -111,7 +123,7 @@ test("sanity check Matcher #6", () => {
"192.168.0.254/31"
];
const matcher = new match.Matcher(input);
expect(matcher.has("192.168.0.25")).toEqual(false);
expect(matcher.has("192.168.0.25")).toEqual(null);
});

test("sanity check Matcher #6", () => {
Expand All @@ -131,7 +143,7 @@ test("sanity check Matcher #6", () => {
];

const matcher = new match.Matcher(input);
expect(matcher.has("192.168.0.0")).toEqual(false);
expect(matcher.has("192.168.0.0")).toEqual(null);
});

test("sanity check Matcher #7", () => {
Expand All @@ -150,7 +162,11 @@ test("sanity check Matcher #7", () => {
"192.168.0.254/32"
];
const matcher = new match.Matcher(input);
expect(matcher.has("192.168.0.124/32")).toEqual(true);
expect(matcher.has("192.168.0.124/32")).toEqual({
addr: { arr: [192, 168, 0, 123] },
netbits: 31,
network: "192.168.0.123/32"
});
});

test("sanity check Matcher #8", () => {
Expand All @@ -169,7 +185,11 @@ test("sanity check Matcher #8", () => {
"192.168.0.254/31"
];
const matcher = new match.Matcher(input);
expect(matcher.has("192.168.0.255")).toEqual(true);
expect(matcher.has("192.168.0.255")).toEqual({
addr: { arr: [192, 168, 0, 254] },
netbits: 31,
network: "192.168.0.254/31"
});
});

test("sanity check Matcher #9", () => {
Expand All @@ -191,5 +211,9 @@ test("sanity check Matcher #9", () => {
input.forEach((s: string) => {
matcher.add(s);
});
expect(matcher.has("192.168.0.255")).toEqual(true);
expect(matcher.has("192.168.0.255")).toEqual({
addr: { arr: [192, 168, 0, 254] },
netbits: 31,
network: "192.168.0.254/31"
});
});