From 73f504a37c139555f61b4db954aa0ae992f2fb65 Mon Sep 17 00:00:00 2001 From: Massimo Candela Date: Thu, 30 Jul 2020 14:25:10 +0200 Subject: [PATCH] cover with tests prefix generation procedure --- .gitignore | 3 +- .npmignore | 1 + .travis.yml | 2 + package.json | 1 + src/inputs/inputYml.js | 1 - tests/generate_tests/config.test.yml | 84 ++++++++++++ .../generate_tests/prefixes.final.default.yml | 46 +++++++ tests/generate_tests/prefixes.final.group.yml | 46 +++++++ tests/generate_tests/tests.js | 122 ++++++++++++++++++ 9 files changed, 304 insertions(+), 2 deletions(-) create mode 100644 tests/generate_tests/config.test.yml create mode 100644 tests/generate_tests/prefixes.final.default.yml create mode 100644 tests/generate_tests/prefixes.final.group.yml create mode 100644 tests/generate_tests/tests.js diff --git a/.gitignore b/.gitignore index aea185e..3099317 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ alertdata/ .npmrc .cache/ volumetests/ -tests/rpki_tests/vrp.json \ No newline at end of file +tests/rpki_tests/vrp.json +tests/generate_tests/prefixes.yml \ No newline at end of file diff --git a/.npmignore b/.npmignore index ef92925..3099317 100644 --- a/.npmignore +++ b/.npmignore @@ -12,3 +12,4 @@ alertdata/ .cache/ volumetests/ tests/rpki_tests/vrp.json +tests/generate_tests/prefixes.yml \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 3495fdd..9412db0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,8 @@ jobs: script: npm run test-reports - name: "Tests rpki" script: npm run test-rpki + - name: "Tests prefix generation" + script: npm run test-generate - name: "Tests with proxy" before_script: - npm install -g anyproxy diff --git a/package.json b/package.json index 10f4765..70058b3 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "test": "rm -rf volumetests/ && ./node_modules/.bin/mocha --exit tests/*.js --require @babel/register && rm -rf volumetests/", "test-reports": "./node_modules/.bin/mocha --exit tests/reports_tests/*.js --require @babel/register", "test-proxy": "./node_modules/.bin/mocha --exit tests/proxy_tests/*.js --require @babel/register", + "test-generate": "./node_modules/.bin/mocha --exit tests/generate_tests/*.js --require @babel/register", "test-rpki": "./node_modules/.bin/mocha --exit tests/rpki_tests/tests.default.js --require @babel/register && ./node_modules/.bin/mocha --exit tests/rpki_tests/tests.external.js --require @babel/register", "build": "./build.sh", "watch-and-serve": "nodemon -e yml,js,json,txt --inspect --exec babel-node index.js", diff --git a/src/inputs/inputYml.js b/src/inputs/inputYml.js index 11cc0b3..7aad6a7 100644 --- a/src/inputs/inputYml.js +++ b/src/inputs/inputYml.js @@ -36,7 +36,6 @@ import Input from "./input"; import ipUtils from "ip-sub"; import { AS } from "../model"; - export default class InputYml extends Input { constructor(config){ diff --git a/tests/generate_tests/config.test.yml b/tests/generate_tests/config.test.yml new file mode 100644 index 0000000..d0fb10f --- /dev/null +++ b/tests/generate_tests/config.test.yml @@ -0,0 +1,84 @@ +environment: test + +connectors: + - file: connectorTest + name: tes + params: + testType: withdrawal + +monitors: + - file: monitorHijack + channel: hijack + name: basic-hijack-detection + params: + thresholdMinPeers: 0 + + - file: monitorNewPrefix + channel: newprefix + name: prefix-detection + params: + thresholdMinPeers: 0 + + - file: monitorVisibility + channel: visibility + name: withdrawal-detection + params: + thresholdMinPeers: 4 + + - file: monitorPath + channel: path + name: path-matching + params: + thresholdMinPeers: 0 + + - file: monitorAS + channel: misconfiguration + name: asn-monitor + params: + thresholdMinPeers: 2 + + - file: monitorRPKI + channel: rpki + name: rpki-monitor + params: + thresholdMinPeers: 1 + preCacheROAs: false + checkUncovered: true + + +reports: +# - file: reportFile +# channels: +# - hijack +# - newprefix +# - visibility +# - path +# - misconfiguration +# - rpki +# params: +# persistAlertData: false +# alertDataDirectory: alertdata/ + + +# The file containing the monitored prefixes. Please see monitored_prefixes_test.yml for an example +# This is an array (use new lines and dashes!) +monitoredPrefixesFiles: + - prefixes.yml + +logging: + directory: logs + logRotatePattern: YYYY-MM-DD # Whenever the pattern changes, a new file is created and the old one rotated + backlogSize: 1 + maxRetainedFiles: 10 + maxFileSizeMB: 15 + compressOnRotation: true + +checkForUpdatesAtBoot: false + +notificationIntervalSeconds: 1800 # Repeat the same alert (which keeps being triggered) after x seconds +alertOnlyOnce: false +fadeOffSeconds: 10 +checkFadeOffGroupsSeconds: 2 +pidFile: bgpalerter.pid +multiProcess: false +maxMessagesPerSecond: 6000 \ No newline at end of file diff --git a/tests/generate_tests/prefixes.final.default.yml b/tests/generate_tests/prefixes.final.default.yml new file mode 100644 index 0000000..4a9435a --- /dev/null +++ b/tests/generate_tests/prefixes.final.default.yml @@ -0,0 +1,46 @@ +193.0.10.0/23: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: default +193.0.20.0/23: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: default +193.0.0.0/21: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: default +193.0.12.0/23: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: default +193.0.18.0/23: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: default +193.0.22.0/23: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: default +options: + monitorASns: + '3333': + group: default diff --git a/tests/generate_tests/prefixes.final.group.yml b/tests/generate_tests/prefixes.final.group.yml new file mode 100644 index 0000000..70b26a2 --- /dev/null +++ b/tests/generate_tests/prefixes.final.group.yml @@ -0,0 +1,46 @@ +193.0.10.0/23: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: test +193.0.20.0/23: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: test +193.0.0.0/21: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: test +193.0.12.0/23: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: test +193.0.18.0/23: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: test +193.0.22.0/23: + description: No description provided (valid ROA available) + asn: + - 3333 + ignoreMorespecifics: false + ignore: false + group: test +options: + monitorASns: + '3333': + group: test diff --git a/tests/generate_tests/tests.js b/tests/generate_tests/tests.js new file mode 100644 index 0000000..d373dd1 --- /dev/null +++ b/tests/generate_tests/tests.js @@ -0,0 +1,122 @@ +/* + * BSD 3-Clause License + * + * Copyright (c) 2019, NTT Ltd. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +const chai = require("chai"); +const fs = require("fs"); +const yaml = require("js-yaml"); +const chaiSubset = require('chai-subset'); +const generatePrefixes = require('../../src/generatePrefixesList'); +const expect = chai.expect; +const asyncTimeout = 60000; + +chai.use(chaiSubset); + +global.EXTERNAL_CONFIG_FILE = "tests/generate_tests/config.test.yml"; +// const worker = require("../../index"); +// const pubSub = worker.pubSub; + +describe("Prefix List", function() { + + it("generate file - default group", function (done) { + const asns = ["3333"]; + const outputFile = "tests/generate_tests/prefixes.yml"; + const originalFile = "tests/generate_tests/prefixes.final.default.yml"; + + const inputParameters = { + asnList: asns, + outputFile, + exclude: ["2001:67c:2e8::/48"], + excludeDelegated: true, + prefixes: null, + monitoredASes: asns, + httpProxy: null, + debug: false, + historical: false, + group: null + } + console.log = function(){}; + generatePrefixes(inputParameters) + .then(() => { + const result = fs.readFileSync(outputFile, 'utf8'); + fs.unlinkSync(outputFile); + const original = fs.readFileSync(originalFile, 'utf8'); + const resultJson = yaml.safeLoad(result) || {}; + const originalJson = yaml.safeLoad(original) || {}; + + Object.keys(resultJson) + Object.keys(originalJson) + + expect(resultJson).to.contain.keys(Object.keys(originalJson)); + expect(Object.keys(resultJson).length).to.equal(Object.keys(originalJson).length); + + expect(resultJson).to.containSubset(originalJson); + done(); + }); + }).timeout(asyncTimeout); + + it("generate file - specific group", function (done) { + const asns = ["3333"]; + const outputFile = "tests/generate_tests/prefixes.yml"; + const originalFile = "tests/generate_tests/prefixes.final.group.yml"; + + const inputParameters = { + asnList: asns, + outputFile, + exclude: ["2001:67c:2e8::/48"], + excludeDelegated: true, + prefixes: null, + monitoredASes: asns, + httpProxy: null, + debug: false, + historical: false, + group: "test" + } + console.log = function(){}; + generatePrefixes(inputParameters) + .then(() => { + const result = fs.readFileSync(outputFile, 'utf8'); + fs.unlinkSync(outputFile); + const original = fs.readFileSync(originalFile, 'utf8'); + const resultJson = yaml.safeLoad(result) || {}; + const originalJson = yaml.safeLoad(original) || {}; + + Object.keys(resultJson) + Object.keys(originalJson) + + expect(resultJson).to.contain.keys(Object.keys(originalJson)); + expect(Object.keys(resultJson).length).to.equal(Object.keys(originalJson).length); + + expect(resultJson).to.containSubset(originalJson); + done(); + }); + }).timeout(asyncTimeout); +}); \ No newline at end of file