Path: blob/master/test/jdk/sun/security/util/DerInputBuffer/PaddedBitString.java
66645 views
/*1* Copyright (c) 2002, 2021, 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* @bug 451155626* @summary Verify BitString value containing padding bits is accepted.27* @modules java.base/sun.security.util28* @library /test/lib29*/30import java.io.*;31import java.math.BigInteger;32import java.util.Arrays;33import java.util.HexFormat;34import jdk.test.lib.Asserts;35import jdk.test.lib.Utils;3637import sun.security.util.BitArray;38import sun.security.util.DerInputStream;3940public class PaddedBitString {4142// Relaxed the BitString parsing routine to accept bit strings43// with padding bits, ex. treat DER_BITSTRING_PAD6_b as the same44// bit string as DER_BITSTRING_PAD6_0/DER_BITSTRING_NOPAD.45// Note:46// 1. the number of padding bits has to be in [0...7]47// 2. value of the padding bits is ignored4849// With no padding bits50private final static byte[] DER_BITSTRING_NOPAD = { 3, 3, 0,51(byte)0x5d, (byte)0xc0 };52// With 6 zero padding bits (01011101 11000000)53private final static byte[] DER_BITSTRING_PAD6_0 = { 3, 3, 6,54(byte)0x5d, (byte)0xc0 };5556// With 6 nonzero padding bits (01011101 11001011)57private final static byte[] DER_BITSTRING_PAD6_b = { 3, 3, 6,58(byte)0x5d, (byte)0xcb };5960// With 8 padding bits61private final static byte[] DER_BITSTRING_PAD8_0 = { 3, 3, 8,62(byte)0x5d, (byte)0xc0 };6364private final static byte[] BITS = { (byte)0x5d, (byte)0xc0 };6566static enum Type {67BIT_STRING,68UNALIGNED_BIT_STRING;69}7071public static void main(String args[]) throws Exception {72test(DER_BITSTRING_NOPAD, new BitArray(16, BITS));73test(DER_BITSTRING_PAD6_0, new BitArray(10, BITS));74test(DER_BITSTRING_PAD6_b, new BitArray(10, BITS));75test(DER_BITSTRING_PAD8_0, null);76System.out.println("Tests Passed");77}7879private static void test(byte[] in, BitArray ans) throws IOException {80System.out.println("Testing " +81HexFormat.of().withUpperCase().formatHex(in));82for (Type t : Type.values()) {83DerInputStream derin = new DerInputStream(in);84boolean shouldPass = (ans != null);85switch (t) {86case BIT_STRING:87if (shouldPass) {88Asserts.assertTrue(Arrays.equals(ans.toByteArray(),89derin.getBitString()));90} else {91Utils.runAndCheckException(() -> derin.getBitString(),92IOException.class);93}94break;95case UNALIGNED_BIT_STRING:96if (shouldPass) {97Asserts.assertEQ(ans, derin.getUnalignedBitString());98} else {99Utils.runAndCheckException(() ->100derin.getUnalignedBitString(), IOException.class);101}102break;103}104}105}106}107108109