Path: blob/master/test/jdk/sun/security/provider/SecureRandom/DRBGS11n.java
41154 views
/*1* Copyright (c) 2016, 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*/22import sun.security.provider.DRBG;23import sun.security.provider.MoreDrbgParameters;2425import java.io.ByteArrayInputStream;26import java.io.ByteArrayOutputStream;27import java.io.ObjectInputStream;28import java.io.ObjectOutputStream;29import java.lang.reflect.Field;3031/**32* @test33* @bug 815730834* @modules java.base/sun.security.provider:+open35* @summary Make AbstractDrbg non-Serializable36* @run main DRBGS11n mech37* @run main DRBGS11n capability38*/39public class DRBGS11n {4041public static void main(String[] args) throws Exception {4243DRBG d = new DRBG(null);44Field f = DRBG.class.getDeclaredField("mdp");45f.setAccessible(true);46MoreDrbgParameters mdp = (MoreDrbgParameters)f.get(d);4748// Corrupt the mech or capability fields inside DRBG#mdp.49f = MoreDrbgParameters.class.getDeclaredField(args[0]);50f.setAccessible(true);51f.set(mdp, null);5253try {54revive(d);55} catch (IllegalArgumentException iae) {56// Expected57return;58}5960throw new Exception("revive should fail");61}6263static <T> T revive(T in) throws Exception {64ByteArrayOutputStream bout = new ByteArrayOutputStream();65new ObjectOutputStream(bout).writeObject(in);66return (T) new ObjectInputStream(67new ByteArrayInputStream(bout.toByteArray())).readObject();68}69}707172