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;
21  
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.when;
24  
25  import java.io.IOException;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.FileSystems;
28  import java.nio.file.Files;
29  import java.util.Arrays;
30  import java.util.Optional;
31  
32  import org.junit.Assert;
33  import org.junit.Test;
34  import org.sonar.api.batch.fs.internal.DefaultFileSystem;
35  import org.sonar.api.batch.fs.internal.DefaultInputFile;
36  import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
37  import org.sonar.api.batch.rule.ActiveRules;
38  import org.sonar.api.batch.rule.internal.DefaultActiveRules;
39  import org.sonar.api.batch.rule.internal.NewActiveRule;
40  import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
41  import org.sonar.api.batch.sensor.internal.SensorContextTester;
42  import org.sonar.api.config.Configuration;
43  import org.sonar.api.rule.RuleKey;
44  import org.sonar.api.scan.filesystem.PathResolver;
45  
46  import com.hack23.sonar.cloudformation.reports.process.CloudformationConstants;
47  
48  /**
49   * The Class CloudformationSensorTest.
50   */
51  public class CloudformationSensorTest extends Assert {
52  
53  	/**
54  	 * Describe test.
55  	 */
56  	@Test
57  	public void describeTest() {
58  		final CloudformationSensor cloudformationSensor = new CloudformationSensor(null, null, null);
59  		final DefaultSensorDescriptor sensorDescriptor = new DefaultSensorDescriptor();
60  		cloudformationSensor.describe(sensorDescriptor);
61  		assertEquals(CloudformationSensor.SENSOR_NAME, sensorDescriptor.name());
62  	}
63  
64  	/**
65  	 * Execute simple nag report test.
66  	 *
67  	 * @throws IOException Signals that an I/O exception has occurred.
68  	 */
69  	@Test
70  	public void executeSimpleNagReportTest() throws IOException {
71  		final Configuration configuration = mock(Configuration.class);
72  		when(configuration.get(CloudformationConstants.CFN_NAG_REPORT_FILES_PROPERTY))
73  				.thenReturn(Optional.of("src/test/resources/aws-cross-account-manager-master.yml.nag"));
74  
75  		final CloudformationSensorConfiguration cloudformationSensorConfiguration = new CloudformationSensorConfiguration(
76  				configuration);
77  
78  		final DefaultFileSystem fileSystem = new DefaultFileSystem(
79  				FileSystems.getDefault().getPath(".").toAbsolutePath());
80  
81  		final DefaultInputFile inputFile = new TestInputFileBuilder("key",
82  				"src/test/resources/aws-cross-account-manager-master.yml")
83  						.setLanguage("yaml")
84  						.initMetadata(new String(Files.readAllBytes(
85  								FileSystems.getDefault().getPath("src/test/resources/aws-cross-account-manager-master.yml"))))
86  						.setCharset(StandardCharsets.UTF_8).build();
87  		fileSystem.add(inputFile);
88  
89  		final CloudformationSensor cloudformationSensor = new CloudformationSensor(cloudformationSensorConfiguration,
90  				fileSystem, new PathResolver());
91  
92  		final SensorContextTester sensorContext = SensorContextTester
93  				.create(FileSystems.getDefault().getPath(".").toAbsolutePath());
94  		sensorContext.fileSystem().add(inputFile);
95  		sensorContext.setActiveRules(new DefaultActiveRules(Arrays.asList()));
96  		cloudformationSensor.execute(sensorContext);
97  		assertFalse(sensorContext.allIssues().isEmpty());
98  		assertEquals(44,sensorContext.allIssues().size());
99  	}
100 
101 	/**
102 	 * Execute simple checkov report test.
103 	 *
104 	 * @throws IOException Signals that an I/O exception has occurred.
105 	 */
106 	@Test
107 	public void executeSimpleCheckovReportTest() throws IOException {
108 		final Configuration configuration = mock(Configuration.class);
109 		when(configuration.get(CloudformationConstants.CHECKOV_REPORT_FILES_PROPERTY))
110 				.thenReturn(Optional.of("src/test/resources/checkov/cia-dist-cloudformation.checkov-report"));
111 
112 		final CloudformationSensorConfiguration cloudformationSensorConfiguration = new CloudformationSensorConfiguration(
113 				configuration);
114 
115 		final DefaultFileSystem fileSystem = new DefaultFileSystem(
116 				FileSystems.getDefault().getPath(".").toAbsolutePath());
117 
118 		final DefaultInputFile inputFile = new TestInputFileBuilder("key",
119 				"src/test/resources/checkov/cia-dist-cloudformation.json")
120 						.setLanguage("json")
121 						.initMetadata(new String(Files.readAllBytes(
122 								FileSystems.getDefault().getPath("src/test/resources/checkov/cia-dist-cloudformation.json"))))
123 						.setCharset(StandardCharsets.UTF_8).build();
124 		fileSystem.add(inputFile);
125 
126 		final CloudformationSensor cloudformationSensor = new CloudformationSensor(cloudformationSensorConfiguration,
127 				fileSystem, new PathResolver());
128 
129 		final SensorContextTester sensorContext = SensorContextTester
130 				.create(FileSystems.getDefault().getPath(".").toAbsolutePath());
131 		sensorContext.fileSystem().add(inputFile);
132 		final ActiveRules activeRules = new DefaultActiveRules(Arrays.asList(new NewActiveRule.Builder().setRuleKey(RuleKey.of("cloudformation-plugin-cfn","cloudformation-CKV_AWS_157")).build()));
133 		sensorContext.setActiveRules(activeRules);
134 		cloudformationSensor.execute(sensorContext);
135 
136 		assertFalse(sensorContext.allIssues().isEmpty());
137 		assertEquals(1,sensorContext.allIssues().size());
138 	}
139 
140 
141 }