Path: blob/master/test/jdk/javax/crypto/Cipher/TestGetInstance.java
41152 views
/*1* Copyright (c) 2003, 2008, 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 489842826* @summary test that the new getInstance() implementation works correctly27* @author Andreas Sterbenz28*/2930import java.security.*;31import java.security.spec.*;3233import javax.crypto.*;3435public class TestGetInstance {3637private static void same(Provider p1, Provider p2) throws Exception {38if (p1 != p2) {39throw new Exception("not same object");40}41}4243public static void main(String[] args) throws Exception {44Provider p = Security.getProvider("SunJCE");4546Cipher c;4748c = Cipher.getInstance("PBEWithMD5AndTripleDES");49same(p, c.getProvider());5051c = Cipher.getInstance("des", "SunJCE");52same(p, c.getProvider());53c = Cipher.getInstance("des/cbc/pkcs5padding", "SunJCE");54same(p, c.getProvider());5556c = Cipher.getInstance("des", p);57same(p, c.getProvider());58c = Cipher.getInstance("des/cbc/pkcs5padding", p);59same(p, c.getProvider());6061try {62c = Cipher.getInstance("DES/XYZ/PKCS5Padding");63throw new AssertionError();64} catch (NoSuchAlgorithmException e) {65System.out.println(e);66}67try {68c = Cipher.getInstance("DES/XYZ/PKCS5Padding", "SunJCE");69throw new AssertionError();70} catch (NoSuchAlgorithmException e) {71System.out.println(e);72}73try {74c = Cipher.getInstance("DES/XYZ/PKCS5Padding", p);75throw new AssertionError();76} catch (NoSuchAlgorithmException e) {77System.out.println(e);78}7980try {81c = Cipher.getInstance("DES/CBC/XYZPadding");82throw new AssertionError();83} catch (NoSuchAlgorithmException e) {84System.out.println(e);85}86try {87c = Cipher.getInstance("DES/CBC/XYZPadding", "SunJCE");88throw new AssertionError();89} catch (NoSuchPaddingException e) {90System.out.println(e);91}92try {93c = Cipher.getInstance("DES/CBC/XYZPadding", p);94throw new AssertionError();95} catch (NoSuchPaddingException e) {96System.out.println(e);97}9899try {100c = Cipher.getInstance("foo");101throw new AssertionError();102} catch (NoSuchAlgorithmException e) {103System.out.println(e);104}105try {106c = Cipher.getInstance("foo", "SunJCE");107throw new AssertionError();108} catch (NoSuchAlgorithmException e) {109System.out.println(e);110}111try {112c = Cipher.getInstance("foo", p);113throw new AssertionError();114} catch (NoSuchAlgorithmException e) {115System.out.println(e);116}117118try {119c = Cipher.getInstance("foo", "SUN");120throw new AssertionError();121} catch (NoSuchAlgorithmException e) {122System.out.println(e);123}124try {125c = Cipher.getInstance("foo", Security.getProvider("SUN"));126throw new AssertionError();127} catch (NoSuchAlgorithmException e) {128System.out.println(e);129}130try {131c = Cipher.getInstance("foo", "bar");132throw new AssertionError();133} catch (NoSuchProviderException e) {134System.out.println(e);135}136137System.out.println("All Tests ok");138}139}140141142