1 /*
2  * Copyright (c) 2017, 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 Verify that upgradeable modules are not hashed in java.base
27  *          whereas non-upgradeable modules are.
28  * @modules java.base/jdk.internal.module
29  * @run main UpgradeableModules
30  */
31 
32 import jdk.internal.module.ModuleHashes;
33 import jdk.internal.module.ModuleReferenceImpl;
34 
35 import java.lang.module.ModuleFinder;
36 import java.lang.module.ModuleReference;
37 import java.lang.module.ResolvedModule;
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.Optional;
41 import java.util.Set;
42 import java.util.stream.Collectors;
43 
44 public class UpgradeableModules {
45     private static final List<String> UPGRADEABLE_MODULES =
46         List.of("java.compiler",
47                 "jdk.aot",
48                 "jdk.internal.vm.compiler",
49                 "jdk.internal.vm.compiler.management");
50 
main(String... args)51     public static void main(String... args) {
52         Set<String> hashedModules = hashedModules();
53         if (hashedModules.isEmpty())
54             return;
55 
56         if (UPGRADEABLE_MODULES.stream().anyMatch(hashedModules::contains)) {
57             throw new RuntimeException("upgradeable modules are hashed: " +
58                 UPGRADEABLE_MODULES.stream()
59                     .filter(hashedModules::contains)
60                     .collect(Collectors.joining(" ")));
61         }
62 
63         Set<String> nonUpgradeableModules =
64             ModuleFinder.ofSystem().findAll().stream()
65                 .map(mref -> mref.descriptor().name())
66                 .filter(mn -> !UPGRADEABLE_MODULES.contains(mn))
67                 .collect(Collectors.toSet());
68 
69         if (nonUpgradeableModules.stream().anyMatch(mn -> !hashedModules.contains(mn))) {
70             throw new RuntimeException("non-upgradeable modules are not hashed: " +
71                 nonUpgradeableModules.stream()
72                     .filter(mn -> !hashedModules.contains(mn))
73                     .collect(Collectors.joining(" ")));
74         }
75     }
76 
hashedModules()77     private static Set<String> hashedModules() {
78         Optional<ResolvedModule> resolvedModule = ModuleLayer.boot()
79             .configuration()
80             .findModule("java.base");
81         assert resolvedModule.isPresent();
82         ModuleReference mref = resolvedModule.get().reference();
83         assert mref instanceof ModuleReferenceImpl;
84         ModuleHashes hashes = ((ModuleReferenceImpl) mref).recordedHashes();
85         if (hashes != null) {
86             Set<String> names = new HashSet<>(hashes.names());
87             names.add("java.base");
88             return names;
89         }
90 
91         return Set.of();
92     }
93 }
94