import { assert } from 'chai' ; import * as fs from 'fs' ; import { ComponentDetails , IArtifact , IGraphResponse , ISummaryResponse } from 'jfrog-client-js' ; import * as path from 'path' ; import Set from 'typescript-collections/dist/lib/Set' ; import * as vscode from 'vscode' ; import { ConnectionManager } from '../../main/connect/connectionManager' ; import { ScanCacheManager } from '../../main/scanCache/scanCacheManager' ; import { AbstractScanLogic } from '../../main/scanLogic/abstractScanLogic' ; import { ComponentSummaryScanLogic } from '../../main/scanLogic/componentSummaryScanLogic' ; import { GraphScanLogic } from '../../main/scanLogic/graphScanLogic' ; import { ILicenseKey } from '../../main/types/licenseKey' ; import { INodeInfo } from '../../main/types/nodeInfo' ; import { Severity } from '../../main/types/severity' ; import { createScanCacheManager } from './utils/utils.test' ; describe ( 'Scan Logic Tests' , ( ) => { const scanResponses = path . join ( __dirname , '..' , 'resources' , 'scanResponses' ) ; it ( 'component/summary' , async ( ) => { let dummyScanCacheManager = createScanCacheManager ( ) ; let dummyConnectionManager = createComponentSummaryConnectionManager ( ) ; let scanLogic = new ComponentSummaryScanLogic ( dummyConnectionManager , dummyScanCacheManager ) ; testScanLogic ( dummyScanCacheManager , scanLogic , false ) ; } ) ; it ( 'scan/graph vulnerabilities' , async ( ) => { let dummyScanCacheManager = createScanCacheManager ( ) ; let dummyConnectionManager = createScanGraphConnectionManager ( 'graphScanVulnerabilities' ) ; let scanLogic = new GraphScanLogic ( dummyConnectionManager , dummyScanCacheManager ) ; testScanLogic ( dummyScanCacheManager , scanLogic , false ) ; } ) ; it ( 'scan/graph violations' , async ( ) => { let dummyScanCacheManager = createScanCacheManager ( ) ; let dummyConnectionManager = createScanGraphConnectionManager ( 'graphScanViolations' ) ; let scanLogic = new GraphScanLogic ( dummyConnectionManager , dummyScanCacheManager ) ; testScanLogic ( dummyScanCacheManager , scanLogic , true ) ; } ) ; async function testScanLogic ( scanCacheManager , scanLogic , licenseViolated ) { let componentsToScan = new Set < ComponentDetails > ( component => component . component_id ) ; componentsToScan . add ( { component_id : 'gav://io.netty:netty-codec-http:4.1.31.Final' } ) ; componentsToScan . add ( { component_id : 'gav://org.apache.commons:commons-lang3:3.12.0' } ) ; componentsToScan . add ( { component_id : 'gav://commons-io:commons-io:2.11.0' } ) ; await scanLogic . scanAndCache ( { report : ( ) => false } as vscode . Progress < { message ? : string ; increment ? : number ; } > , componentsToScan , ( ) => false ) ; let nodeInfo = scanCacheManager . getNodeInfo ( 'io.netty:netty-codec-http:4.1.31.Final' ) ; assert . isDefined ( nodeInfo ) ; if ( nodeInfo ) { assert . equal ( nodeInfo . top_severity , Severity . Critical ) ; assert . lengthOf ( nodeInfo . issues , 8 ) ; assert . lengthOf ( nodeInfo . licenses , 1 ) ; assert . deepEqual ( nodeInfo . licenses [ 0 ] , { licenseName : 'Apache-2.0' , violated : licenseViolated } as ILicenseKey ) ; } nodeInfo = scanCacheManager . getNodeInfo ( 'org.apache.commons:commons-lang3:3.12.0' ) ; assert . isDefined ( nodeInfo ) ; if ( nodeInfo ) { assert . equal ( nodeInfo . top_severity , Severity . Unknown ) ; assert . isEmpty ( nodeInfo . issues ) ; assert . lengthOf ( nodeInfo . licenses , 1 ) ; assert . deepEqual ( nodeInfo . licenses [ 0 ] , { licenseName : 'Apache-2.0' , violated : licenseViolated } as ILicenseKey ) ; } nodeInfo = scanCacheManager . getNodeInfo ( 'commons-io:commons-io:2.11.0' ) ; assert . isDefined ( nodeInfo ) ; if ( nodeInfo ) { assert . equal ( nodeInfo . top_severity , Severity . Unknown ) ; assert . isEmpty ( nodeInfo . issues ) ; assert . isEmpty ( nodeInfo . licenses ) ; } } function createComponentSummaryConnectionManager ( ) { return { async summaryComponent ( componentDetails ) { let graphResponse = fs . readFileSync ( path . join ( scanResponses , 'summaryComponent.json' ) , 'utf8' ) ; let response = Object . assign ( { } as ISummaryResponse , JSON . parse ( graphResponse ) ) ; return response . artifacts ; } } as ConnectionManager ; } function createScanGraphConnectionManager ( type ) { return { async scanGraph ( componentsToScan , checkCanceled , project ) { let graphResponse = fs . readFileSync ( path . join ( scanResponses , type + '.json' ) , 'utf8' ) ; return Object . assign ( { } as IGraphResponse , JSON . parse ( graphResponse ) ) ; } } as ConnectionManager ; } } ) ;