-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for workflows using the callback server in unit tests.
This change requires: - Delaying the initialization of detectors to when the test runs rather than when it is being created; - Change to the test interface: instead of providing the detector tested, each test is now only provided with the name of the detector. It is now the responsibility of each test to initialize the detectors depending on their environment; This change might make unit tests slightly slower, especially as the list of plugin grows, but provide a lot more flexibility for testing hermetically. PiperOrigin-RevId: 725549365 Change-Id: I58f2a6f1f8955615b8ecbd1abe1c97aa1d62499d
- Loading branch information
1 parent
64c5541
commit 817c341
Showing
3 changed files
with
195 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...e/tsunami/plugins/detectors/templateddetector/actions/CallbackServerActionRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.google.tsunami.plugins.detectors.templateddetector.actions; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.inject.Guice; | ||
import com.google.tsunami.common.net.http.HttpClientModule; | ||
import com.google.tsunami.plugin.TcsClient; | ||
import com.google.tsunami.plugin.payload.testing.FakePayloadGeneratorModule; | ||
import com.google.tsunami.plugin.payload.testing.PayloadTestHelper; | ||
import com.google.tsunami.plugins.detectors.templateddetector.Environment; | ||
import com.google.tsunami.proto.NetworkService; | ||
import com.google.tsunami.templatedplugin.proto.CallbackServerAction; | ||
import com.google.tsunami.templatedplugin.proto.PluginAction; | ||
import java.io.IOException; | ||
import java.security.SecureRandom; | ||
import java.util.Arrays; | ||
import javax.inject.Inject; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public final class CallbackServerActionRunnerTest { | ||
private CallbackServerActionRunner runner; | ||
private Environment environment; | ||
private MockWebServer mockCallbackServer; | ||
private NetworkService service; | ||
|
||
@Inject private TcsClient tcsClient; | ||
|
||
private static final SecureRandom testSecureRandom = | ||
new SecureRandom() { | ||
@Override | ||
public void nextBytes(byte[] bytes) { | ||
Arrays.fill(bytes, (byte) 0xFF); | ||
} | ||
}; | ||
|
||
@Before | ||
public void setup() { | ||
this.environment = new Environment(false); | ||
this.environment.set("T_CBS_SECRET", "irrelevant"); | ||
this.service = NetworkService.getDefaultInstance(); | ||
} | ||
|
||
@Before | ||
public void setupMockHttp() { | ||
this.mockCallbackServer = new MockWebServer(); | ||
} | ||
|
||
@After | ||
public void tearMockHttp() throws IOException { | ||
this.mockCallbackServer.shutdown(); | ||
} | ||
|
||
@Test | ||
public void checkAction_whenCallbackServerDisabled_returnsFalse() throws IOException { | ||
PluginAction action = | ||
PluginAction.newBuilder() | ||
.setName("action") | ||
.setCallbackServer( | ||
CallbackServerAction.newBuilder() | ||
.setActionType(CallbackServerAction.ActionType.CHECK)) | ||
.build(); | ||
|
||
setupCallbackServer(false, false); | ||
|
||
assertThat(runner.run(this.service, action, this.environment)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void checkAction_whenCallbackServerReturnsFalse_returnsFalse() throws IOException { | ||
PluginAction action = | ||
PluginAction.newBuilder() | ||
.setName("action") | ||
.setCallbackServer( | ||
CallbackServerAction.newBuilder() | ||
.setActionType(CallbackServerAction.ActionType.CHECK)) | ||
.build(); | ||
|
||
setupCallbackServer(true, false); | ||
|
||
assertThat(runner.run(this.service, action, this.environment)).isFalse(); | ||
assertThat(this.mockCallbackServer.getRequestCount()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void checkAction_whenCallbackServerReturnsTrue_returnsTrue() throws IOException { | ||
PluginAction action = | ||
PluginAction.newBuilder() | ||
.setName("action") | ||
.setCallbackServer( | ||
CallbackServerAction.newBuilder() | ||
.setActionType(CallbackServerAction.ActionType.CHECK)) | ||
.build(); | ||
|
||
setupCallbackServer(true, true); | ||
|
||
assertThat(runner.run(this.service, action, this.environment)).isTrue(); | ||
assertThat(this.mockCallbackServer.getRequestCount()).isEqualTo(1); | ||
} | ||
|
||
private final void setupCallbackServer(boolean enabled, boolean response) throws IOException { | ||
FakePayloadGeneratorModule.Builder payloadGeneratorModuleBuilder = | ||
FakePayloadGeneratorModule.builder().setSecureRng(testSecureRandom); | ||
|
||
if (enabled) { | ||
payloadGeneratorModuleBuilder.setCallbackServer(mockCallbackServer); | ||
|
||
if (response) { | ||
mockCallbackServer.enqueue(PayloadTestHelper.generateMockSuccessfulCallbackResponse()); | ||
} else { | ||
mockCallbackServer.enqueue(PayloadTestHelper.generateMockUnsuccessfulCallbackResponse()); | ||
} | ||
} | ||
|
||
Guice.createInjector( | ||
new HttpClientModule.Builder().build(), payloadGeneratorModuleBuilder.build()) | ||
.injectMembers(this); | ||
this.runner = new CallbackServerActionRunner(tcsClient, false); | ||
} | ||
} |