35
35
import com .google .tsunami .common .time .testing .FakeUtcClock ;
36
36
import com .google .tsunami .common .time .testing .FakeUtcClockModule ;
37
37
import com .google .tsunami .plugin .payload .testing .FakePayloadGeneratorModule ;
38
+ import com .google .tsunami .plugin .payload .testing .PayloadTestHelper ;
38
39
import com .google .tsunami .proto .NetworkService ;
39
40
import com .google .tsunami .proto .TargetInfo ;
40
41
import com .google .tsunami .proto .TransportProtocol ;
@@ -62,6 +63,7 @@ public final class TemplatedDetectorDynamicTest {
62
63
63
64
private Environment environment ;
64
65
private MockWebServer mockWebServer ;
66
+ private MockWebServer mockCallbackServer ;
65
67
66
68
private static final FakeUtcClock fakeUtcClock =
67
69
FakeUtcClock .create ().setNow (Instant .parse ("2020-01-01T00:00:00.00Z" ));
@@ -74,21 +76,33 @@ public void nextBytes(byte[] bytes) {
74
76
};
75
77
76
78
@ Before
77
- public void setupMockWebServer () throws IOException {
79
+ public void setupMockServers () throws IOException {
78
80
environment = new Environment (false );
79
81
mockWebServer = new MockWebServer ();
82
+ mockCallbackServer = new MockWebServer ();
80
83
var baseUrl = "http://" + mockWebServer .getHostName () + ":" + mockWebServer .getPort () + "/" ;
81
84
environment .set ("T_NS_BASEURL" , baseUrl );
82
85
}
83
86
84
87
@ After
85
88
public void tearDown () throws IOException {
86
89
mockWebServer .shutdown ();
90
+ mockCallbackServer .shutdown ();
87
91
}
88
92
89
93
@ Test
90
94
@ TestParameters (valuesProvider = TestProvider .class )
91
- public void runTest (TemplatedDetector detector , TemplatedPluginTests .Test testCase ) {
95
+ public void runTest (String pluginName , TemplatedPluginTests .Test testCase ) {
96
+ var detectors = getDetectorsForCase (testCase );
97
+
98
+ if (!detectors .containsKey (pluginName )) {
99
+ throw new IllegalArgumentException (
100
+ "Plugin '"
101
+ + pluginName
102
+ + "' not found (ensure the tested_plugin field is set correctly)." );
103
+ }
104
+
105
+ var detector = detectors .get (pluginName );
92
106
switch (testCase .getAnyActionCase ()) {
93
107
case MOCK_HTTP_SERVER :
94
108
forHttpAction (detector , testCase );
@@ -98,6 +112,38 @@ public void runTest(TemplatedDetector detector, TemplatedPluginTests.Test testCa
98
112
}
99
113
}
100
114
115
+ private final ImmutableMap <String , TemplatedDetector > getDetectorsForCase (
116
+ TemplatedPluginTests .Test testCase ) {
117
+ // Inject the adequate callback server configuration.
118
+ FakePayloadGeneratorModule .Builder payloadGeneratorModuleBuilder =
119
+ FakePayloadGeneratorModule .builder ().setSecureRng (testSecureRandom );
120
+
121
+ if (testCase .getMockCallbackServer ().getEnabled ()) {
122
+ payloadGeneratorModuleBuilder .setCallbackServer (mockCallbackServer );
123
+
124
+ try {
125
+ if (testCase .getMockCallbackServer ().getHasInteraction ()) {
126
+ mockCallbackServer .enqueue (PayloadTestHelper .generateMockSuccessfulCallbackResponse ());
127
+ } else {
128
+ mockCallbackServer .enqueue (PayloadTestHelper .generateMockUnsuccessfulCallbackResponse ());
129
+ }
130
+ } catch (IOException e ) {
131
+ throw new RuntimeException (e );
132
+ }
133
+ }
134
+
135
+ // Inject dependencies and get the detectors.
136
+ var bootstrap = new TemplatedDetectorBootstrapModule ();
137
+ bootstrap .setForceLoadDetectors (true );
138
+ Guice .createInjector (
139
+ new FakeUtcClockModule (fakeUtcClock ),
140
+ new HttpClientModule .Builder ().build (),
141
+ payloadGeneratorModuleBuilder .build (),
142
+ bootstrap );
143
+
144
+ return bootstrap .getDetectors ();
145
+ }
146
+
101
147
private final void forHttpAction (TemplatedDetector detector , TemplatedPluginTests .Test testCase ) {
102
148
ImmutableList <NetworkService > httpServices =
103
149
ImmutableList .of (
@@ -199,34 +245,21 @@ private final MockResponse createResponse(HttpTestAction.MockResponse testRespon
199
245
static final class TestProvider extends TestParametersValuesProvider {
200
246
@ Override
201
247
public ImmutableList <TestParametersValues > provideValues (Context context ) {
202
- var detectors = getDetectors ();
203
248
return getResourceNames ().stream ()
204
249
.map (TestProvider ::loadPlugin )
205
250
.filter (plugin -> plugin != null )
206
- .flatMap (plugin -> parametersForPlugin (plugin , detectors ).stream ())
251
+ .flatMap (plugin -> parametersForPlugin (plugin ).stream ())
207
252
.collect (toImmutableList ());
208
253
}
209
254
210
- private static final ImmutableMap <String , TemplatedDetector > getDetectors () {
211
- var bootstrap = new TemplatedDetectorBootstrapModule ();
212
- bootstrap .setForceLoadDetectors (true );
213
- Guice .createInjector (
214
- new FakeUtcClockModule (fakeUtcClock ),
215
- new HttpClientModule .Builder ().build (),
216
- FakePayloadGeneratorModule .builder ().setSecureRng (testSecureRandom ).build (),
217
- bootstrap );
218
- return bootstrap .getDetectors ();
219
- }
220
-
221
- private static ImmutableList <TestParametersValues > generateCommonTests (
222
- TemplatedDetector detector ) {
255
+ private static ImmutableList <TestParametersValues > generateCommonTests (String pluginName ) {
223
256
// Echo server test: plugins should never return a vulnerability when the response just
224
257
// contains the request.
225
- var testName = detector . getName () + ", autogenerated_whenEchoServer_returnsFalse" ;
258
+ var testName = pluginName + ", autogenerated_whenEchoServer_returnsFalse" ;
226
259
return ImmutableList .of (
227
260
TestParametersValues .builder ()
228
261
.name (testName )
229
- .addParameter ("detector " , detector )
262
+ .addParameter ("pluginName " , pluginName )
230
263
.addParameter (
231
264
"testCase" ,
232
265
TemplatedPluginTests .Test .newBuilder ()
@@ -242,31 +275,25 @@ private static ImmutableList<TestParametersValues> generateCommonTests(
242
275
}
243
276
244
277
private static ImmutableList <TestParametersValues > parametersForPlugin (
245
- TemplatedPluginTests pluginTests , ImmutableMap < String , TemplatedDetector > detectors ) {
278
+ TemplatedPluginTests pluginTests ) {
246
279
var pluginName = pluginTests .getConfig ().getTestedPlugin ();
247
280
248
281
if (pluginTests .getConfig ().getDisabled ()) {
249
282
logger .atWarning ().log ("Plugin '%s' tests are disabled." , pluginName );
250
283
return ImmutableList .of ();
251
284
}
252
285
253
- if (!detectors .containsKey (pluginName )) {
254
- logger .atWarning ().log ("Plugin '%s' not found or disabled. Skipping test." , pluginName );
255
- return ImmutableList .of ();
256
- }
257
-
258
- var detector = detectors .get (pluginName );
259
286
var testsBuilder = ImmutableList .<TestParametersValues >builder ();
260
287
// Inject tests that are common to all plugins.
261
- testsBuilder .addAll (generateCommonTests (detector ));
288
+ testsBuilder .addAll (generateCommonTests (pluginName ));
262
289
263
290
// Tests defined in the plugin test file.
264
291
pluginTests .getTestsList ().stream ()
265
292
.map (
266
293
t ->
267
294
TestParametersValues .builder ()
268
295
.name (pluginName + ", " + t .getName ())
269
- .addParameter ("detector " , detector )
296
+ .addParameter ("pluginName " , pluginName )
270
297
.addParameter ("testCase" , t )
271
298
.build ())
272
299
.forEach (testsBuilder ::add );
0 commit comments