Path: blob/master/test/jdk/javax/security/auth/login/LoginContext/ModuleSubject.java
51695 views
/*1* Copyright (c) 2000, 2017, 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* @bug 437810026* @modules jdk.security.auth27* @summary LoginContext doesn't reinit modules with new Subject28* if authentication fails29*30* @build ModuleSubject ModuleSubjectModule31* @run main/othervm -Djava.security.auth.login.config=file:${test.src}/ModuleSubject.config ModuleSubject32*/3334import java.security.Principal;35import javax.security.auth.login.LoginContext;36import javax.security.auth.login.LoginException;3738public class ModuleSubject {3940public static void main(String[] args) {4142LoginContext lc = null;43try {44lc = new LoginContext("SampleLogin");45} catch (LoginException le) {46System.out.println47("ModuleSubject test failed - login construction failed");48throw new SecurityException(le.getMessage());49}5051// first attempt must fail52try {53lc.login();54throw new SecurityException55("ModuleSubject test failed: 1st login attempt did not fail!");56} catch (LoginException le) {57// good!58System.out.println59("Good: first attempt failed");60le.printStackTrace();61}6263if (lc.getSubject() != null) {64throw new SecurityException65("ModuleSubject test failed - " +66"Subject after failed attempt not null: " +67lc.getSubject().toString());68}6970// second attempt succeeds, and the correct subject comes back71try {72lc.login();73java.util.Set principals = lc.getSubject().getPrincipals();7475if (principals.size() != 1) {76throw new SecurityException("ModuleSubject test failed: " +77"corrupted subject");78}79java.util.Iterator i = principals.iterator();80while (i.hasNext()) {81Principal p = (Principal)i.next();82System.out.println("principal after authentication = " +83p.toString());84}85} catch (LoginException le) {86System.out.println87("ModuleSubject test failed - 2nd login attempt failed");88throw new SecurityException(le.getMessage());89}9091System.out.println("ModuleSubject test succeeded");92}93}949596