• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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 org.junit.Assume.assumeFalse;
18 import static org.junit.Assume.assumeTrue;
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.displayName;
22 import static org.junit.platform.testkit.engine.EventConditions.event;
23 import static org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully;
24 import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure;
25 import static org.junit.platform.testkit.engine.EventConditions.test;
26 import static org.junit.platform.testkit.engine.EventConditions.type;
27 import static org.junit.platform.testkit.engine.EventConditions.uniqueIdSubstrings;
28 import static org.junit.platform.testkit.engine.EventType.DYNAMIC_TEST_REGISTERED;
29 import static org.junit.platform.testkit.engine.EventType.FINISHED;
30 import static org.junit.platform.testkit.engine.EventType.REPORTING_ENTRY_PUBLISHED;
31 import static org.junit.platform.testkit.engine.EventType.SKIPPED;
32 import static org.junit.platform.testkit.engine.EventType.STARTED;
33 import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf;
34 
35 import java.io.IOException;
36 import java.nio.file.Path;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.platform.testkit.engine.EngineExecutionResults;
41 import org.junit.platform.testkit.engine.EngineTestKit;
42 import org.junit.rules.TemporaryFolder;
43 
44 public class LifecycleTest {
45   private static final String ENGINE = "engine:junit-jupiter";
46   private static final String CLAZZ = "class:com.example.LifecycleFuzzTest";
47   private static final String DISABLED_FUZZ = "test-template:disabledFuzz([B)";
48   private static final String LIFECYCLE_FUZZ = "test-template:lifecycleFuzz([B)";
49   private static final String INVOCATION = "test-template-invocation:#";
50   @Rule public TemporaryFolder temp = new TemporaryFolder();
51   Path baseDir;
52 
53   @Before
setup()54   public void setup() {
55     baseDir = temp.getRoot().toPath();
56   }
57 
executeTests()58   private EngineExecutionResults executeTests() {
59     return EngineTestKit.engine("junit-jupiter")
60         .selectors(selectClass("com.example.LifecycleFuzzTest"))
61         .configurationParameter(
62             "jazzer.instrument", "com.other.package.**,com.example.**,com.yet.another.package.*")
63         .configurationParameter("jazzer.internal.basedir", baseDir.toAbsolutePath().toString())
64         .execute();
65   }
66 
67   @Test
fuzzingEnabled()68   public void fuzzingEnabled() {
69     assumeFalse(System.getenv("JAZZER_FUZZ").isEmpty());
70 
71     EngineExecutionResults results = executeTests();
72 
73     results.containerEvents().assertEventsMatchExactly(event(type(STARTED), container(ENGINE)),
74         event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ))),
75         event(type(SKIPPED), container(uniqueIdSubstrings(ENGINE, CLAZZ, DISABLED_FUZZ))),
76         event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ))),
77         // Warning because the seed corpus directory hasn't been found.
78         event(type(REPORTING_ENTRY_PUBLISHED),
79             container(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ))),
80         event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ)),
81             finishedSuccessfully()),
82         event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ)),
83             finishedWithFailure(instanceOf(IOException.class))),
84         event(type(FINISHED), container(ENGINE), finishedSuccessfully()));
85 
86     results.testEvents().assertEventsMatchExactly(
87         event(
88             type(DYNAMIC_TEST_REGISTERED), test(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ))),
89         event(type(STARTED),
90             test(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ, INVOCATION + 1)),
91             displayName("<empty input>")),
92         event(type(FINISHED),
93             test(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ, INVOCATION + 1)),
94             displayName("<empty input>"), finishedSuccessfully()),
95         event(
96             type(DYNAMIC_TEST_REGISTERED), test(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ))),
97         event(type(STARTED),
98             test(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ, INVOCATION + 2)),
99             displayName("Fuzzing...")),
100         event(type(FINISHED),
101             test(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ, INVOCATION + 2)),
102             displayName("Fuzzing..."), finishedSuccessfully()));
103   }
104 
105   @Test
fuzzingDisabled()106   public void fuzzingDisabled() {
107     assumeTrue(System.getenv("JAZZER_FUZZ").isEmpty());
108 
109     EngineExecutionResults results = executeTests();
110 
111     results.containerEvents().assertEventsMatchExactly(event(type(STARTED), container(ENGINE)),
112         event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ))),
113         event(type(SKIPPED), container(uniqueIdSubstrings(ENGINE, CLAZZ, DISABLED_FUZZ))),
114         event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ))),
115         event(type(REPORTING_ENTRY_PUBLISHED),
116             container(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ))),
117         event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ))),
118         event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ)),
119             finishedWithFailure(instanceOf(IOException.class))),
120         event(type(FINISHED), container(ENGINE), finishedSuccessfully()));
121 
122     results.testEvents().assertEventsMatchExactly(
123         event(
124             type(DYNAMIC_TEST_REGISTERED), test(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ))),
125         event(type(STARTED),
126             test(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ, INVOCATION + 1)),
127             displayName("<empty input>")),
128         event(type(FINISHED),
129             test(uniqueIdSubstrings(ENGINE, CLAZZ, LIFECYCLE_FUZZ, INVOCATION + 1)),
130             displayName("<empty input>"), finishedSuccessfully()));
131   }
132 }
133