diff --git a/test/jdk/com/sun/crypto/provider/Cipher/PBE/PBEKeyTest.java b/test/jdk/com/sun/crypto/provider/Cipher/PBE/PBEKeyTest.java index 9091480fae0..c6284135a70 100644 --- a/test/jdk/com/sun/crypto/provider/Cipher/PBE/PBEKeyTest.java +++ b/test/jdk/com/sun/crypto/provider/Cipher/PBE/PBEKeyTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,7 +23,7 @@ /* * @test - * @bug 0000000 + * @bug 8348732 * @summary test PBEKey * @author Jan Luehe */ @@ -39,22 +39,26 @@ public class PBEKeyTest { // Valid password char[] pass = new char[] { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' }; + testPassword(pass, fac, "ASCII password", true); + + pass = new char[] { 'p', 'a', 's', 's', 'w', 'o', 'r', '\u0019' }; + testPassword(pass, fac, "non-visible characters", true); + + pass = new char[] { 'p', 'a', 's', 's', 'w', 'o', 'r', (char)0xff }; + testPassword(pass, fac, "non-ASCII characters", false); + } + + private static void testPassword(char[] pass, SecretKeyFactory fac, String desc, + boolean expectPass) throws Exception { PBEKeySpec spec = new PBEKeySpec(pass); SecretKey skey = fac.generateSecret(spec); KeySpec spec1 = fac.getKeySpec(skey, PBEKeySpec.class); SecretKey skey1 = fac.generateSecret(spec1); - if (!skey.equals(skey1)) - throw new Exception("Equal keys not equal"); - System.out.println(new String(((PBEKeySpec)spec1).getPassword())); - - // Invalid password - pass = new char[] { 'p', 'a', 's', 's', 'w', 'o', 'r', '\u0019' }; - spec = new PBEKeySpec(pass); - try { - skey = fac.generateSecret(spec); - throw new Exception("Expected exception not thrown"); - } catch (Exception e) { - System.out.println("Expected exception thrown"); + if (expectPass && !skey.equals(skey1)) { + throw new Exception(desc + ": Equal keys not equal"); + } else if (!expectPass && skey.equals(skey1)) { + throw new Exception(desc + ": Keys should not be the same but are!"); } + System.out.println(new String(((PBEKeySpec)spec1).getPassword())); } }