Path: blob/master/test/jdk/java/security/KeyPairGenerator/FinalizeHalf.java
41152 views
/*1* Copyright (c) 2016, 2019, 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 8163896 822300326* @summary Finalizing one key of a KeyPair invalidates the other key27*/2829import java.security.Key;30import java.security.KeyPair;31import java.security.KeyPairGenerator;32import java.security.NoSuchAlgorithmException;33import java.security.Provider;34import java.security.ProviderException;35import java.security.Security;36import java.util.function.Consumer;37import java.util.ArrayList;38import java.util.List;3940public class FinalizeHalf {4142static int failures = 0;4344public static void main(String[] args) throws Throwable {45List<Consumer<Key>> methods = new ArrayList<>();46methods.add((Key k) -> k.getEncoded());47methods.add((Key k) -> k.toString());4849for (String algo : new String[] {"DiffieHellman", "DSA", "RSA"}) {50for (Provider provider : Security.getProviders()) {51for (boolean priv : new boolean[] {true, false}) {52for (Consumer<Key> method : methods) {53test(algo, provider, priv, method);54}55}56}57}5859if (failures > 0) {60throw new RuntimeException(failures + " test(s) failed.");61}62}6364static void test(String algo, Provider provider, boolean priv,65Consumer<Key> method) throws Exception {66KeyPairGenerator generator;67try {68generator = KeyPairGenerator.getInstance(algo, provider);69} catch (NoSuchAlgorithmException nsae) {70return;71}7273System.out.println("Checking " + provider.getName() + ", " + algo);7475KeyPair pair = generator.generateKeyPair();76Key key = priv ? pair.getPrivate() : pair.getPublic();7778pair = null;79for (int i = 0; i < 32; ++i) {80System.gc();81}8283try {84method.accept(key);85} catch (ProviderException pe) {86failures++;87}88}89}909192