Path: blob/master/test/jdk/tools/jimage/JImageExtractTest.java
41144 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Tests to verify jimage 'extract' action26* @library /test/lib27* @modules jdk.jlink/jdk.tools.jimage28* @build jdk.test.lib.Asserts29* @run main/othervm/timeout=300 JImageExtractTest30*/3132import java.io.IOException;33import java.io.UncheckedIOException;34import java.nio.file.Files;35import java.nio.file.Path;36import java.nio.file.Paths;37import java.nio.file.attribute.*;38import java.util.Arrays;39import java.util.HashSet;40import java.util.List;41import java.util.Set;42import java.util.stream.Collectors;43import java.util.spi.ToolProvider;4445import static jdk.test.lib.Asserts.assertEquals;46import static jdk.test.lib.Asserts.assertTrue;4748public class JImageExtractTest extends JImageCliTest {49private static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")50.orElseThrow(() ->51new RuntimeException("jlink tool not found")52);535455private String smallBootImagePath;5657public JImageExtractTest() {58try {59Path tmp = Files.createTempDirectory(Paths.get("."), getClass().getName());60tmp = tmp.toAbsolutePath();61tmp.toFile().deleteOnExit();62Path smalljre = tmp.resolve("smalljdk");63if (JLINK_TOOL.run(System.out, System.err,64"--add-modules", "java.base",65"--add-modules", "jdk.zipfs",66"--output", smalljre.toString()) != 0) {67throw new RuntimeException("failed to create small boot image");68}69this.smallBootImagePath = smalljre.resolve("lib").resolve("modules").toString();70} catch (IOException ioExp) {71throw new UncheckedIOException(ioExp);72}73}7475@Override76public String getImagePath() {77return smallBootImagePath;78}7980public void testExtract() throws IOException {81Set<Path> notJImageModules = Files.walk(Paths.get("."),1).collect(Collectors.toSet());82jimage("extract", getImagePath())83.assertSuccess()84.resultChecker(r -> {85assertTrue(r.output.isEmpty(), "Output is not expected");86});87verifyExplodedImage(Paths.get("."), notJImageModules);88}8990public void testExtractHelp() {91for (String opt : Arrays.asList("-h", "--help")) {92jimage("extract", "--help")93.assertSuccess()94.resultChecker(r -> {95// extract - descriptive text96assertMatches("\\s+extract\\s+-\\s+.*", r.output);97});98}99}100101public void testExtractToDir() throws IOException {102Path tmp = Files.createTempDirectory(Paths.get("."), getClass().getName());103Set<Path> notJImageModules = Files.walk(tmp,1).collect(Collectors.toSet());104jimage("extract", "--dir", tmp.toString(), getImagePath())105.assertSuccess()106.resultChecker(r -> {107assertTrue(r.output.isEmpty(), "Output is not expected");108});109verifyExplodedImage(tmp, notJImageModules);110}111112public void testExtractNoImageSpecified() {113jimage("extract", "")114.assertFailure()115.assertShowsError();116}117118public void testExtractNotAnImage() throws IOException {119Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "not_an_image");120jimage("extract", tmp.toString())121.assertFailure()122.assertShowsError();123}124125public void testExtractNotExistingImage() throws IOException {126Path tmp = Paths.get(".", "not_existing_image");127Files.deleteIfExists(tmp);128jimage("extract", tmp.toString())129.assertFailure()130.assertShowsError();131}132133public void testExtractToUnspecifiedDir() {134jimage("extract", "--dir", "--", getImagePath())135.assertFailure()136.assertShowsError();137}138139public void testExtractToNotExistingDir() throws IOException {140Path tmp = Files.createTempDirectory(Paths.get("."), getClass().getName());141Set<Path> notJImageModules = Files.walk(tmp,1).collect(Collectors.toSet());142Files.delete(tmp);143jimage("extract", "--dir", tmp.toString(), getImagePath())144.assertSuccess()145.resultChecker(r -> {146assertTrue(r.output.isEmpty(), "Output is not expected");147});148verifyExplodedImage(tmp, notJImageModules);149}150151public void testExtractFromDir() {152Path imagePath = Paths.get(getImagePath());153Path imageDirPath = imagePath.subpath(0, imagePath.getNameCount() - 1);154jimage("extract", imageDirPath.toString())155.assertFailure()156.assertShowsError();157}158159public void testExtractToDirBySymlink() throws IOException {160Path tmp = Files.createTempDirectory(Paths.get("."), getClass().getName());161Path symlink;162try {163symlink = Files.createSymbolicLink(Paths.get(".", "symlink"), tmp);164} catch (IOException|UnsupportedOperationException e) {165// symlinks are not supported166// nothing to test167return;168}169Set<Path> notJImageModules = Files.walk(tmp,1).collect(Collectors.toSet());170jimage("extract", "--dir", symlink.toString(), getImagePath())171.assertSuccess()172.resultChecker(r -> {173assertTrue(r.output.isEmpty(), "Output is not expected");174});175verifyExplodedImage(tmp, notJImageModules);176}177178public void testExtractToReadOnlyDir() throws IOException {179Path filePath = Files.createTempDirectory(Paths.get("."), getClass().getName());180Set<String> supportedAttr = filePath.getFileSystem().supportedFileAttributeViews();181if (supportedAttr.contains("posix")) {182Files.setPosixFilePermissions(filePath, PosixFilePermissions.fromString("r-xr--r--"));183} else if (supportedAttr.contains("acl")) {184System.out.println("Entered into acl block");185UserPrincipal fileOwner = Files.getOwner(filePath);186AclFileAttributeView view = Files.getFileAttributeView(filePath, AclFileAttributeView.class);187AclEntry entry = AclEntry.newBuilder()188.setType(AclEntryType.DENY)189.setPrincipal(fileOwner)190.setPermissions(AclEntryPermission.WRITE_DATA)191.setFlags(AclEntryFlag.FILE_INHERIT, AclEntryFlag.DIRECTORY_INHERIT)192.build();193List<AclEntry> acl = view.getAcl();194acl.add(0, entry);195view.setAcl(acl);196}197jimage("extract", "--dir", filePath.toString(), getImagePath())198.assertFailure()199.assertShowsError();200}201202public void testExtractToNotEmptyDir() throws IOException {203Path tmp = Files.createTempDirectory(Paths.get("."), getClass().getName());204Files.createFile(Paths.get(tmp.toString(), ".not_empty"));205jimage("extract", "--dir", tmp.toString(), getImagePath())206.assertSuccess()207.resultChecker(r -> {208assertTrue(r.output.isEmpty(), "Output is not expected");209});210}211212public void testExtractToFile() throws IOException {213Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "not_a_dir");214jimage("extract", "--dir", tmp.toString(), getImagePath())215.assertFailure()216.assertShowsError();217}218219private void verifyExplodedImage(Path imagePath, Set<Path> notJImageModules) throws IOException {220Set<Path> allModules = Files.walk(imagePath, 1).collect(Collectors.toSet());221assertTrue(allModules.stream().anyMatch(p -> "java.base".equals(p.getFileName().toString())),222"Exploded image contains java.base module.");223Set<Path> badModules = allModules.stream()224.filter(p -> !Files.exists(p.resolve("module-info.class")))225.collect(Collectors.toSet());226// filter bad modules which are not part of jimage227badModules.removeAll(notJImageModules);228assertEquals(badModules, new HashSet<Path>() {{}},229"There are no exploded modules with missing 'module-info.class'");230}231232public static void main(String[] args) throws Throwable {233new JImageExtractTest().runTests();234}235}236237238239