1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
50
51 public class CloudformationSensorTest extends Assert {
52
53
54
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
66
67
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
103
104
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 }