View Javadoc
1   /*
2    * Cloudformation Plugin for SonarQube
3    * Copyright (C) 2019 James Pether Sörling
4    * james@hack23.com
5    *
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with this program; if not, write to the Free Software Foundation,
18   * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   */
20  package com.hack23.sonar.cloudformation.reports.process;
21  
22  import java.io.IOException;
23  import java.nio.charset.StandardCharsets;
24  import java.nio.file.FileSystems;
25  import java.nio.file.Files;
26  import java.util.Arrays;
27  import java.util.Optional;
28  
29  import org.junit.Assert;
30  import org.junit.Test;
31  import org.sonar.api.batch.fs.internal.DefaultFileSystem;
32  import org.sonar.api.batch.fs.internal.DefaultInputFile;
33  import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
34  import org.sonar.api.batch.rule.ActiveRules;
35  import org.sonar.api.batch.rule.internal.DefaultActiveRules;
36  import org.sonar.api.batch.rule.internal.NewActiveRule;
37  import org.sonar.api.batch.sensor.internal.SensorContextTester;
38  import org.sonar.api.rule.RuleKey;
39  import org.sonar.api.scan.filesystem.PathResolver;
40  
41  /**
42   * The Class CheckovProcessReportsTest.
43   */
44  public class CheckovProcessReportsTest extends Assert {
45  
46  	/**
47  	 * Execute simple checkov report test.
48  	 *
49  	 * @throws IOException Signals that an I/O exception has occurred.
50  	 */
51  	@Test
52  	public void executeSimpleCheckovReportTest() throws IOException {
53  		final DefaultFileSystem fileSystem = new DefaultFileSystem(
54  				FileSystems.getDefault().getPath(".").toAbsolutePath());
55  
56  		final DefaultInputFile inputFile = new TestInputFileBuilder("key",
57  				"src/test/resources/checkov/cia-dist-cloudformation.json")
58  						.setLanguage("json")
59  						.initMetadata(new String(Files.readAllBytes(FileSystems.getDefault()
60  								.getPath("src/test/resources/checkov/cia-dist-cloudformation.json"))))
61  						.setCharset(StandardCharsets.UTF_8).build();
62  		fileSystem.add(inputFile);
63  
64  		final CheckovProcessReports cloudformationSensor = new CheckovProcessReports(fileSystem, new PathResolver());
65  
66  		final SensorContextTester sensorContext = SensorContextTester
67  				.create(FileSystems.getDefault().getPath(".").toAbsolutePath());
68  		sensorContext.fileSystem().add(inputFile);
69  		final ActiveRules activeRules = new DefaultActiveRules(Arrays.asList(new NewActiveRule.Builder().setRuleKey(RuleKey.of("cloudformation-plugin-cfn","cloudformation-CKV_AWS_157")).build()));
70  		sensorContext.setActiveRules(activeRules);
71  
72  
73  		cloudformationSensor.processCheckovReport(sensorContext,
74  				Optional.of("src/test/resources/checkov/cia-dist-cloudformation.checkov-report"));
75  		assertFalse(sensorContext.allIssues().isEmpty());
76  		assertEquals(1,sensorContext.allIssues().size());
77  	}
78  
79  	/**
80  	 * Execute simple checkov report found no template test.
81  	 *
82  	 * @throws IOException Signals that an I/O exception has occurred.
83  	 */
84  	@Test
85  	public void executeSimpleCheckovReportFoundNoTemplateTest() throws IOException {
86  		final DefaultFileSystem fileSystem = new DefaultFileSystem(
87  				FileSystems.getDefault().getPath(".").toAbsolutePath());
88  
89  		final CheckovProcessReports cloudformationSensor = new CheckovProcessReports(fileSystem, new PathResolver());
90  
91  		final SensorContextTester sensorContext = SensorContextTester
92  				.create(FileSystems.getDefault().getPath(".").toAbsolutePath());
93  
94  		final ActiveRules activeRules = new DefaultActiveRules(Arrays.asList(new NewActiveRule.Builder().setRuleKey(RuleKey.of("cfn-yaml","cloudformation-CKV_AWS_111")).build()));
95  		sensorContext.setActiveRules(activeRules);
96  
97  		cloudformationSensor.processCheckovReport(sensorContext,
98  				Optional.of("src/test/resources/checkov/cia-dist-cloudformation.checkov-report"));
99  		assertTrue(sensorContext.allIssues().isEmpty());
100 	}
101 
102 }