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 org.apache.harmony.tests.javax.security.auth; 19 20 import junit.framework.TestCase; 21 import javax.security.auth.Subject; 22 import java.security.AccessControlContext; 23 import java.security.AccessController; 24 import java.security.PrivilegedAction; 25 import java.security.PrivilegedActionException; 26 import java.security.PrivilegedExceptionAction; 27 import java.security.ProtectionDomain; 28 import org.apache.harmony.testframework.serialization.SerializationTest; 29 30 /** 31 * Tests for <code>Subject</code> class constructors and methods. 32 * 33 */ 34 public class SubjectTest extends TestCase { 35 36 /** 37 * javax.security.auth.Subject#Subject() 38 */ 39 public void test_Constructor_01() { 40 try { 41 Subject s = new Subject(); 42 assertNotNull("Null object returned", s); 43 assertTrue("Set of principal is not empty", s.getPrincipals().isEmpty()); 44 assertTrue("Set of private credentials is not empty", s.getPrivateCredentials().isEmpty()); 45 assertTrue("Set of public credentials is not empty", s.getPublicCredentials().isEmpty()); 46 } catch (Exception e) { 47 fail("Unexpected exception: " + e); 48 } 49 } 50 51 /** 52 * javax.security.auth.Subject#doAs(Subject subject, PrivilegedAction action) 53 */ 54 public void test_doAs_01() { 55 Subject subj = new Subject(); 56 PrivilegedAction<Object> pa = new myPrivilegedAction(); 57 PrivilegedAction<Object> paNull = null; 58 59 try { 60 Object obj = Subject.doAs(null, pa); 61 } catch (Exception e) { 62 fail("Unexpected exception: " + e); 63 } 64 65 try { 66 Object obj = Subject.doAs(subj, pa); 67 } catch (Exception e) { 68 fail("Unexpected exception: " + e); 69 } 70 71 try { 72 Object obj = Subject.doAs(subj, paNull); 73 fail("NullPointerException wasn't thrown"); 74 } catch (NullPointerException npe) { 75 } 76 } 77 78 /** 79 * javax.security.auth.Subject#doAs(Subject subject, PrivilegedExceptionAction action) 80 */ 81 public void test_doAs_02() { 82 Subject subj = new Subject(); 83 PrivilegedExceptionAction<Object> pea = new myPrivilegedExceptionAction(); 84 PrivilegedExceptionAction<Object> peaNull = null; 85 86 try { 87 Object obj = Subject.doAs(null, pea); 88 } catch (Exception e) { 89 fail("Unexpected exception: " + e); 90 } 91 92 try { 93 Object obj = Subject.doAs(subj, pea); 94 } catch (Exception e) { 95 fail("Unexpected exception: " + e); 96 } 97 98 try { 99 Object obj = Subject.doAs(subj, peaNull); 100 fail("NullPointerException wasn't thrown"); 101 } catch (NullPointerException npe) { 102 } catch (Exception e) { 103 fail(e + " was thrown instead of NullPointerException"); 104 } 105 106 try { 107 Subject.doAs(subj, new PrivilegedExceptionAction<Object>(){ 108 public Object run() throws PrivilegedActionException { 109 throw new PrivilegedActionException(null); 110 } 111 }); 112 fail("PrivilegedActionException wasn't thrown"); 113 } catch (PrivilegedActionException e) { 114 } 115 } 116 117 /** 118 * javax.security.auth.Subject#doAsPrivileged(Subject subject, 119 * PrivilegedAction action, 120 * AccessControlContext acc) 121 */ 122 public void test_doAsPrivileged_01() { 123 Subject subj = new Subject(); 124 PrivilegedAction<Object> pa = new myPrivilegedAction(); 125 PrivilegedAction<Object> paNull = null; 126 AccessControlContext acc = AccessController.getContext(); 127 128 try { 129 Object obj = Subject.doAsPrivileged(null, pa, acc); 130 } catch (Exception e) { 131 fail("Unexpected exception: " + e); 132 } 133 134 try { 135 Object obj = Subject.doAsPrivileged(subj, pa, acc); 136 } catch (Exception e) { 137 fail("Unexpected exception: " + e); 138 } 139 140 try { 141 Object obj = Subject.doAsPrivileged(subj, paNull, acc); 142 fail("NullPointerException wasn't thrown"); 143 } catch (NullPointerException npe) { 144 } 145 } 146 147 /** 148 * javax.security.auth.Subject#doAsPrivileged(Subject subject, 149 * PrivilegedExceptionAction action, 150 * AccessControlContext acc) 151 */ 152 public void test_doAsPrivileged_02() { 153 Subject subj = new Subject(); 154 PrivilegedExceptionAction<Object> pea = new myPrivilegedExceptionAction(); 155 PrivilegedExceptionAction<Object> peaNull = null; 156 AccessControlContext acc = AccessController.getContext(); 157 158 try { 159 Object obj = Subject.doAsPrivileged(null, pea, acc); 160 } catch (Exception e) { 161 fail("Unexpected exception: " + e); 162 } 163 164 try { 165 Object obj = Subject.doAsPrivileged(subj, pea, acc); 166 } catch (Exception e) { 167 fail("Unexpected exception: " + e); 168 } 169 170 try { 171 Object obj = Subject.doAsPrivileged(subj, peaNull, acc); 172 fail("NullPointerException wasn't thrown"); 173 } catch (NullPointerException npe) { 174 } catch (Exception e) { 175 fail(e + " was thrown instead of NullPointerException"); 176 } 177 178 try { 179 Subject.doAsPrivileged(subj, new PrivilegedExceptionAction<Object>(){ 180 public Object run() throws PrivilegedActionException { 181 throw new PrivilegedActionException(null); 182 } 183 }, acc); 184 fail("PrivilegedActionException wasn't thrown"); 185 } catch (PrivilegedActionException e) { 186 } 187 } 188 189 /** 190 * javax.security.auth.Subject#getSubject(AccessControlContext acc) 191 */ 192 public void test_getSubject() { 193 Subject subj = new Subject(); 194 AccessControlContext acc = new AccessControlContext(new ProtectionDomain[0]); 195 196 try { 197 assertNull(Subject.getSubject(acc)); 198 } catch (Exception e) { 199 fail("Unexpected exception " + e); 200 } 201 } 202 203 /** 204 * javax.security.auth.Subject#toString() 205 */ 206 public void test_toString() { 207 Subject subj = new Subject(); 208 209 try { 210 assertNotNull("Null returned", subj.toString()); 211 } catch (Exception e) { 212 fail("Unexpected exception: " + e); 213 } 214 } 215 216 /** 217 * javax.security.auth.Subject#hashCode() 218 */ 219 public void test_hashCode() { 220 Subject subj = new Subject(); 221 222 try { 223 assertNotNull("Null returned", subj.hashCode()); 224 } catch (Exception e) { 225 fail("Unexpected exception: " + e); 226 } 227 } 228 229 public void testSerializationSelf() throws Exception { 230 SerializationTest.verifySelf(getSerializationData()); 231 } 232 233 public void testSerializationGolden() throws Exception { 234 SerializationTest.verifyGolden(this, getSerializationData()); 235 } 236 237 private Object[] getSerializationData() { 238 Subject subject = new Subject(); 239 return new Object[] { subject, subject.getPrincipals(), 240 subject.getPrivateCredentials(), subject.getPublicCredentials() }; 241 } 242 } 243 244 245 class myPrivilegedAction implements PrivilegedAction <Object> { 246 myPrivilegedAction(){} 247 public Object run() { 248 return new Object(); 249 } 250 } 251 252 class myPrivilegedExceptionAction implements PrivilegedExceptionAction <Object> { 253 myPrivilegedExceptionAction(){} 254 public Object run() { 255 return new Object(); 256 } 257 } 258