|
20 | 20 | package org.zaproxy.addon.automation.jobs; |
21 | 21 |
|
22 | 22 | import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | +import static org.hamcrest.Matchers.contains; |
23 | 24 | import static org.hamcrest.Matchers.equalTo; |
24 | 25 | import static org.hamcrest.Matchers.is; |
25 | 26 | import static org.hamcrest.Matchers.notNullValue; |
|
43 | 44 | import java.util.Locale; |
44 | 45 | import java.util.Map; |
45 | 46 | import java.util.Objects; |
| 47 | +import org.apache.commons.configuration.ConfigurationException; |
46 | 48 | import org.junit.jupiter.api.AfterAll; |
47 | 49 | import org.junit.jupiter.api.BeforeAll; |
48 | 50 | import org.junit.jupiter.api.BeforeEach; |
|
74 | 76 | import org.zaproxy.addon.automation.ContextWrapper; |
75 | 77 | import org.zaproxy.zap.extension.ascan.ActiveScan; |
76 | 78 | import org.zaproxy.zap.extension.ascan.ExtensionActiveScan; |
| 79 | +import org.zaproxy.zap.extension.ascan.PolicyManager; |
77 | 80 | import org.zaproxy.zap.extension.ascan.ScanPolicy; |
78 | 81 | import org.zaproxy.zap.model.Context; |
79 | 82 | import org.zaproxy.zap.model.Target; |
|
83 | 86 | class ActiveScanJobUnitTest { |
84 | 87 |
|
85 | 88 | private static MockedStatic<CommandLine> mockedCmdLine; |
| 89 | + private PolicyManager policyManager; |
86 | 90 | private ExtensionActiveScan extAScan; |
87 | 91 | private static AbstractPlugin plugin; |
88 | 92 |
|
@@ -120,6 +124,9 @@ void setUp() throws Exception { |
120 | 124 | extAScan = mock(ExtensionActiveScan.class, withSettings().strictness(Strictness.LENIENT)); |
121 | 125 | given(extensionLoader.getExtension(ExtensionActiveScan.class)).willReturn(extAScan); |
122 | 126 |
|
| 127 | + policyManager = mock(); |
| 128 | + given(extAScan.getPolicyManager()).willReturn(policyManager); |
| 129 | + |
123 | 130 | Control.initSingletonForTesting(Model.getSingleton(), extensionLoader); |
124 | 131 | Model.getSingleton().getOptionsParam().load(new ZapXmlConfiguration()); |
125 | 132 | } |
@@ -152,36 +159,14 @@ void shouldReturnCustomConfigParams() { |
152 | 159 | assertThat(params.get("context"), is(equalTo(""))); |
153 | 160 | } |
154 | 161 |
|
155 | | - @Test |
156 | | - void shouldApplyCustomConfigParams() { |
157 | | - // Given |
158 | | - String yamlStr = |
159 | | - "parameters:\n" |
160 | | - + " maxScanDurationInMins: 12\n" |
161 | | - + " maxAlertsPerRule: 5\n" |
162 | | - + " policy: testPolicy"; |
163 | | - AutomationProgress progress = new AutomationProgress(); |
164 | | - Yaml yaml = new Yaml(); |
165 | | - Object data = yaml.load(yamlStr); |
166 | | - |
167 | | - ActiveScanJob job = new ActiveScanJob(); |
168 | | - job.setJobData(((LinkedHashMap<?, ?>) data)); |
169 | | - |
170 | | - // When |
171 | | - job.verifyParameters(progress); |
172 | | - |
173 | | - // Then |
174 | | - assertThat(job.getParameters().getMaxScanDurationInMins(), is(equalTo(12))); |
175 | | - assertThat(job.getParameters().getMaxAlertsPerRule(), is(equalTo(5))); |
176 | | - assertThat(job.getParameters().getPolicy(), is(equalTo("testPolicy"))); |
177 | | - assertThat(progress.hasErrors(), is(equalTo(false))); |
178 | | - assertThat(progress.hasWarnings(), is(equalTo(false))); |
179 | | - } |
180 | | - |
181 | 162 | @Test |
182 | 163 | void shouldFailWithUnknownConfigParam() { |
183 | 164 | // Given |
184 | | - String yamlStr = "parameters:\n" + " blah: 12\n" + " policy: testPolicy"; |
| 165 | + String yamlStr = |
| 166 | + """ |
| 167 | + parameters: |
| 168 | + blah: 12 |
| 169 | + """; |
185 | 170 | AutomationProgress progress = new AutomationProgress(); |
186 | 171 | Yaml yaml = new Yaml(); |
187 | 172 | Object data = yaml.load(yamlStr); |
@@ -795,8 +780,10 @@ void shouldWarnOnInvalidIntThreshold() throws MalformedURLException { |
795 | 780 | } |
796 | 781 |
|
797 | 782 | @Test |
798 | | - void shouldVerifyParameters() { |
| 783 | + void shouldVerifyParameters() throws Exception { |
799 | 784 | // Given |
| 785 | + given(policyManager.getPolicy("policy1")).willReturn(mock(ScanPolicy.class)); |
| 786 | + |
800 | 787 | AutomationEnvironment env = mock(AutomationEnvironment.class); |
801 | 788 | given(env.getAllUserNames()).willReturn(List.of("user0", "user1")); |
802 | 789 | ActiveScanJob job = new ActiveScanJob(); |
@@ -844,4 +831,30 @@ void shouldVerifyParameters() { |
844 | 831 | assertThat(job.getParameters().getThreadPerHost(), is(equalTo(2))); |
845 | 832 | assertThat(job.getParameters().getMaxAlertsPerRule(), is(equalTo(5))); |
846 | 833 | } |
| 834 | + |
| 835 | + @Test |
| 836 | + void shouldErrorOnUnknownPolicy() throws Exception { |
| 837 | + // Given |
| 838 | + given(policyManager.getPolicy("missingPolicy")).willThrow(ConfigurationException.class); |
| 839 | + |
| 840 | + String yamlStr = |
| 841 | + """ |
| 842 | + parameters: |
| 843 | + policy: missingPolicy |
| 844 | + """; |
| 845 | + AutomationProgress progress = new AutomationProgress(); |
| 846 | + Yaml yaml = new Yaml(); |
| 847 | + Object data = yaml.load(yamlStr); |
| 848 | + |
| 849 | + ActiveScanJob job = new ActiveScanJob(); |
| 850 | + job.setJobData(((LinkedHashMap<?, ?>) data)); |
| 851 | + |
| 852 | + // When |
| 853 | + job.verifyParameters(progress); |
| 854 | + |
| 855 | + // Then |
| 856 | + assertThat(progress.hasErrors(), is(equalTo(true))); |
| 857 | + assertThat(progress.hasWarnings(), is(equalTo(false))); |
| 858 | + assertThat(progress.getErrors(), contains("!automation.error.ascan.policy.name!")); |
| 859 | + } |
847 | 860 | } |
0 commit comments