16
16
17
17
package com .example ;
18
18
19
+ import static com .google .common .truth .Truth .assertThat ;
20
+ import static java .util .Arrays .asList ;
21
+ import static java .util .Collections .unmodifiableList ;
22
+
23
+ import com .code_intelligence .jazzer .api .FuzzerSecurityIssueLow ;
19
24
import com .code_intelligence .jazzer .junit .FuzzTest ;
25
+ import com .example .LifecycleFuzzTest .LifecycleCallbacks1 ;
26
+ import com .example .LifecycleFuzzTest .LifecycleCallbacks2 ;
27
+ import com .example .LifecycleFuzzTest .LifecycleCallbacks3 ;
20
28
import java .io .IOException ;
29
+ import java .util .ArrayList ;
30
+ import java .util .Collections ;
31
+ import java .util .List ;
21
32
import org .junit .jupiter .api .AfterAll ;
22
33
import org .junit .jupiter .api .AfterEach ;
23
- import org .junit .jupiter .api .Assertions ;
24
34
import org .junit .jupiter .api .BeforeAll ;
25
35
import org .junit .jupiter .api .BeforeEach ;
26
36
import org .junit .jupiter .api .Disabled ;
27
37
import org .junit .jupiter .api .MethodOrderer ;
28
38
import org .junit .jupiter .api .TestMethodOrder ;
39
+ import org .junit .jupiter .api .extension .AfterEachCallback ;
40
+ import org .junit .jupiter .api .extension .BeforeEachCallback ;
29
41
import org .junit .jupiter .api .extension .ExtendWith ;
30
42
import org .junit .jupiter .api .extension .ExtensionContext ;
31
43
import org .junit .jupiter .api .extension .TestInstancePostProcessor ;
32
44
33
45
@ TestMethodOrder (MethodOrderer .MethodName .class )
34
46
@ ExtendWith (LifecycleFuzzTest .LifecycleInstancePostProcessor .class )
47
+ @ ExtendWith (LifecycleCallbacks1 .class )
48
+ @ ExtendWith (LifecycleCallbacks2 .class )
49
+ @ ExtendWith (LifecycleCallbacks3 .class )
35
50
class LifecycleFuzzTest {
36
- // In fuzzing mode, the test is invoked once on the empty input and once with Jazzer.
37
- private static final int EXPECTED_EACH_COUNT =
38
- System .getenv ().getOrDefault ("JAZZER_FUZZ" , "" ).isEmpty () ? 1 : 2 ;
39
-
40
- private static int beforeAllCount = 0 ;
41
- private static int beforeEachGlobalCount = 0 ;
42
- private static int afterEachGlobalCount = 0 ;
43
- private static int afterAllCount = 0 ;
51
+ private static final ArrayList <String > events = new ArrayList <>();
44
52
45
53
private boolean beforeEachCalledOnInstance = false ;
46
54
private boolean testInstancePostProcessorCalledOnInstance = false ;
47
55
48
56
@ BeforeAll
49
57
static void beforeAll () {
50
- beforeAllCount ++ ;
58
+ events . add ( "beforeAll" ) ;
51
59
}
52
60
53
61
@ BeforeEach
54
- void beforeEach () {
55
- beforeEachGlobalCount ++ ;
62
+ void beforeEach1 () {
63
+ events . add ( "beforeEach1" ) ;
56
64
beforeEachCalledOnInstance = true ;
57
65
}
58
66
67
+ @ BeforeEach
68
+ void beforeEach2 () {
69
+ events .add ("beforeEach2" );
70
+ }
71
+
72
+ @ BeforeEach
73
+ void beforeEach3 () {
74
+ events .add ("beforeEach3" );
75
+ }
76
+
59
77
@ Disabled
60
78
@ FuzzTest
61
79
void disabledFuzz (byte [] data ) {
80
+ events .add ("disabledFuzz" );
62
81
throw new AssertionError ("This test should not be executed" );
63
82
}
64
83
65
- @ FuzzTest (maxDuration = "1s" )
84
+ @ FuzzTest (maxExecutions = 3 )
66
85
void lifecycleFuzz (byte [] data ) {
67
- Assertions .assertEquals (1 , beforeAllCount );
68
- Assertions .assertEquals (beforeEachGlobalCount , afterEachGlobalCount + 1 );
69
- Assertions .assertTrue (beforeEachCalledOnInstance );
70
- Assertions .assertTrue (testInstancePostProcessorCalledOnInstance );
86
+ events .add ("lifecycleFuzz" );
87
+ assertThat (beforeEachCalledOnInstance ).isTrue ();
88
+ assertThat (testInstancePostProcessorCalledOnInstance ).isTrue ();
89
+ }
90
+
91
+ @ AfterEach
92
+ void afterEach1 () {
93
+ events .add ("afterEach1" );
71
94
}
72
95
73
96
@ AfterEach
74
- void afterEach () {
75
- afterEachGlobalCount ++;
97
+ void afterEach2 () {
98
+ events .add ("afterEach2" );
99
+ }
100
+
101
+ @ AfterEach
102
+ void afterEach3 () {
103
+ events .add ("afterEach3" );
76
104
}
77
105
78
106
@ AfterAll
79
- static void afterAll () throws IOException {
80
- afterAllCount ++;
81
- Assertions .assertEquals (1 , beforeAllCount );
82
- Assertions .assertEquals (EXPECTED_EACH_COUNT , beforeEachGlobalCount );
83
- Assertions .assertEquals (EXPECTED_EACH_COUNT , afterEachGlobalCount );
84
- Assertions .assertEquals (1 , afterAllCount );
85
- throw new IOException ();
107
+ static void afterAll () throws TestSuccessfulException {
108
+ events .add ("afterAll" );
109
+
110
+ boolean isRegressionTest = "" .equals (System .getenv ("JAZZER_FUZZ" ));
111
+ boolean isFuzzingFromCommandLine = System .getenv ("JAZZER_FUZZ" ) == null ;
112
+ boolean isFuzzingFromJUnit = !isFuzzingFromCommandLine && !isRegressionTest ;
113
+
114
+ final List <String > expectedBeforeEachEvents = unmodifiableList (asList ("beforeEachCallback1" ,
115
+ "beforeEachCallback2" , "beforeEachCallback3" , "beforeEach1" , "beforeEach2" , "beforeEach3" ));
116
+ final List <String > expectedAfterEachEvents = unmodifiableList (asList ("afterEach1" , "afterEach2" ,
117
+ "afterEach3" , "afterEachCallback3" , "afterEachCallback2" , "afterEachCallback1" ));
118
+
119
+ ArrayList <String > expectedEvents = new ArrayList <>();
120
+ expectedEvents .add ("beforeAll" );
121
+
122
+ // When run from the command-line, the fuzz test is not separately executed on the empty seed.
123
+ if (isRegressionTest || isFuzzingFromJUnit ) {
124
+ expectedEvents .addAll (expectedBeforeEachEvents );
125
+ expectedEvents .add ("lifecycleFuzz" );
126
+ expectedEvents .addAll (expectedAfterEachEvents );
127
+ }
128
+ if (isFuzzingFromJUnit || isFuzzingFromCommandLine ) {
129
+ expectedEvents .addAll (expectedBeforeEachEvents );
130
+ // TODO: Fuzz tests currently don't run before each and after each methods between fuzz test
131
+ // invocations.
132
+ expectedEvents .addAll (Collections .nCopies (3 , "lifecycleFuzz" ));
133
+ expectedEvents .addAll (expectedAfterEachEvents );
134
+ }
135
+
136
+ expectedEvents .add ("afterAll" );
137
+
138
+ assertThat (events ).containsExactlyElementsIn (expectedEvents ).inOrder ();
139
+ throw new TestSuccessfulException ("Lifecycle methods invoked as expected" );
86
140
}
87
141
88
142
static class LifecycleInstancePostProcessor implements TestInstancePostProcessor {
@@ -91,4 +145,40 @@ public void postProcessTestInstance(Object o, ExtensionContext extensionContext)
91
145
((LifecycleFuzzTest ) o ).testInstancePostProcessorCalledOnInstance = true ;
92
146
}
93
147
}
148
+
149
+ static class LifecycleCallbacks1 implements BeforeEachCallback , AfterEachCallback {
150
+ @ Override
151
+ public void beforeEach (ExtensionContext extensionContext ) {
152
+ events .add ("beforeEachCallback1" );
153
+ }
154
+
155
+ @ Override
156
+ public void afterEach (ExtensionContext extensionContext ) {
157
+ events .add ("afterEachCallback1" );
158
+ }
159
+ }
160
+
161
+ static class LifecycleCallbacks2 implements BeforeEachCallback , AfterEachCallback {
162
+ @ Override
163
+ public void beforeEach (ExtensionContext extensionContext ) {
164
+ events .add ("beforeEachCallback2" );
165
+ }
166
+
167
+ @ Override
168
+ public void afterEach (ExtensionContext extensionContext ) {
169
+ events .add ("afterEachCallback2" );
170
+ }
171
+ }
172
+
173
+ static class LifecycleCallbacks3 implements BeforeEachCallback , AfterEachCallback {
174
+ @ Override
175
+ public void beforeEach (ExtensionContext extensionContext ) {
176
+ events .add ("beforeEachCallback3" );
177
+ }
178
+
179
+ @ Override
180
+ public void afterEach (ExtensionContext extensionContext ) {
181
+ events .add ("afterEachCallback3" );
182
+ }
183
+ }
94
184
}
0 commit comments