• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 Code Intelligence GmbH
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.code_intelligence.jazzer.junit;
16 
17 import static com.google.common.truth.Truth8.assertThat;
18 import static org.junit.Assume.assumeFalse;
19 import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
20 import static org.junit.platform.testkit.engine.EventConditions.container;
21 import static org.junit.platform.testkit.engine.EventConditions.event;
22 import static org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully;
23 import static org.junit.platform.testkit.engine.EventConditions.type;
24 import static org.junit.platform.testkit.engine.EventConditions.uniqueIdSubstrings;
25 import static org.junit.platform.testkit.engine.EventType.FINISHED;
26 import static org.junit.platform.testkit.engine.EventType.REPORTING_ENTRY_PUBLISHED;
27 import static org.junit.platform.testkit.engine.EventType.STARTED;
28 
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.util.stream.Stream;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.platform.testkit.engine.EngineExecutionResults;
37 import org.junit.platform.testkit.engine.EngineTestKit;
38 import org.junit.rules.TemporaryFolder;
39 
40 public class FindingsBaseDirTest {
41   private static final String ENGINE = "engine:junit-jupiter";
42   private static final String CLAZZ = "class:com.example.ThrowingFuzzTest";
43   private static final String INPUTS_FUZZ =
44       "test-template:throwingFuzz(com.code_intelligence.jazzer.api.FuzzedDataProvider)";
45 
46   @Rule public TemporaryFolder temp = new TemporaryFolder();
47 
48   private Path baseDir;
49 
50   @Before
setup()51   public void setup() {
52     baseDir = temp.getRoot().toPath();
53   }
54 
55   @Test
fuzzingEnabledNoFindingsDir()56   public void fuzzingEnabledNoFindingsDir() throws IOException {
57     assumeFalse(System.getenv("JAZZER_FUZZ").isEmpty());
58 
59     EngineExecutionResults results =
60         EngineTestKit.engine("junit-jupiter")
61             .selectors(selectClass("com.example.ThrowingFuzzTest"))
62             .configurationParameter("jazzer.internal.basedir", baseDir.toAbsolutePath().toString())
63             .execute();
64 
65     results.containerEvents().assertEventsMatchExactly(event(type(STARTED), container(ENGINE)),
66         event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ))),
67         event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ))),
68         // Warning because the inputs directory hasn't been found in the source tree.
69         event(type(REPORTING_ENTRY_PUBLISHED),
70             container(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ))),
71         event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ)),
72             finishedSuccessfully()),
73         event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ)), finishedSuccessfully()),
74         event(type(FINISHED), container(ENGINE), finishedSuccessfully()));
75 
76     // Crash should be emitted into the base directory, as no findings dir available.
77     try (Stream<Path> baseDirFiles = Files.list(baseDir)) {
78       Stream<Path> crashFiles =
79           baseDirFiles.filter(f -> f.getFileName().toString().startsWith("crash-"));
80       assertThat(crashFiles).hasSize(1);
81     }
82   }
83 }
84