Skip to content

Commit

Permalink
full coverage on port-scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
iSkore committed May 15, 2020
1 parent 07b313b commit 72c4399
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 20 deletions.
6 changes: 6 additions & 0 deletions packages/port-scanner/esm/port-scanner.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mod from "../lib/port-scanner.js";

export default mod;
export const commonPorts = mod.commonPorts;
export const convertHighResolutionTime = mod.convertHighResolutionTime;
export const connect = mod.connect;
1 change: 0 additions & 1 deletion packages/port-scanner/lib/port-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const
LightMap = require( '@mi-sec/lightmap' ),
NetworkCidr = require( '@mi-sec/network-cidr' );

console.log( NetworkCidr );
const commonPorts = new LightMap( [
[ 7, 'echo' ],
[ 9, 'discard' ],
Expand Down
14 changes: 11 additions & 3 deletions packages/port-scanner/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/port-scanner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"build": "gen-esm-wrapper ./lib/* ./esm/port-scanner.mjs",
"lint": "eslint .",
"test": "nyc --reporter=lcov --reporter=text-summary mocha",
"testd": "nyc mocha"
"testd": "NODE_ENV=TESTING nyc mocha"
},
"keywords": [
"netx",
Expand All @@ -33,7 +33,7 @@
},
"dependencies": {
"@mi-sec/lightmap": "0.1.6",
"@mi-sec/network-cidr": "latest"
"@mi-sec/network-cidr": "1.0.2"
},
"devDependencies": {
"chai": "4.2.0",
Expand Down
59 changes: 45 additions & 14 deletions packages/port-scanner/test/port-scanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
'use strict';

const
net = require( 'net' ),
chai = require( 'chai' ),
{ expect } = chai;
chai = require( 'chai' ),
net = require( 'net' ),
{ ADDRCONFIG, V4MAPPED } = require( 'dns' ),
{ expect } = chai;

const
PortScanner = require( '../lib/port-scanner' ),
{ convertHighResolutionTime } = PortScanner;
PortScanner = require( '../lib/port-scanner' ),
{
connect,
convertHighResolutionTime
} = PortScanner;

describe( `${ process.env.npm_package_name } v${ process.env.npm_package_version } localhost`, function () {
const
host = '127.0.0.1/32',
banner = 'hello\r\n';
const
host = '127.0.0.1/32',
banner = 'hello\r\n';

// TODO: these tests are a bit flaky and rely on assumed compute drift timeouts. circle back sometime
describe( `${ process.env.npm_package_name } v${ process.env.npm_package_version } localhost`, function () {
let server, ports;

beforeEach( ( done ) => {
Expand Down Expand Up @@ -49,7 +54,7 @@ describe( `${ process.env.npm_package_name } v${ process.env.npm_package_version
} );
} );

it( `should scan target ${ host } and determine it "open" with banner "hello\\r\\n"`, async () => {
it( `should scan target ${ host } and determine "open" with banner "hello\\r\\n"`, async () => {
const start = process.hrtime();
const result = new PortScanner( { host, ports, attemptToIdentify: true } );

Expand All @@ -64,7 +69,7 @@ describe( `${ process.env.npm_package_name } v${ process.env.npm_package_version

const end = convertHighResolutionTime( process.hrtime( start ) );

expect( end ).to.be.lte( 5.0 );
expect( end ).to.be.lte( 10.0 );

expect( data ).to.be.an( 'object' );
expect( data ).to.have.property( 'host' ).and.be.a( 'string' ).and.eq( host.split( '/' )[ 0 ] );
Expand All @@ -81,7 +86,34 @@ describe( `${ process.env.npm_package_name } v${ process.env.npm_package_version
expect( done.get( key ) ).to.deep.eq( data );
} );

it( `should scan target ${ host } and determine it "close"`, async () => {
it( `should scan target ${ host } and determine "timeout"`, async () => {
const port = ports[ 0 ];
let d1, d2;

d1 = await connect( host, port, { timeout: 1, onlyReportOpen: true } );

try {
await connect( host, port, {
timeout: 0,
connectionOpts: {
hints: ADDRCONFIG | V4MAPPED
}
} );
}
catch ( e ) {
d2 = e;
}

expect( d1 ).to.eq( undefined );

expect( d2 ).to.be.an( 'object' );
expect( d2 ).to.have.property( 'host' ).and.eq( host );
expect( d2 ).to.have.property( 'port' ).and.eq( port );
expect( d2 ).to.have.property( 'status' ).and.eq( 'closed' );
expect( d2 ).to.have.property( 'error' ).and.be.an.instanceOf( Error );
} );

it( `should scan target ${ host } and determine "close"`, async () => {
server.close();

const start = process.hrtime();
Expand All @@ -97,7 +129,7 @@ describe( `${ process.env.npm_package_name } v${ process.env.npm_package_version
await result.scan();
const end = convertHighResolutionTime( process.hrtime( start ) );

expect( end ).to.be.lte( 5.0 );
expect( end ).to.be.lte( 10.0 );

expect( data ).to.be.an( 'object' );
expect( data ).to.have.property( 'host' ).and.be.a( 'string' ).and.eq( host.split( '/' )[ 0 ] );
Expand Down Expand Up @@ -168,7 +200,6 @@ describe( [
.on( 'progress', ( d ) => progress = d )
.on( 'done', ( d ) => {
expect( progress ).to.be.a( 'number' ).and.eq( 1 );
y;

expect( d.size ).to.eq( 4 );
expect( d.has( '127.0.0.1:80' ) ).to.eq( true );
Expand Down

0 comments on commit 72c4399

Please sign in to comment.