0N/A/*
2362N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @author Weijun Wang
0N/A * @bug 4654195
0N/A * @summary BIT STRING types with named bits must remove trailing 0 bits
0N/A */
0N/A
0N/Aimport sun.security.util.BitArray;
0N/Aimport sun.security.util.DerOutputStream;
0N/Aimport sun.security.util.DerValue;
0N/Aimport sun.security.x509.DNSName;
0N/Aimport sun.security.x509.DistributionPoint;
0N/Aimport sun.security.x509.GeneralName;
0N/Aimport sun.security.x509.GeneralNames;
0N/Aimport sun.security.x509.KeyUsageExtension;
0N/Aimport sun.security.x509.NetscapeCertTypeExtension;
0N/Aimport sun.security.x509.ReasonFlags;
0N/A
0N/Apublic class NamedBitList {
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A boolean[] bb = (new boolean[] {true, false, true, false, false, false});
0N/A GeneralNames gns = new GeneralNames();
0N/A gns.add(new GeneralName(new DNSName("dns")));
0N/A DerOutputStream out;
0N/A
0N/A // length should be 5 since only {T,F,T} should be encoded
0N/A KeyUsageExtension x1 = new KeyUsageExtension(bb);
0N/A check(new DerValue(x1.getExtensionValue()).getUnalignedBitString().length(), 3);
0N/A
0N/A NetscapeCertTypeExtension x2 = new NetscapeCertTypeExtension(bb);
0N/A check(new DerValue(x2.getExtensionValue()).getUnalignedBitString().length(), 3);
0N/A
0N/A ReasonFlags r = new ReasonFlags(bb);
0N/A out = new DerOutputStream();
0N/A r.encode(out);
0N/A check(new DerValue(out.toByteArray()).getUnalignedBitString().length(), 3);
0N/A
0N/A // Read sun.security.x509.DistributionPoint for ASN.1 definition
0N/A DistributionPoint dp = new DistributionPoint(gns, bb, gns);
0N/A out = new DerOutputStream();
0N/A dp.encode(out);
0N/A DerValue v = new DerValue(out.toByteArray());
0N/A // skip distributionPoint
0N/A v.data.getDerValue();
0N/A // read reasons
0N/A DerValue v2 = v.data.getDerValue();
0N/A // reset to BitString since it's context-specfic[1] encoded
0N/A v2.resetTag(DerValue.tag_BitString);
0N/A // length should be 5 since only {T,F,T} should be encoded
0N/A check(v2.getUnalignedBitString().length(), 3);
0N/A
0N/A BitArray ba;
0N/A ba = new BitArray(new boolean[] {false, false, false});
0N/A check(ba.length(), 3);
0N/A ba = ba.truncate();
0N/A check(ba.length(), 1);
0N/A
0N/A ba = new BitArray(new boolean[] {
0N/A true, true, true, true, true, true, true, true,
0N/A false, false});
0N/A check(ba.length(), 10);
0N/A check(ba.toByteArray().length, 2);
0N/A ba = ba.truncate();
0N/A check(ba.length(), 8);
0N/A check(ba.toByteArray().length, 1);
0N/A
0N/A ba = new BitArray(new boolean[] {
0N/A true, true, true, true, true, true, true, true,
0N/A true, false});
0N/A check(ba.length(), 10);
0N/A check(ba.toByteArray().length, 2);
0N/A ba = ba.truncate();
0N/A check(ba.length(), 9);
0N/A check(ba.toByteArray().length, 2);
0N/A }
0N/A
0N/A static void check(int la, int lb) throws Exception {
0N/A if (la != lb) {
0N/A System.err.println("Length is " + la + ", should be " + lb);
0N/A throw new Exception("Encoding Error");
0N/A } else {
0N/A System.err.println("Correct, which is " + lb);
0N/A }
0N/A }
0N/A}