Skip to content

Commit 90f9a70

Browse files
fgualliniXueleiFan
authored andcommitted
8255546: Missing coverage for javax.smartcardio.CardPermission and ResponseAPDU
Reviewed-by: xuelei
1 parent 1c0b490 commit 90f9a70

File tree

2 files changed

+110
-40
lines changed

2 files changed

+110
-40
lines changed

test/jdk/javax/smartcardio/ResponseAPDUTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,8 +23,8 @@
2323

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

4647
@BeforeClass
4748
public static void setUpClass() throws Exception {
@@ -57,6 +58,9 @@ public static void setUpClass() throws Exception {
5758
expectedSw1 = R1[apduLen - 2] & 0xff;
5859
expectedSw2 = R1[apduLen - 1] & 0xff;
5960
expectedSw = (expectedSw1 << 8) | expectedSw2;
61+
62+
expectedToString = "ResponseAPDU: " + R1.length +
63+
" bytes, SW=" + Integer.toHexString(expectedSw);
6064
}
6165

6266
@Test
@@ -67,5 +71,6 @@ public static void test() {
6771
assertEquals(RAPDU.getSW(), expectedSw);
6872
assertEquals(RAPDU.getSW1(), expectedSw1);
6973
assertEquals(RAPDU.getSW2(), expectedSw2);
74+
assertEquals(RAPDU.toString(), expectedToString);
7075
}
7176
}
Lines changed: 102 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,60 +23,125 @@
2323

2424
/**
2525
* @test
26-
* @bug 6293767 6469513
26+
* @bug 6293767 6469513 8255546
2727
* @summary Test for the CardPermission class
2828
* @author Andreas Sterbenz
29+
* @run testng TestCardPermission
2930
*/
3031

32+
import org.testng.annotations.DataProvider;
33+
import org.testng.annotations.Test;
34+
3135
import javax.smartcardio.*;
36+
import java.security.Permission;
37+
38+
import static org.testng.Assert.*;
3239

3340
public class TestCardPermission {
3441

35-
public static void main(String[] args) throws Exception {
36-
CardPermission perm;
37-
38-
test("*");
39-
test("connect");
40-
test("reset");
41-
test("exclusive");
42-
test("transmitControl");
43-
test("getBasicChannel");
44-
test("openLogicalChannel");
45-
46-
test("connect,reset");
47-
test("Reset,coNnect", "connect,reset");
48-
test("exclusive,*,connect", "*");
49-
test("connect,reset,exclusive,transmitControl,getBasicChannel,openLogicalChannel", "*");
50-
test(null, null);
51-
52-
invalid("");
53-
invalid("foo");
54-
invalid("connect, reset");
55-
invalid("connect,,reset");
56-
invalid("connect,");
57-
invalid(",connect");
42+
@DataProvider(name = "actions")
43+
Object[][] getActions() {
44+
return new Object[][]{
45+
{"*"},
46+
{"connect"},
47+
{"reset"},
48+
{"exclusive"},
49+
{"transmitControl"},
50+
{"getBasicChannel"},
51+
{"openLogicalChannel"},
52+
{"connect,reset"}
53+
};
5854
}
5955

60-
private static void invalid(String s) throws Exception {
61-
try {
62-
CardPermission c = new CardPermission("*", s);
63-
throw new Exception("Created invalid action: " + c);
64-
} catch (IllegalArgumentException e) {
65-
System.out.println("OK: " + e);
66-
}
56+
@DataProvider(name = "actionsCanon")
57+
Object[][] getActionsCanon() {
58+
return new Object[][]{
59+
{"Reset,coNnect", "connect,reset"},
60+
{"exclusive,*,connect", "*"},
61+
{"connect,reset,exclusive,transmitControl,getBasicChannel,openLogicalChannel", "*"},
62+
{null, null}
63+
};
64+
}
65+
66+
@DataProvider(name = "invalidActions")
67+
Object[][] getInvalidActions() {
68+
return new Object[][]{
69+
{""},
70+
{"foo"},
71+
{"connect, reset"},
72+
{"connect,,reset"},
73+
{"connect,"},
74+
{",connect"}
75+
};
76+
}
77+
78+
@Test(dataProvider = "actions")
79+
public void testActions(String actions) throws Exception {
80+
testActions(actions, actions);
81+
}
82+
83+
@Test(dataProvider = "actionsCanon")
84+
public void testActionsCanon(String actions, String canon) throws Exception {
85+
testActions(actions, canon);
86+
}
87+
88+
@Test(dataProvider = "invalidActions")
89+
public void testInvalidActions(String actions) {
90+
assertThrows(IllegalArgumentException.class, () -> new CardPermission("*", actions));
91+
}
92+
93+
// Should return false since p2 is not a CardPermission instance
94+
@Test
95+
public void testImpliesNotCardPermissionInstance() {
96+
String actions = "connect";
97+
CardPermission p1 = new CardPermission("*", actions);
98+
Permission p2 = new Permission(actions) {
99+
@Override public boolean implies(Permission permission) { return false; }
100+
@Override public boolean equals(Object obj) { return false; }
101+
@Override public int hashCode() { return 0; }
102+
@Override public String getActions() { return null; }
103+
};
104+
assertFalse(p1.implies(p2));
105+
}
106+
107+
// Should return false since p2 actions are not a subset of p1
108+
@Test
109+
public void testImpliesNotSubsetCardPermission() {
110+
CardPermission p1 = new CardPermission("*", "connect,reset");
111+
CardPermission p2 = new CardPermission("*", "transmitControl");
112+
assertFalse(p1.implies(p2));
67113
}
68114

69-
private static void test(String actions) throws Exception {
70-
test(actions, actions);
115+
// Should return true since p1 name is * and p2 actions are a subset of p1
116+
@Test
117+
public void testImpliesNameEqualsAll() {
118+
CardPermission p1 = new CardPermission("*", "connect,reset");
119+
CardPermission p2 = new CardPermission("None", "reset");
120+
assertTrue(p1.implies(p2));
71121
}
72122

73-
private static void test(String actions, String canon) throws Exception {
123+
// Should return true since p1 and p2 names are equal
124+
@Test
125+
public void testImpliesBothSameNameNotAll() {
126+
CardPermission p1 = new CardPermission("None", "connect,reset");
127+
CardPermission p2 = new CardPermission("None", "reset");
128+
assertTrue(p1.implies(p2));
129+
}
130+
131+
// Should return false since p1 and p2 names are not equal
132+
@Test
133+
public void testImpliesNameNotSameNotAll() {
134+
CardPermission p1 = new CardPermission("None", "connect,reset");
135+
CardPermission p2 = new CardPermission("Other", "reset");
136+
assertFalse(p1.implies(p2));
137+
}
138+
139+
private void testActions(String actions, String canon) throws Exception {
74140
CardPermission p = new CardPermission("*", actions);
75141
System.out.println(p);
76142
String a = p.getActions();
77-
if (canon != null && canon.equals(a) == false) {
143+
if (canon != null && !canon.equals(a)) {
78144
throw new Exception("Canonical actions mismatch: " + canon + " != " + a);
79145
}
80146
}
81-
82147
}

0 commit comments

Comments
 (0)