Path: blob/master/test/jdk/javax/security/auth/PrivateCredentialPermission/MoreThenOnePrincipals.java
41153 views
/*1* Copyright (c) 2015, 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*/2223import com.sun.security.auth.NTUserPrincipal;24import com.sun.security.auth.UnixPrincipal;25import java.util.Arrays;26import java.util.Collections;27import java.util.HashSet;28import javax.security.auth.Subject;29import org.testng.annotations.DataProvider;30import org.testng.annotations.Test;3132/*33* @test34* @bug 805040935* @modules jdk.security.auth36* @summary Tests with Subject.getPrivateCredentials to check permission checks with one or more principals.37* @run testng/othervm/policy=MoreThenOnePrincipals.policy MoreThenOnePrincipals38*/39public class MoreThenOnePrincipals {40private static final String[] CRED_VALUES =41new String[]{"testPrivateCredential-1", "testPrivateCredentials-2"};42private static final HashSet CREDS = new HashSet<>(Arrays.asList(CRED_VALUES));4344/**45* Policy file grants access to the private Credential,belonging to a46* Subject with at least two associated Principals:"com.sun.security.auth47* .NTUserPrincipal", with the name,"NTUserPrincipal-1", and48* "com.sun.security.auth.UnixPrincipal", with the name, "UnixPrincipals-1".49*50* For test1 and test2, subjects are associated with none or only one of51* principals mentioned above, SecurityException is expected.52* For test 3 and test 4, subjects are associated with two or more53* Principals (above principals are included), no exception is expected.54*55*/5657@Test(dataProvider = "Provider1", expectedExceptions = SecurityException.class)58public void test1(Subject s) {59s.getPrivateCredentials(String.class);60}6162@Test(dataProvider = "Provider1", expectedExceptions = SecurityException.class)63public void test2(Subject s) {64s.getPrivateCredentials().iterator().next();65}6667@Test(dataProvider = "Provider2")68public void test3(Subject s) {69s.getPrivateCredentials(String.class);70}7172@Test(dataProvider = "Provider2")73public void test4(Subject s) {74s.getPrivateCredentials().iterator().next();75}7677@DataProvider78public Object[][] Provider1() {79Subject s1 = new Subject(false, Collections.EMPTY_SET, Collections.EMPTY_SET, CREDS);80s1.getPrincipals().add(new NTUserPrincipal("NTUserPrincipal-2"));81Subject s2 = new Subject(false, Collections.EMPTY_SET, Collections.EMPTY_SET, CREDS);82s2.getPrincipals().add(new NTUserPrincipal("NTUserPrincipal-1"));83return new Object[][]{{s1}, {s2}};84}8586@DataProvider87public Object[][] Provider2() {88Subject s3 = new Subject(false, Collections.EMPTY_SET, Collections.EMPTY_SET, CREDS);89s3.getPrincipals().add(new NTUserPrincipal("NTUserPrincipal-1"));90s3.getPrincipals().add(new UnixPrincipal("UnixPrincipals-1"));91Subject s4 = new Subject(false, Collections.EMPTY_SET, Collections.EMPTY_SET, CREDS);92s4.getPrincipals().add(new NTUserPrincipal("NTUserPrincipal-1"));93s4.getPrincipals().add(new UnixPrincipal("UnixPrincipals-1"));94s4.getPrincipals().add(new UnixPrincipal("UnixPrincipals-2"));95return new Object[][]{{s3}, {s4}};96}9798}99100101