Merge branch 'csmith-ip-format-octal'
This commit is contained in:
commit
1c9c0a48be
@ -29,12 +29,12 @@ class ChangeIPFormat extends Operation {
|
|||||||
{
|
{
|
||||||
"name": "Input format",
|
"name": "Input format",
|
||||||
"type": "option",
|
"type": "option",
|
||||||
"value": ["Dotted Decimal", "Decimal", "Hex"]
|
"value": ["Dotted Decimal", "Decimal", "Octal", "Hex"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Output format",
|
"name": "Output format",
|
||||||
"type": "option",
|
"type": "option",
|
||||||
"value": ["Dotted Decimal", "Decimal", "Hex"]
|
"value": ["Dotted Decimal", "Decimal", "Octal", "Hex"]
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -54,7 +54,6 @@ class ChangeIPFormat extends Operation {
|
|||||||
if (lines[i] === "") continue;
|
if (lines[i] === "") continue;
|
||||||
let baIp = [];
|
let baIp = [];
|
||||||
let octets;
|
let octets;
|
||||||
let decimal;
|
|
||||||
|
|
||||||
if (inFormat === outFormat) {
|
if (inFormat === outFormat) {
|
||||||
output += lines[i] + "\n";
|
output += lines[i] + "\n";
|
||||||
@ -70,11 +69,10 @@ class ChangeIPFormat extends Operation {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "Decimal":
|
case "Decimal":
|
||||||
decimal = lines[i].toString();
|
baIp = this.fromNumber(lines[i].toString(), 10);
|
||||||
baIp.push(decimal >> 24 & 255);
|
break;
|
||||||
baIp.push(decimal >> 16 & 255);
|
case "Octal":
|
||||||
baIp.push(decimal >> 8 & 255);
|
baIp = this.fromNumber(lines[i].toString(), 8);
|
||||||
baIp.push(decimal & 255);
|
|
||||||
break;
|
break;
|
||||||
case "Hex":
|
case "Hex":
|
||||||
baIp = fromHex(lines[i]);
|
baIp = fromHex(lines[i]);
|
||||||
@ -100,6 +98,10 @@ class ChangeIPFormat extends Operation {
|
|||||||
decIp = ((baIp[0] << 24) | (baIp[1] << 16) | (baIp[2] << 8) | baIp[3]) >>> 0;
|
decIp = ((baIp[0] << 24) | (baIp[1] << 16) | (baIp[2] << 8) | baIp[3]) >>> 0;
|
||||||
output += decIp.toString() + "\n";
|
output += decIp.toString() + "\n";
|
||||||
break;
|
break;
|
||||||
|
case "Octal":
|
||||||
|
decIp = ((baIp[0] << 24) | (baIp[1] << 16) | (baIp[2] << 8) | baIp[3]) >>> 0;
|
||||||
|
output += "0" + decIp.toString(8) + "\n";
|
||||||
|
break;
|
||||||
case "Hex":
|
case "Hex":
|
||||||
hexIp = "";
|
hexIp = "";
|
||||||
for (j = 0; j < baIp.length; j++) {
|
for (j = 0; j < baIp.length; j++) {
|
||||||
@ -115,6 +117,22 @@ class ChangeIPFormat extends Operation {
|
|||||||
return output.slice(0, output.length-1);
|
return output.slice(0, output.length-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an array of IP address octets from a numerical value.
|
||||||
|
* @param {string} value The value of the IP address
|
||||||
|
* @param {number} radix The numeral system to be used
|
||||||
|
* @returns {number[]}
|
||||||
|
*/
|
||||||
|
fromNumber(value, radix) {
|
||||||
|
const decimal = parseInt(value, radix);
|
||||||
|
const baIp = [];
|
||||||
|
baIp.push(decimal >> 24 & 255);
|
||||||
|
baIp.push(decimal >> 16 & 255);
|
||||||
|
baIp.push(decimal >> 8 & 255);
|
||||||
|
baIp.push(decimal & 255);
|
||||||
|
return baIp;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ChangeIPFormat;
|
export default ChangeIPFormat;
|
||||||
|
@ -26,6 +26,7 @@ import "./tests/BitwiseOp";
|
|||||||
import "./tests/ByteRepr";
|
import "./tests/ByteRepr";
|
||||||
import "./tests/CartesianProduct";
|
import "./tests/CartesianProduct";
|
||||||
import "./tests/CharEnc";
|
import "./tests/CharEnc";
|
||||||
|
import "./tests/ChangeIPFormat";
|
||||||
import "./tests/Charts";
|
import "./tests/Charts";
|
||||||
import "./tests/Checksum";
|
import "./tests/Checksum";
|
||||||
import "./tests/Ciphers";
|
import "./tests/Ciphers";
|
||||||
|
52
tests/operations/tests/ChangeIPFormat.mjs
Normal file
52
tests/operations/tests/ChangeIPFormat.mjs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
/**
|
||||||
|
* Change IP format tests.
|
||||||
|
*
|
||||||
|
* @author Chris Smith
|
||||||
|
* @copyright Crown Copyright 2019
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
import TestRegister from "../../lib/TestRegister.mjs";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "Change IP format: Dotted Decimal to Hex",
|
||||||
|
input: "192.168.1.1",
|
||||||
|
expectedOutput: "c0a80101",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Change IP format",
|
||||||
|
args: ["Dotted Decimal", "Hex"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}, {
|
||||||
|
name: "Change IP format: Decimal to Dotted Decimal",
|
||||||
|
input: "3232235777",
|
||||||
|
expectedOutput: "192.168.1.1",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Change IP format",
|
||||||
|
args: ["Decimal", "Dotted Decimal"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}, {
|
||||||
|
name: "Change IP format: Hex to Octal",
|
||||||
|
input: "c0a80101",
|
||||||
|
expectedOutput: "030052000401",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Change IP format",
|
||||||
|
args: ["Hex", "Octal"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}, {
|
||||||
|
name: "Change IP format: Octal to Decimal",
|
||||||
|
input: "030052000401",
|
||||||
|
expectedOutput: "3232235777",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Change IP format",
|
||||||
|
args: ["Octal", "Decimal"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
Loading…
x
Reference in New Issue
Block a user