From 8aa69bf146b0830e80d78a54b53d3ca25f4a1672 Mon Sep 17 00:00:00 2001 From: Mohsen Ahmadi Date: Thu, 18 Mar 2021 12:53:56 -0700 Subject: [PATCH 1/2] feat: pass configattrs to javaCallGraphBuilder --- lib/index.ts | 7 +++++++ package.json | 2 +- test/system/plugin.test.ts | 5 ++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index e240a89..2ec2971 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -125,11 +125,18 @@ export async function inspect( ); } + let confAttrs: string | undefined; + + if (options['configuration-attributes']) { + confAttrs = options['configuration-attributes']; + } + debugLog(`getting call graph from path ${targetPath}`); callGraph = await javaCallGraphBuilder.getCallGraphGradle( path.dirname(targetPath), command, initScriptPath, + confAttrs, ); debugLog('got call graph successfully'); } diff --git a/package.json b/package.json index 1146670..dba8003 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "dependencies": { "@snyk/cli-interface": "2.11.0", "@snyk/dep-graph": "^1.28.0", - "@snyk/java-call-graph-builder": "1.20.0", + "@snyk/java-call-graph-builder": "1.22.0", "@types/debug": "^4.1.4", "chalk": "^3.0.0", "debug": "^4.1.1", diff --git a/test/system/plugin.test.ts b/test/system/plugin.test.ts index eacaa52..a86cfd5 100644 --- a/test/system/plugin.test.ts +++ b/test/system/plugin.test.ts @@ -99,10 +99,12 @@ test('run inspect() with reachableVulns', async (t) => { { reachableVulns: true, initScript: path.join(rootNoWrapper, 'init.gradle'), + 'configuration-attributes': + 'buildtype:release,usage:java-runtime,newdim:appA', }, ); - // test with init script param + // test with init script param/configuration attributes t.ok( javaCallGraphBuilderStub.calledTwice, 'called to the call graph builder', @@ -112,6 +114,7 @@ test('run inspect() with reachableVulns', async (t) => { path.join('.', rootNoWrapper), 'gradle', formatArgWithWhiteSpace(path.join(rootNoWrapper, 'init.gradle')), // arg should be normalized with quotes + 'buildtype:release,usage:java-runtime,newdim:appA', ), 'call graph builder was called with the correct path and init file', ); From 9b173dfa63bac28b46b2d854fe29c0c0f1856630 Mon Sep 17 00:00:00 2001 From: Dar Malovani Date: Thu, 20 May 2021 13:18:31 +0300 Subject: [PATCH 2/2] test: put reachability tests in their own file --- test/system/plugin.test.ts | 76 +------------------- test/system/reachability.test.ts | 119 +++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 75 deletions(-) create mode 100644 test/system/reachability.test.ts diff --git a/test/system/plugin.test.ts b/test/system/plugin.test.ts index a86cfd5..142440f 100644 --- a/test/system/plugin.test.ts +++ b/test/system/plugin.test.ts @@ -1,11 +1,7 @@ import * as path from 'path'; import { fixtureDir } from '../common'; import { test } from 'tap'; -import { inspect, formatArgWithWhiteSpace } from '../../lib'; -import * as fs from 'fs'; -import * as sinon from 'sinon'; -import * as javaCallGraphBuilder from '@snyk/java-call-graph-builder'; -import { CallGraph } from '@snyk/cli-interface/legacy/common'; +import { inspect } from '../../lib'; const rootNoWrapper = fixtureDir('no wrapper'); const withInitScript = fixtureDir('with-init-script'); @@ -59,76 +55,6 @@ test('run inspect() with on project that depends on gradle init script', async ( ); }); -test('run inspect() with reachableVulns', async (t) => { - const gradleCallGraph = JSON.parse( - fs.readFileSync( - path.join(fixtureDir('call-graphs'), 'simple.json'), - 'utf-8', - ), - ); - const javaCallGraphBuilderStub = sinon - .stub(javaCallGraphBuilder, 'getCallGraphGradle') - .resolves(gradleCallGraph as CallGraph); - const result = await inspect('.', path.join(rootNoWrapper, 'build.gradle'), { - reachableVulns: true, - }); - - const pkgs = result.dependencyGraph.getDepPkgs(); - const nodeIds: string[] = []; - Object.keys(pkgs).forEach((id) => { - nodeIds.push(`${pkgs[id].name}@${pkgs[id].version}`); - }); - - t.ok( - nodeIds.indexOf('com.android.tools:annotations@25.3.0') !== -1, - 'correct version found', - ); - t.ok(javaCallGraphBuilderStub.calledOnce, 'called to the call graph builder'); - t.ok( - javaCallGraphBuilderStub.calledWith( - path.join('.', rootNoWrapper), - 'gradle', - ), - 'call graph builder was called with the correct path', - ); - t.same(gradleCallGraph, result.callGraph, 'returns expected callgraph'); - - const resultWithInit = await inspect( - '.', - path.join(rootNoWrapper, 'build.gradle'), - { - reachableVulns: true, - initScript: path.join(rootNoWrapper, 'init.gradle'), - 'configuration-attributes': - 'buildtype:release,usage:java-runtime,newdim:appA', - }, - ); - - // test with init script param/configuration attributes - t.ok( - javaCallGraphBuilderStub.calledTwice, - 'called to the call graph builder', - ); - t.ok( - javaCallGraphBuilderStub.calledWith( - path.join('.', rootNoWrapper), - 'gradle', - formatArgWithWhiteSpace(path.join(rootNoWrapper, 'init.gradle')), // arg should be normalized with quotes - 'buildtype:release,usage:java-runtime,newdim:appA', - ), - 'call graph builder was called with the correct path and init file', - ); - t.same( - gradleCallGraph, - resultWithInit.callGraph, - 'returns expected callgraph', - ); - - t.teardown(() => { - javaCallGraphBuilderStub.restore(); - }); -}); - test('multi-config: both compile and runtime deps picked up by default', async (t) => { const result = await inspect( '.', diff --git a/test/system/reachability.test.ts b/test/system/reachability.test.ts new file mode 100644 index 0000000..b02aae1 --- /dev/null +++ b/test/system/reachability.test.ts @@ -0,0 +1,119 @@ +import * as path from 'path'; +import { fixtureDir } from '../common'; +import { test } from 'tap'; +import { inspect, formatArgWithWhiteSpace } from '../../lib'; +import * as fs from 'fs'; +import * as sinon from 'sinon'; +import * as javaCallGraphBuilder from '@snyk/java-call-graph-builder'; +import { CallGraph } from '@snyk/cli-interface/legacy/common'; + +const rootNoWrapper = fixtureDir('no wrapper'); + +test('reachableVulns', async (t) => { + const gradleCallGraph = JSON.parse( + fs.readFileSync( + path.join(fixtureDir('call-graphs'), 'simple.json'), + 'utf-8', + ), + ); + const javaCallGraphBuilderStub = sinon + .stub(javaCallGraphBuilder, 'getCallGraphGradle') + .resolves(gradleCallGraph as CallGraph); + + t.test('simple reachability scenario', async (t) => { + const result = await inspect( + '.', + path.join(rootNoWrapper, 'build.gradle'), + { + reachableVulns: true, + }, + ); + + const pkgs = result.dependencyGraph.getDepPkgs(); + const nodeIds: string[] = []; + Object.keys(pkgs).forEach((id) => { + nodeIds.push(`${pkgs[id].name}@${pkgs[id].version}`); + }); + + t.ok( + nodeIds.indexOf('com.android.tools:annotations@25.3.0') !== -1, + 'correct version found', + ); + t.ok( + javaCallGraphBuilderStub.calledOnce, + 'called to the call graph builder', + ); + t.ok( + javaCallGraphBuilderStub.calledWith( + path.join('.', rootNoWrapper), + 'gradle', + ), + 'call graph builder was called with the correct path', + ); + t.same(gradleCallGraph, result.callGraph, 'returns expected callgraph'); + }); + + t.test('with init script', async (t) => { + const resultWithInit = await inspect( + '.', + path.join(rootNoWrapper, 'build.gradle'), + { + reachableVulns: true, + initScript: path.join(rootNoWrapper, 'init.gradle'), + }, + ); + + t.ok( + javaCallGraphBuilderStub.calledTwice, + 'called to the call graph builder', + ); + t.ok( + javaCallGraphBuilderStub.calledWith( + path.join('.', rootNoWrapper), + 'gradle', + formatArgWithWhiteSpace(path.join(rootNoWrapper, 'init.gradle')), // arg should be normalized with quotes + ), + 'call graph builder was called with the correct path and init file', + ); + t.same( + gradleCallGraph, + resultWithInit.callGraph, + 'returns expected callgraph', + ); + }); + + t.test('with configuration attributes', async (t) => { + const resultWithConfigAttrs = await inspect( + '.', + path.join(rootNoWrapper, 'build.gradle'), + { + reachableVulns: true, + 'configuration-attributes': + 'buildtype:release,usage:java-runtime,newdim:appA', + }, + ); + + t.ok( + javaCallGraphBuilderStub.calledThrice, + 'called to the call graph builder', + ); + t.ok( + javaCallGraphBuilderStub.calledWith( + path.join('.', rootNoWrapper), + 'gradle', + undefined, + 'buildtype:release,usage:java-runtime,newdim:appA', + ), + 'call graph builder was called with the correct path and init file', + ); + t.same( + gradleCallGraph, + resultWithConfigAttrs.callGraph, + 'returns expected callgraph', + ); + }); + + t.teardown(() => { + javaCallGraphBuilderStub.restore(); + }); +});