|
1 | 1 | /*
|
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
23 | 23 |
|
24 | 24 | /**
|
25 | 25 | * @test
|
26 |
| - * @bug 6293767 6469513 |
| 26 | + * @bug 6293767 6469513 8255546 |
27 | 27 | * @summary Test for the CardPermission class
|
28 | 28 | * @author Andreas Sterbenz
|
| 29 | + * @run testng TestCardPermission |
29 | 30 | */
|
30 | 31 |
|
| 32 | +import org.testng.annotations.DataProvider; |
| 33 | +import org.testng.annotations.Test; |
| 34 | + |
31 | 35 | import javax.smartcardio.*;
|
| 36 | +import java.security.Permission; |
| 37 | + |
| 38 | +import static org.testng.Assert.*; |
32 | 39 |
|
33 | 40 | public class TestCardPermission {
|
34 | 41 |
|
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 | + }; |
58 | 54 | }
|
59 | 55 |
|
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)); |
67 | 113 | }
|
68 | 114 |
|
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)); |
71 | 121 | }
|
72 | 122 |
|
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 { |
74 | 140 | CardPermission p = new CardPermission("*", actions);
|
75 | 141 | System.out.println(p);
|
76 | 142 | String a = p.getActions();
|
77 |
| - if (canon != null && canon.equals(a) == false) { |
| 143 | + if (canon != null && !canon.equals(a)) { |
78 | 144 | throw new Exception("Canonical actions mismatch: " + canon + " != " + a);
|
79 | 145 | }
|
80 | 146 | }
|
81 |
| - |
82 | 147 | }
|
0 commit comments