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