From 488c458720bd2c5ab2d2933eab9075a4afbee323 Mon Sep 17 00:00:00 2001 From: jb30795 <> Date: Fri, 20 Sep 2024 10:46:09 +0100 Subject: [PATCH] updates to operation and addition of more tests --- .../operations/IPv6TransitionAddresses.mjs | 26 ++++++++++--------- tests/operations/tests/IPv6Transition.mjs | 20 ++++++++++++++ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/core/operations/IPv6TransitionAddresses.mjs b/src/core/operations/IPv6TransitionAddresses.mjs index 7f2f05c5..4af5eb72 100644 --- a/src/core/operations/IPv6TransitionAddresses.mjs +++ b/src/core/operations/IPv6TransitionAddresses.mjs @@ -43,7 +43,7 @@ class IPv6TransitionAddresses extends Operation { * @returns {string} */ run(input, args) { - const XOR = {"0": "2", "1": "3", "2": "0", "3": "1", "4": "6", "5": "7", "6": "4", "7": "5", "8": "A", "9": "B", "A": "8", "B": "9", "C": "E", "D": "F", "E": "C", "F": "D"}; + const XOR = {"0": "2", "1": "3", "2": "0", "3": "1", "4": "6", "5": "7", "6": "4", "7": "5", "8": "a", "9": "b", "a": "8", "b": "9", "c": "e", "d": "f", "e": "c", "f": "d"}; /** * Function to convert to hex @@ -126,12 +126,13 @@ class IPv6TransitionAddresses extends Operation { */ function macTransition(input) { let output = ""; + console.log(input); const MACPARTS = input.split(":"); if (!args[1]) { output += "EUI-64 Interface ID: "; } const MAC = MACPARTS[0] + MACPARTS[1] + ":" + MACPARTS[2] + "ff:fe" + MACPARTS[3] + ":" + MACPARTS[4] + MACPARTS[5]; - output += MAC.slice(0, 1) + XOR[MAC.slice(1, 2).toUpperCase()].toLowerCase() + MAC.slice(2); + output += MAC.slice(0, 1) + XOR[MAC.slice(1, 2)] + MAC.slice(2); return output; } @@ -183,17 +184,18 @@ class IPv6TransitionAddresses extends Operation { let inputs = input.split("\n"); // Remove blank rows inputs = inputs.filter(Boolean); - for (let input = 0; input < inputs.length; input++) { + + for (let i = 0; i < inputs.length; i++) { // if ignore ranges is checked and input is a range, skip - if ((args[0] && !inputs[input].includes("/")) || (!args[0])) { - if (/^[0-9]{1,3}(?:\.[0-9]{1,3}){3}$/.test(inputs[input])) { - output += ipTransition(inputs[input], false); - } else if (/\/24$/.test(inputs[input])) { - output += ipTransition(inputs[input], true); - } else if (/^([0-9A-F]{2}:){5}[0-9A-F]{2}$/.test(inputs[input].toUpperCase())) { - output += macTransition(input.toLowerCase()); - } else if (/^((?:[0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?:(?::[0-9a-fA-F]{1,4}){1,6})|:(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(?::[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])|(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/.test(inputs[input])) { - output += unTransition(inputs[input]); + if ((args[0] && !inputs[i].includes("/")) || (!args[0])) { + if (/^[0-9]{1,3}(?:\.[0-9]{1,3}){3}$/.test(inputs[i])) { + output += ipTransition(inputs[i], false); + } else if (/\/24$/.test(inputs[i])) { + output += ipTransition(inputs[i], true); + } else if (/^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$/.test(inputs[i])) { + output += macTransition(inputs[i]); + } else if (/^((?:[0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?:(?::[0-9a-fA-F]{1,4}){1,6})|:(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(?::[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])|(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/.test(inputs[i])) { + output += unTransition(inputs[i]); } else { output = "Enter compressed or expanded IPv6 address, IPv4 address or MAC Address."; } diff --git a/tests/operations/tests/IPv6Transition.mjs b/tests/operations/tests/IPv6Transition.mjs index c658f0fb..f7558c31 100644 --- a/tests/operations/tests/IPv6Transition.mjs +++ b/tests/operations/tests/IPv6Transition.mjs @@ -19,6 +19,26 @@ TestRegister.addTests([ args: [true, false], }, ], + }, { + name: "IPv6 Transition: IPv4 /24 Range to IPv6", + input: "198.51.100.0/24", + expectedOutput: "6to4: 2002:c633:6400::/40\nIPv4 Mapped: ::ffff:c633:6400/120\nIPv4 Translated: ::ffff:0:c633:6400/120\nNat 64: 64:ff9b::c633:6400/120", + recipeConfig: [ + { + op: "IPv6 Transition Addresses", + args: [false, false], + }, + ], + }, { + name: "IPv6 Transition: IPv4 to IPv6 Remove headers", + input: "198.51.100.7", + expectedOutput: "2002:c633:6407::/48\n::ffff:c633:6407\n::ffff:0:c633:6407\n64:ff9b::c633:6407", + recipeConfig: [ + { + op: "IPv6 Transition Addresses", + args: [true, true], + }, + ], }, { name: "IPv6 Transition: IPv6 to IPv4", input: "64:ff9b::c633:6407",