Subset.java revision 0
0N/A/*
0N/A * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @author Ram Marti
0N/A * @bug 4326852
0N/A * @summary Retrive a subset of private credentials can be accessed
0N/A * @run main/othervm/policy=Subset.policy Subset
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport com.sun.security.auth.SolarisPrincipal;
0N/Aimport javax.security.auth.Subject;
0N/A
0N/A/*
0N/A * Author : Ram Marti
0N/A * This is a test program to verify the fix for Bug 4326852
0N/A * (impossible to extract a subset of private credentials)
0N/A * The policy file used allows read access only to String classes.
0N/A * grant {
0N/A * permission javax.security.auth.AuthPermission \
0N/A * "modifyPrivateCredentials";
0N/A * permission javax.security.auth.PrivateCredentialPermission \
0N/A * "java.lang.String com.sun.security.auth.SolarisPrincipal \"user"", "read";
0N/A * };
0N/A
0N/A * The test verifies the following:
0N/A * - String class creds can be retrieved by using
0N/A * getPrivateCredentials(String.class)
0N/A * - The above set is not backed internally
0N/A * - getPrivateCredentials(Boolean or Integer) returns an empty set
0N/A * - Set is returned by getPrivateCredentials() throws
0N/A * security exception when trying to access non-String
0N/A * class credentials
0N/A * - The above set is internally backed up and any changes in
0N/A * internal private creds are reflected in the set returned
0N/A * - When the above set throws security exception the iterator
0N/A * - is advanced to the next item in the list of creds.
0N/A * - equals,contains,containsAll,add,remove operations work correctly
0N/A */
0N/A
0N/Apublic class Subset {
0N/A public static void main(String[] args) throws Exception {
0N/A int exceptionCounter =0;
0N/A Iterator iter1;
0N/A HashSet creds = new HashSet();
0N/A Subject emptys =
0N/A new Subject(false, //readOnly
0N/A Collections.singleton(new SolarisPrincipal("user")),
0N/A Collections.EMPTY_SET,
0N/A creds);
0N/A /* Test principals */
0N/A
0N/A Set princ= emptys.getPrincipals();
0N/A HashSet collp= new HashSet();
0N/A collp.add(new String("abc"));
0N/A collp.add(new String("def"));
0N/A collp.add(new String("Exists"));
0N/A collp.add(new String("Does not Exist"));
0N/A try {
0N/A if (princ.containsAll(collp)) {
0N/A throw new Exception ("Error: Contains the collection");
0N/A } else
0N/A System.out.println ("Does not Contain the collection");
0N/A } catch (SecurityException e) {
0N/A throw new Exception ("Error: Exception in containsAll (string coll)!!");
0N/A }
0N/A
0N/A
0N/A Set p1 = emptys.getPrivateCredentials();
0N/A
0N/A if (p1.size() != 0) {
0N/A throw new Exception("Error:p1 size should have been 6 and was " +
0N/A p1.size());
0N/A }
0N/A
0N/A creds.add("abc");
0N/A creds.add(new Integer(3));
0N/A creds.add(Boolean.TRUE);
0N/A Subject sremove =
0N/A new Subject(false, //readOnly
0N/A Collections.singleton(new SolarisPrincipal("user")),
0N/A Collections.EMPTY_SET,
0N/A creds);
0N/A Set p2 = sremove.getPrivateCredentials();
0N/A
0N/A if (p2.size() !=3){
0N/A throw new Exception("Error: p2 size should have been 3 and was " +
0N/A p2.size());
0N/A }
0N/A iter1 = p2.iterator();
0N/A exceptionCounter=0;
0N/A while (iter1.hasNext()) {
0N/A try {
0N/A Object o = iter1.next();
0N/A System.out.println(" private creds of class " +
0N/A o.getClass() + "value is " + o.toString());
0N/A } catch (SecurityException e) {
0N/A System.out.println("Expected Exception occured");
0N/A exceptionCounter++;
0N/A }
0N/A }
0N/A if (exceptionCounter != 2) {
0N/A throw new Exception("Expected number of exceptions was 2 " +
0N/A "The actual number was " + exceptionCounter);
0N/A }
0N/A
0N/A // Verify that remove op was successful
0N/A
0N/A iter1.remove();
0N/A if (p2.size() !=2) {
0N/A throw new RuntimeException("Error: p2 size should have been 2 and was " +
0N/A p2.size());
0N/A }
0N/A System.out.println ("Checking the value after removal");
0N/A p2 = sremove.getPrivateCredentials();
0N/A try {
0N/A if (!p2.add(new String("XYZ"))) {
0N/A
0N/A throw new RuntimeException("Error in adding string");
0N/A }
0N/A if (!p2.add(new Integer(99))) {
0N/A
0N/A throw new RuntimeException("Error in adding Integer");
0N/A }
0N/A HashSet coll1 = new HashSet();
0N/A coll1.add(new String("RST"));
0N/A coll1.add(new Integer(1));
0N/A if (!p2.addAll(coll1)) {
0N/A
0N/A throw new RuntimeException("Error in addAll");
0N/A }
0N/A
0N/A } catch (Exception e){
0N/A e.printStackTrace();
0N/A throw new RuntimeException("Unexpected exception in add");
0N/A
0N/A }
0N/A iter1 = p2.iterator();
0N/A
0N/A while (iter1.hasNext()) {
0N/A try {
0N/A Object o = iter1.next();
0N/A System.out.println(" private creds of class " +
0N/A o.getClass() + "value is " + o.toString());
0N/A } catch (SecurityException e) {
0N/A // System.out.println("Exception!!");
0N/A }
0N/A }
0N/A iter1 = p2.iterator();
0N/A
0N/A System.out.println ("Checked the value after removal");
0N/A
0N/A HashSet creds1 = new HashSet();
0N/A creds1.add("abc");
0N/A creds1.add("def");
0N/A creds1.add(Boolean.TRUE);
0N/A creds1.add(new Integer(1));
0N/A creds1.add(new String("Exists"));
0N/A Subject scontain =
0N/A new Subject(false, //readOnly
0N/A Collections.singleton(new SolarisPrincipal("user")),
0N/A Collections.EMPTY_SET,
0N/A creds1);
0N/A p2 = scontain.getPrivateCredentials();
0N/A try {
0N/A Object ObjAr = p2.toArray();
0N/A } catch (SecurityException e) {
0N/A System.out.println("Should get an Exception in toArray()");
0N/A }
0N/A
0N/A HashSet creds3 = new HashSet();
0N/A creds3.add (new String("abc"));
0N/A p2 = scontain.getPrivateCredentials();
0N/A
0N/A try {
0N/A Object ObjCred = (Object)creds3.clone();
0N/A System.out.println ("Size of p2 is " + p2.size() +
0N/A "Size of ObjCred is " +
0N/A ((HashSet)ObjCred).size()
0N/A );
0N/A if (p2.equals(ObjCred))
0N/A throw new RuntimeException("Error:Equals ObjCred *** ");
0N/A else
0N/A System.out.println ("Does not Equal Objcred");
0N/A } catch (SecurityException e) {
0N/A throw new RuntimeException("Error:Should not get an Exception in equals of creds3");
0N/A
0N/A
0N/A }
0N/A
0N/A try {
0N/A Object ObjCred = (Object)creds1.clone();
0N/A System.out.println ("Size of p2 is " + p2.size() +
0N/A "Size of ObjCred is " +
0N/A ((HashSet)ObjCred).size()
0N/A );
0N/A if (p2.equals(ObjCred))
0N/A throw new RuntimeException ("Error: Equals ObjCred");
0N/A else
0N/A throw new RuntimeException ("Error: Does not Equal Objcred");
0N/A } catch (SecurityException e) {
0N/A System.out.println("Should get an Exception in equals of creds1");
0N/A }
0N/A /* We can store only string types of creds
0N/A * Let us create a subject with only string type of creds
0N/A */
0N/A
0N/A HashSet creds2 = new HashSet();
0N/A creds2.add("abc");
0N/A creds2.add("def");
0N/A creds2.add("ghi");
0N/A Subject sstring =
0N/A new Subject(false, //readOnly
0N/A Collections.singleton(new SolarisPrincipal("user")),
0N/A Collections.EMPTY_SET,
0N/A creds2);
0N/A p2 = sstring.getPrivateCredentials();
0N/A try {
0N/A String[] selectArray = { "exits", "Does not exist"};
0N/A Object ObjAr = p2.toArray(selectArray);
0N/A System.out.println(" No Exception in ObjAr- String");
0N/A
0N/A } catch (SecurityException e) {
0N/A throw new RuntimeException(" Error: Exception in ObjAr- String!!");
0N/A }
0N/A /*
0N/A * New subject scontain1, set p3, creds4
0N/A */
0N/A
0N/A
0N/A HashSet creds4 = new HashSet();
0N/A creds4.add("abc");
0N/A creds4.add("def");
0N/A creds4.add("ghi");
0N/A creds4.add(new Integer(1));
0N/A creds4.add("Exists");
0N/A Subject scontain1 =
0N/A new Subject(false, //readOnly
0N/A Collections.singleton(new SolarisPrincipal("user")),
0N/A Collections.EMPTY_SET,
0N/A creds4);
0N/A Set p3 = scontain1.getPrivateCredentials();
0N/A try {
0N/A Object Obj = new String("Exists");
0N/A if (p3.contains(Obj))
0N/A System.out.println ("Contains String cred");
0N/A else
0N/A throw new RuntimeException ("Error Does not Contain the stringcred exists");
0N/A } catch (SecurityException e) {
0N/A throw new RuntimeException("Error:Exception!!");
0N/A
0N/A }
0N/A try {
0N/A Object ObjCred = (Object)creds4.clone();
0N/A if (p3.equals(ObjCred))
0N/A throw new RuntimeException ("Error:Equals ObjCred");
0N/A else
0N/A throw new RuntimeException ("Error:Does not Equal Objcred");
0N/A } catch (SecurityException e) {
0N/A System.out.println("Should get an Exception in equals");
0N/A }
0N/A
0N/A try {
0N/A Object Obj = new Integer(1);
0N/A if (p3.contains(Obj))
0N/A throw new RuntimeException ("Error:Contains integer cred");
0N/A else
0N/A throw new RuntimeException ("Error:Does not Contain integer cred");
0N/A } catch (SecurityException e) {
0N/A System.out.println("Should get an Exception in contains Integer cred");
0N/A }
0N/A
0N/A
0N/A
0N/A HashSet coll = new HashSet();
0N/A coll.add(new String("abc"));
0N/A coll.add(new String("def"));
0N/A coll.add(new String("Exists"));
0N/A coll.add(new String("Does not Exist"));
0N/A try {
0N/A if (p3.containsAll(coll))
0N/A throw new RuntimeException ("Error: Contains the collection");
0N/A else
0N/A System.out.println ("Does not Contain the collection");
0N/A } catch (SecurityException e) {
0N/A throw new RuntimeException("Error: Exception in containsAll (string coll)!!");
0N/A
0N/A }
0N/A coll.remove(new String("Exists"));
0N/A coll.remove(new String("Does not Exist"));
0N/A try {
0N/A if (p3.containsAll(coll))
0N/A System.out.println ("Contains the collection");
0N/A else
0N/A throw new RuntimeException ("Error:Does not Contain the collection");
0N/A } catch (SecurityException e) {
0N/A throw new RuntimeException("Error: Exception in containsAll (string coll)!!");
0N/A }
0N/A
0N/A Object Obj = new String("Exists");
0N/A try {
0N/A if (p3.contains(Obj))
0N/A System.out.println ("Contains String cred exists");
0N/A else
0N/A System.out.println ("Does not Contain String cred exists");
0N/A } catch (SecurityException e) {
0N/A System.out.println("Exception in String cred!!");
0N/A }
0N/A
0N/A Obj = new String("Does not exist");
0N/A try {
0N/A if (p3.contains(Obj))
0N/A throw new RuntimeException ("Error: Contains the String does not exist");
0N/A else
0N/A System.out.println ("Does not Contain the String cred Does not exist");
0N/A } catch (SecurityException e) {
0N/A throw new RuntimeException("Error: Exception in Contains!!");
0N/A }
0N/A p3.add(new Integer(2));
0N/A coll.add(new Integer(2));
0N/A p3.add("XYZ");
0N/A
0N/A System.out.println ("Testing Retainall ");
0N/A exceptionCounter =0;
0N/A iter1 = p3.iterator();
0N/A while (iter1.hasNext())
0N/A {
0N/A try {
0N/A Object o = iter1.next();
0N/A System.out.println(" private creds of class " +
0N/A o.getClass() + "value is " + o.toString());
0N/A } catch (SecurityException e) {
0N/A System.out.println(" We should get exception");
0N/A System.out.println("Exception!!");
0N/A exceptionCounter++;
0N/A }
0N/A }
0N/A System.out.println(" After the retainall Operation");
0N/A try {
0N/A if (p3.retainAll(coll))
0N/A System.out.println ("Retained the collection");
0N/A else
0N/A throw new RuntimeException ("Error: RetainAll did not succeed");
0N/A } catch (SecurityException e) {
0N/A e.printStackTrace();
0N/A throw new RuntimeException("Error: Unexpected Exception in retainAll!");
0N/A }
0N/A iter1 = p3.iterator();
0N/A while (iter1.hasNext())
0N/A {
0N/A try {
0N/A Object o = iter1.next();
0N/A System.out.println(" private creds of class " +
0N/A o.getClass() + "value is " + o.toString());
0N/A } catch (SecurityException e) {
0N/A exceptionCounter++;
0N/A }
0N/A }
0N/A System.out.println ("Retainall collection");
0N/A p3.add(new Integer (3));
0N/A iter1 = p3.iterator();
0N/A while (iter1.hasNext()) {
0N/A try {
0N/A Object o = iter1.next();
0N/A System.out.println(" private creds of class " +
0N/A o.getClass() + "value is " + o.toString());
0N/A } catch (SecurityException e) {
0N/A System.out.println("Should get Exception ");
0N/A }
0N/A }
0N/A exceptionCounter=0;
0N/A HashSet coll2 = new HashSet();
0N/A coll2.add(new String("abc"));
0N/A coll2.add(new Integer (3));
0N/A System.out.println(" before removeall");
0N/A iter1 = p3.iterator();
0N/A exceptionCounter =0;
0N/A while (iter1.hasNext()) {
0N/A try {
0N/A Object o = iter1.next();
0N/A System.out.println(" private creds of class " +
0N/A o.getClass() + "value is " + o.toString());
0N/A } catch (SecurityException e) {
0N/A System.out.println("Expected Exception thrown ");
0N/A exceptionCounter++;
0N/A }
0N/A }
0N/A // We added two integer creds so there must be two exceptions only
0N/A
0N/A if (exceptionCounter != 2) {
0N/A throw new RuntimeException("Expected 2 Exceptions; received " +
0N/A exceptionCounter + "exceptions ");
0N/A }
0N/A
0N/A try {
0N/A p3.removeAll(coll2);
0N/A System.out.println(" removeall successful! ");
0N/A } catch (SecurityException e) {
0N/A throw new RuntimeException(" Error: removeAll Security Exception!!");
0N/A }
0N/A
0N/A iter1 = p3.iterator();
0N/A System.out.println(" After removeall");
0N/A exceptionCounter = 0;
0N/A while (iter1.hasNext()) {
0N/A try {
0N/A Object o = iter1.next();
0N/A System.out.println (" private creds of class " +
0N/A o.getClass() + "value is " + o.toString());
0N/A } catch (SecurityException e) {
0N/A System.out.println("Expected Exception thrown ");
0N/A exceptionCounter++;
0N/A }
0N/A }
0N/A // We had two integer creds; removed one as a part of coll2; so
0N/A // only one exception must have been thrown
0N/A if (exceptionCounter != 1) {
0N/A throw new RuntimeException("Expected 1 Exceptions; received " +
0N/A exceptionCounter + "exceptions ");
0N/A }
0N/A try {
0N/A p3.clear();
0N/A System.out.println(" Clear() successful! ");
0N/A } catch (SecurityException e) {
0N/A throw new RuntimeException(" Error: Clear Security Exception!!");
0N/A }
0N/A
0N/A
0N/A /* New subject s with creds and privCredSet
0N/A *
0N/A */
0N/A creds.clear();
0N/A creds.add("abc");
0N/A creds.add("def");
0N/A creds.add("ghi");
0N/A creds.add(new Integer(1));
0N/A Subject s =
0N/A new Subject(false, //readOnly
0N/A Collections.singleton(new SolarisPrincipal("user")),
0N/A Collections.EMPTY_SET,
0N/A creds);
0N/A try {
0N/A Set privCredSet = s.getPrivateCredentials(char.class);
0N/A if (privCredSet.size() != 0) {
0N/A throw new RuntimeException("Error:String Privcred size should have been 0 and was " +
0N/A privCredSet.size());
0N/A }
0N/A
0N/A } catch (Exception e) {
0N/A throw new RuntimeException ("Error " + e.toString());
0N/A }
0N/A
0N/A
0N/A try {
0N/A Set privCredSet = s.getPrivateCredentials(String.class);
0N/A if (privCredSet.size() != 3) {
0N/A throw new RuntimeException("Error:String Privcred size should have been 2 and was " +
0N/A privCredSet.size());
0N/A }
0N/A s.getPrivateCredentials().add("XYZ");
0N/A /*
0N/A * Since the privCredSet is not backed by internal private
0N/A * creds adding to it should not make any difference to
0N/A * privCredSet and theize should still be 3
0N/A */
0N/A
0N/A if (privCredSet.size() != 3) {
0N/A throw new RuntimeException("Error:String Privcred size should have been 2 and was " +
0N/A privCredSet.size());
0N/A }
0N/A s.getPrivateCredentials().remove("XYZ");
0N/A /*
0N/A * Let us try to get the elements
0N/A * No exception should occur
0N/A */
0N/A
0N/A Iterator iter = privCredSet.iterator();
0N/A while (iter.hasNext()) {
0N/A try {
0N/A Object o = iter.next();
0N/A System.out.println(" private creds of class " +
0N/A o.getClass() + "value is " + o.toString());
0N/A } catch (SecurityException e) {
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A throw new RuntimeException("Unexcpected Exception");
0N/A }
0N/A
0N/A /*
0N/A * Can we add and remove the creds
0N/A */
0N/A s.getPrivateCredentials().add("XYZ");
0N/A s.getPrivateCredentials().remove("XYZ");
0N/A s.getPrivateCredentials().add(new Integer(2));
0N/A s.getPrivateCredentials().remove(new Integer(2));
0N/A
0N/A
0N/A // We don't have permission to read Boolean creds
0N/A // SInce the creds have no boolean creds we should get an empty
0N/A // set
0N/A try {
0N/A Set privCredSet1 = s.getPrivateCredentials(Boolean.class);
0N/A if (privCredSet1.size() != 0){
0N/A throw new RuntimeException("Error:String PrivcredSet1 of Boolean size should have been 0 and was " +
0N/A privCredSet1.size());
0N/A }
0N/A } catch (SecurityException e) {
0N/A e.printStackTrace();
0N/A throw new RuntimeException("Unexcpected Exception");
0N/A }
0N/A System.out.println ("Checked Boolean Creds ");
0N/A
0N/A /*
0N/A * We don't have permission to read Integer creds
0N/A * We should get an empty set even though the private creds
0N/A * has an integer cred. No security exception either !
0N/A */
0N/A
0N/A try {
0N/A Set privCredSet1 = s.getPrivateCredentials(Integer.class);
0N/A if (privCredSet1.size() != 0){
0N/A throw new RuntimeException("Error:String PrivcredSet1 of Integer size should have been 0 and was " +
0N/A privCredSet1.size());
0N/A }
0N/A } catch (SecurityException e) {
0N/A System.out.println ("Expected exception");
0N/A }
0N/A System.out.println ("Checked Integer Creds ");
0N/A
0N/A Set privCredSet2 = s.getPrivateCredentials();
0N/A
0N/A if (privCredSet2.size() != 4){
0N/A throw new RuntimeException("Error:String PrivcredSet1 size should have been 4 and was " +
0N/A privCredSet2.size());
0N/A }
0N/A
0N/A /*
0N/A * Since the returned privCredSet2 is internally backed by the
0N/A * private creds, any additions to it should be reflected in
0N/A * privcredSet2
0N/A */
0N/A s.getPrivateCredentials().add("XYZ");
0N/A if (privCredSet2.size() != 5) {
0N/A throw new RuntimeException("Error:String PrivcredSet1 size should have been 5 and was " +
0N/A privCredSet2.size());
0N/A }
0N/A s.getPrivateCredentials().remove("XYZ");
0N/A if (privCredSet2.size() != 4) {
0N/A throw new RuntimeException("String privCredSet2 size should have been 5 and was " +
0N/A privCredSet2.size());
0N/A }
0N/A System.out.println("Checked remove(String) operation");
0N/A /* Let us add a couple of Boolean creds */
0N/A s.getPrivateCredentials().add(Boolean.TRUE);
0N/A s.getPrivateCredentials().add(new Integer(2));
0N/A
0N/A exceptionCounter =0;
0N/A iter1 = privCredSet2.iterator();
0N/A while (iter1.hasNext())
0N/A {
0N/A try {
0N/A Object o = iter1.next();
0N/A System.out.println(" private creds of class " +
0N/A o.getClass() + "value is " + o.toString());
0N/A } catch (SecurityException e) {
0N/A System.out.println(" We should get exception");
0N/A System.out.println("Exception!!");
0N/A exceptionCounter++;
0N/A }
0N/A }
0N/A if (exceptionCounter != 3) {
0N/A throw new RuntimeException("Expected number of exception was 3 " +
0N/A "The actual number was " + exceptionCounter);
0N/A }
0N/A privCredSet2.add (new Integer(3));
0N/A try {
0N/A int hashCode = privCredSet2.hashCode();
0N/A } catch (SecurityException e) {
0N/A System.out.println ("hashCode Expected exception");
0N/A }
0N/A System.out.println ("Tests completed");
0N/A }
0N/A
0N/A}