Search in sources :

Example 1 with CachedScript

use of org.zaproxy.zap.extension.script.ScriptsCache.CachedScript in project zaproxy by zaproxy.

the class ScriptsCacheUnitTest method shouldCacheEnabledScripts.

@Test
void shouldCacheEnabledScripts() throws Exception {
    // Given
    Script script1 = mock(Script.class);
    ScriptWrapper scriptWrapper1 = mockScriptWrapper(script1);
    Script script2 = mock(Script.class);
    ScriptWrapper scriptWrapper2 = mockScriptWrapper(script2);
    given(extensionScript.getScripts(scriptType)).willReturn(asList(scriptWrapper1, scriptWrapper2));
    // When
    scriptsCache.refresh();
    // Then
    List<CachedScript<Script>> cachedScripts = scriptsCache.getCachedScripts();
    assertThat(cachedScripts, hasSize(2));
    assertCachedScript(cachedScripts.get(0), scriptWrapper1, script1);
    assertCachedScript(cachedScripts.get(1), scriptWrapper2, script2);
    verify(extensionScript, times(1)).getScripts(scriptType);
    verify(extensionScript, times(1)).getInterface(scriptWrapper1, targetInterface);
    verify(extensionScript, times(1)).getInterface(scriptWrapper2, targetInterface);
    verifyNoMoreInteractions(extensionScript);
}
Also used : CachedScript(org.zaproxy.zap.extension.script.ScriptsCache.CachedScript) CachedScript(org.zaproxy.zap.extension.script.ScriptsCache.CachedScript) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with CachedScript

use of org.zaproxy.zap.extension.script.ScriptsCache.CachedScript in project zaproxy by zaproxy.

the class ScriptsCacheUnitTest method shouldNotCacheScriptsThatDoNotImplementInterface.

@Test
void shouldNotCacheScriptsThatDoNotImplementInterface() throws Exception {
    // Given
    Script script1 = mock(Script.class);
    ScriptWrapper scriptWrapper1 = mockScriptWrapper(script1);
    Script script2 = mock(Script.class);
    ScriptWrapper scriptWrapper2 = mockScriptWrapper(script2);
    given(extensionScript.getInterface(scriptWrapper2, targetInterface)).willReturn(null);
    given(extensionScript.getScripts(scriptType)).willReturn(asList(scriptWrapper1, scriptWrapper2));
    // When
    scriptsCache.refresh();
    // Then
    List<CachedScript<Script>> cachedScripts = scriptsCache.getCachedScripts();
    assertThat(cachedScripts, hasSize(1));
    assertCachedScript(cachedScripts.get(0), scriptWrapper1, script1);
    verify(extensionScript, times(1)).getScripts(scriptType);
    verify(extensionScript, times(1)).getInterface(scriptWrapper1, targetInterface);
    verify(extensionScript, times(1)).getInterface(scriptWrapper2, targetInterface);
    verify(extensionScript, times(1)).handleFailedScriptInterface(scriptWrapper2, interfaceErrorMessage);
    verifyNoMoreInteractions(extensionScript);
}
Also used : CachedScript(org.zaproxy.zap.extension.script.ScriptsCache.CachedScript) CachedScript(org.zaproxy.zap.extension.script.ScriptsCache.CachedScript) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with CachedScript

use of org.zaproxy.zap.extension.script.ScriptsCache.CachedScript in project zaproxy by zaproxy.

the class ScriptsCacheUnitTest method shouldCacheEnabledScriptsAndIgnoreDisabledScriptsWhenRefreshing.

@Test
void shouldCacheEnabledScriptsAndIgnoreDisabledScriptsWhenRefreshing() throws Exception {
    // Given
    Script script1 = mock(Script.class);
    ScriptWrapper scriptWrapper1 = mockScriptWrapper(script1);
    given(scriptWrapper1.isEnabled()).willReturn(false);
    Script script2 = mock(Script.class);
    ScriptWrapper scriptWrapper2 = mockScriptWrapper(script2);
    given(extensionScript.getScripts(scriptType)).willReturn(asList(scriptWrapper1, scriptWrapper2));
    // When
    scriptsCache.refresh();
    // Then
    List<CachedScript<Script>> cachedScripts = scriptsCache.getCachedScripts();
    assertThat(cachedScripts, hasSize(1));
    assertCachedScript(cachedScripts.get(0), scriptWrapper2, script2);
    verify(extensionScript, times(1)).getScripts(scriptType);
    verify(extensionScript, times(1)).getInterface(scriptWrapper2, targetInterface);
    verifyNoMoreInteractions(extensionScript);
}
Also used : CachedScript(org.zaproxy.zap.extension.script.ScriptsCache.CachedScript) CachedScript(org.zaproxy.zap.extension.script.ScriptsCache.CachedScript) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with CachedScript

use of org.zaproxy.zap.extension.script.ScriptsCache.CachedScript in project zaproxy by zaproxy.

the class ScriptsCacheUnitTest method shouldRefreshCachedScriptIfChanged.

@Test
void shouldRefreshCachedScriptIfChanged() throws Exception {
    // Given
    Script script1 = mock(Script.class);
    ScriptWrapper scriptWrapper1 = mockScriptWrapper(script1);
    Script script2 = mock(Script.class);
    ScriptWrapper scriptWrapper2 = mockScriptWrapper(script2);
    given(extensionScript.getScripts(scriptType)).willReturn(asList(scriptWrapper1, scriptWrapper2));
    // When
    scriptsCache.refresh();
    given(scriptWrapper2.getModCount()).willReturn(1);
    Script refreshedScript = mock(Script.class);
    given(extensionScript.getInterface(scriptWrapper2, targetInterface)).willReturn(refreshedScript);
    scriptsCache.refresh();
    // Then
    List<CachedScript<Script>> cachedScripts = scriptsCache.getCachedScripts();
    assertThat(cachedScripts, hasSize(2));
    assertCachedScript(cachedScripts.get(0), scriptWrapper1, script1);
    assertCachedScript(cachedScripts.get(1), scriptWrapper2, refreshedScript);
    verify(extensionScript, times(2)).getScripts(scriptType);
    verify(extensionScript, times(1)).getInterface(scriptWrapper1, targetInterface);
    verify(extensionScript, times(2)).getInterface(scriptWrapper2, targetInterface);
    verifyNoMoreInteractions(extensionScript);
}
Also used : CachedScript(org.zaproxy.zap.extension.script.ScriptsCache.CachedScript) CachedScript(org.zaproxy.zap.extension.script.ScriptsCache.CachedScript) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with CachedScript

use of org.zaproxy.zap.extension.script.ScriptsCache.CachedScript in project zaproxy by zaproxy.

the class ScriptsCacheUnitTest method shouldNotCacheScriptsNoLongerEnabledWhenRefreshing.

@Test
void shouldNotCacheScriptsNoLongerEnabledWhenRefreshing() throws Exception {
    // Given
    Script script1 = mock(Script.class);
    ScriptWrapper scriptWrapper1 = mockScriptWrapper(script1);
    Script script2 = mock(Script.class);
    ScriptWrapper scriptWrapper2 = mockScriptWrapper(script2);
    given(extensionScript.getScripts(scriptType)).willReturn(asList(scriptWrapper1, scriptWrapper2));
    // When
    scriptsCache.refresh();
    given(scriptWrapper1.isEnabled()).willReturn(false);
    scriptsCache.refresh();
    // Then
    List<CachedScript<Script>> cachedScripts = scriptsCache.getCachedScripts();
    assertThat(cachedScripts, hasSize(1));
    assertCachedScript(cachedScripts.get(0), scriptWrapper2, script2);
    verify(extensionScript, times(2)).getScripts(scriptType);
    verify(extensionScript, times(1)).getInterface(scriptWrapper1, targetInterface);
    verify(extensionScript, times(1)).getInterface(scriptWrapper2, targetInterface);
    verifyNoMoreInteractions(extensionScript);
}
Also used : CachedScript(org.zaproxy.zap.extension.script.ScriptsCache.CachedScript) CachedScript(org.zaproxy.zap.extension.script.ScriptsCache.CachedScript) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Test (org.junit.jupiter.api.Test)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 CachedScript (org.zaproxy.zap.extension.script.ScriptsCache.CachedScript)6 ScriptException (javax.script.ScriptException)1