|
| 1 | +import * as IP from "utils/IP"; |
| 2 | + |
| 3 | +describe("IP", () => { |
| 4 | + describe("getCIDRAddress()", () => { |
| 5 | + test("throws and error if start parameter is not a valid IPv4", () => { |
| 6 | + expect(() => { |
| 7 | + IP.getCIDRAddress(1, "1.1.1.1"); |
| 8 | + }).toThrow("ipaddr: the address has neither IPv6 nor IPv4 format"); |
| 9 | + }); |
| 10 | + |
| 11 | + test("throws and error if end parameter is not a valid IPv4", () => { |
| 12 | + expect(() => { |
| 13 | + IP.getCIDRAddress("1.1.1.1", 1); |
| 14 | + }).toThrow("ipaddr: the address has neither IPv6 nor IPv4 format"); |
| 15 | + }); |
| 16 | + |
| 17 | + test("returns /32 if both IPv4 addresses are the same", () => { |
| 18 | + expect(IP.getCIDRAddress("1.1.1.1", "1.1.1.1")).toBe("1.1.1.0/32"); |
| 19 | + }); |
| 20 | + |
| 21 | + test("returns different values depending on how many bits are different between start and end IPv4 addresses", () => { |
| 22 | + expect(IP.getCIDRAddress("1.1.1.1", "1.1.1.0")).toBe("1.1.1.0/31"); |
| 23 | + expect(IP.getCIDRAddress("1.1.1.1", "1.1.1.2")).toBe("1.1.1.0/30"); |
| 24 | + expect(IP.getCIDRAddress("1.1.1.1", "1.1.1.4")).toBe("1.1.1.0/29"); |
| 25 | + expect(IP.getCIDRAddress("1.1.1.1", "1.1.1.8")).toBe("1.1.1.0/28"); |
| 26 | + expect(IP.getCIDRAddress("1.1.1.1", "1.1.2.1")).toBe("1.1.1.0/22"); |
| 27 | + }); |
| 28 | + |
| 29 | + test("returns '<start with last character changed to 0>/32' no matter what valid IPv6 addresses are provided for start and end", () => { |
| 30 | + expect(IP.getCIDRAddress("2001:db8:1234::1", "2001:db8:1234::1")).toBe( |
| 31 | + "2001:db8:1234::0/32" |
| 32 | + ); |
| 33 | + expect(IP.getCIDRAddress("2001:db8:1234::32", "2001:db8:1234::1")).toBe( |
| 34 | + "2001:db8:1234::30/32" |
| 35 | + ); |
| 36 | + expect(IP.getCIDRAddress("2001:db8:1234::3000", "2001:db8:1234::1")).toBe( |
| 37 | + "2001:db8:1234::3000/32" |
| 38 | + ); |
| 39 | + expect(IP.getCIDRAddress("2002:db8:1234::1", "2001:db8:1234::1")).toBe( |
| 40 | + "2002:db8:1234::0/32" |
| 41 | + ); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe("getCIDR()", () => { |
| 46 | + test("throws and error if start parameter is not a valid IPv4", () => { |
| 47 | + expect(() => { |
| 48 | + IP.getCIDR(1, "1.1.1.1"); |
| 49 | + }).toThrow("ipaddr: the address has neither IPv6 nor IPv4 format"); |
| 50 | + }); |
| 51 | + |
| 52 | + test("throws and error if end parameter is not a valid IPv4", () => { |
| 53 | + expect(() => { |
| 54 | + IP.getCIDR("1.1.1.1", 1); |
| 55 | + }).toThrow("ipaddr: the address has neither IPv6 nor IPv4 format"); |
| 56 | + }); |
| 57 | + |
| 58 | + test("returns 32 if both IPv4 addresses are the same", () => { |
| 59 | + expect(IP.getCIDR("1.1.1.1", "1.1.1.1")).toBe(32); |
| 60 | + }); |
| 61 | + |
| 62 | + test("returns different values depending on how many bits are different between start and end IPv4 addresses", () => { |
| 63 | + expect(IP.getCIDR("1.1.1.1", "1.1.1.0")).toBe(31); |
| 64 | + expect(IP.getCIDR("1.1.1.1", "1.1.1.2")).toBe(30); |
| 65 | + expect(IP.getCIDR("1.1.1.1", "1.1.1.4")).toBe(29); |
| 66 | + expect(IP.getCIDR("1.1.1.1", "1.1.1.8")).toBe(28); |
| 67 | + }); |
| 68 | + |
| 69 | + test("returns 32 no matter what valid IPv6 addresses are provided for start and end", () => { |
| 70 | + expect(IP.getCIDR("2001:db8:1234::1", "2001:db8:1234::1")).toBe(32); |
| 71 | + expect(IP.getCIDR("2001:db8:1234::1", "2001:db8:1234::0")).toBe(32); |
| 72 | + expect(IP.getCIDR("2001:db8:1234::1", "2001:db8:1234::2")).toBe(32); |
| 73 | + expect(IP.getCIDR("2001:db8:1234::1", "2001:db8:1235::1")).toBe(32); |
| 74 | + expect(IP.getCIDR("2002:db8:1234::1", "2001:db8:1234::1")).toBe(32); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe("toInt()", () => { |
| 79 | + test("returns an IP in integer format when given a valid IPv4", () => { |
| 80 | + expect(IP.toInt("1.1.1.1")).toBe(16843009); |
| 81 | + }); |
| 82 | + |
| 83 | + test("throw an error if a string that is not an IP is provided as input", () => { |
| 84 | + expect(() => { |
| 85 | + IP.toInt("some string that is not an IPv4 or IPv6"); |
| 86 | + }).toThrow("ipaddr: the address has neither IPv6 nor IPv4 format"); |
| 87 | + }); |
| 88 | + |
| 89 | + test("throw an error if addr is undefined", () => { |
| 90 | + expect(IP.toInt).toThrow( |
| 91 | + "ipaddr: the address has neither IPv6 nor IPv4 format" |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + test("returns 0 when given a valid IPv6", () => { |
| 96 | + expect(IP.toInt("2001:db8:1234::1")).toBe(0); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe("validateIP()", () => { |
| 101 | + test("returns true if the provided string is a valid IPv4 address", () => { |
| 102 | + expect(IP.validateIP("1.1.1.1")).toBe(true); |
| 103 | + }); |
| 104 | + |
| 105 | + test("returns true if the provided string is a valid IPv6 address", () => { |
| 106 | + expect(IP.validateIP("2001:db8:1234::1")).toBe(true); |
| 107 | + }); |
| 108 | + |
| 109 | + test("returns false if the provided string is not a valid IPv4 or IPv6 address", () => { |
| 110 | + expect( |
| 111 | + IP.validateIP("some string that is not an IPv4 or IPv6 address") |
| 112 | + ).toBe(false); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe("normilizeIP()", () => { |
| 117 | + test("returns an IPv4 address with no leading 0's for each octet", () => { |
| 118 | + expect(IP.normilizeIP("01.01.01.01")).toBe("1.1.1.1"); |
| 119 | + }); |
| 120 | + |
| 121 | + test("returns an IPv6 address with explicit 0's octets (if any) and no leading 0's (if any)", () => { |
| 122 | + expect(IP.normilizeIP("2001:0db8:1234::0001")).toBe( |
| 123 | + "2001:db8:1234:0:0:0:0:1" |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + test("throw an error if a string that is not an IP is provided as input", () => { |
| 128 | + expect(() => { |
| 129 | + IP.normilizeIP("some string that is not an IPv4 or IPv6"); |
| 130 | + }).toThrow("ipaddr: the address has neither IPv6 nor IPv4 format"); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe("validateCIDR()", () => { |
| 135 | + test("returns true if provided a valid IPv4 CIDR address", () => { |
| 136 | + expect(IP.validateCIDR("1.1.1.0/24")).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + test("returns true if provided a valid IPv6 CIDR address", () => { |
| 140 | + expect(IP.validateCIDR("2001:0db8:1234::/64")).toBe(true); |
| 141 | + }); |
| 142 | + |
| 143 | + test("throw an error if a string that is not an IPv4 or IPv6 CIDR is provided as input", () => { |
| 144 | + expect( |
| 145 | + IP.validateCIDR("some string that is not an IPv4 or IPv6 CIDR address") |
| 146 | + ).toBe(false); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments