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