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 import java.lang.module.ModuleDescriptor; 25 import java.lang.module.ModuleDescriptor.*; 26 import java.lang.module.ModuleFinder; 27 import java.lang.module.ModuleReference; 28 import java.io.IOException; 29 import java.io.UncheckedIOException; 30 import java.nio.file.Files; 31 import java.nio.file.Path; 32 import java.nio.file.Paths; 33 import java.util.List; 34 import java.util.Map; 35 import java.util.Set; 36 37 import jdk.internal.access.JavaLangModuleAccess; 38 import jdk.internal.access.SharedSecrets; 39 import org.testng.annotations.Test; 40 import static org.testng.Assert.*; 41 42 /** 43 * @test 44 * @bug 8142968 8173381 45 * @modules java.base/jdk.internal.access 46 * @modules java.base/jdk.internal.module 47 * @modules java.base/jdk.internal.org.objectweb.asm 48 * @build ModuleTargetHelper 49 * @run testng SystemModulesTest 50 * @summary Verify the properties of ModuleDescriptor created 51 * by SystemModules 52 */ 53 54 public class SystemModulesTest { 55 private static final JavaLangModuleAccess JLMA = 56 SharedSecrets.getJavaLangModuleAccess(); 57 private static final String OS_NAME = System.getProperty("os.name"); 58 private static final String OS_ARCH = System.getProperty("os.arch"); 59 // system modules containing no package 60 private static final Set<String> EMPTY_MODULES = 61 Set.of("java.se", "jdk.jdwp.agent", "jdk.pack"); 62 63 @Test 64 public void testSystemModules() { 65 Path jimage = Paths.get(System.getProperty("java.home"), "lib", "modules"); 66 if (Files.notExists(jimage)) 67 return; 68 69 ModuleFinder.ofSystem().findAll().stream() 70 .forEach(this::checkAttributes); 71 } 72 73 // JMOD files are created with OS name and arch matching the bundle name 74 private boolean checkOSName(String name) { 75 if (OS_NAME.startsWith("Windows")) { 76 return name.equals("windows"); 77 } 78 79 switch (OS_NAME) { 80 case "Linux": 81 return name.equals("linux"); 82 case "SunOS": 83 return name.equals("solaris"); 84 case "Mac OS X": 85 return name.equals("macos"); 86 default: 87 // skip validation on unknown platform 88 System.out.println("Skip checking OS name in ModuleTarget: " + name); 89 return true; 90 } 91 } 92 93 private boolean checkOSArch(String name) { 94 if (name.equals(OS_ARCH)) 95 return true; 96 97 switch (OS_ARCH) { 98 case "i386": 99 case "x86": 100 return name.equals("x86"); 101 case "amd64": 102 case "x86_64": 103 return name.equals("amd64"); 104 default: 105 // skip validation on unknown platform 106 System.out.println("Skip checking OS arch in ModuleTarget: " + name); 107 return true; 108 } 109 } 110 111 private void checkAttributes(ModuleReference modRef) { 112 try { 113 ModuleTargetHelper.ModuleTarget mt = ModuleTargetHelper.read(modRef); 114 String[] values = mt.targetPlatform().split("-"); 115 assertTrue(checkOSName(values[0])); 116 assertTrue(checkOSArch(values[1])); 117 } catch (IOException exp) { 118 throw new UncheckedIOException(exp); 119 } 120 } 121 122 /** 123 * Verify ModuleDescriptor contains unmodifiable sets 124 */ 125 @Test 126 public void testUnmodifableDescriptors() throws Exception { 127 ModuleFinder.ofSystem().findAll() 128 .stream() 129 .map(ModuleReference::descriptor) 130 .forEach(this::testModuleDescriptor); 131 } 132 133 private void testModuleDescriptor(ModuleDescriptor md) { 134 assertUnmodifiable(md.packages(), "package"); 135 assertUnmodifiable(md.requires(), 136 JLMA.newRequires(Set.of(Requires.Modifier.TRANSITIVE), 137 "require", null)); 138 for (Requires req : md.requires()) { 139 assertUnmodifiable(req.modifiers(), Requires.Modifier.TRANSITIVE); 140 } 141 142 assertUnmodifiable(md.exports(), JLMA.newExports(Set.of(), "export", Set.of())); 143 for (Exports exp : md.exports()) { 144 assertUnmodifiable(exp.modifiers(), Exports.Modifier.SYNTHETIC); 145 assertUnmodifiable(exp.targets(), "target"); 146 } 147 148 assertUnmodifiable(md.opens(), JLMA.newOpens(Set.of(), "open", Set.of())); 149 for (Opens opens : md.opens()) { 150 assertUnmodifiable(opens.modifiers(), Opens.Modifier.SYNTHETIC); 151 assertUnmodifiable(opens.targets(), "target"); 152 } 153 154 assertUnmodifiable(md.uses(), "use"); 155 156 assertUnmodifiable(md.provides(), 157 JLMA.newProvides("provide", List.of("provide"))); 158 for (Provides provides : md.provides()) { 159 assertUnmodifiable(provides.providers(), "provide"); 160 } 161 162 } 163 164 private <T> void assertUnmodifiable(Set<T> set, T dummy) { 165 try { 166 set.add(dummy); 167 fail("Should throw UnsupportedOperationException"); 168 } catch (UnsupportedOperationException e) { 169 // pass 170 } catch (Exception e) { 171 fail("Should throw UnsupportedOperationException"); 172 } 173 } 174 175 private <T> void assertUnmodifiable(List<T> list, T dummy) { 176 try { 177 list.add(dummy); 178 fail("Should throw UnsupportedOperationException"); 179 } catch (UnsupportedOperationException e) { 180 // pass 181 } catch (Exception e) { 182 fail("Should throw UnsupportedOperationException"); 183 } 184 } 185 186 private <T, V> void assertUnmodifiable(Map<T, V> set, T dummyKey, V dummyValue) { 187 try { 188 set.put(dummyKey, dummyValue); 189 fail("Should throw UnsupportedOperationException"); 190 } catch (UnsupportedOperationException e) { 191 // pass 192 } catch (Exception e) { 193 fail("Should throw UnsupportedOperationException"); 194 } 195 } 196 197 }