1/* 2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @author Charlie Lai 27 * @bug 4394746 28 * @summary Resources should not be loaded until necessar 29 * @run main/othervm/policy=Format.policy -Djava.security.auth.login.config=file:${test.src}/Format.config Format 30 * 31 */ 32 33import javax.security.auth.*; 34import javax.security.auth.login.*; 35import javax.security.auth.callback.*; 36import java.io.*; 37import java.util.*; 38 39import com.sun.security.auth.*; 40 41public class Format 42 implements java.io.Serializable, java.security.Principal 43{ 44 45 public String getName() { return null; } 46 47 public static void main(String[] args) { 48 49 try { 50 PrivateCredentialPermission pcp = new PrivateCredentialPermission 51 ("test", "write"); 52 throw new SecurityException("test 1 failed"); 53 } catch (IllegalArgumentException iae) { 54 // good 55 System.out.println("Test 1: " + iae.getMessage()); 56 } 57 58 try { 59 PrivateCredentialPermission pcp = new PrivateCredentialPermission 60 ("Format", "read"); 61 throw new SecurityException("test 2 failed"); 62 } catch (IllegalArgumentException iae) { 63 // good 64 System.out.println("Test 2: " + iae.getMessage()); 65 } 66 67 try { 68 PrivateCredentialPermission pcp = new PrivateCredentialPermission 69 ("Format Format", "read"); 70 throw new SecurityException("test 3 failed"); 71 } catch (IllegalArgumentException iae) { 72 // good 73 System.out.println("Test 3: " + iae.getMessage()); 74 } 75 76 try { 77 PrivateCredentialPermission pcp = new PrivateCredentialPermission 78 ("Format Format Format", "read"); 79 throw new SecurityException("test 4 failed"); 80 } catch (IllegalArgumentException iae) { 81 // good 82 System.out.println("Test 4: " + iae.getMessage()); 83 } 84 85 try { 86 PrivateCredentialPermission pcp = new PrivateCredentialPermission 87 ("Format Format \"Format", "read"); 88 throw new SecurityException("test 5 failed"); 89 } catch (IllegalArgumentException iae) { 90 // good 91 System.out.println("Test 5: " + iae.getMessage()); 92 } 93 94 try { 95 PrivateCredentialPermission pcp = new PrivateCredentialPermission 96 ("Format * \"Format\"", "read"); 97 throw new SecurityException("test 6 failed"); 98 } catch (IllegalArgumentException iae) { 99 // good 100 System.out.println("Test 6: " + iae.getMessage()); 101 } 102 103 try { 104 javax.security.auth.x500.X500Principal p = 105 new javax.security.auth.x500.X500Principal((String)null); 106 throw new SecurityException("test 7 failed"); 107 } catch (NullPointerException npe) { 108 // good 109 System.out.println("Test 7: " + npe.getMessage()); 110 } 111 112 try { 113 Subject s = new Subject(false, null, null, null); 114 throw new SecurityException("test 8 failed"); 115 } catch (NullPointerException npe) { 116 // good 117 System.out.println("Test 8: " + npe.getMessage()); 118 } 119 120 try { 121 Subject.getSubject(null); 122 throw new SecurityException("test 9 failed"); 123 } catch (NullPointerException npe) { 124 // good 125 System.out.println("Test 9: " + npe.getMessage()); 126 } 127 128 try { 129 Subject.doAs(null, (java.security.PrivilegedAction)null); 130 throw new SecurityException("test 10 failed"); 131 } catch (NullPointerException npe) { 132 // good 133 System.out.println("Test 10: " + npe.getMessage()); 134 } 135 136 try { 137 Subject s = new Subject(); 138 s.getPrincipals(null); 139 throw new SecurityException("test 11 failed"); 140 } catch (NullPointerException npe) { 141 // good 142 System.out.println("Test 11: " + npe.getMessage()); 143 } 144 145 try { 146 Subject s = new Subject(); 147 s.getPrincipals().add(new javax.security.auth.x500.X500Principal 148 ("cn=test1")); 149 s.getPublicCredentials().add(new Format()); 150 s.getPrivateCredentials().add(new Format()); 151 System.out.println("Test 12, s.toString() = " + s.toString()); 152 } catch (Exception e) { 153 throw new SecurityException("test 12 failed: e.toString()", e); 154 } 155 156 try { 157 Subject s = new Subject(); 158 s.getPrincipals().add(new javax.security.auth.x500.X500Principal 159 ("cn=test1")); 160 s.setReadOnly(); 161 java.util.Iterator i = s.getPrincipals().iterator(); 162 i.next(); 163 i.remove(); 164 throw new SecurityException("test 13 failed"); 165 } catch (IllegalStateException ise) { 166 // good 167 System.out.println("Test 13: " + ise.getMessage()); 168 } 169 170 try { 171 Subject s = new Subject(); 172 s.getPrincipals().add(new Format()); 173 throw new SecurityException("test 14 failed"); 174 } catch (SecurityException se) { 175 // good 176 System.out.println("Test 14: " + se.getMessage()); 177 } 178 179 try { 180 Subject s = new Subject(); 181 s.getPrincipals((Class)Format.class).add(new Subject()); 182 throw new SecurityException("test 15 failed"); 183 } catch (SecurityException se) { 184 // good 185 System.out.println("Test 15: " + se.getMessage()); 186 } 187 188 try { 189 LoginContext lc = new LoginContext(null); 190 throw new SecurityException("test 16 failed"); 191 } catch (LoginException le) { 192 // good 193 System.out.println("Test 16: " + le.getMessage()); 194 } 195 196 try { 197 LoginContext lc = new LoginContext("nothing"); 198 throw new SecurityException("test 17 failed"); 199 } catch (LoginException le) { 200 // good 201 System.out.println("Test 17: " + le.getMessage()); 202 } 203 204 try { 205 LoginContext lc = new LoginContext("test", (Subject)null); 206 throw new SecurityException("test 18 failed"); 207 } catch (LoginException le) { 208 // good 209 System.out.println("Test 18: " + le.getMessage()); 210 } 211 212 try { 213 LoginContext lc = new LoginContext("test", (CallbackHandler)null); 214 throw new SecurityException("test 19 failed"); 215 } catch (LoginException le) { 216 // good 217 System.out.println("Test 19: " + le.getMessage()); 218 } 219 220 try { 221 LoginContext lc = new LoginContext("test"); 222 lc.logout(); 223 throw new SecurityException("test 20 failed"); 224 } catch (LoginException le) { 225 // good 226 System.out.println("Test 20: " + le.getMessage()); 227 } 228 229 try { 230 LoginContext lc = new LoginContext("test2"); 231 lc.login(); 232 throw new SecurityException("test 21 failed"); 233 } catch (LoginException le) { 234 // good 235 System.out.println("Test 21: " + le.getMessage()); 236 } 237 238 /*** TESTS FOR THE COM PACKAGE RESOURCES ***/ 239 240 try { 241 NTDomainPrincipal p = new NTDomainPrincipal(null); 242 throw new SecurityException("test 22 failed"); 243 } catch (NullPointerException e) { 244 // good 245 System.out.println("Test 22: " + e.getMessage()); 246 } 247 248 try { 249 NTDomainPrincipal p = new NTDomainPrincipal("test"); 250 System.out.println("NTDomainPrincipal = " + p.toString()); 251 } catch (Exception e) { 252 throw new SecurityException("test 23 failed"); 253 } 254 255 try { 256 NTNumericCredential p = new NTNumericCredential(1L); 257 System.out.println("NTNumericCredential = " + p.toString()); 258 } catch (Exception e) { 259 throw new SecurityException("test 24 failed"); 260 } 261 262 try { 263 NTSid p = new NTSid(""); 264 throw new SecurityException("test 25 failed"); 265 } catch (IllegalArgumentException e) { 266 // good 267 System.out.println("Test 25: " + e.getMessage()); 268 } 269 270 try { 271 NTSid p = new NTSid("test"); 272 System.out.println("NTSid = " + p.toString()); 273 } catch (Exception e) { 274 throw new SecurityException("test 26 failed"); 275 } 276 277 try { 278 NTSidDomainPrincipal p = new NTSidDomainPrincipal("test"); 279 System.out.println("NTSidDomainPrincipal = " + p.toString()); 280 } catch (Exception e) { 281 throw new SecurityException("test 27 failed"); 282 } 283 284 try { 285 NTSidGroupPrincipal p = new NTSidGroupPrincipal("test"); 286 System.out.println("NTSidGroupPrincipal = " + p.toString()); 287 } catch (Exception e) { 288 throw new SecurityException("test 28 failed"); 289 } 290 291 try { 292 NTSidPrimaryGroupPrincipal p = 293 new NTSidPrimaryGroupPrincipal("test"); 294 System.out.println("NTSidPrimaryGroupPrincipal = " + p.toString()); 295 } catch (Exception e) { 296 throw new SecurityException("test 29 failed"); 297 } 298 299 try { 300 NTSidUserPrincipal p = new NTSidUserPrincipal("test"); 301 System.out.println("NTSidUserPrincipal = " + p.toString()); 302 } catch (Exception e) { 303 throw new SecurityException("test 30 failed"); 304 } 305 306 try { 307 NTUserPrincipal p = new NTUserPrincipal("test"); 308 System.out.println("NTUserPrincipal = " + p.toString()); 309 } catch (Exception e) { 310 throw new SecurityException("test 31 failed"); 311 } 312 313 try { 314 UnixNumericGroupPrincipal p = new UnixNumericGroupPrincipal("0", 315 true); 316 System.out.println("NTPrimaryGroupPrincipal = " + p.toString()); 317 } catch (Exception e) { 318 throw new SecurityException("test 32 failed"); 319 } 320 321 try { 322 UnixNumericGroupPrincipal p = new UnixNumericGroupPrincipal("1", 323 false); 324 System.out.println("UnixNumericGroupPrincipal = " + p.toString()); 325 } catch (Exception e) { 326 throw new SecurityException("test 33 failed"); 327 } 328 329 try { 330 UnixNumericUserPrincipal p = new UnixNumericUserPrincipal("2"); 331 System.out.println("UnixNumericUserPrincipal = " + p.toString()); 332 } catch (Exception e) { 333 throw new SecurityException("test 34 failed"); 334 } 335 336 try { 337 UnixPrincipal p = new UnixPrincipal("test"); 338 System.out.println("UnixPrincipal = " + p.toString()); 339 } catch (Exception e) { 340 throw new SecurityException("test 35 failed"); 341 } 342 343 System.out.println("Format test succeeded"); 344 } 345} 346