Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/util/BitArray/NamedBitList.java
38854 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*/2930import sun.security.util.BitArray;31import sun.security.util.DerOutputStream;32import sun.security.util.DerValue;33import sun.security.x509.DNSName;34import sun.security.x509.DistributionPoint;35import sun.security.x509.GeneralName;36import sun.security.x509.GeneralNames;37import sun.security.x509.KeyUsageExtension;38import sun.security.x509.NetscapeCertTypeExtension;39import sun.security.x509.ReasonFlags;4041public class NamedBitList {42public static void main(String[] args) throws Exception {4344boolean[] bb = (new boolean[] {true, false, true, false, false, false});45GeneralNames gns = new GeneralNames();46gns.add(new GeneralName(new DNSName("dns")));47DerOutputStream out;4849// length should be 5 since only {T,F,T} should be encoded50KeyUsageExtension x1 = new KeyUsageExtension(bb);51check(new DerValue(x1.getExtensionValue()).getUnalignedBitString().length(), 3);5253NetscapeCertTypeExtension x2 = new NetscapeCertTypeExtension(bb);54check(new DerValue(x2.getExtensionValue()).getUnalignedBitString().length(), 3);5556ReasonFlags r = new ReasonFlags(bb);57out = new DerOutputStream();58r.encode(out);59check(new DerValue(out.toByteArray()).getUnalignedBitString().length(), 3);6061// Read sun.security.x509.DistributionPoint for ASN.1 definition62DistributionPoint dp = new DistributionPoint(gns, bb, gns);63out = new DerOutputStream();64dp.encode(out);65DerValue v = new DerValue(out.toByteArray());66// skip distributionPoint67v.data.getDerValue();68// read reasons69DerValue v2 = v.data.getDerValue();70// reset to BitString since it's context-specfic[1] encoded71v2.resetTag(DerValue.tag_BitString);72// length should be 5 since only {T,F,T} should be encoded73check(v2.getUnalignedBitString().length(), 3);7475BitArray ba;76ba = new BitArray(new boolean[] {false, false, false});77check(ba.length(), 3);78ba = ba.truncate();79check(ba.length(), 1);8081ba = new BitArray(new boolean[] {82true, true, true, true, true, true, true, true,83false, false});84check(ba.length(), 10);85check(ba.toByteArray().length, 2);86ba = ba.truncate();87check(ba.length(), 8);88check(ba.toByteArray().length, 1);8990ba = new BitArray(new boolean[] {91true, true, true, true, true, true, true, true,92true, false});93check(ba.length(), 10);94check(ba.toByteArray().length, 2);95ba = ba.truncate();96check(ba.length(), 9);97check(ba.toByteArray().length, 2);98}99100static void check(int la, int lb) throws Exception {101if (la != lb) {102System.err.println("Length is " + la + ", should be " + lb);103throw new Exception("Encoding Error");104} else {105System.err.println("Correct, which is " + lb);106}107}108}109110111