|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.web.embedded.jetty; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.URI; |
| 23 | +import java.nio.file.FileSystems; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | +import java.util.jar.JarOutputStream; |
| 29 | +import java.util.zip.ZipEntry; |
| 30 | + |
| 31 | +import org.eclipse.jetty.util.resource.PathResourceFactory; |
| 32 | +import org.eclipse.jetty.util.resource.Resource; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.io.TempDir; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests for {@link LoaderHidingResource}. |
| 40 | + * |
| 41 | + * @author Andy Wilkinson |
| 42 | + */ |
| 43 | +class LoaderHidingResourceTests { |
| 44 | + |
| 45 | + @Test |
| 46 | + void listHidesLoaderResources(@TempDir File temp) throws IOException { |
| 47 | + URI warUri = createExampleWar(temp); |
| 48 | + Resource resource = new PathResourceFactory().newResource(warUri); |
| 49 | + LoaderHidingResource loaderHidingResource = new LoaderHidingResource(resource, resource); |
| 50 | + assertThat(deepList(loaderHidingResource)).hasOnlyElementsOfType(LoaderHidingResource.class) |
| 51 | + .extracting(Resource::getName) |
| 52 | + .contains("/assets/image.jpg") |
| 53 | + .doesNotContain("/org/springframework/boot/Loader.class"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void getAllResourcesHidesLoaderResources(@TempDir File temp) throws IOException { |
| 58 | + URI warUri = createExampleWar(temp); |
| 59 | + Resource resource = new PathResourceFactory().newResource(warUri); |
| 60 | + LoaderHidingResource loaderHidingResource = new LoaderHidingResource(resource, resource); |
| 61 | + Collection<Resource> allResources = loaderHidingResource.getAllResources(); |
| 62 | + assertThat(allResources).hasOnlyElementsOfType(LoaderHidingResource.class) |
| 63 | + .extracting(Resource::getName) |
| 64 | + .contains("/assets/image.jpg") |
| 65 | + .doesNotContain("/org/springframework/boot/Loader.class"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void resolveHidesLoaderResources(@TempDir File temp) throws IOException { |
| 70 | + URI warUri = createExampleWar(temp); |
| 71 | + Resource resource = new PathResourceFactory().newResource(warUri); |
| 72 | + LoaderHidingResource loaderHidingResource = new LoaderHidingResource(resource, resource); |
| 73 | + assertThat(loaderHidingResource.resolve("/assets/image.jpg").exists()).isTrue(); |
| 74 | + assertThat(loaderHidingResource.resolve("/assets/image.jpg")).isInstanceOf(LoaderHidingResource.class); |
| 75 | + assertThat(loaderHidingResource.resolve("/assets/non-existent.jpg").exists()).isFalse(); |
| 76 | + assertThat(loaderHidingResource.resolve("/assets/non-existent.jpg")).isInstanceOf(LoaderHidingResource.class); |
| 77 | + assertThat(loaderHidingResource.resolve("/org/springframework/boot/Loader.class")).isNull(); |
| 78 | + } |
| 79 | + |
| 80 | + private URI createExampleWar(File temp) throws IOException { |
| 81 | + File exampleWarFile = new File(temp, "example.war"); |
| 82 | + try (JarOutputStream out = new JarOutputStream(new FileOutputStream(exampleWarFile))) { |
| 83 | + out.putNextEntry(new ZipEntry("org/")); |
| 84 | + out.putNextEntry(new ZipEntry("org/springframework/")); |
| 85 | + out.putNextEntry(new ZipEntry("org/springframework/boot/")); |
| 86 | + out.putNextEntry(new ZipEntry("org/springframework/boot/Loader.class")); |
| 87 | + out.putNextEntry(new ZipEntry("assets/")); |
| 88 | + out.putNextEntry(new ZipEntry("assets/image.jpg")); |
| 89 | + } |
| 90 | + URI warUri = URI.create("jar:" + exampleWarFile.toURI() + "!/"); |
| 91 | + FileSystems.newFileSystem(warUri, Collections.emptyMap()); |
| 92 | + return warUri; |
| 93 | + } |
| 94 | + |
| 95 | + private List<Resource> deepList(Resource resource) { |
| 96 | + List<Resource> all = new ArrayList<>(); |
| 97 | + for (Resource listed : resource.list()) { |
| 98 | + all.add(listed); |
| 99 | + all.addAll(deepList(listed)); |
| 100 | + } |
| 101 | + return all; |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments