Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/PrivateCredentialPermission/MoreThenOnePrincipals.java
38854 views
/*1* Copyright (c) 2015, 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* @summary Tests with Subject.getPrivateCredentials to check permission checks with one or more principals.36* @run testng/othervm/policy=MoreThenOnePrincipals.policy MoreThenOnePrincipals37*/38public class MoreThenOnePrincipals {39private static final String[] CRED_VALUES =40new String[]{"testPrivateCredential-1", "testPrivateCredentials-2"};41private static final HashSet CREDS = new HashSet<>(Arrays.asList(CRED_VALUES));4243/**44* Policy file grants access to the private Credential,belonging to a45* Subject with at least two associated Principals:"com.sun.security.auth46* .NTUserPrincipal", with the name,"NTUserPrincipal-1", and47* "com.sun.security.auth.UnixPrincipal", with the name, "UnixPrincipals-1".48*49* For test1 and test2, subjects are associated with none or only one of50* principals mentioned above, SecurityException is expected.51* For test 3 and test 4, subjects are associated with two or more52* Principals (above principals are included), no exception is expected.53*54*/5556@Test(dataProvider = "Provider1", expectedExceptions = SecurityException.class)57public void test1(Subject s) {58s.getPrivateCredentials(String.class);59}6061@Test(dataProvider = "Provider1", expectedExceptions = SecurityException.class)62public void test2(Subject s) {63s.getPrivateCredentials().iterator().next();64}6566@Test(dataProvider = "Provider2")67public void test3(Subject s) {68s.getPrivateCredentials(String.class);69}7071@Test(dataProvider = "Provider2")72public void test4(Subject s) {73s.getPrivateCredentials().iterator().next();74}7576@DataProvider77public Object[][] Provider1() {78Subject s1 = new Subject(false, Collections.EMPTY_SET, Collections.EMPTY_SET, CREDS);79s1.getPrincipals().add(new NTUserPrincipal("NTUserPrincipal-2"));80Subject s2 = new Subject(false, Collections.EMPTY_SET, Collections.EMPTY_SET, CREDS);81s2.getPrincipals().add(new NTUserPrincipal("NTUserPrincipal-1"));82return new Object[][]{{s1}, {s2}};83}8485@DataProvider86public Object[][] Provider2() {87Subject s3 = new Subject(false, Collections.EMPTY_SET, Collections.EMPTY_SET, CREDS);88s3.getPrincipals().add(new NTUserPrincipal("NTUserPrincipal-1"));89s3.getPrincipals().add(new UnixPrincipal("UnixPrincipals-1"));90Subject s4 = new Subject(false, Collections.EMPTY_SET, Collections.EMPTY_SET, CREDS);91s4.getPrincipals().add(new NTUserPrincipal("NTUserPrincipal-1"));92s4.getPrincipals().add(new UnixPrincipal("UnixPrincipals-1"));93s4.getPrincipals().add(new UnixPrincipal("UnixPrincipals-2"));94return new Object[][]{{s3}, {s4}};95}9697}9899100