View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2015 Institute for Defense Analyses. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.analyzer;
19  
20  import org.junit.After;
21  import org.junit.Before;
22  import org.junit.Test;
23  import org.owasp.dependencycheck.BaseTest;
24  import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
25  import org.owasp.dependencycheck.dependency.Dependency;
26  
27  import java.io.File;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  
32  /**
33   * Unit tests for AutoconfAnalyzer. The test resources under autoconf/ were
34   * obtained from outside open source software projects. Links to those projects
35   * are given below.
36   *
37   * @author Dale Visser <dvisser@ida.org>
38   * @see <a href="http://readable.sourceforge.net/">Readable Lisp S-expressions
39   *      Project</a>
40   * @see <a href="https://gnu.org/software/binutils/">GNU Binutils</a>
41   * @see <a href="https://gnu.org/software/ghostscript/">GNU Ghostscript</a>
42   */
43  public class AutoconfAnalyzerTest extends BaseTest {
44  
45  	/**
46  	 * The analyzer to test.
47  	 */
48  	AutoconfAnalyzer analyzer;
49  
50  	private void assertCommonEvidence(Dependency result, String product,
51  			String version, String vendor) {
52  		assertProductAndVersion(result, product, version);
53  		assertTrue("Expected vendor evidence to contain \"" + vendor + "\".",
54  				result.getVendorEvidence().toString().contains(vendor));
55  	}
56  
57  	private void assertProductAndVersion(Dependency result, String product,
58  			String version) {
59  		assertTrue("Expected product evidence to contain \"" + product + "\".",
60  				result.getProductEvidence().toString().contains(product));
61  		assertTrue("Expected version evidence to contain \"" + version + "\".",
62  				result.getVersionEvidence().toString().contains(version));
63  	}
64  
65  	/**
66  	 * Correctly setup the analyzer for testing.
67  	 *
68  	 * @throws Exception
69  	 *             thrown if there is a problem
70  	 */
71  	@Before
72  	public void setUp() throws Exception {
73  		analyzer = new AutoconfAnalyzer();
74  		analyzer.setFilesMatched(true);
75  		analyzer.initialize();
76  	}
77  
78  	/**
79  	 * Cleanup the analyzer's temp files, etc.
80  	 *
81  	 * @throws Exception
82  	 *             thrown if there is a problem
83  	 */
84  	@After
85  	public void tearDown() throws Exception {
86  		analyzer.close();
87  		analyzer = null;
88  	}
89  
90  	/**
91  	 * Test whether expected evidence is gathered from Ghostscript's
92  	 * configure.ac.
93  	 *
94  	 * @throws AnalysisException
95  	 *             is thrown when an exception occurs.
96  	 */
97  	@Test
98  	public void testAnalyzeConfigureAC1() throws AnalysisException {
99  		final Dependency result = new Dependency(BaseTest.getResourceAsFile(
100 				this, "autoconf/ghostscript/configure.ac"));
101 		analyzer.analyze(result, null);
102 		assertCommonEvidence(result, "ghostscript", "8.62.0", "gnu");
103 	}
104 
105 	/**
106 	 * Test whether expected evidence is gathered from Readable's configure.ac.
107 	 *
108 	 * @throws AnalysisException
109 	 *             is thrown when an exception occurs.
110 	 */
111 	@Test
112 	public void testAnalyzeConfigureAC2() throws AnalysisException {
113 		final Dependency result = new Dependency(BaseTest.getResourceAsFile(
114 				this, "autoconf/readable-code/configure.ac"));
115 		analyzer.analyze(result, null);
116 		assertReadableCodeEvidence(result);
117 	}
118 
119 	private void assertReadableCodeEvidence(final Dependency result) {
120 		assertCommonEvidence(result, "readable", "1.0.7", "dwheeler");
121 		final String url = "http://readable.sourceforge.net/";
122 		assertTrue("Expected product evidence to contain \"" + url + "\".",
123 				result.getVendorEvidence().toString().contains(url));
124 	}
125 
126 	/**
127 	 * Test whether expected evidence is gathered from GNU Binutil's configure.
128 	 *
129 	 * @throws AnalysisException
130 	 *             is thrown when an exception occurs.
131 	 */
132 	@Test
133 	public void testAnalyzeConfigureScript() throws AnalysisException {
134 		final Dependency result = new Dependency(BaseTest.getResourceAsFile(
135 				this, "autoconf/binutils/configure"));
136 		analyzer.analyze(result, null);
137 		assertProductAndVersion(result, "binutils", "2.25.51");
138 	}
139 
140 	/**
141 	 * Test whether expected evidence is gathered from GNU Ghostscript's
142 	 * configure.
143 	 *
144 	 * @throws AnalysisException
145 	 *             is thrown when an exception occurs.
146 	 */
147 	@Test
148 	public void testAnalyzeReadableConfigureScript() throws AnalysisException {
149 		final Dependency result = new Dependency(BaseTest.getResourceAsFile(
150 				this, "autoconf/readable-code/configure"));
151 		analyzer.analyze(result, null);
152 		assertReadableCodeEvidence(result);
153 	}
154 
155 	/**
156 	 * Test of getName method, of {@link AutoconfAnalyzer}.
157 	 */
158 	@Test
159 	public void testGetName() {
160 		assertEquals("Analyzer name wrong.", "Autoconf Analyzer",
161 				analyzer.getName());
162 	}
163 
164 	/**
165 	 * Test of {@link AutoconfAnalyzer#accept(File)}.
166 	 */
167 	@Test
168 	public void testSupportsFileExtension() {
169 		assertTrue("Should support \"ac\" extension.",
170 				analyzer.accept(new File("configure.ac")));
171 		assertTrue("Should support \"in\" extension.",
172 				analyzer.accept(new File("configure.in")));
173 		assertTrue("Should support \"configure\" extension.",
174 				analyzer.accept(new File("configure")));
175 	}
176 }