1 /*
2 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
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 *
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.
22 */
23
24 /**
25 * @test
26 * @bug 6293769 6294527
27 * @summary test that the isCardPresent()/waitForX() APIs work correctly
28 * @author Andreas Sterbenz
29 * @run main/manual TestPresent
30 */
31
32 // This test requires special hardware.
33
34 import java.util.List;
35 import javax.smartcardio.CardTerminal;
36 import javax.smartcardio.TerminalFactory;
37
38 public class TestPresent {
39
40 private static class Timer {
41 private long time = System.currentTimeMillis();
42 long update() {
43 long t = System.currentTimeMillis();
44 long diff = t - time;
45 time = t;
46 return diff;
47 }
48 long print() {
49 long t = update();
50 System.out.println("Elapsed time: " + t + " ms.");
51 return t;
52 }
53 }
54
55 private static boolean isFalse(boolean b) throws Exception {
56 if (b) {
57 throw new Exception("not false");
58 }
59 return b;
60 }
61
62 private static boolean isTrue(boolean b) throws Exception {
63 if (!b) {
64 throw new Exception("not true");
65 }
66 return b;
67 }
68
69 public static void main(String[] args) throws Exception {
70 TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
71 System.out.println(factory);
72
73 List<CardTerminal> terminals = factory.terminals().list();
74 System.out.println("Terminals: " + terminals);
75 if (terminals.isEmpty()) {
76 throw new Exception("No card terminals available");
77 }
78 CardTerminal terminal = terminals.get(0);
79
80 while (terminal.isCardPresent()) {
81 System.out.println("*** Remove card!");
82 Thread.sleep(1000);
83 }
84
85 Timer timer = new Timer();
86
87 System.out.println("Testing waitForCardAbsent() with card already absent...");
88 isTrue(terminal.waitForCardAbsent(10));
89 timer.print();
90 isTrue(terminal.waitForCardAbsent(100));
91 timer.print();
92 isTrue(terminal.waitForCardAbsent(10000));
93 timer.print();
94 isTrue(terminal.waitForCardAbsent(0));
95 timer.print();
96
97 System.out.println("Testing waitForCardPresent() timeout...");
98 isFalse(terminal.waitForCardPresent(10));
|
1 /*
2 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
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 *
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.
22 */
23
24 /**
25 * @test
26 * @bug 6293769 6294527
27 * @summary test that the isCardPresent()/waitForX() APIs work correctly
28 * @author Andreas Sterbenz
29 * @run main/manual TestPresent
30 */
31
32 // This test requires special hardware.
33
34 import java.util.List;
35 import javax.smartcardio.CardTerminal;
36 import javax.smartcardio.TerminalFactory;
37
38 public class TestPresent extends Utils {
39
40 private static class Timer {
41 private long time = System.currentTimeMillis();
42 long update() {
43 long t = System.currentTimeMillis();
44 long diff = t - time;
45 time = t;
46 return diff;
47 }
48 long print() {
49 long t = update();
50 System.out.println("Elapsed time: " + t + " ms.");
51 return t;
52 }
53 }
54
55 private static boolean isFalse(boolean b) throws Exception {
56 if (b) {
57 throw new Exception("not false");
58 }
59 return b;
60 }
61
62 private static boolean isTrue(boolean b) throws Exception {
63 if (!b) {
64 throw new Exception("not true");
65 }
66 return b;
67 }
68
69 public static void main(String[] args) throws Exception {
70 CardTerminal terminal = getTerminal(args);
71 if (terminal == null) {
72 System.out.println("Skipping the test: " +
73 "no card terminals available");
74 return;
75 }
76
77 while (terminal.isCardPresent()) {
78 System.out.println("*** Remove card!");
79 Thread.sleep(1000);
80 }
81
82 Timer timer = new Timer();
83
84 System.out.println("Testing waitForCardAbsent() with card already absent...");
85 isTrue(terminal.waitForCardAbsent(10));
86 timer.print();
87 isTrue(terminal.waitForCardAbsent(100));
88 timer.print();
89 isTrue(terminal.waitForCardAbsent(10000));
90 timer.print();
91 isTrue(terminal.waitForCardAbsent(0));
92 timer.print();
93
94 System.out.println("Testing waitForCardPresent() timeout...");
95 isFalse(terminal.waitForCardPresent(10));
|