Path: blob/master/test/jdk/javax/security/auth/Subject/Serial.java
41154 views
/*1* Copyright (c) 2000, 2017, 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* @modules jdk.security.auth27* @summary Subject serialized principal set is28* implementation-dependent class29* @run main/othervm/policy=Serial.policy Serial30*/3132import java.io.FileInputStream;33import java.io.FileOutputStream;34import java.io.ObjectInputStream;35import java.io.ObjectOutputStream;36import java.util.HashSet;37import java.util.Set;38import javax.security.auth.Subject;3940public class Serial implements java.io.Serializable {4142public static void main(String[] args) {4344try {45FileOutputStream fos = new FileOutputStream("serial.tmp");46ObjectOutputStream oos = new ObjectOutputStream(fos);4748HashSet principals = new HashSet();49principals.add50(new com.sun.security.auth.NTUserPrincipal("test"));51principals.add52(new com.sun.security.auth.NTDomainPrincipal("test2"));5354Subject s = new Subject55(false,56principals,57new HashSet(),58new HashSet());59oos.writeObject(s);60oos.flush();61fos.close();6263FileInputStream fis = new FileInputStream("serial.tmp");64ObjectInputStream ois = new ObjectInputStream(fis);6566Subject s2 = (Subject)ois.readObject();67fis.close();6869System.out.println("s2 = " + s2.toString());70System.out.println("s2.getPrincipals().size() = " +71s2.getPrincipals().size());72if (!s.equals(s2) || !s2.equals(s)) {73throw new SecurityException("Serial test failed: " +74"EQUALS TEST FAILED");75}7677// make sure private credentials are not serializable78// without permissions7980Set privateCredentials = s.getPrivateCredentials();81privateCredentials.add(new Serial());8283fos = new FileOutputStream("serial2.tmp");84oos = new ObjectOutputStream(fos);85try {86oos.writeObject(privateCredentials);87oos.flush();88fos.close();89throw new RuntimeException("Serial test failed: " +90"allowed to serialize private credential set");91} catch (SecurityException se) {92// good93se.printStackTrace();94}9596System.out.println("Serial test succeeded");97} catch (Exception e) {98e.printStackTrace();99throw new SecurityException("Serial test failed");100}101}102}103104105