Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/util/BitArray/NamedBitList.java
41153 views
1
/*
2
* Copyright (c) 2006, 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
* @author Weijun Wang
27
* @bug 4654195
28
* @summary BIT STRING types with named bits must remove trailing 0 bits
29
* @modules java.base/sun.security.util
30
* java.base/sun.security.x509
31
*/
32
33
import sun.security.util.BitArray;
34
import sun.security.util.DerOutputStream;
35
import sun.security.util.DerValue;
36
import sun.security.x509.DNSName;
37
import sun.security.x509.DistributionPoint;
38
import sun.security.x509.GeneralName;
39
import sun.security.x509.GeneralNames;
40
import sun.security.x509.KeyUsageExtension;
41
import sun.security.x509.NetscapeCertTypeExtension;
42
import sun.security.x509.ReasonFlags;
43
44
public class NamedBitList {
45
public static void main(String[] args) throws Exception {
46
47
boolean[] bb = (new boolean[] {true, false, true, false, false, false});
48
GeneralNames gns = new GeneralNames();
49
gns.add(new GeneralName(new DNSName("dns")));
50
DerOutputStream out;
51
52
// length should be 5 since only {T,F,T} should be encoded
53
KeyUsageExtension x1 = new KeyUsageExtension(bb);
54
check(new DerValue(x1.getExtensionValue()).getUnalignedBitString().length(), 3);
55
56
NetscapeCertTypeExtension x2 = new NetscapeCertTypeExtension(bb);
57
check(new DerValue(x2.getExtensionValue()).getUnalignedBitString().length(), 3);
58
59
ReasonFlags r = new ReasonFlags(bb);
60
out = new DerOutputStream();
61
r.encode(out);
62
check(new DerValue(out.toByteArray()).getUnalignedBitString().length(), 3);
63
64
// Read sun.security.x509.DistributionPoint for ASN.1 definition
65
DistributionPoint dp = new DistributionPoint(gns, bb, gns);
66
out = new DerOutputStream();
67
dp.encode(out);
68
DerValue v = new DerValue(out.toByteArray());
69
// skip distributionPoint
70
v.data.getDerValue();
71
// read reasons
72
DerValue v2 = v.data.getDerValue();
73
// reset to BitString since it's context-specfic[1] encoded
74
v2.resetTag(DerValue.tag_BitString);
75
// length should be 5 since only {T,F,T} should be encoded
76
check(v2.getUnalignedBitString().length(), 3);
77
78
BitArray ba;
79
ba = new BitArray(new boolean[] {false, false, false});
80
check(ba.length(), 3);
81
ba = ba.truncate();
82
check(ba.length(), 1);
83
84
ba = new BitArray(new boolean[] {
85
true, true, true, true, true, true, true, true,
86
false, false});
87
check(ba.length(), 10);
88
check(ba.toByteArray().length, 2);
89
ba = ba.truncate();
90
check(ba.length(), 8);
91
check(ba.toByteArray().length, 1);
92
93
ba = new BitArray(new boolean[] {
94
true, true, true, true, true, true, true, true,
95
true, false});
96
check(ba.length(), 10);
97
check(ba.toByteArray().length, 2);
98
ba = ba.truncate();
99
check(ba.length(), 9);
100
check(ba.toByteArray().length, 2);
101
}
102
103
static void check(int la, int lb) throws Exception {
104
if (la != lb) {
105
System.err.println("Length is " + la + ", should be " + lb);
106
throw new Exception("Encoding Error");
107
} else {
108
System.err.println("Correct, which is " + lb);
109
}
110
}
111
}
112
113