Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/util/Oid/OidFormat.java
38853 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 641842227* @bug 641842528* @bug 641843329* @summary ObjectIdentifier should reject 1.2.3.-4 and throw IOException on all format errors30*/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}8687int[][] goodInts = {88{0,0}, {0,1}, {1,0}, {1,2},89{0,39}, {1,39}, {2,47}, {2,40,3,6}, {2,100,3}, {2,123456,3},90{1,2,3}, {1,2,3445},91{1,3,6,1,4,1,42,2,17},92};9394for (int[] is: goodInts) {95testGood(is);96}9798int[][] badInts = new int[][] {99{0}, {1}, {2},100{3,1,1}, {3}, {4},101{1,40}, {1,111,1},102{-1,2}, {0,-2}, {1,-2}, {2,-2},103{1,2,-3,4}, {1,2,3,-4},104};105106for (int[] is: badInts) {107testBad(is);108}109110}111112static void testBad(int[] ints) throws Exception {113System.err.println("Trying " + Arrays.toString(ints));114try {115new ObjectIdentifier(ints);116throw new Exception("should be invalid ObjectIdentifier");117} catch (IOException ioe) {118System.err.println(ioe);119}120}121122static void testGood(int[] ints) throws Exception {123System.err.println("Trying " + Arrays.toString(ints));124ObjectIdentifier oid = new ObjectIdentifier(ints);125DerOutputStream os = new DerOutputStream();126os.putOID(oid);127DerInputStream is = new DerInputStream(os.toByteArray());128ObjectIdentifier oid2 = is.getOID();129if (!oid.equals((Object)oid2)) {130throw new Exception("Test DER I/O fails: " + oid + " and " + oid2);131}132}133134static void testGood(String s) throws Exception {135System.err.println("Trying " + s);136ObjectIdentifier oid = new ObjectIdentifier(s);137if (!oid.toString().equals(s)) {138throw new Exception("equal test fail");139}140DerOutputStream os = new DerOutputStream();141os.putOID(oid);142DerInputStream is = new DerInputStream(os.toByteArray());143ObjectIdentifier oid2 = is.getOID();144if (!oid.equals((Object)oid2)) {145throw new Exception("Test DER I/O fails: " + oid + " and " + oid2);146}147}148149static void testBad(String s) throws Exception {150System.err.println("Trying " + s);151try {152new ObjectIdentifier(s);153throw new Exception("should be invalid ObjectIdentifier");154} catch (IOException ioe) {155System.err.println(ioe);156}157158try {159new Oid(s);160throw new Exception("should be invalid Oid");161} catch (GSSException gsse) {162;163}164165try {166new EncryptedPrivateKeyInfo(s, new byte[8]);167throw new Exception("should be invalid algorithm");168} catch (NoSuchAlgorithmException e) {169;170}171}172}173174175