Search in sources :

Example 11 with ScriptWrapper

use of org.zaproxy.zap.extension.script.ScriptWrapper in project zaproxy by zaproxy.

the class ScriptsActiveScannerUnitTest method shouldCreateScriptsCacheWithExpectedConfiguration.

@Test
@SuppressWarnings("unchecked")
void shouldCreateScriptsCacheWithExpectedConfiguration() throws Exception {
    // Given
    ActiveScript script = mock(ActiveScript.class);
    ScriptWrapper scriptWrapper = createScriptWrapper(script, ActiveScript.class);
    given(extensionScript.getScripts(SCRIPT_TYPE)).willReturn(asList(scriptWrapper));
    VariantFactory variantFactory = mock(VariantFactory.class);
    given(variantFactory.createVariants(any(), any())).willReturn(asList(mock(Variant.class)));
    given(model.getVariantFactory()).willReturn(variantFactory);
    ScriptsActiveScanner scriptsActiveScanner = new ScriptsActiveScanner();
    scriptsActiveScanner.init(message, parent);
    // When
    scriptsActiveScanner.scan();
    // Then
    ArgumentCaptor<Configuration<ActiveScript>> argumentCaptor = ArgumentCaptor.forClass(Configuration.class);
    verify(extensionScript).createScriptsCache(argumentCaptor.capture());
    Configuration<ActiveScript> configuration = argumentCaptor.getValue();
    assertThat(configuration.getScriptType(), is(equalTo(SCRIPT_TYPE)));
    assertThat(configuration.getTargetInterface(), is(equalTo(TARGET_INTERFACE_CACHE)));
    assertThat(configuration.getInterfaceProvider(), is(not(nullValue())));
    assertThat(configuration.getInterfaceErrorMessageProvider(), is(nullValue()));
}
Also used : Configuration(org.zaproxy.zap.extension.script.ScriptsCache.Configuration) ScriptWrapper(org.zaproxy.zap.extension.script.ScriptWrapper) Test(org.junit.jupiter.api.Test) WithConfigsTest(org.zaproxy.zap.WithConfigsTest)

Example 12 with ScriptWrapper

use of org.zaproxy.zap.extension.script.ScriptWrapper in project zaproxy by zaproxy.

the class ScriptsActiveScannerUnitTest method shouldScanNodesWithActiveScript2.

@Test
void shouldScanNodesWithActiveScript2() throws Exception {
    // Given
    ActiveScript2 script1 = mock(ActiveScript2.class);
    ScriptWrapper scriptWrapper1 = createScriptWrapper(script1, ActiveScript2.class);
    ActiveScript2 script2 = mock(ActiveScript2.class);
    ScriptWrapper scriptWrapper2 = createScriptWrapper(script2, ActiveScript2.class);
    given(extensionScript.getScripts(SCRIPT_TYPE)).willReturn(asList(scriptWrapper1, scriptWrapper2));
    VariantFactory variantFactory = mock(VariantFactory.class);
    given(variantFactory.createVariants(any(), any())).willReturn(asList(mock(Variant.class)));
    given(model.getVariantFactory()).willReturn(variantFactory);
    ScriptsActiveScanner scriptsActiveScanner = new ScriptsActiveScanner();
    scriptsActiveScanner.init(message, parent);
    // When
    scriptsActiveScanner.scan();
    // Then
    verify(script1, times(1)).scanNode(scriptsActiveScanner, message);
    verify(script2, times(1)).scanNode(scriptsActiveScanner, message);
}
Also used : ScriptWrapper(org.zaproxy.zap.extension.script.ScriptWrapper) Test(org.junit.jupiter.api.Test) WithConfigsTest(org.zaproxy.zap.WithConfigsTest)

Example 13 with ScriptWrapper

use of org.zaproxy.zap.extension.script.ScriptWrapper in project zaproxy by zaproxy.

the class ScriptsActiveScannerUnitTest method shouldNotCallScanNodeOnDisabledActiveScript2.

@Test
void shouldNotCallScanNodeOnDisabledActiveScript2() throws Exception {
    // Given
    ScriptWrapper scriptWrapper1 = mock(ScriptWrapper.class);
    given(scriptWrapper1.isEnabled()).willReturn(false);
    ActiveScript2 script2 = mock(ActiveScript2.class);
    ScriptWrapper scriptWrapper2 = createScriptWrapper(script2, ActiveScript2.class);
    given(extensionScript.getScripts(SCRIPT_TYPE)).willReturn(asList(scriptWrapper1, scriptWrapper2));
    VariantFactory variantFactory = mock(VariantFactory.class);
    given(variantFactory.createVariants(any(), any())).willReturn(asList(mock(Variant.class)));
    given(model.getVariantFactory()).willReturn(variantFactory);
    ScriptsActiveScanner scriptsActiveScanner = new ScriptsActiveScanner();
    scriptsActiveScanner.init(message, parent);
    // When
    scriptsActiveScanner.scan();
    // Then
    verify(extensionScript, times(0)).getInterface(scriptWrapper1, ActiveScript2.class);
    verify(script2, times(1)).scanNode(scriptsActiveScanner, message);
}
Also used : ScriptWrapper(org.zaproxy.zap.extension.script.ScriptWrapper) Test(org.junit.jupiter.api.Test) WithConfigsTest(org.zaproxy.zap.WithConfigsTest)

Example 14 with ScriptWrapper

use of org.zaproxy.zap.extension.script.ScriptWrapper in project zaproxy by zaproxy.

the class ScriptsActiveScannerUnitTest method shouldStopScanningNodesWithActiveScript2WhenScanStopped.

@Test
void shouldStopScanningNodesWithActiveScript2WhenScanStopped() throws Exception {
    // Given
    ActiveScript2 script1 = mock(ActiveScript2.class);
    doAnswer(stopScan()).when(script1).scanNode(any(), any());
    ScriptWrapper scriptWrapper1 = createScriptWrapper(script1, ActiveScript2.class);
    ScriptWrapper scriptWrapper2 = mock(ScriptWrapper.class);
    given(extensionScript.getScripts(SCRIPT_TYPE)).willReturn(asList(scriptWrapper1, scriptWrapper2));
    ScriptsActiveScanner scriptsActiveScanner = new ScriptsActiveScanner();
    scriptsActiveScanner.init(message, parent);
    // When
    scriptsActiveScanner.scan();
    // Then
    verify(script1, times(1)).scanNode(scriptsActiveScanner, message);
    verify(extensionScript, times(0)).getInterface(scriptWrapper2, ActiveScript2.class);
}
Also used : ScriptWrapper(org.zaproxy.zap.extension.script.ScriptWrapper) Test(org.junit.jupiter.api.Test) WithConfigsTest(org.zaproxy.zap.WithConfigsTest)

Example 15 with ScriptWrapper

use of org.zaproxy.zap.extension.script.ScriptWrapper in project zaproxy by zaproxy.

the class ScriptsActiveScannerUnitTest method shouldFailScriptsThatDoNotImplementNeitherActiveScript2NorActiveScript.

@Test
void shouldFailScriptsThatDoNotImplementNeitherActiveScript2NorActiveScript() {
    // Given
    ScriptWrapper scriptWrapper = mock(ScriptWrapper.class);
    given(scriptWrapper.isEnabled()).willReturn(true);
    given(extensionScript.getScripts(SCRIPT_TYPE)).willReturn(asList(scriptWrapper));
    VariantFactory variantFactory = mock(VariantFactory.class);
    given(variantFactory.createVariants(any(), any())).willReturn(asList(mock(Variant.class)));
    given(model.getVariantFactory()).willReturn(variantFactory);
    ScriptsActiveScanner scriptsActiveScanner = new ScriptsActiveScanner();
    scriptsActiveScanner.init(message, parent);
    given(extensionScript.<ActiveScript>createScriptsCache(any())).willAnswer(e -> {
        Configuration<ActiveScript> configuration = e.getArgument(0);
        configuration.getInterfaceProvider().getInterface(scriptWrapper, ActiveScript.class);
        return null;
    });
    // When
    scriptsActiveScanner.scan();
    // Then
    verify(extensionScript).handleFailedScriptInterface(eq(scriptWrapper), any());
}
Also used : ScriptWrapper(org.zaproxy.zap.extension.script.ScriptWrapper) Test(org.junit.jupiter.api.Test) WithConfigsTest(org.zaproxy.zap.WithConfigsTest)

Aggregations

ScriptWrapper (org.zaproxy.zap.extension.script.ScriptWrapper)25 Test (org.junit.jupiter.api.Test)16 WithConfigsTest (org.zaproxy.zap.WithConfigsTest)10 HttpMessage (org.parosproxy.paros.network.HttpMessage)7 ExtensionScript (org.zaproxy.zap.extension.script.ExtensionScript)7 ArrayList (java.util.ArrayList)4 ScriptException (javax.script.ScriptException)4 Variant (org.parosproxy.paros.core.scanner.Variant)4 NameValuePair (org.parosproxy.paros.core.scanner.NameValuePair)3 ScannerParam (org.parosproxy.paros.core.scanner.ScannerParam)3 ApiException (org.zaproxy.zap.extension.api.ApiException)3 Configuration (org.zaproxy.zap.extension.script.ScriptsCache.Configuration)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 JSONObject (net.sf.json.JSONObject)2 ConfigurationException (org.apache.commons.configuration.ConfigurationException)2 HttpException (org.apache.commons.httpclient.HttpException)2 DatabaseException (org.parosproxy.paros.db.DatabaseException)2 RecordContext (org.parosproxy.paros.db.RecordContext)2 ApiDynamicActionImplementor (org.zaproxy.zap.extension.api.ApiDynamicActionImplementor)2