/
NodePackageAnalyzerTest.java
312 lines (279 loc) · 12.4 KB
/
NodePackageAnalyzerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* This file is part of dependency-check-core.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) 2015 Institute for Defense Analyses. All Rights Reserved.
*/
package org.owasp.dependencycheck.analyzer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.owasp.dependencycheck.BaseTest;
import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
import org.owasp.dependencycheck.dependency.Dependency;
import java.io.File;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import org.junit.Assume;
import org.owasp.dependencycheck.Engine;
import org.owasp.dependencycheck.dependency.EvidenceType;
import org.owasp.dependencycheck.exception.InitializationException;
import org.owasp.dependencycheck.utils.InvalidSettingException;
import org.owasp.dependencycheck.utils.Settings;
/**
* Unit tests for NodePackageAnalyzer.
*
* @author Dale Visser
*/
public class NodePackageAnalyzerTest extends BaseTest {
/**
* The analyzer to test.
*/
private NodePackageAnalyzer analyzer;
/**
* A reference to the engine.
*/
private Engine engine;
/**
* Retrieves the node audit analyzer from the engine.
*
* @param engine the ODC engine
* @return returns the node audit analyzer from the engine
*/
private NodeAuditAnalyzer getNodeAuditAnalyzer(Engine engine) {
for (Analyzer a : engine.getAnalyzers()) {
if (a instanceof NodeAuditAnalyzer) {
return (NodeAuditAnalyzer) a;
}
}
return null;
}
/**
* Retrieves the node package analyzer from the engine.
*
* @param engine the ODC engine
* @return returns the node package analyzer from the engine
*/
private NodePackageAnalyzer getNodePackageAnalyzer(Engine engine) {
for (Analyzer a : engine.getAnalyzers()) {
if (a instanceof NodePackageAnalyzer) {
return (NodePackageAnalyzer) a;
}
}
return null;
}
/**
* Correctly setup the analyzer for testing.
*
* @throws Exception thrown if there is a problem
*/
@Before
@Override
public void setUp() throws Exception {
super.setUp();
if (getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED)) {
engine = new Engine(this.getSettings());
NodeAuditAnalyzer auditAnalyzer = getNodeAuditAnalyzer(engine);
auditAnalyzer.setFilesMatched(true);
analyzer = getNodePackageAnalyzer(engine);
analyzer.setFilesMatched(true);
analyzer.initialize(getSettings());
try {
analyzer.prepare(engine);
} catch (InitializationException ex) {
if (!ex.getMessage().startsWith("Missing package.lock or npm-shrinkwrap.lock file")) {
throw ex;
}
}
}
}
/**
* Cleanup temp files, close resources, etc.
*
* @throws Exception thrown if there is a problem
*/
@After
@Override
public void tearDown() throws Exception {
if (getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED)) {
analyzer.close();
engine.close();
}
super.tearDown();
}
/**
* Test of getName method, of class PythonDistributionAnalyzer.
*/
@Test
public void testGetName() throws InvalidSettingException {
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED), is(true));
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_AUDIT_ENABLED), is(true));
assertThat(analyzer.getName(), is("Node.js Package Analyzer"));
}
/**
* Test of supportsExtension method, of class PythonDistributionAnalyzer.
*/
@Test
public void testSupportsFiles() throws InvalidSettingException {
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED), is(true));
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_AUDIT_ENABLED), is(true));
assertThat(analyzer.accept(new File("package-lock.json")), is(true));
assertThat(analyzer.accept(new File("npm-shrinkwrap.json")), is(true));
}
/**
* Test of inspect method, of class PythonDistributionAnalyzer.
*
* @throws AnalysisException is thrown when an exception occurs.
*/
@Test
public void testAnalyzeShrinkwrapJson() throws AnalysisException, InvalidSettingException {
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED), is(true));
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_AUDIT_ENABLED), is(true));
final Dependency toScan = new Dependency(BaseTest.getResourceAsFile(this,
"nodejs/npm-shrinkwrap.json"));
final Dependency toCombine = new Dependency(BaseTest.getResourceAsFile(this,
"nodejs/node_modules/dns-sync/package.json"));
engine.addDependency(toScan);
engine.addDependency(toCombine);
analyzer.analyze(toScan, engine);
analyzer.analyze(toCombine, engine);
testLock();
}
private void testLock() {
final boolean isMac = System.getProperty("os.name").toLowerCase().contains("mac");
// test some dependencies
boolean bracesFound = false;
boolean expandRangeFound = false;
Dependency result = null;
for (Dependency dep : engine.getDependencies()) {
if (!isMac && "fsevents".equals(dep.getName())) {
fail("fsevents need to be skipped on non mac");
}
if ("react-dom".equals(dep.getName())) {
fail("react-dom need to be skipped because it's an alias");
}
if ("braces".equals(dep.getName())) {
bracesFound = true;
}
if ("expand-range".equals(dep.getName())) {
expandRangeFound = true;
}
if ("fake_submodule".equals(dep.getName())) {
fail("start with file: need to be skipped because it's a local package");
}
if ("react-dom".equals(dep.getName())) {
fail("start with file: need to be skipped because it's a local package");
}
if ("dns-sync".equals(dep.getName())) {
result = dep;
}
}
assertTrue("need to contain braces", bracesFound);
//check if dependencies of dependencies are imported
assertTrue("need to contain expand-range (dependency of braces)", expandRangeFound);
final String vendorString = result.getEvidence(EvidenceType.VENDOR).toString();
assertThat(vendorString, containsString("Sanjeev Koranga"));
assertThat(vendorString, containsString("dns-sync"));
assertThat(result.getEvidence(EvidenceType.PRODUCT).toString(), containsString("dns-sync"));
assertThat(result.getEvidence(EvidenceType.VERSION).toString(), containsString("0.1.3"));
assertEquals(NodePackageAnalyzer.DEPENDENCY_ECOSYSTEM, result.getEcosystem());
assertEquals("dns-sync", result.getName());
assertEquals("0.1.3", result.getVersion());
// with npm install run on a "non-macOs" system, 90 else
// dependencies length change often, maybe not a good idea to test the length, check some dependencies instead
// assertEquals("Expected 40 dependencies", 40, engine.getDependencies().length);
// shrinkWrap is not removed because the NodeAudit analyzer is enabled
//assertFalse(shrinkwrap.equals(engine.getDependencies()[0]));
}
/**
* Test of inspect method, of class PythonDistributionAnalyzer.
*
* @throws AnalysisException is thrown when an exception occurs.
*/
@Test
public void testAnalyzePackageJsonWithShrinkwrap() throws AnalysisException, InvalidSettingException {
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED), is(true));
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_AUDIT_ENABLED), is(true));
final Dependency packageJson = new Dependency(BaseTest.getResourceAsFile(this,
"nodejs/package.json"));
final Dependency shrinkwrap = new Dependency(BaseTest.getResourceAsFile(this,
"nodejs/npm-shrinkwrap.json"));
engine.addDependency(packageJson);
engine.addDependency(shrinkwrap);
assertEquals(2, engine.getDependencies().length);
analyzer.analyze(packageJson, engine);
assertEquals(1, engine.getDependencies().length); //package-lock was removed without analysis
assertEquals(shrinkwrap, engine.getDependencies()[0]);
analyzer.analyze(shrinkwrap, engine);
testLock();
}
/**
* Test of inspect method, of class PythonDistributionAnalyzer.
*
* @throws AnalysisException is thrown when an exception occurs.
*/
@Test
public void testWithoutLock() throws AnalysisException, InvalidSettingException {
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED), is(true));
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_AUDIT_ENABLED), is(true));
final Dependency packageJson = new Dependency(BaseTest.getResourceAsFile(this,
"nodejs/no_lock/package.json"));
engine.addDependency(packageJson);
analyzer.analyze(packageJson, engine);
//final boolean isMac = !System.getProperty("os.name").toLowerCase().contains("mac");
assertEquals("Expected 1 dependencies", 1, engine.getDependencies().length);
}
/**
* Test of inspect method for package-lock v2
*
* @throws AnalysisException is thrown when an exception occurs.
*/
@Test
public void testPackageLockV2() throws AnalysisException, InvalidSettingException {
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED), is(true));
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_AUDIT_ENABLED), is(true));
final Dependency packageJson = new Dependency(BaseTest.getResourceAsFile(this,
"nodejs/test_lockv2/package.json"));
final Dependency packageLockJson = new Dependency(BaseTest.getResourceAsFile(this,
"nodejs/test_lockv2/package-lock.json"));
engine.addDependency(packageJson);
engine.addDependency(packageLockJson);
analyzer.analyze(packageJson, engine);
assertEquals("Expected 1 dependencies", 1, engine.getDependencies().length);
analyzer.analyze(packageLockJson, engine);
assertEquals("Expected 1 dependencies", 6, engine.getDependencies().length);
}
/**
* Test of inspect method for package-lock v3
*
* @throws AnalysisException is thrown when an exception occurs.
*/
@Test
public void testPackageLockV3() throws AnalysisException, InvalidSettingException {
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED), is(true));
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_AUDIT_ENABLED), is(true));
final Dependency packageJson = new Dependency(BaseTest.getResourceAsFile(this,
"nodejs/test_lockv3/package.json"));
final Dependency packageLockJson = new Dependency(BaseTest.getResourceAsFile(this,
"nodejs/test_lockv3/package-lock.json"));
engine.addDependency(packageJson);
engine.addDependency(packageLockJson);
analyzer.analyze(packageJson, engine);
assertEquals("Expected 1 dependencies", 1, engine.getDependencies().length);
analyzer.analyze(packageLockJson, engine);
assertEquals("Expected 1 dependencies", 6, engine.getDependencies().length);
}
}