Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Dec 17, 2024
1 parent 4c95274 commit 643d3d8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 49 deletions.
14 changes: 14 additions & 0 deletions library/helpers/ip-matcher/Address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as t from "tap";
import { Address } from "./Address";

t.test("it works", async (t) => {
t.same(new Address("1.2.3.5").isIPv4(), true);
t.same(new Address("::1").isIPv4(), false);
t.same(new Address("::1").isIPv6(), true);

t.same(new Address("1.2.3.4").compare(new Address("1.2.3.5")), -1);
t.same(new Address("1.2.3.4").compare(new Address("1.2.3.4")), 0);
t.same(new Address("1.2.3.4").compare(new Address("1.2.3.4").duplicate()), 0);

t.same(new Address("1.2.3.4").bytes(), [1, 2, 3, 4]);
});
10 changes: 2 additions & 8 deletions library/helpers/ip-matcher/Address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const AFTER = 1;
export class Address {
private arr: number[];

public constructor(address?: string, throwErrors?: boolean) {
public constructor(address?: string) {
if (address) {
const net = parse.network(address, throwErrors);
const net = parse.network(address);
if (net) {
this.arr = net.bytes;
return;
Expand Down Expand Up @@ -61,12 +61,6 @@ export class Address {
return this.compare(address) === EQUALS;
}

Check warning on line 62 in library/helpers/ip-matcher/Address.ts

View check run for this annotation

Codecov / codecov/patch

library/helpers/ip-matcher/Address.ts#L61-L62

Added lines #L61 - L62 were not covered by tests

public greaterThanOrEqual(address: Address) {
const result = this.compare(address);
if (result === null) return false;
return result >= EQUALS;
}

public compare(address: Address) {
// check that both addresses are valid
if (!this.isValid() || !address.isValid()) return null;
Expand Down
1 change: 0 additions & 1 deletion library/helpers/ip-matcher/IPMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as t from "tap";

import { IPMatcher } from "./IPMatcher";

t.test("check with single Ipv4s", async (t) => {
Expand Down
39 changes: 0 additions & 39 deletions library/helpers/ip-matcher/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ export class Network {
return this;
}

public lastAddr() {
const addr = this.addr.duplicate().applySubnetMask(this.netbits);
const maxCIDR = this.addr.bytes().length * 8;
for (let i = this.netbits + 1; i <= maxCIDR; i++) addr.increase(i);
return addr;
}

public setCIDR(cidr: number) {
if (!this.addr.isValid()) {
this.destroy();

Check warning on line 60 in library/helpers/ip-matcher/Network.ts

View check run for this annotation

Codecov / codecov/patch

library/helpers/ip-matcher/Network.ts#L60

Added line #L60 was not covered by tests
Expand Down Expand Up @@ -120,38 +113,6 @@ export class Network {
return true;
}

public intersects(network: Network) {
// check that both networks are valid
if (!this.isValid() || !network.isValid()) return false;

// ensure that both IPs are of the same type
if (this.addr.bytes().length !== network.addr.bytes().length) return false;

// handle edge cases
if (this.netbits === 0 || network.netbits == 0) return true;
const cmp = this.addr.compare(network.addr);
if (cmp === EQUALS) return true;

// ensure that alpha addr contains the baseAddress that comes first
let alpha: Network, bravo: Network;
if (cmp === BEFORE) {
alpha = this.duplicate().next();
bravo = network.duplicate().next();
} else {
alpha = network.duplicate().next();
bravo = this.duplicate().next();
}

// if either addresses overflowed than an intersection has occured
if (!alpha.isValid() || !bravo.isValid()) return true;

// if alpha addr is now greater than or equal to bravo addr than we've intersected
if (alpha.addr.greaterThanOrEqual(bravo.addr)) return true;

// otherwise we haven't intersected
return false;
}

public adjacent(network: Network) {
// check that both networks are valid
if (!this.isValid() || !network.isValid()) return false;
Expand Down
2 changes: 1 addition & 1 deletion library/helpers/ip-matcher/parse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Based on https://github.com/demskie/netparser
// MIT License - Copyright (c) 2019 alex

export function network(s: string, throwErrors?: boolean) {
export function network(s: string) {
s = s.trim();
const parts = s.split("/");
if (parts.length === 0 || parts.length > 2) return null;
Expand Down

0 comments on commit 643d3d8

Please sign in to comment.