Path: blob/master/test/jdk/sun/security/util/Oid/OidFormat.java
41152 views
/*1* Copyright (c) 2006, 2020, 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 6418422 6418425 6418433 824215127* @summary ObjectIdentifier should reject 1.2.3.-4 and throw IOException on all format errors28* @modules java.base/sun.security.util29* java.security.jgss30*/3132import java.io.IOException;33import java.security.NoSuchAlgorithmException;34import org.ietf.jgss.GSSException;35import org.ietf.jgss.Oid;36import javax.crypto.EncryptedPrivateKeyInfo;37import sun.security.util.*;38import java.util.Arrays;3940public class OidFormat {41public static void main(String[] args) throws Exception {42String[] badOids = {43// number problems44"0", "1", "2",45"3.1.1", "3", "4",46"1.40", "1.111.1",47"-1.2", "0,-2", "1.-2", "2.-2",48"1.2.-3.4", "1.2.3.-4",49// format problems50"aa", "aa.aa",51"", "....", "1.2..4", "1.2.3.", "1.", "1.2.", "0.1.",52"1,2",53};5455for (String s: badOids) {56testBad(s);57}5859String[] goodOids = {60"0.0", "0.1", "1.0", "1.2",61"0.39", "1.39", "2.47", "2.40.3.6", "2.100.3", "2.123456.3",62"1.2.3", "1.2.3445",63"1.3.6.1.4.1.42.2.17",64// 4811968: ASN.1 cannot handle huge OID components65"2.16.764.1.3101555394.1.0.100.2.1",66"2.2726957624935694386592435", // as huge as possible67"1.2.777777777777777777",68"1.2.888888888888888888.111111111111111.2222222222222.33333333333333333.44444444444444",69"1.2." +70"1111111111111111111111111111111111111111111111111111111111111." +71"2222222222222222222222222222222222222222222222222222222222222222." +72"333333333333333333333333333333333333333333333333333333333333333." +73"4444444444444444444444444444444444444444444444444444444." +74"55555555555555555555555555555555555555555555555555555555555555555555555." +75"666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666." +76"77777777777777777777777777777777777777777777777777777777777777777777777777." +77"8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888." +78"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",79"1.2.2147483647.4",80"1.2.268435456.4",81};8283for (String s: goodOids) {84testGood(s);85}86}8788static void testGood(String s) throws Exception {89System.err.println("Trying " + s);90ObjectIdentifier oid = ObjectIdentifier.of(s);91if (!oid.toString().equals(s)) {92throw new Exception("equal test fail");93}94DerOutputStream os = new DerOutputStream();95os.putOID(oid);96DerInputStream is = new DerInputStream(os.toByteArray());97ObjectIdentifier oid2 = is.getOID();98if (!oid.equals(oid2)) {99throw new Exception("Test DER I/O fails: " + oid + " and " + oid2);100}101}102103static void testBad(String s) throws Exception {104System.err.println("Trying " + s);105try {106ObjectIdentifier.of(s);107throw new Exception("should be invalid ObjectIdentifier");108} catch (IOException ioe) {109System.err.println(ioe);110}111112try {113new Oid(s);114throw new Exception("should be invalid Oid");115} catch (GSSException gsse) {116;117}118119try {120new EncryptedPrivateKeyInfo(s, new byte[8]);121throw new Exception("should be invalid algorithm");122} catch (NoSuchAlgorithmException e) {123;124}125}126}127128129