1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package tests.api.javax.security.auth; 19 20 import junit.framework.TestCase; 21 22 import javax.security.auth.AuthPermission; 23 import javax.security.auth.PrivateCredentialPermission; 24 import javax.security.auth.Subject; 25 26 import java.util.Set; 27 import java.util.HashSet; 28 import java.security.Permission; 29 import java.security.Principal; 30 import java.security.PrivilegedAction; 31 import java.security.PrivilegedActionException; 32 import java.security.PrivilegedExceptionAction; 33 import java.security.AccessControlContext; 34 import java.security.AccessController; 35 import java.security.ProtectionDomain; 36 37 /** 38 * Tests for <code>Subject</code> class constructors and methods. 39 * 40 */ 41 public class SubjectTest extends TestCase { 42 43 /** 44 * javax.security.auth.Subject#Subject() 45 */ 46 public void test_Constructor_01() { 47 try { 48 Subject s = new Subject(); 49 assertNotNull("Null object returned", s); 50 assertTrue("Set of principal is not empty", s.getPrincipals().isEmpty()); 51 assertTrue("Set of private credentials is not empty", s.getPrivateCredentials().isEmpty()); 52 assertTrue("Set of public credentials is not empty", s.getPublicCredentials().isEmpty()); 53 } catch (Exception e) { 54 fail("Unexpected exception: " + e); 55 } 56 } 57 58 /** 59 * javax.security.auth.Subject#doAs(Subject subject, PrivilegedAction action) 60 */ 61 public void test_doAs_01() { 62 Subject subj = new Subject(); 63 PrivilegedAction<Object> pa = new myPrivilegedAction(); 64 PrivilegedAction<Object> paNull = null; 65 66 try { 67 Object obj = Subject.doAs(null, pa); 68 } catch (Exception e) { 69 fail("Unexpected exception: " + e); 70 } 71 72 try { 73 Object obj = Subject.doAs(subj, pa); 74 } catch (Exception e) { 75 fail("Unexpected exception: " + e); 76 } 77 78 try { 79 Object obj = Subject.doAs(subj, paNull); 80 fail("NullPointerException wasn't thrown"); 81 } catch (NullPointerException npe) { 82 } 83 } 84 85 /** 86 * javax.security.auth.Subject#doAs(Subject subject, PrivilegedExceptionAction action) 87 */ 88 public void test_doAs_02() { 89 Subject subj = new Subject(); 90 PrivilegedExceptionAction<Object> pea = new myPrivilegedExceptionAction(); 91 PrivilegedExceptionAction<Object> peaNull = null; 92 93 try { 94 Object obj = Subject.doAs(null, pea); 95 } catch (Exception e) { 96 fail("Unexpected exception: " + e); 97 } 98 99 try { 100 Object obj = Subject.doAs(subj, pea); 101 } catch (Exception e) { 102 fail("Unexpected exception: " + e); 103 } 104 105 try { 106 Object obj = Subject.doAs(subj, peaNull); 107 fail("NullPointerException wasn't thrown"); 108 } catch (NullPointerException npe) { 109 } catch (Exception e) { 110 fail(e + " was thrown instead of NullPointerException"); 111 } 112 113 try { 114 Subject.doAs(subj, new PrivilegedExceptionAction<Object>(){ 115 public Object run() throws PrivilegedActionException { 116 throw new PrivilegedActionException(null); 117 } 118 }); 119 fail("PrivilegedActionException wasn't thrown"); 120 } catch (PrivilegedActionException e) { 121 } 122 } 123 124 /** 125 * javax.security.auth.Subject#doAsPrivileged(Subject subject, 126 * PrivilegedAction action, 127 * AccessControlContext acc) 128 */ 129 public void test_doAsPrivileged_01() { 130 Subject subj = new Subject(); 131 PrivilegedAction<Object> pa = new myPrivilegedAction(); 132 PrivilegedAction<Object> paNull = null; 133 AccessControlContext acc = AccessController.getContext(); 134 135 try { 136 Object obj = Subject.doAsPrivileged(null, pa, acc); 137 } catch (Exception e) { 138 fail("Unexpected exception: " + e); 139 } 140 141 try { 142 Object obj = Subject.doAsPrivileged(subj, pa, acc); 143 } catch (Exception e) { 144 fail("Unexpected exception: " + e); 145 } 146 147 try { 148 Object obj = Subject.doAsPrivileged(subj, paNull, acc); 149 fail("NullPointerException wasn't thrown"); 150 } catch (NullPointerException npe) { 151 } 152 } 153 154 /** 155 * javax.security.auth.Subject#doAsPrivileged(Subject subject, 156 * PrivilegedExceptionAction action, 157 * AccessControlContext acc) 158 */ 159 public void test_doAsPrivileged_02() { 160 Subject subj = new Subject(); 161 PrivilegedExceptionAction<Object> pea = new myPrivilegedExceptionAction(); 162 PrivilegedExceptionAction<Object> peaNull = null; 163 AccessControlContext acc = AccessController.getContext(); 164 165 try { 166 Object obj = Subject.doAsPrivileged(null, pea, acc); 167 } catch (Exception e) { 168 fail("Unexpected exception: " + e); 169 } 170 171 try { 172 Object obj = Subject.doAsPrivileged(subj, pea, acc); 173 } catch (Exception e) { 174 fail("Unexpected exception: " + e); 175 } 176 177 try { 178 Object obj = Subject.doAsPrivileged(subj, peaNull, acc); 179 fail("NullPointerException wasn't thrown"); 180 } catch (NullPointerException npe) { 181 } catch (Exception e) { 182 fail(e + " was thrown instead of NullPointerException"); 183 } 184 185 try { 186 Subject.doAsPrivileged(subj, new PrivilegedExceptionAction<Object>(){ 187 public Object run() throws PrivilegedActionException { 188 throw new PrivilegedActionException(null); 189 } 190 }, acc); 191 fail("PrivilegedActionException wasn't thrown"); 192 } catch (PrivilegedActionException e) { 193 } 194 } 195 196 /** 197 * javax.security.auth.Subject#getSubject(AccessControlContext acc) 198 */ 199 public void test_getSubject() { 200 Subject subj = new Subject(); 201 AccessControlContext acc = new AccessControlContext(new ProtectionDomain[0]); 202 203 try { 204 assertNull(Subject.getSubject(acc)); 205 } catch (Exception e) { 206 fail("Unexpected exception " + e); 207 } 208 } 209 210 /** 211 * javax.security.auth.Subject#toString() 212 */ 213 public void test_toString() { 214 Subject subj = new Subject(); 215 216 try { 217 assertNotNull("Null returned", subj.toString()); 218 } catch (Exception e) { 219 fail("Unexpected exception: " + e); 220 } 221 } 222 223 /** 224 * javax.security.auth.Subject#hashCode() 225 */ 226 public void test_hashCode() { 227 Subject subj = new Subject(); 228 229 try { 230 assertNotNull("Null returned", subj.hashCode()); 231 } catch (Exception e) { 232 fail("Unexpected exception: " + e); 233 } 234 } 235 } 236 237 238 class myPrivilegedAction implements PrivilegedAction <Object> { 239 myPrivilegedAction(){} 240 public Object run() { 241 return new Object(); 242 } 243 } 244 245 class myPrivilegedExceptionAction implements PrivilegedExceptionAction <Object> { 246 myPrivilegedExceptionAction(){} 247 public Object run() { 248 return new Object(); 249 } 250 } 251