author | wetmore |
Tue, 18 Mar 2014 15:04:59 -0700 | |
changeset 23357 | 7225e28e91dd |
parent 5506 | 202f599c92aa |
child 25656 | 676435b3c625 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23357
7225e28e91dd
8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 6239117 6445367 |
|
27 |
* @summary test that CardTerminals.waitForCard() works |
|
28 |
* @author Andreas Sterbenz |
|
23357
7225e28e91dd
8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents:
5506
diff
changeset
|
29 |
* @run main/manual TestMultiplePresent |
2 | 30 |
*/ |
31 |
||
23357
7225e28e91dd
8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents:
5506
diff
changeset
|
32 |
// This test requires special hardware. |
7225e28e91dd
8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents:
5506
diff
changeset
|
33 |
|
2 | 34 |
import java.io.*; |
35 |
import java.util.*; |
|
36 |
||
37 |
import javax.smartcardio.*; |
|
38 |
import static javax.smartcardio.CardTerminals.State.*; |
|
39 |
||
40 |
public class TestMultiplePresent { |
|
41 |
||
42 |
public static void main(String[] args) throws Exception { |
|
43 |
Utils.setLibrary(args); |
|
44 |
TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null); |
|
45 |
System.out.println(factory); |
|
46 |
||
47 |
CardTerminals terminals = factory.terminals(); |
|
48 |
List<CardTerminal> list = terminals.list(); |
|
49 |
System.out.println("Terminals: " + list); |
|
50 |
boolean multipleReaders = true; |
|
51 |
if (list.size() < 2) { |
|
52 |
if (list.isEmpty()) { |
|
53 |
throw new Exception("no terminals"); |
|
54 |
} |
|
55 |
System.out.println("Only one reader present, using simplified test"); |
|
56 |
multipleReaders = false; |
|
57 |
} |
|
58 |
||
59 |
while (true) { |
|
60 |
boolean present = false; |
|
61 |
for (CardTerminal terminal : list) { |
|
62 |
present |= terminal.isCardPresent(); |
|
63 |
} |
|
64 |
if (present == false) { |
|
65 |
break; |
|
66 |
} |
|
67 |
System.out.println("*** Remove all cards!"); |
|
68 |
Thread.sleep(1000); |
|
69 |
} |
|
70 |
System.out.println("OK"); |
|
71 |
||
72 |
List<CardTerminal> result; |
|
73 |
||
74 |
result = terminals.list(CARD_PRESENT); |
|
75 |
if (result.size() != 0) { |
|
76 |
throw new Exception("List not empty: " + result); |
|
77 |
} |
|
78 |
||
79 |
if (terminals.waitForChange(1000)) { |
|
80 |
throw new Exception("no timeout"); |
|
81 |
} |
|
82 |
if (terminals.waitForChange(1000)) { |
|
83 |
throw new Exception("no timeout"); |
|
84 |
} |
|
85 |
result = terminals.list(CARD_PRESENT); |
|
86 |
if (result.size() != 0) { |
|
87 |
throw new Exception("List not empty: " + result); |
|
88 |
} |
|
89 |
||
90 |
System.out.println("*** Insert card!"); |
|
91 |
terminals.waitForChange(); |
|
92 |
||
93 |
result = terminals.list(CARD_INSERTION); |
|
94 |
System.out.println(result); |
|
95 |
if (result.size() != 1) { |
|
96 |
throw new Exception("no card present"); |
|
97 |
} |
|
98 |
CardTerminal t1 = result.get(0); |
|
99 |
||
100 |
result = terminals.list(CARD_INSERTION); |
|
101 |
System.out.println(result); |
|
102 |
if ((result.size() != 1) || (result.get(0) != t1)) { |
|
103 |
throw new Exception("no card present"); |
|
104 |
} |
|
105 |
||
106 |
if (terminals.list(CARD_REMOVAL).size() != 0) { |
|
107 |
throw new Exception("List not empty: " + result); |
|
108 |
} |
|
109 |
if (terminals.list(CARD_REMOVAL).size() != 0) { |
|
110 |
throw new Exception("List not empty: " + result); |
|
111 |
} |
|
112 |
||
113 |
||
114 |
if (terminals.waitForChange(1000)) { |
|
115 |
throw new Exception("no timeout"); |
|
116 |
} |
|
117 |
||
118 |
if (terminals.list(CARD_REMOVAL).size() != 0) { |
|
119 |
throw new Exception("List not empty: " + result); |
|
120 |
} |
|
121 |
||
122 |
if (multipleReaders) { |
|
123 |
System.out.println("*** Insert card into other reader!"); |
|
124 |
terminals.waitForChange(); |
|
125 |
||
126 |
result = terminals.list(CARD_INSERTION); |
|
127 |
System.out.println(result); |
|
128 |
if (result.size() != 1) { |
|
129 |
throw new Exception("no card present"); |
|
130 |
} |
|
131 |
CardTerminal t2 = result.get(0); |
|
132 |
if (t1.getName().equals(t2.getName())) { |
|
133 |
throw new Exception("same terminal"); |
|
134 |
} |
|
135 |
||
136 |
System.out.println("*** Remove card from 2nd reader!"); |
|
137 |
terminals.waitForChange(); |
|
138 |
||
139 |
if (terminals.list(CARD_INSERTION).size() != 0) { |
|
140 |
throw new Exception("List not empty: " + result); |
|
141 |
} |
|
142 |
result = terminals.list(CARD_REMOVAL); |
|
143 |
if ((result.size() != 1) || (result.get(0) != t2)) { |
|
144 |
throw new Exception("no or wrong terminal"); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
System.out.println("*** Remove card from 1st reader!"); |
|
149 |
terminals.waitForChange(); |
|
150 |
||
151 |
if (terminals.list(CARD_INSERTION).size() != 0) { |
|
152 |
throw new Exception("List not empty: " + result); |
|
153 |
} |
|
154 |
result = terminals.list(CARD_REMOVAL); |
|
155 |
if ((result.size() != 1) || (result.get(0) != t1)) { |
|
156 |
throw new Exception("no or wrong terminal"); |
|
157 |
} |
|
158 |
||
159 |
System.out.println("OK."); |
|
160 |
} |
|
161 |
||
162 |
} |