From e3783054a80d044a352a8398c9e756a9f59f7ca0 Mon Sep 17 00:00:00 2001 From: devodev Date: Sat, 10 Jul 2021 16:34:07 -0400 Subject: [PATCH] Add ipAddressStringToBigInt components util func. - Create new convertIPAddress component utils module. - Add tests for common use-cases. - Re-order component utils exports in index.ts. Signed-off-by: devodev --- .../utils/__tests__/convertIPAddress.test.ts | 69 +++++++++++++++++++ src/renderer/utils/convertIPAddress.ts | 44 ++++++++++++ src/renderer/utils/index.ts | 28 ++++---- 3 files changed, 127 insertions(+), 14 deletions(-) create mode 100644 src/renderer/utils/__tests__/convertIPAddress.test.ts create mode 100644 src/renderer/utils/convertIPAddress.ts diff --git a/src/renderer/utils/__tests__/convertIPAddress.test.ts b/src/renderer/utils/__tests__/convertIPAddress.test.ts new file mode 100644 index 0000000000..dadf14bbaf --- /dev/null +++ b/src/renderer/utils/__tests__/convertIPAddress.test.ts @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +import { ipAddressStringToBigInt } from "../convertIPAddress"; + +describe("ipAddressStringToBigInt tests", () => { + test("Empty string", () => { + expect(ipAddressStringToBigInt("")).toStrictEqual(BigInt(0)); + }); + + test("None value", () => { + expect(ipAddressStringToBigInt("None")).toStrictEqual(BigInt(0)); + }); + + test("Invalid IPV4", () => { + expect(ipAddressStringToBigInt("256.0.0.0")).toStrictEqual(BigInt(0)); + }); + + test("Invalid IPV6", () => { + expect(ipAddressStringToBigInt("fffg:ffff:ffff:ffff:ffff:ffff:ffff:ffff")).toStrictEqual(BigInt(0)); + }); + + test("Min IPV4", () => { + expect(ipAddressStringToBigInt("0.0.0.0")).toStrictEqual(BigInt(0)); + }); + + test("Min IPV6", () => { + expect(ipAddressStringToBigInt("::")).toStrictEqual(BigInt(0)); + }); + + test("Random IPV4", () => { + expect(ipAddressStringToBigInt("10.10.10.10")).toStrictEqual(BigInt("168430090")); + }); + + test("Random IPV4 2", () => { + expect(ipAddressStringToBigInt("172.16.1.1")).toStrictEqual(BigInt("2886729985")); + }); + + test("Random IPV6", () => { + expect(ipAddressStringToBigInt("2001:0db8:85a3::8a2e:0370:7334")).toStrictEqual(BigInt("42540766452641154071740215577757643572")); + }); + + + test("Max IPV4", () => { + expect(ipAddressStringToBigInt("255.255.255.255")).toStrictEqual(BigInt("4294967295")); + }); + + test("Max IPV6", () => { + expect(ipAddressStringToBigInt("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")).toStrictEqual(BigInt("340282366920938463463374607431768211455")); + }); +}); diff --git a/src/renderer/utils/convertIPAddress.ts b/src/renderer/utils/convertIPAddress.ts new file mode 100644 index 0000000000..bfce21427c --- /dev/null +++ b/src/renderer/utils/convertIPAddress.ts @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +import ipaddr from "ipaddr.js"; + +export function ipAddressStringToBigInt(value: string): BigInt { + let result = BigInt(0); + + if (!ipaddr.isValid(value)) { + return result; + } + + const byteArr = ipaddr.parse(value).toByteArray(); + let multiplier = byteArr.length; + + byteArr.forEach(byte => { + if (byte !== 0) { + const value = BigInt(byte) << BigInt(8*(multiplier-1)); + + result += value; + } + multiplier -= 1; + }); + + return result; +} diff --git a/src/renderer/utils/index.ts b/src/renderer/utils/index.ts index a0b20d1e2d..1ca538efa4 100755 --- a/src/renderer/utils/index.ts +++ b/src/renderer/utils/index.ts @@ -21,21 +21,21 @@ // Common usage utils & helpers -export * from "../../common/utils"; - -export * from "./cssVar"; -export * from "./cssNames"; export * from "../../common/event-emitter"; -export * from "./saveFile"; -export * from "./prevDefault"; -export * from "./storageHelper"; -export * from "./createStorage"; -export * from "./interval"; -export * from "./copyToClipboard"; -export * from "./formatDuration"; -export * from "./isReactNode"; -export * from "./convertMemory"; +export * from "../../common/utils"; export * from "./convertCpu"; -export * from "./metricUnitsToNumber"; +export * from "./convertIPAddress"; +export * from "./convertMemory"; +export * from "./copyToClipboard"; +export * from "./createStorage"; +export * from "./cssNames"; +export * from "./cssVar"; export * from "./display-booleans"; +export * from "./formatDuration"; +export * from "./interval"; export * from "./isMiddleClick"; +export * from "./isReactNode"; +export * from "./metricUnitsToNumber"; +export * from "./prevDefault"; +export * from "./saveFile"; +export * from "./storageHelper";