From 6630906944a2e14cfecca2dd28280bd375fcfdc8 Mon Sep 17 00:00:00 2001 From: L4chsalter Date: Fri, 22 May 2020 15:02:54 +0200 Subject: [PATCH 1/2] Added calculation of Checksum, so it can be checked if the checksum is actually correct --- src/CommonValidator.js | 27 +++++++++++++++++++++++++++ src/TLEValidator.js | 18 ++++++++++++++++++ test/unit/CommonValidator.test.js | 12 ++++++++++++ test/unit/TLEValidator.test.js | 16 ++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/src/CommonValidator.js b/src/CommonValidator.js index edc58e4..e1f7b7c 100644 --- a/src/CommonValidator.js +++ b/src/CommonValidator.js @@ -19,6 +19,33 @@ module.exports = { let checkSum = line.substr(68, 1); return numberRegExp.test(checkSum); }, + checkCheckSum: function(line) { + let result = {isValid: true}; + + let providedCheckSum = Number(line.slice(-1)); //extract the checksum + line = line.slice(0, -1); //remove checksum from line + let checksum = 0; + line.split('').forEach(function(element) { + if (element === '-') { + checksum++; + } + if (!isNaN(element)) { + checksum += Number(element); + } + }); + + checksum = checksum % 10; + if (checksum === providedCheckSum) { + + } else { + result = { + isValid: false, + calculatedCheckSum: checksum + }; + } + + return result; + }, validSatNo: function(line) { let result = false; let satNo = line.substr(2, 5); diff --git a/src/TLEValidator.js b/src/TLEValidator.js index 4d37932..be532bb 100644 --- a/src/TLEValidator.js +++ b/src/TLEValidator.js @@ -68,6 +68,15 @@ module.exports = { let result = { isValid: lineOneRegExp.test(line1) }; + if (result.isValid) { + let checkSumResult = CommonValidator.checkCheckSum(line1); + if (checkSumResult.isValid === false) { + result = { + invalidSubStr: [68, 1], + message: "Incorrect Checksum, expected: "+checkSumResult.calculatedCheckSum + }; + } + } if (!result.isValid) { if (!LineOneValidator.validLineOneNumber(line1)) { result = { @@ -188,6 +197,15 @@ module.exports = { let result = { isValid: lineTwoRegExp.test(line2) }; + if (result.isValid) { + let checkSumResult = CommonValidator.checkCheckSum(line2); + if (checkSumResult.isValid === false) { + result = { + invalidSubStr: [68, 1], + message: "Incorrect Checksum, expected: "+checkSumResult.calculatedCheckSum + }; + } + } if (!result.isValid) { if (!LineTwoValidator.validLineTwoNumber(line2)) { result = { diff --git a/test/unit/CommonValidator.test.js b/test/unit/CommonValidator.test.js index 717b18c..4e53f6d 100644 --- a/test/unit/CommonValidator.test.js +++ b/test/unit/CommonValidator.test.js @@ -23,6 +23,18 @@ test('validates invalid checksum', () => { expect(CommonValidator.validCheckSum("1 25544U 98067777 08264.51782528 -.00002182 00000-0 -00000-0 0 0001 ")).toBe(false); }); +test('validates checksum calculation', () => { + expect(CommonValidator.checkCheckSum("1 41868U 16061G 20139.90454403 +.00034898 +87835-5 +88966-4 0 9994")).toEqual({ + isValid: true, + }); +}); +test('validates invalid checksum calculation', () => { + expect(CommonValidator.checkCheckSum("1 41868U 16061G 20139.90454403 +.00034898 +87835-5 +88966-4 0 9995")).toEqual({ + isValid: false, + calculatedCheckSum: 4 + }); +}); + test('validates valid satNo', () => { const validLineOne = "1 25544 "; expect(CommonValidator.validSatNo(validLineOne)).toBe(true); diff --git a/test/unit/TLEValidator.test.js b/test/unit/TLEValidator.test.js index e08183a..2b4fc82 100644 --- a/test/unit/TLEValidator.test.js +++ b/test/unit/TLEValidator.test.js @@ -106,6 +106,14 @@ test('line1 invalid checksum', () => { }); }); +test('line1 incorrect checksum', () => { + expect(TLEValidator.validateLineOneWithMessage("1 25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2928")).toEqual({ + + invalidSubStr: [68, 1], + message: "Incorrect Checksum, expected: 7" + }); +}); + test('line1 invalid space', () => { expect(TLEValidator.validateLineOneWithMessage("1a25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2927")).toEqual({ @@ -230,6 +238,14 @@ test('line2 invalid Checksum', () => { }); }); +test('line2 incorrect Checksum', () => { + expect(TLEValidator.validateLineTwoWithMessage("2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563538")).toEqual({ + + invalidSubStr: [68, 1], + message: "Incorrect Checksum, expected: 7" + }); +}); + test('line2 invalid space', () => { expect(TLEValidator.validateLineTwoWithMessage("2.25544 51.6416 247.4627 0006703 130.5360 325.0288 15.7212539156353a")).toEqual({ invalidSubStr: [1, 1], From 8a64db0799ddd1a637aa2601c8ef06713536df31 Mon Sep 17 00:00:00 2001 From: lachsalter Date: Thu, 28 May 2020 11:08:05 +0200 Subject: [PATCH 2/2] Modified the checking for line1: FirstMeanMotion, SecondeMeanMotion, BStarDrag It is typical to find multiple styles for those values, eg with or without leading space or +-0 --- src/LineOneValidator.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LineOneValidator.js b/src/LineOneValidator.js index 7624a72..8a09043 100644 --- a/src/LineOneValidator.js +++ b/src/LineOneValidator.js @@ -51,7 +51,7 @@ module.exports = { let result = false; let firstChar = line1.substr(33, 1); let secondChar = line1.substr(34, 1); - if (CommonValidator.isPosNegOrZero(firstChar) && CommonValidator.isPeriod(secondChar)) { + if ((CommonValidator.isPosNegOrZero(firstChar) || CommonValidator.isSpace(line1, 33) ) && CommonValidator.isPeriod(secondChar)) { let firstMeanMotionFraction = line1.substr(35, 8); if (eightDigitsRegExp.test(firstMeanMotionFraction)) { result = true; @@ -64,7 +64,7 @@ module.exports = { let preDashNums = line1.substr(45, 5); let hyphen = line1.substr(50, 1); let lastChar = line1.substr(51, 1); - if (CommonValidator.isSpace(line1, 44)) { + if (CommonValidator.isPosNegOrZero(line1.substr(44, 1)) || CommonValidator.isSpace(line1, 44)) { if (fiveDigitsRegExp.test(preDashNums)) { if (CommonValidator.isHyphen(hyphen)) { if (!isNaN(lastChar)) { @@ -82,7 +82,7 @@ module.exports = { let preDashNums = line1.substr(54, 5); let hyphen = line1.substr(59, 1); let lastChar = line1.substr(60, 1); - if (CommonValidator.isPosNegOrZero(firstChar)) { + if (CommonValidator.isPosNegOrZero(firstChar) || CommonValidator.isSpace(line1, 53)) { if (fiveDigitsRegExp.test(preDashNums)) { if (CommonValidator.isHyphen(hyphen)) { if (!isNaN(lastChar)) {