|
4 | 4 | import static org.hamcrest.Matchers.is; |
5 | 5 | import static org.hamcrest.Matchers.notNullValue; |
6 | 6 |
|
7 | | -import com.contrastsecurity.sarif.Region; |
| 7 | +import com.contrastsecurity.sarif.SarifSchema210; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
8 | 9 | import com.google.inject.Guice; |
9 | 10 | import com.google.inject.Injector; |
10 | 11 | import io.codemodder.*; |
11 | 12 | import io.codemodder.codetf.CodeTFReference; |
12 | 13 | import java.io.IOException; |
13 | | -import java.nio.file.Files; |
14 | 14 | import java.nio.file.Path; |
15 | | -import java.nio.file.StandardOpenOption; |
16 | 15 | import java.util.List; |
| 16 | +import java.util.Optional; |
17 | 17 | import javax.inject.Inject; |
18 | | -import org.junit.jupiter.api.BeforeEach; |
19 | 18 | import org.junit.jupiter.api.Test; |
20 | 19 | import org.junit.jupiter.api.io.TempDir; |
21 | 20 |
|
22 | 21 | final class AppScanModuleTest { |
23 | 22 |
|
24 | | - private Path repoDir; |
25 | | - |
26 | 23 | @Codemod( |
27 | 24 | id = "appscan-test:java/finds-stuff", |
28 | 25 | importance = Importance.LOW, |
@@ -56,30 +53,45 @@ public String getIndividualChangeDescription(Path filePath, CodemodChange change |
56 | 53 | } |
57 | 54 | } |
58 | 55 |
|
59 | | - @BeforeEach |
60 | | - void setup(@TempDir final Path tmpDir) { |
61 | | - AppScanRuleSarifFactory factory = new AppScanRuleSarifFactory(); |
62 | | - factory.build("appscan", "SA2813462719", null, null); |
63 | | - this.repoDir = tmpDir; |
64 | | - } |
| 56 | + private static final String emptySarif = |
| 57 | + """ |
| 58 | + { |
| 59 | + "$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json", |
| 60 | + "version": "2.1.0", |
| 61 | + "runs": [ |
| 62 | + { |
| 63 | + "tool": { |
| 64 | + "driver": { |
| 65 | + "name": "HCL AppScan Static Analyzer" |
| 66 | + } |
| 67 | + }, |
| 68 | + "artifacts": [ |
| 69 | + { |
| 70 | + "location": { |
| 71 | + "uri": "file:///com/acme/MyVulnerableType.java" |
| 72 | + } |
| 73 | + } |
| 74 | + ], |
| 75 | + "results": [] |
| 76 | + } |
| 77 | + ] |
| 78 | + } |
| 79 | + """; |
65 | 80 |
|
| 81 | + /** This only tests that the module binds the rule sarif to the codemod. */ |
66 | 82 | @Test |
67 | | - void it_works_with_appscan_sarif() throws IOException { |
68 | | - String javaCode = "class Foo { \n Object a = new Thing(); \n }"; |
69 | | - |
70 | | - Path javaFile = Files.createTempFile(repoDir, "HasThing", ".java"); |
71 | | - Files.writeString(javaFile, javaCode, StandardOpenOption.TRUNCATE_EXISTING); |
72 | | - AppScanModule module = createModule(List.of(AppScanSarifTestCodemod.class)); |
| 83 | + void it_works_with_appscan_sarif(@TempDir final Path repoDir) throws IOException { |
| 84 | + SarifSchema210 rawSarif = |
| 85 | + new ObjectMapper().readValue(AppScanModuleTest.emptySarif, SarifSchema210.class); |
| 86 | + AppScanRuleSarifFactory ruleSarifFactory = new AppScanRuleSarifFactory(); |
| 87 | + Optional<RuleSarif> ruleSarif = |
| 88 | + ruleSarifFactory.build("HCL AppScan Static Analyzer", "SA2813462719", rawSarif, repoDir); |
| 89 | + assertThat(ruleSarif.isPresent(), is(true)); |
| 90 | + AppScanModule module = |
| 91 | + new AppScanModule(List.of(AppScanSarifTestCodemod.class), List.of(ruleSarif.get())); |
73 | 92 | Injector injector = Guice.createInjector(module); |
74 | 93 | AppScanSarifTestCodemod instance = injector.getInstance(AppScanSarifTestCodemod.class); |
75 | | - |
76 | | - RuleSarif ruleSarif = instance.ruleSarif; |
77 | | - assertThat(ruleSarif, is(notNullValue())); |
78 | | - List<Region> regions = ruleSarif.getRegionsFromResultsByRule(javaFile); |
79 | | - assertThat(regions.size(), is(1)); |
80 | | - } |
81 | | - |
82 | | - private AppScanModule createModule(final List<Class<? extends CodeChanger>> codemodTypes) { |
83 | | - return new AppScanModule(codemodTypes, List.of()); |
| 94 | + assertThat(instance, is(notNullValue())); |
| 95 | + assertThat(instance.ruleSarif, is(notNullValue())); |
84 | 96 | } |
85 | 97 | } |
0 commit comments