Path: blob/master/test/jdk/sun/security/util/Resources/Format.java
41152 views
/*1* Copyright (c) 2000, 2012, 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* @test25* @author Charlie Lai26* @bug 439474627* @summary Resources should not be loaded until necessar28* @run main/othervm/policy=Format.policy -Djava.security.auth.login.config=file:${test.src}/Format.config Format29*30*/3132import javax.security.auth.*;33import javax.security.auth.login.*;34import javax.security.auth.callback.*;35import java.io.*;36import java.util.*;3738import com.sun.security.auth.*;3940public class Format41implements java.io.Serializable, java.security.Principal42{4344public String getName() { return null; }4546public static void main(String[] args) {4748try {49PrivateCredentialPermission pcp = new PrivateCredentialPermission50("test", "write");51throw new SecurityException("test 1 failed");52} catch (IllegalArgumentException iae) {53// good54System.out.println("Test 1: " + iae.getMessage());55}5657try {58PrivateCredentialPermission pcp = new PrivateCredentialPermission59("Format", "read");60throw new SecurityException("test 2 failed");61} catch (IllegalArgumentException iae) {62// good63System.out.println("Test 2: " + iae.getMessage());64}6566try {67PrivateCredentialPermission pcp = new PrivateCredentialPermission68("Format Format", "read");69throw new SecurityException("test 3 failed");70} catch (IllegalArgumentException iae) {71// good72System.out.println("Test 3: " + iae.getMessage());73}7475try {76PrivateCredentialPermission pcp = new PrivateCredentialPermission77("Format Format Format", "read");78throw new SecurityException("test 4 failed");79} catch (IllegalArgumentException iae) {80// good81System.out.println("Test 4: " + iae.getMessage());82}8384try {85PrivateCredentialPermission pcp = new PrivateCredentialPermission86("Format Format \"Format", "read");87throw new SecurityException("test 5 failed");88} catch (IllegalArgumentException iae) {89// good90System.out.println("Test 5: " + iae.getMessage());91}9293try {94PrivateCredentialPermission pcp = new PrivateCredentialPermission95("Format * \"Format\"", "read");96throw new SecurityException("test 6 failed");97} catch (IllegalArgumentException iae) {98// good99System.out.println("Test 6: " + iae.getMessage());100}101102try {103javax.security.auth.x500.X500Principal p =104new javax.security.auth.x500.X500Principal((String)null);105throw new SecurityException("test 7 failed");106} catch (NullPointerException npe) {107// good108System.out.println("Test 7: " + npe.getMessage());109}110111try {112Subject s = new Subject(false, null, null, null);113throw new SecurityException("test 8 failed");114} catch (NullPointerException npe) {115// good116System.out.println("Test 8: " + npe.getMessage());117}118119try {120Subject.getSubject(null);121throw new SecurityException("test 9 failed");122} catch (NullPointerException npe) {123// good124System.out.println("Test 9: " + npe.getMessage());125}126127try {128Subject.doAs(null, (java.security.PrivilegedAction)null);129throw new SecurityException("test 10 failed");130} catch (NullPointerException npe) {131// good132System.out.println("Test 10: " + npe.getMessage());133}134135try {136Subject s = new Subject();137s.getPrincipals(null);138throw new SecurityException("test 11 failed");139} catch (NullPointerException npe) {140// good141System.out.println("Test 11: " + npe.getMessage());142}143144try {145Subject s = new Subject();146s.getPrincipals().add(new javax.security.auth.x500.X500Principal147("cn=test1"));148s.getPublicCredentials().add(new Format());149s.getPrivateCredentials().add(new Format());150System.out.println("Test 12, s.toString() = " + s.toString());151} catch (Exception e) {152throw new SecurityException("test 12 failed: e.toString()", e);153}154155try {156Subject s = new Subject();157s.getPrincipals().add(new javax.security.auth.x500.X500Principal158("cn=test1"));159s.setReadOnly();160java.util.Iterator i = s.getPrincipals().iterator();161i.next();162i.remove();163throw new SecurityException("test 13 failed");164} catch (IllegalStateException ise) {165// good166System.out.println("Test 13: " + ise.getMessage());167}168169try {170Subject s = new Subject();171s.getPrincipals().add(new Format());172throw new SecurityException("test 14 failed");173} catch (SecurityException se) {174// good175System.out.println("Test 14: " + se.getMessage());176}177178try {179Subject s = new Subject();180s.getPrincipals((Class)Format.class).add(new Subject());181throw new SecurityException("test 15 failed");182} catch (SecurityException se) {183// good184System.out.println("Test 15: " + se.getMessage());185}186187try {188LoginContext lc = new LoginContext(null);189throw new SecurityException("test 16 failed");190} catch (LoginException le) {191// good192System.out.println("Test 16: " + le.getMessage());193}194195try {196LoginContext lc = new LoginContext("nothing");197throw new SecurityException("test 17 failed");198} catch (LoginException le) {199// good200System.out.println("Test 17: " + le.getMessage());201}202203try {204LoginContext lc = new LoginContext("test", (Subject)null);205throw new SecurityException("test 18 failed");206} catch (LoginException le) {207// good208System.out.println("Test 18: " + le.getMessage());209}210211try {212LoginContext lc = new LoginContext("test", (CallbackHandler)null);213throw new SecurityException("test 19 failed");214} catch (LoginException le) {215// good216System.out.println("Test 19: " + le.getMessage());217}218219try {220LoginContext lc = new LoginContext("test");221lc.logout();222throw new SecurityException("test 20 failed");223} catch (LoginException le) {224// good225System.out.println("Test 20: " + le.getMessage());226}227228try {229LoginContext lc = new LoginContext("test2");230lc.login();231throw new SecurityException("test 21 failed");232} catch (LoginException le) {233// good234System.out.println("Test 21: " + le.getMessage());235}236237/*** TESTS FOR THE COM PACKAGE RESOURCES ***/238239try {240NTDomainPrincipal p = new NTDomainPrincipal(null);241throw new SecurityException("test 22 failed");242} catch (NullPointerException e) {243// good244System.out.println("Test 22: " + e.getMessage());245}246247try {248NTDomainPrincipal p = new NTDomainPrincipal("test");249System.out.println("NTDomainPrincipal = " + p.toString());250} catch (Exception e) {251throw new SecurityException("test 23 failed");252}253254try {255NTNumericCredential p = new NTNumericCredential(1L);256System.out.println("NTNumericCredential = " + p.toString());257} catch (Exception e) {258throw new SecurityException("test 24 failed");259}260261try {262NTSid p = new NTSid("");263throw new SecurityException("test 25 failed");264} catch (IllegalArgumentException e) {265// good266System.out.println("Test 25: " + e.getMessage());267}268269try {270NTSid p = new NTSid("test");271System.out.println("NTSid = " + p.toString());272} catch (Exception e) {273throw new SecurityException("test 26 failed");274}275276try {277NTSidDomainPrincipal p = new NTSidDomainPrincipal("test");278System.out.println("NTSidDomainPrincipal = " + p.toString());279} catch (Exception e) {280throw new SecurityException("test 27 failed");281}282283try {284NTSidGroupPrincipal p = new NTSidGroupPrincipal("test");285System.out.println("NTSidGroupPrincipal = " + p.toString());286} catch (Exception e) {287throw new SecurityException("test 28 failed");288}289290try {291NTSidPrimaryGroupPrincipal p =292new NTSidPrimaryGroupPrincipal("test");293System.out.println("NTSidPrimaryGroupPrincipal = " + p.toString());294} catch (Exception e) {295throw new SecurityException("test 29 failed");296}297298try {299NTSidUserPrincipal p = new NTSidUserPrincipal("test");300System.out.println("NTSidUserPrincipal = " + p.toString());301} catch (Exception e) {302throw new SecurityException("test 30 failed");303}304305try {306NTUserPrincipal p = new NTUserPrincipal("test");307System.out.println("NTUserPrincipal = " + p.toString());308} catch (Exception e) {309throw new SecurityException("test 31 failed");310}311312try {313UnixNumericGroupPrincipal p = new UnixNumericGroupPrincipal("0",314true);315System.out.println("NTPrimaryGroupPrincipal = " + p.toString());316} catch (Exception e) {317throw new SecurityException("test 32 failed");318}319320try {321UnixNumericGroupPrincipal p = new UnixNumericGroupPrincipal("1",322false);323System.out.println("UnixNumericGroupPrincipal = " + p.toString());324} catch (Exception e) {325throw new SecurityException("test 33 failed");326}327328try {329UnixNumericUserPrincipal p = new UnixNumericUserPrincipal("2");330System.out.println("UnixNumericUserPrincipal = " + p.toString());331} catch (Exception e) {332throw new SecurityException("test 34 failed");333}334335try {336UnixPrincipal p = new UnixPrincipal("test");337System.out.println("UnixPrincipal = " + p.toString());338} catch (Exception e) {339throw new SecurityException("test 35 failed");340}341342System.out.println("Format test succeeded");343}344}345346347