|
| 1 | +/* |
| 2 | + * Zed Attack Proxy (ZAP) and its related class files. |
| 3 | + * |
| 4 | + * ZAP is an HTTP/HTTPS proxy for assessing web application security. |
| 5 | + * |
| 6 | + * Copyright 2026 The ZAP Development Team |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +package org.zaproxy.gradle.addon.misc; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.List; |
| 28 | +import java.util.stream.Stream; |
| 29 | +import org.gradle.testkit.runner.BuildResult; |
| 30 | +import org.gradle.testkit.runner.GradleRunner; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.Arguments; |
| 35 | +import org.junit.jupiter.params.provider.MethodSource; |
| 36 | +import org.zaproxy.gradle.addon.AddOnStatus; |
| 37 | +import org.zaproxy.gradle.addon.FunctionalTest; |
| 38 | + |
| 39 | +class CopyAddOnFunctionalTest extends FunctionalTest { |
| 40 | + |
| 41 | + private static final String COPY_ADD_ON_TASK = ":copyZapAddOn"; |
| 42 | + private static final String ADD_ON_ID = "testaddon"; |
| 43 | + private static final String ADD_ON_VERSION = "1"; |
| 44 | + private static final String ADD_ON_FILE = |
| 45 | + ADD_ON_ID + "-" + AddOnStatus.ALPHA + "-" + ADD_ON_VERSION + ".zap"; |
| 46 | + |
| 47 | + private Path destDir; |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + void setup() throws Exception { |
| 51 | + destDir = Files.createDirectories(projectDir.resolve("dest")); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void buildFile(String content) throws Exception { |
| 56 | + super.buildFile( |
| 57 | + """ |
| 58 | + plugins { |
| 59 | + java |
| 60 | + id("org.zaproxy.add-on") |
| 61 | + } |
| 62 | + repositories { |
| 63 | + mavenCentral() |
| 64 | + } |
| 65 | + version = "%s" |
| 66 | + zapAddOn { |
| 67 | + addOnId.set("%s") |
| 68 | + addOnName.set("Test Add-On") |
| 69 | + } |
| 70 | + """ |
| 71 | + .formatted(ADD_ON_VERSION, ADD_ON_ID) |
| 72 | + + content); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void shouldCopyAddOnToDefaultDirectory() throws Exception { |
| 77 | + // Given |
| 78 | + Path nestedProjectDir = projectDir.resolve("project"); |
| 79 | + createFile( |
| 80 | + """ |
| 81 | + plugins { |
| 82 | + java |
| 83 | + id("org.zaproxy.add-on") |
| 84 | + } |
| 85 | + repositories { |
| 86 | + mavenCentral() |
| 87 | + } |
| 88 | + version = "%s" |
| 89 | + zapAddOn { |
| 90 | + addOnId.set("%s") |
| 91 | + addOnName.set("Test Add-On") |
| 92 | + } |
| 93 | + """ |
| 94 | + .formatted(ADD_ON_VERSION, ADD_ON_ID), |
| 95 | + nestedProjectDir.resolve("build.gradle.kts")); |
| 96 | + |
| 97 | + // When |
| 98 | + BuildResult result = |
| 99 | + GradleRunner.create() |
| 100 | + .withProjectDir(nestedProjectDir.toFile()) |
| 101 | + .withArguments(COPY_ADD_ON_TASK) |
| 102 | + .withPluginClasspath() |
| 103 | + .build(); |
| 104 | + |
| 105 | + // Then |
| 106 | + assertTaskSuccess(result, COPY_ADD_ON_TASK); |
| 107 | + Path defaultDest = projectDir.resolve("zaproxy/zap/src/main/dist/plugin"); |
| 108 | + assertThat(defaultDest.resolve(ADD_ON_FILE)).exists(); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void shouldCopyAddOnToSpecifiedDirectory() throws Exception { |
| 113 | + // Given |
| 114 | + buildFile(""); |
| 115 | + |
| 116 | + // When |
| 117 | + BuildResult result = runCopyAddOn(); |
| 118 | + |
| 119 | + // Then |
| 120 | + assertTaskSuccess(result, COPY_ADD_ON_TASK); |
| 121 | + assertThat(destDir.resolve(ADD_ON_FILE)).exists(); |
| 122 | + } |
| 123 | + |
| 124 | + @ParameterizedTest |
| 125 | + @MethodSource("existingAddOnVersionFileNames") |
| 126 | + void shouldDeleteExistingAddOnsWithSameIdBeforeCopying(List<String> existingFileNames) |
| 127 | + throws Exception { |
| 128 | + // Given |
| 129 | + buildFile(""); |
| 130 | + |
| 131 | + for (String name : existingFileNames) { |
| 132 | + createAddOnFile(destDir, name); |
| 133 | + } |
| 134 | + |
| 135 | + // When |
| 136 | + BuildResult result = runCopyAddOn(); |
| 137 | + |
| 138 | + // Then |
| 139 | + assertTaskSuccess(result, COPY_ADD_ON_TASK); |
| 140 | + for (String name : existingFileNames) { |
| 141 | + assertThat(destDir.resolve(name)).doesNotExist(); |
| 142 | + } |
| 143 | + assertThat(destDir.resolve(ADD_ON_FILE)).exists(); |
| 144 | + } |
| 145 | + |
| 146 | + static Stream<Arguments> existingAddOnVersionFileNames() { |
| 147 | + return Stream.of( |
| 148 | + Arguments.of(List.of(ADD_ON_ID + "-0.zap")), |
| 149 | + Arguments.of(List.of(ADD_ON_ID + "-alpha-2.zap", ADD_ON_ID + "-beta-1.0.0.zap"))); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void shouldNotDeleteAddOnsWithDifferentIdBeforeCopying() throws Exception { |
| 154 | + // Given |
| 155 | + buildFile(""); |
| 156 | + |
| 157 | + String unrelatedFile = "otheraddon-1.zap"; |
| 158 | + createAddOnFile(destDir, unrelatedFile); |
| 159 | + |
| 160 | + // When |
| 161 | + BuildResult result = runCopyAddOn(); |
| 162 | + |
| 163 | + // Then |
| 164 | + assertTaskSuccess(result, COPY_ADD_ON_TASK); |
| 165 | + assertThat(destDir.resolve(unrelatedFile)).exists(); |
| 166 | + assertThat(destDir.resolve(ADD_ON_FILE)).exists(); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + void shouldBeUpToDateWhenAddOnAlreadyCopied() throws Exception { |
| 171 | + // Given |
| 172 | + buildFile(""); |
| 173 | + runCopyAddOn(); |
| 174 | + |
| 175 | + // When |
| 176 | + BuildResult result = runCopyAddOn(); |
| 177 | + |
| 178 | + // Then |
| 179 | + assertTaskUpToDate(result, COPY_ADD_ON_TASK); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + void shouldNotBeUpToDateWhenMultipleAddOnsWithSameIdExistInDestination() throws Exception { |
| 184 | + // Given |
| 185 | + buildFile(""); |
| 186 | + runCopyAddOn(); |
| 187 | + String otherVersionAddOnFile = ADD_ON_ID + "-99.zap"; |
| 188 | + createAddOnFile(destDir, otherVersionAddOnFile); |
| 189 | + |
| 190 | + // When |
| 191 | + BuildResult result = runCopyAddOn(); |
| 192 | + |
| 193 | + // Then |
| 194 | + assertTaskSuccess(result, COPY_ADD_ON_TASK); |
| 195 | + assertThat(destDir.resolve(otherVersionAddOnFile)).doesNotExist(); |
| 196 | + assertThat(destDir.resolve(ADD_ON_FILE)).exists(); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + void shouldUsedefaultAddOnId() throws Exception { |
| 201 | + // Given |
| 202 | + String customId = "custom-addon"; |
| 203 | + settingsFile("rootProject.name = \"%s\"".formatted(customId)); |
| 204 | + super.buildFile( |
| 205 | + """ |
| 206 | + plugins { |
| 207 | + java |
| 208 | + id("org.zaproxy.add-on") |
| 209 | + } |
| 210 | + repositories { |
| 211 | + mavenCentral() |
| 212 | + } |
| 213 | + version = "%s" |
| 214 | + zapAddOn { |
| 215 | + addOnName.set("Test Add-On") |
| 216 | + } |
| 217 | + """ |
| 218 | + .formatted(ADD_ON_VERSION)); |
| 219 | + |
| 220 | + createAddOnFile(destDir, customId + "-0.zap"); |
| 221 | + |
| 222 | + // When |
| 223 | + BuildResult result = runCopyAddOn(); |
| 224 | + |
| 225 | + // Then |
| 226 | + assertTaskSuccess(result, COPY_ADD_ON_TASK); |
| 227 | + assertThat(destDir.resolve(customId + "-0.zap")).doesNotExist(); |
| 228 | + assertThat( |
| 229 | + destDir.resolve( |
| 230 | + customId + "-" + AddOnStatus.ALPHA + "-" + ADD_ON_VERSION + ".zap")) |
| 231 | + .exists(); |
| 232 | + } |
| 233 | + |
| 234 | + private static void createAddOnFile(Path dir, String fileName) throws IOException { |
| 235 | + Files.createFile(dir.resolve(fileName)); |
| 236 | + } |
| 237 | + |
| 238 | + private BuildResult runCopyAddOn() throws Exception { |
| 239 | + return build(COPY_ADD_ON_TASK, "--into", destDir.toString()); |
| 240 | + } |
| 241 | +} |
0 commit comments