Path: blob/master/test/jdk/javax/security/auth/Subject/Subject.java
41154 views
/*1* Copyright (c) 2014, 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* An implementation of the Subject class that provides basic functionality25* for the construction of Subject objects with null Principal elements.26* This is a helper class for serialization tests tied to bug 801508127* (see SubjectNullTests.java).28*/29package jjjjj.security.auth;3031import javax.management.remote.JMXPrincipal;32import javax.security.auth.kerberos.KerberosPrincipal;33import javax.security.auth.x500.X500Principal;34import java.io.ByteArrayOutputStream;35import java.io.ObjectOutputStream;36import java.io.ObjectStreamField;37import java.lang.Exception;38import java.lang.RuntimeException;39import java.security.Principal;40import java.util.AbstractSet;41import java.util.Collections;42import java.util.HashSet;43import java.util.Iterator;44import java.util.LinkedList;45import java.util.Set;4647import java.io.FileOutputStream;4849public class Subject implements java.io.Serializable {5051private static final long serialVersionUID = -8308522755600156056L;5253Set<Principal> principals;54private volatile boolean readOnly = false;55private static final int PRINCIPAL_SET = 1;5657public Subject(Set<? extends Principal> principals) {58this.principals = Collections.synchronizedSet(new SecureSet<Principal>59(this, PRINCIPAL_SET, principals));60}6162public Set<Principal> getPrincipals() {63return principals;64}6566private static class SecureSet<E>67extends AbstractSet<E>68implements java.io.Serializable {6970private static final long serialVersionUID = 7911754171111800359L;71private static final ObjectStreamField[] serialPersistentFields = {72new ObjectStreamField("this$0", Subject.class),73new ObjectStreamField("elements", LinkedList.class),74new ObjectStreamField("which", int.class)75};7677Subject subject;78LinkedList<E> elements;79private int which;8081SecureSet(Subject subject, int which, Set<? extends E> set) {82this.subject = subject;83this.which = which;84this.elements = new LinkedList<E>(set);85}8687public Iterator<E> iterator() {88return elements.iterator();89}9091public int size() {92return elements.size();93}9495private void writeObject(java.io.ObjectOutputStream oos)96throws java.io.IOException {9798ObjectOutputStream.PutField fields = oos.putFields();99fields.put("this$0", subject);100fields.put("elements", elements);101fields.put("which", which);102oos.writeFields();103}104}105106public static byte[] enc(Object obj) {107try {108ByteArrayOutputStream bout;109bout = new ByteArrayOutputStream();110new ObjectOutputStream(bout).writeObject(obj);111byte[] data = bout.toByteArray();112for (int i = 0; i < data.length - 5; i++) {113if (data[i] == 'j' && data[i + 1] == 'j' && data[i + 2] == 'j'114&& data[i + 3] == 'j' && data[i + 4] == 'j') {115System.arraycopy("javax".getBytes(), 0, data, i, 5);116}117}118return data;119} catch (Exception e) {120throw new RuntimeException(e);121}122}123}124125126