author | vromero |
Wed, 09 Jan 2019 08:07:23 -0500 | |
changeset 53224 | bae765528fcc |
parent 52914 | 4fa75d8ad418 |
permissions | -rw-r--r-- |
52914 | 1 |
/* |
53224
bae765528fcc
8215510: j.l.c.ClassDesc is accepting descriptors not allowed by the spec
vromero
parents:
52914
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
52914 | 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 |
|
53224
bae765528fcc
8215510: j.l.c.ClassDesc is accepting descriptors not allowed by the spec
vromero
parents:
52914
diff
changeset
|
26 |
* @bug 8215510 |
52914 | 27 |
* @compile NameValidationTest.java |
28 |
* @run testng NameValidationTest |
|
29 |
* @summary unit tests for verifying member names |
|
30 |
*/ |
|
31 |
||
32 |
import java.lang.constant.*; |
|
33 |
import java.lang.invoke.*; |
|
34 |
||
35 |
import org.testng.annotations.Test; |
|
36 |
||
37 |
import static java.lang.constant.DirectMethodHandleDesc.*; |
|
38 |
import static java.lang.constant.ConstantDescs.*; |
|
39 |
import static java.lang.constant.DirectMethodHandleDesc.Kind.VIRTUAL; |
|
40 |
||
41 |
import static org.testng.Assert.fail; |
|
42 |
||
43 |
@Test |
|
44 |
public class NameValidationTest { |
|
45 |
||
46 |
private static final String[] badMemberNames = new String[] {"xx.xx", "zz;zz", "[l", "aa/aa", "<cinit>"}; |
|
47 |
private static final String[] goodMemberNames = new String[] {"<clinit>", "<init>", "3", "~", "$", "qq"}; |
|
48 |
||
53224
bae765528fcc
8215510: j.l.c.ClassDesc is accepting descriptors not allowed by the spec
vromero
parents:
52914
diff
changeset
|
49 |
private static final String[] badClassNames = new String[] {"zz;zz", "[l", "aa/aa", ".", "a..b"}; |
bae765528fcc
8215510: j.l.c.ClassDesc is accepting descriptors not allowed by the spec
vromero
parents:
52914
diff
changeset
|
50 |
private static final String[] goodClassNames = new String[] {"3", "~", "$", "qq", "a.a"}; |
52914 | 51 |
|
52 |
public void testMemberNames() { |
|
53 |
DirectMethodHandleDesc mh = MethodHandleDesc.of(Kind.VIRTUAL, CD_String, "isEmpty", "()Z"); |
|
54 |
for (String badName : badMemberNames) { |
|
55 |
try { |
|
56 |
memberNamesHelper(badName, mh, CD_int, null); |
|
57 |
fail("Expected failure for name " + badName); |
|
58 |
} catch (IllegalArgumentException iae) { |
|
59 |
// expected |
|
60 |
} |
|
61 |
try { |
|
62 |
memberNamesHelper(badName, mh, CD_int, new ConstantDesc[0]); |
|
63 |
fail("Expected failure for name " + badName); |
|
64 |
} catch (IllegalArgumentException iae) { |
|
65 |
// expected |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
for (String badName : goodMemberNames) { |
|
70 |
memberNamesHelper(badName, mh, CD_int, null); |
|
71 |
memberNamesHelper(badName, mh, CD_int, new ConstantDesc[0]); |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
private void memberNamesHelper(String constantName, |
|
76 |
DirectMethodHandleDesc bootstrapMethod, |
|
77 |
ClassDesc constantType, |
|
78 |
ConstantDesc... bootstrapArgs) { |
|
79 |
if (bootstrapArgs == null) { |
|
80 |
DynamicConstantDesc.ofNamed(bootstrapMethod, constantName, constantType); |
|
81 |
} else { |
|
82 |
DynamicConstantDesc.ofNamed(bootstrapMethod, constantName, constantType, bootstrapArgs); |
|
83 |
} |
|
84 |
} |
|
85 |
||
86 |
public void testClassNames() { |
|
87 |
for (String badName : badClassNames) { |
|
88 |
try { |
|
89 |
ClassDesc.of(badName); |
|
90 |
fail("Expected failure for name " + badName); |
|
91 |
} catch (IllegalArgumentException iae) { |
|
92 |
// expected |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
for (String goodName : goodClassNames) { |
|
97 |
ClassDesc.of(goodName); |
|
98 |
} |
|
99 |
} |
|
100 |
} |