Path: blob/master/test/jdk/sun/security/util/BitArray/NamedBitList.java
41153 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @author Weijun Wang26* @bug 465419527* @summary BIT STRING types with named bits must remove trailing 0 bits28* @modules java.base/sun.security.util29* java.base/sun.security.x50930*/3132import sun.security.util.BitArray;33import sun.security.util.DerOutputStream;34import sun.security.util.DerValue;35import sun.security.x509.DNSName;36import sun.security.x509.DistributionPoint;37import sun.security.x509.GeneralName;38import sun.security.x509.GeneralNames;39import sun.security.x509.KeyUsageExtension;40import sun.security.x509.NetscapeCertTypeExtension;41import sun.security.x509.ReasonFlags;4243public class NamedBitList {44public static void main(String[] args) throws Exception {4546boolean[] bb = (new boolean[] {true, false, true, false, false, false});47GeneralNames gns = new GeneralNames();48gns.add(new GeneralName(new DNSName("dns")));49DerOutputStream out;5051// length should be 5 since only {T,F,T} should be encoded52KeyUsageExtension x1 = new KeyUsageExtension(bb);53check(new DerValue(x1.getExtensionValue()).getUnalignedBitString().length(), 3);5455NetscapeCertTypeExtension x2 = new NetscapeCertTypeExtension(bb);56check(new DerValue(x2.getExtensionValue()).getUnalignedBitString().length(), 3);5758ReasonFlags r = new ReasonFlags(bb);59out = new DerOutputStream();60r.encode(out);61check(new DerValue(out.toByteArray()).getUnalignedBitString().length(), 3);6263// Read sun.security.x509.DistributionPoint for ASN.1 definition64DistributionPoint dp = new DistributionPoint(gns, bb, gns);65out = new DerOutputStream();66dp.encode(out);67DerValue v = new DerValue(out.toByteArray());68// skip distributionPoint69v.data.getDerValue();70// read reasons71DerValue v2 = v.data.getDerValue();72// reset to BitString since it's context-specfic[1] encoded73v2.resetTag(DerValue.tag_BitString);74// length should be 5 since only {T,F,T} should be encoded75check(v2.getUnalignedBitString().length(), 3);7677BitArray ba;78ba = new BitArray(new boolean[] {false, false, false});79check(ba.length(), 3);80ba = ba.truncate();81check(ba.length(), 1);8283ba = new BitArray(new boolean[] {84true, true, true, true, true, true, true, true,85false, false});86check(ba.length(), 10);87check(ba.toByteArray().length, 2);88ba = ba.truncate();89check(ba.length(), 8);90check(ba.toByteArray().length, 1);9192ba = new BitArray(new boolean[] {93true, true, true, true, true, true, true, true,94true, false});95check(ba.length(), 10);96check(ba.toByteArray().length, 2);97ba = ba.truncate();98check(ba.length(), 9);99check(ba.toByteArray().length, 2);100}101102static void check(int la, int lb) throws Exception {103if (la != lb) {104System.err.println("Length is " + la + ", should be " + lb);105throw new Exception("Encoding Error");106} else {107System.err.println("Correct, which is " + lb);108}109}110}111112113