ip-navigator
Version:
A tool for IP address manipulation and calculation
95 lines • 6.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = require("../conversion/index");
// Describe the test suite
describe("IP Address Conversion", function () {
// Describe the test suite for the ipToBinary function
describe("ipToBinary", function () {
it("should convert valid IP addresses to binary", function () {
expect((0, index_1.ipToBinary)("192.168.1.1")).toBe("11000000.10101000.00000001.00000001");
expect((0, index_1.ipToBinary)("10.0.0.1")).toBe("00001010.00000000.00000000.00000001");
expect((0, index_1.ipToBinary)("255.255.255.255")).toBe("11111111.11111111.11111111.11111111");
expect((0, index_1.ipToBinary)("0.0.0.0")).toBe("00000000.00000000.00000000.00000000");
});
it("should throw an error for invalid IP addresses", function () {
expect(function () { return (0, index_1.ipToBinary)("256.0.0.1"); }).toThrow("Invalid IP address");
expect(function () { return (0, index_1.ipToBinary)("192.168.1"); }).toThrow("Invalid IP address");
expect(function () { return (0, index_1.ipToBinary)("a.b.c.d"); }).toThrow("Invalid IP address");
});
});
// Describe the test suite for the binaryToIP function
describe("binaryToIP", function () {
it("should convert valid binary representations to IP addresses", function () {
expect((0, index_1.binaryToIP)("11000000.10101000.00000001.00000001")).toBe("192.168.1.1");
expect((0, index_1.binaryToIP)("00001010.00000000.00000000.00000001")).toBe("10.0.0.1");
expect((0, index_1.binaryToIP)("11111111.11111111.11111111.11111111")).toBe("255.255.255.255");
expect((0, index_1.binaryToIP)("00000000.00000000.00000000.00000000")).toBe("0.0.0.0");
});
it("should throw an error for invalid binary representations", function () {
expect(function () { return (0, index_1.binaryToIP)("1100000.10101000.00000001.00000001"); }).toThrow("Invalid binary IP address");
expect(function () {
return (0, index_1.binaryToIP)("11000000.10101000.00000001.00000001.00000000");
}).toThrow("Invalid binary IP address");
expect(function () { return (0, index_1.binaryToIP)("11000000.10101000.00000001.000000012"); }).toThrow("Invalid binary IP address");
});
});
// Describe the test suite for the ipToInteger function
describe("ipToInteger", function () {
it("should convert valid IP addresses to integers", function () {
expect((0, index_1.ipToInteger)("192.168.1.1")).toBe(3232235777);
expect((0, index_1.ipToInteger)("10.0.0.1")).toBe(167772161);
expect((0, index_1.ipToInteger)("255.255.255.255")).toBe(4294967295);
expect((0, index_1.ipToInteger)("0.0.0.0")).toBe(0);
});
it("should throw an error for invalid IP addresses", function () {
expect(function () { return (0, index_1.ipToInteger)("256.0.0.1"); }).toThrow("Invalid IP address");
expect(function () { return (0, index_1.ipToInteger)("192.168.1"); }).toThrow("Invalid IP address");
expect(function () { return (0, index_1.ipToInteger)("a.b.c.d"); }).toThrow("Invalid IP address");
});
});
// Describe the test suite for the integerToIP function
describe("integerToIP", function () {
it("should convert valid integers to IP addresses", function () {
expect((0, index_1.integerToIP)(3232235777)).toBe("192.168.1.1");
expect((0, index_1.integerToIP)(167772161)).toBe("10.0.0.1");
expect((0, index_1.integerToIP)(4294967295)).toBe("255.255.255.255");
expect((0, index_1.integerToIP)(0)).toBe("0.0.0.0");
});
it("should throw an error for invalid integers", function () {
expect(function () { return (0, index_1.integerToIP)(-1); }).toThrow("Input must be an integer between 0 and 4294967295");
expect(function () { return (0, index_1.integerToIP)(4294967296); }).toThrow("Input must be an integer between 0 and 4294967295");
expect(function () { return (0, index_1.integerToIP)(3.14); }).toThrow("Input must be an integer between 0 and 4294967295");
});
});
// Describe the test suite for the cidrToSubnetMask function
describe("cidrToSubnetMask", function () {
it("should convert valid CIDR notations to subnet masks", function () {
expect((0, index_1.cidrToSubnetMask)(24)).toBe("255.255.255.0");
expect((0, index_1.cidrToSubnetMask)(16)).toBe("255.255.0.0");
expect((0, index_1.cidrToSubnetMask)(8)).toBe("255.0.0.0");
expect((0, index_1.cidrToSubnetMask)(32)).toBe("255.255.255.255");
expect((0, index_1.cidrToSubnetMask)(0)).toBe("0.0.0.0");
});
it("should throw an error for invalid CIDR notations", function () {
expect(function () { return (0, index_1.cidrToSubnetMask)(-1); }).toThrow("CIDR must be an integer between 0 and 32");
expect(function () { return (0, index_1.cidrToSubnetMask)(33); }).toThrow("CIDR must be an integer between 0 and 32");
expect(function () { return (0, index_1.cidrToSubnetMask)(3.14); }).toThrow("CIDR must be an integer between 0 and 32");
});
});
describe("subnetMaskToCIDR", function () {
it("should convert valid subnet masks to CIDR notations", function () {
expect((0, index_1.subnetMaskToCIDR)("255.255.255.0")).toBe(24);
expect((0, index_1.subnetMaskToCIDR)("255.255.0.0")).toBe(16);
expect((0, index_1.subnetMaskToCIDR)("255.0.0.0")).toBe(8);
expect((0, index_1.subnetMaskToCIDR)("255.255.255.255")).toBe(32);
expect((0, index_1.subnetMaskToCIDR)("0.0.0.0")).toBe(0);
});
it("should throw an error for invalid subnet masks", function () {
expect(function () { return (0, index_1.subnetMaskToCIDR)("255.255.255.1"); }).toThrow("Invalid subnet mask");
expect(function () { return (0, index_1.subnetMaskToCIDR)("255.0.255.0"); }).toThrow("Invalid subnet mask");
expect(function () { return (0, index_1.subnetMaskToCIDR)("192.168.1.1"); }).toThrow("Invalid subnet mask");
expect(function () { return (0, index_1.subnetMaskToCIDR)("a.b.c.d"); }).toThrow("Invalid subnet mask");
});
});
});
//# sourceMappingURL=conversion.test.js.map