Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/login/LoginContext/ModuleSubject.java
38859 views
/*1* Copyright (c) 2000, 2001, 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* @summary LoginContext doesn't reinit modules with new Subject27* if authentication fails28*29* @build ModuleSubject ModuleSubjectModule30* @run main/othervm -Djava.security.auth.login.config=file:${test.src}/ModuleSubject.config ModuleSubject31*/3233import javax.security.auth.*;34import javax.security.auth.login.*;35import java.security.Principal;3637public class ModuleSubject {3839public static void main(String[] args) {4041LoginContext lc = null;42try {43lc = new LoginContext("SampleLogin");44} catch (LoginException le) {45System.out.println46("ModuleSubject test failed - login construction failed");47throw new SecurityException(le.getMessage());48}4950// first attempt must fail51try {52lc.login();53throw new SecurityException54("ModuleSubject test failed: 1st login attempt did not fail!");55} catch (LoginException le) {56// good!57System.out.println58("Good: first attempt failed");59le.printStackTrace();60}6162if (lc.getSubject() != null) {63throw new SecurityException64("ModuleSubject test failed - " +65"Subject after failed attempt not null: " +66lc.getSubject().toString());67}6869// second attempt succeeds, and the correct subject comes back70try {71lc.login();72java.util.Set principals = lc.getSubject().getPrincipals();7374if (principals.size() != 1) {75throw new SecurityException("ModuleSubject test failed: " +76"corrupted subject");77}78java.util.Iterator i = principals.iterator();79while (i.hasNext()) {80Principal p = (Principal)i.next();81System.out.println("principal after authentication = " +82p.toString());83}84} catch (LoginException le) {85System.out.println86("ModuleSubject test failed - 2nd login attempt failed");87throw new SecurityException(le.getMessage());88}8990System.out.println("ModuleSubject test succeeded");91}92}939495