Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/PBEInvalidParamsTest.java
41161 views
/*1* Copyright (c) 2005, 2012, 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 6209660 638320026* @summary Ensure that InvalidAlgorithmParameterException is27* thrown as javadoc specified when parameters of the wrong28* type are used.29* @author Valerie Peng30*/31import java.security.*;32import java.security.spec.*;33import javax.crypto.*;34import javax.crypto.spec.*;3536public class PBEInvalidParamsTest {3738private static final char[] PASSWORD = { 'p', 'a', 's', 's' };39private static final String[] PBE_ALGOS = {40"PBEWithMD5AndDES",41"PBEWithSHA1AndDESede",42"PBEWithSHA1AndRC2_40",43"PBEWithSHA1AndRC2_128",44"PBEWithSHA1AndRC4_40",45"PBEWithSHA1AndRC4_128",46// skip "PBEWithMD5AndTripleDES" since it requires Unlimited47// version of JCE jurisdiction policy files.48"PBEWithHmacSHA1AndAES_128",49"PBEWithHmacSHA224AndAES_128",50"PBEWithHmacSHA256AndAES_128",51"PBEWithHmacSHA384AndAES_128",52"PBEWithHmacSHA512AndAES_128"53// skip "PBEWithHmacSHAxxxAndAES_256" since they require Unlimited54// version of JCE jurisdiction policy files.55};5657private static final IvParameterSpec INVALID_PARAMS =58new IvParameterSpec(new byte[8]);5960public static void main(String[] args) throws Exception {61PBEKeySpec ks = new PBEKeySpec(PASSWORD);62for (int i = 0; i < PBE_ALGOS.length; i++) {63String algo = PBE_ALGOS[i];64System.out.println("=>testing " + algo);65SecretKeyFactory skf = SecretKeyFactory.getInstance(algo);66SecretKey key = skf.generateSecret(ks);67Cipher c = Cipher.getInstance(algo, "SunJCE");68try {69c.init(Cipher.ENCRYPT_MODE, key, INVALID_PARAMS);70throw new Exception("Test Failed: expected IAPE is " +71"not thrown for " + algo);72} catch (InvalidAlgorithmParameterException iape) {73continue;74}75}76System.out.println("Test Passed");77}78}798081