Skip to content

Commit

Permalink
8255546: Missing coverage for javax.smartcardio.CardPermission and Re…
Browse files Browse the repository at this point in the history
…sponseAPDU

Backport-of: 90f9a70
  • Loading branch information
Amos Shi authored and GoeLin committed Dec 4, 2023
1 parent b88668b commit 5c92da0
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 40 deletions.
11 changes: 8 additions & 3 deletions test/jdk/javax/smartcardio/ResponseAPDUTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,8 +23,8 @@

/*
* @test
* @bug 8049021
* @summary Construct ResponseAPDU from byte array and check NR< SW, SW1 and SW2
* @bug 8049021 8255546
* @summary Construct ResponseAPDU from byte array and check NR< SW, SW1, SW2 and toString
* @run testng ResponseAPDUTest
*/
import javax.smartcardio.ResponseAPDU;
Expand All @@ -42,6 +42,7 @@ public class ResponseAPDUTest {
static final ResponseAPDU RAPDU = new ResponseAPDU(R1);
static byte[] expectedData;
static int expectedNr, expectedSw1, expectedSw2, expectedSw;
static String expectedToString;

@BeforeClass
public static void setUpClass() throws Exception {
Expand All @@ -57,6 +58,9 @@ public static void setUpClass() throws Exception {
expectedSw1 = R1[apduLen - 2] & 0xff;
expectedSw2 = R1[apduLen - 1] & 0xff;
expectedSw = (expectedSw1 << 8) | expectedSw2;

expectedToString = "ResponseAPDU: " + R1.length +
" bytes, SW=" + Integer.toHexString(expectedSw);
}

@Test
Expand All @@ -67,5 +71,6 @@ public static void test() {
assertEquals(RAPDU.getSW(), expectedSw);
assertEquals(RAPDU.getSW1(), expectedSw1);
assertEquals(RAPDU.getSW2(), expectedSw2);
assertEquals(RAPDU.toString(), expectedToString);
}
}
139 changes: 102 additions & 37 deletions test/jdk/javax/smartcardio/TestCardPermission.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,60 +23,125 @@

/**
* @test
* @bug 6293767 6469513
* @bug 6293767 6469513 8255546
* @summary Test for the CardPermission class
* @author Andreas Sterbenz
* @run testng TestCardPermission
*/

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import javax.smartcardio.*;
import java.security.Permission;

import static org.testng.Assert.*;

public class TestCardPermission {

public static void main(String[] args) throws Exception {
CardPermission perm;

test("*");
test("connect");
test("reset");
test("exclusive");
test("transmitControl");
test("getBasicChannel");
test("openLogicalChannel");

test("connect,reset");
test("Reset,coNnect", "connect,reset");
test("exclusive,*,connect", "*");
test("connect,reset,exclusive,transmitControl,getBasicChannel,openLogicalChannel", "*");
test(null, null);

invalid("");
invalid("foo");
invalid("connect, reset");
invalid("connect,,reset");
invalid("connect,");
invalid(",connect");
@DataProvider(name = "actions")
Object[][] getActions() {
return new Object[][]{
{"*"},
{"connect"},
{"reset"},
{"exclusive"},
{"transmitControl"},
{"getBasicChannel"},
{"openLogicalChannel"},
{"connect,reset"}
};
}

private static void invalid(String s) throws Exception {
try {
CardPermission c = new CardPermission("*", s);
throw new Exception("Created invalid action: " + c);
} catch (IllegalArgumentException e) {
System.out.println("OK: " + e);
}
@DataProvider(name = "actionsCanon")
Object[][] getActionsCanon() {
return new Object[][]{
{"Reset,coNnect", "connect,reset"},
{"exclusive,*,connect", "*"},
{"connect,reset,exclusive,transmitControl,getBasicChannel,openLogicalChannel", "*"},
{null, null}
};
}

@DataProvider(name = "invalidActions")
Object[][] getInvalidActions() {
return new Object[][]{
{""},
{"foo"},
{"connect, reset"},
{"connect,,reset"},
{"connect,"},
{",connect"}
};
}

@Test(dataProvider = "actions")
public void testActions(String actions) throws Exception {
testActions(actions, actions);
}

@Test(dataProvider = "actionsCanon")
public void testActionsCanon(String actions, String canon) throws Exception {
testActions(actions, canon);
}

@Test(dataProvider = "invalidActions")
public void testInvalidActions(String actions) {
assertThrows(IllegalArgumentException.class, () -> new CardPermission("*", actions));
}

// Should return false since p2 is not a CardPermission instance
@Test
public void testImpliesNotCardPermissionInstance() {
String actions = "connect";
CardPermission p1 = new CardPermission("*", actions);
Permission p2 = new Permission(actions) {
@Override public boolean implies(Permission permission) { return false; }
@Override public boolean equals(Object obj) { return false; }
@Override public int hashCode() { return 0; }
@Override public String getActions() { return null; }
};
assertFalse(p1.implies(p2));
}

// Should return false since p2 actions are not a subset of p1
@Test
public void testImpliesNotSubsetCardPermission() {
CardPermission p1 = new CardPermission("*", "connect,reset");
CardPermission p2 = new CardPermission("*", "transmitControl");
assertFalse(p1.implies(p2));
}

private static void test(String actions) throws Exception {
test(actions, actions);
// Should return true since p1 name is * and p2 actions are a subset of p1
@Test
public void testImpliesNameEqualsAll() {
CardPermission p1 = new CardPermission("*", "connect,reset");
CardPermission p2 = new CardPermission("None", "reset");
assertTrue(p1.implies(p2));
}

private static void test(String actions, String canon) throws Exception {
// Should return true since p1 and p2 names are equal
@Test
public void testImpliesBothSameNameNotAll() {
CardPermission p1 = new CardPermission("None", "connect,reset");
CardPermission p2 = new CardPermission("None", "reset");
assertTrue(p1.implies(p2));
}

// Should return false since p1 and p2 names are not equal
@Test
public void testImpliesNameNotSameNotAll() {
CardPermission p1 = new CardPermission("None", "connect,reset");
CardPermission p2 = new CardPermission("Other", "reset");
assertFalse(p1.implies(p2));
}

private void testActions(String actions, String canon) throws Exception {
CardPermission p = new CardPermission("*", actions);
System.out.println(p);
String a = p.getActions();
if (canon != null && canon.equals(a) == false) {
if (canon != null && !canon.equals(a)) {
throw new Exception("Canonical actions mismatch: " + canon + " != " + a);
}
}

}

1 comment on commit 5c92da0

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.