Path: blob/master/test/jdk/javax/smartcardio/TestCardPermission.java
41144 views
/*1* Copyright (c) 2005, 2020, 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 6293767 6469513 825554626* @summary Test for the CardPermission class27* @author Andreas Sterbenz28* @run testng TestCardPermission29*/3031import org.testng.annotations.DataProvider;32import org.testng.annotations.Test;3334import javax.smartcardio.*;35import java.security.Permission;3637import static org.testng.Assert.*;3839public class TestCardPermission {4041@DataProvider(name = "actions")42Object[][] getActions() {43return new Object[][]{44{"*"},45{"connect"},46{"reset"},47{"exclusive"},48{"transmitControl"},49{"getBasicChannel"},50{"openLogicalChannel"},51{"connect,reset"}52};53}5455@DataProvider(name = "actionsCanon")56Object[][] getActionsCanon() {57return new Object[][]{58{"Reset,coNnect", "connect,reset"},59{"exclusive,*,connect", "*"},60{"connect,reset,exclusive,transmitControl,getBasicChannel,openLogicalChannel", "*"},61{null, null}62};63}6465@DataProvider(name = "invalidActions")66Object[][] getInvalidActions() {67return new Object[][]{68{""},69{"foo"},70{"connect, reset"},71{"connect,,reset"},72{"connect,"},73{",connect"}74};75}7677@Test(dataProvider = "actions")78public void testActions(String actions) throws Exception {79testActions(actions, actions);80}8182@Test(dataProvider = "actionsCanon")83public void testActionsCanon(String actions, String canon) throws Exception {84testActions(actions, canon);85}8687@Test(dataProvider = "invalidActions")88public void testInvalidActions(String actions) {89assertThrows(IllegalArgumentException.class, () -> new CardPermission("*", actions));90}9192// Should return false since p2 is not a CardPermission instance93@Test94public void testImpliesNotCardPermissionInstance() {95String actions = "connect";96CardPermission p1 = new CardPermission("*", actions);97Permission p2 = new Permission(actions) {98@Override public boolean implies(Permission permission) { return false; }99@Override public boolean equals(Object obj) { return false; }100@Override public int hashCode() { return 0; }101@Override public String getActions() { return null; }102};103assertFalse(p1.implies(p2));104}105106// Should return false since p2 actions are not a subset of p1107@Test108public void testImpliesNotSubsetCardPermission() {109CardPermission p1 = new CardPermission("*", "connect,reset");110CardPermission p2 = new CardPermission("*", "transmitControl");111assertFalse(p1.implies(p2));112}113114// Should return true since p1 name is * and p2 actions are a subset of p1115@Test116public void testImpliesNameEqualsAll() {117CardPermission p1 = new CardPermission("*", "connect,reset");118CardPermission p2 = new CardPermission("None", "reset");119assertTrue(p1.implies(p2));120}121122// Should return true since p1 and p2 names are equal123@Test124public void testImpliesBothSameNameNotAll() {125CardPermission p1 = new CardPermission("None", "connect,reset");126CardPermission p2 = new CardPermission("None", "reset");127assertTrue(p1.implies(p2));128}129130// Should return false since p1 and p2 names are not equal131@Test132public void testImpliesNameNotSameNotAll() {133CardPermission p1 = new CardPermission("None", "connect,reset");134CardPermission p2 = new CardPermission("Other", "reset");135assertFalse(p1.implies(p2));136}137138private void testActions(String actions, String canon) throws Exception {139CardPermission p = new CardPermission("*", actions);140System.out.println(p);141String a = p.getActions();142if (canon != null && !canon.equals(a)) {143throw new Exception("Canonical actions mismatch: " + canon + " != " + a);144}145}146}147148149