1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.analyzer;
19
20 import static org.hamcrest.CoreMatchers.is;
21 import static org.junit.Assert.assertThat;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.File;
25
26 import org.junit.After;
27 import org.junit.Assume;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.owasp.dependencycheck.BaseTest;
31 import org.owasp.dependencycheck.Engine;
32 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
33 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
34 import org.owasp.dependencycheck.dependency.Dependency;
35 import org.owasp.dependencycheck.utils.Settings;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43
44 public class RubyBundleAuditAnalyzerTest extends BaseTest {
45
46 private static final Logger LOGGER = LoggerFactory.getLogger(RubyBundleAuditAnalyzerTest.class);
47
48
49
50
51 RubyBundleAuditAnalyzer analyzer;
52
53
54
55
56
57
58 @Before
59 public void setUp() throws Exception {
60 Settings.initialize();
61 analyzer = new RubyBundleAuditAnalyzer();
62 analyzer.setFilesMatched(true);
63 }
64
65
66
67
68
69
70 @After
71 public void tearDown() throws Exception {
72 Settings.cleanup();
73 analyzer.close();
74 analyzer = null;
75 }
76
77
78
79
80 @Test
81 public void testGetName() {
82 assertThat(analyzer.getName(), is("Ruby Bundle Audit Analyzer"));
83 }
84
85
86
87
88 @Test
89 public void testSupportsFiles() {
90 assertThat(analyzer.accept(new File("Gemfile.lock")), is(true));
91 }
92
93
94
95
96
97
98 @Test
99 public void testAnalysis() throws AnalysisException, DatabaseException {
100 try {
101 analyzer.initialize();
102
103 final Dependency result = new Dependency(BaseTest.getResourceAsFile(this,
104 "ruby/vulnerable/gems/rails-4.1.15/Gemfile.lock"));
105 final Engine engine = new Engine();
106 analyzer.analyze(result, engine);
107 int size = engine.getDependencies().size();
108 assertThat(size, is(1));
109
110 Dependency dependency = engine.getDependencies().get(0);
111 assertTrue(dependency.getProductEvidence().toString().toLowerCase().contains("redcarpet"));
112 assertTrue(dependency.getVersionEvidence().toString().toLowerCase().contains("2.2.2"));
113
114 } catch (Exception e) {
115 LOGGER.warn("Exception setting up RubyBundleAuditAnalyzer. Make sure Ruby gem bundle-audit is installed. You may also need to set property \"analyzer.bundle.audit.path\".", e);
116 Assume.assumeNoException("Exception setting up RubyBundleAuditAnalyzer; bundle audit may not be installed, or property \"analyzer.bundle.audit.path\" may not be set.", e);
117 }
118 }
119
120
121
122
123
124
125 @Test
126 public void testMissingBundleAudit() throws AnalysisException, DatabaseException {
127
128 Settings.setString(Settings.KEYS.ANALYZER_BUNDLE_AUDIT_PATH, "phantom-bundle-audit");
129 try {
130
131 analyzer.initialize();
132 } catch (Exception e) {
133
134 }
135 finally {
136 assertThat(analyzer.isEnabled(), is(false));
137 LOGGER.info("phantom-bundle-audit is not available. Ruby Bundle Audit Analyzer is disabled as expected.");
138 }
139 }
140 }