|
|
|
@ -24,6 +24,7 @@ import java.io.InputStreamReader;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
import java.util.jar.JarEntry;
|
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
@ -46,6 +47,7 @@ import static org.mockito.BDDMockito.given;
|
|
|
|
|
* Tests for {@link ExtractCommand}.
|
|
|
|
|
*
|
|
|
|
|
* @author Phillip Webb
|
|
|
|
|
* @author Andy Wilkinson
|
|
|
|
|
*/
|
|
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
|
|
|
class ExtractCommandTests {
|
|
|
|
@ -82,6 +84,7 @@ class ExtractCommandTests {
|
|
|
|
|
assertThat(new File(this.extract, "b/b/b.jar")).exists();
|
|
|
|
|
assertThat(new File(this.extract, "c/c/c.jar")).exists();
|
|
|
|
|
assertThat(new File(this.extract, "d")).isDirectory();
|
|
|
|
|
assertThat(new File(this.extract.getParentFile(), "e.jar")).doesNotExist();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -104,6 +107,7 @@ class ExtractCommandTests {
|
|
|
|
|
assertThat(this.extract.list()).containsOnly("a", "c");
|
|
|
|
|
assertThat(new File(this.extract, "a/a/a.jar")).exists();
|
|
|
|
|
assertThat(new File(this.extract, "c/c/c.jar")).exists();
|
|
|
|
|
assertThat(new File(this.extract.getParentFile(), "e.jar")).doesNotExist();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -119,7 +123,29 @@ class ExtractCommandTests {
|
|
|
|
|
.withMessageContaining("not compatible with layertools");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void runWithJarFileThatWouldWriteEntriesOutsideDestinationFails() throws Exception {
|
|
|
|
|
this.jarFile = createJarFile("test.jar", (out) -> {
|
|
|
|
|
try {
|
|
|
|
|
out.putNextEntry(new ZipEntry("e/../../e.jar"));
|
|
|
|
|
out.closeEntry();
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex) {
|
|
|
|
|
throw new IllegalStateException(ex);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
given(this.context.getArchiveFile()).willReturn(this.jarFile);
|
|
|
|
|
assertThatIllegalStateException()
|
|
|
|
|
.isThrownBy(() -> this.command.run(Collections.emptyMap(), Collections.emptyList()))
|
|
|
|
|
.withMessageContaining("Entry 'e/../../e.jar' would be written");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private File createJarFile(String name) throws Exception {
|
|
|
|
|
return createJarFile(name, (out) -> {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private File createJarFile(String name, Consumer<ZipOutputStream> streamHandler) throws Exception {
|
|
|
|
|
File file = new File(this.temp, name);
|
|
|
|
|
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file))) {
|
|
|
|
|
out.putNextEntry(new ZipEntry("a/"));
|
|
|
|
@ -139,6 +165,7 @@ class ExtractCommandTests {
|
|
|
|
|
out.putNextEntry(new JarEntry("META-INF/MANIFEST.MF"));
|
|
|
|
|
out.write(getFile("test-manifest.MF").getBytes());
|
|
|
|
|
out.closeEntry();
|
|
|
|
|
streamHandler.accept(out);
|
|
|
|
|
}
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|