jdk

Форк
0
152 строки · 6.1 Кб
1
/*
2
 * Copyright (c) 2017, 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
 * @test
25
 * @summary Class p1.c1 in module first_mod cannot read p2.c2 in module second_mod,
26
 *          even after a read edge is added between first_mod and second_mod.
27
 *          Ensures constant access check answers when not accessible due to readability.
28
 * @library /test/lib
29
 * @modules java.base/jdk.internal.misc
30
 * @modules java.base/jdk.internal.module
31
 * @compile p2/c2.java
32
 * @compile p1/c1.java
33
 * @compile p4/c4.java
34
 * @run main/othervm AccessReadTwice
35
 */
36

37
import static jdk.test.lib.Asserts.*;
38

39
import java.lang.module.Configuration;
40
import java.lang.module.ModuleDescriptor;
41
import java.lang.module.ModuleFinder;
42
import java.util.HashMap;
43
import java.util.Map;
44
import java.util.Set;
45

46
//
47
// ClassLoader1 --> defines first_mod --> packages p1, p4
48
//                  defines second_mod --> package p2
49
//
50
// package p2 in second_mod is exported to first_mod
51
//
52
// class p1.c1 defined in first_mod tries to access p2.c2 defined in second_mod
53
// Access is not allowed, even after a read edge is added between first_mod and second_mod.
54

55
public class AccessReadTwice {
56

57
    // Create a layer over the boot layer.
58
    // Define modules within this layer to test access between
59
    // publicly defined classes within packages of those modules.
60
    public void createLayerOnBoot() throws Throwable {
61

62
        // Define module:     first_mod
63
        // Can read:          java.base
64
        // Packages:          p1, p4
65
        // Packages exported: none
66
        ModuleDescriptor descriptor_first_mod =
67
                ModuleDescriptor.newModule("first_mod")
68
                        .requires("java.base")
69
                        .packages(Set.of("p1", "p4"))
70
                        .build();
71

72
        // Define module:     second_mod
73
        // Can read:          java.base
74
        // Packages:          p2
75
        // Packages exported: p2 is exported to first_mod
76
        ModuleDescriptor descriptor_second_mod =
77
                ModuleDescriptor.newModule("second_mod")
78
                        .requires("java.base")
79
                        .exports("p2", Set.of("first_mod"))
80
                        .build();
81

82
        // Set up a ModuleFinder containing all modules for this layer
83
        ModuleFinder finder = ModuleLibrary.of(descriptor_first_mod, descriptor_second_mod);
84

85
        // Resolves "first_mod" and "second_mod"
86
        Configuration cf = ModuleLayer.boot()
87
                .configuration()
88
                .resolve(finder, ModuleFinder.of(), Set.of("first_mod", "second_mod"));
89

90
        // Map each module to this class loader
91
        Map<String, ClassLoader> map = new HashMap<>();
92
        ClassLoader loader = AccessReadTwice.class.getClassLoader();
93
        map.put("first_mod", loader);
94
        map.put("second_mod", loader);
95

96
        // Create layer that contains first_mod & second_mod
97
        ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
98

99
        assertTrue(layer.findLoader("first_mod") == loader);
100
        assertTrue(layer.findLoader("second_mod") == loader);
101
        assertTrue(layer.findLoader("java.base") == null);
102

103
        Class p2_c2_class = loader.loadClass("p2.c2");
104
        Class p1_c1_class = loader.loadClass("p1.c1");
105
        Class p4_c4_class = loader.loadClass("p4.c4");
106

107
        Module first_mod = p1_c1_class.getModule();
108
        Module second_mod = p2_c2_class.getModule();
109

110
        // Export first_mod/p1 and first_mod/p4 to all unnamed modules so that
111
        // this test can use them
112
        jdk.internal.module.Modules.addExportsToAllUnnamed(first_mod, "p1");
113
        jdk.internal.module.Modules.addExportsToAllUnnamed(first_mod, "p4");
114

115
        // First access check for p1.c1
116
        try {
117
            p1_c1_class.newInstance();
118
            throw new RuntimeException("Test Failed, module first_mod should not have access to p2.c2");
119
        } catch (IllegalAccessError e) {
120
            String message = e.getMessage();
121
            if (!(message.contains("cannot access") &&
122
                  message.contains("because module first_mod does not read module second_mod"))) {
123
                throw new RuntimeException("Wrong message: " + message);
124
            } else {
125
                System.out.println("Test Succeeded at attempt #1");
126
            }
127
        }
128

129
        // Add a read edge from p4/c4's module (first_mod) to second_mod
130
        p4.c4 c4_obj = new p4.c4();
131
        c4_obj.addReads(second_mod);
132

133
        // Second access check for p1.c1, should have same result as first
134
        try {
135
            p1_c1_class.newInstance();
136
            throw new RuntimeException("Test Failed, access should have been cached above");
137
        } catch (IllegalAccessError e) {
138
            String message = e.getMessage();
139
            if (!(message.contains("cannot access") &&
140
                  message.contains("because module first_mod does not read module second_mod"))) {
141
                throw new RuntimeException("Wrong message: " + message);
142
            } else {
143
                System.out.println("Test Succeeded at attempt #2");
144
            }
145
        }
146
    }
147

148
    public static void main(String args[]) throws Throwable {
149
      AccessReadTwice test = new AccessReadTwice();
150
      test.createLayerOnBoot();
151
    }
152
}
153

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.