Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/Subject/Serial.java
38853 views
/*1* Copyright (c) 2000, 2002, 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 436482626* @summary Subject serialized principal set is27* implementation-dependent class28* @run main/othervm/policy=Serial.policy Serial29*/3031import javax.security.auth.*;32import java.io.*;33import java.util.*;3435public class Serial implements java.io.Serializable {3637public static void main(String[] args) {3839try {40FileOutputStream fos = new FileOutputStream("serial.tmp");41ObjectOutputStream oos = new ObjectOutputStream(fos);4243HashSet principals = new HashSet();44principals.add45(new com.sun.security.auth.NTUserPrincipal("test"));46principals.add47(new com.sun.security.auth.NTDomainPrincipal("test2"));4849Subject s = new Subject50(false,51principals,52new HashSet(),53new HashSet());54oos.writeObject(s);55oos.flush();56fos.close();5758FileInputStream fis = new FileInputStream("serial.tmp");59ObjectInputStream ois = new ObjectInputStream(fis);6061Subject s2 = (Subject)ois.readObject();62fis.close();6364System.out.println("s2 = " + s2.toString());65System.out.println("s2.getPrincipals().size() = " +66s2.getPrincipals().size());67if (!s.equals(s2) || !s2.equals(s)) {68throw new SecurityException("Serial test failed: " +69"EQUALS TEST FAILED");70}7172// make sure private credentials are not serializable73// without permissions7475Set privateCredentials = s.getPrivateCredentials();76privateCredentials.add(new Serial());7778fos = new FileOutputStream("serial2.tmp");79oos = new ObjectOutputStream(fos);80try {81oos.writeObject(privateCredentials);82oos.flush();83fos.close();84throw new RuntimeException("Serial test failed: " +85"allowed to serialize private credential set");86} catch (SecurityException se) {87// good88se.printStackTrace();89}9091System.out.println("Serial test succeeded");92} catch (Exception e) {93e.printStackTrace();94throw new SecurityException("Serial test failed");95}96}97}9899100