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)); 96 timer.print(); 97 isFalse(terminal.waitForCardPresent(100)); 98 timer.print(); 99 isFalse(terminal.waitForCardPresent(1000)); 100 timer.print(); 101 102 isFalse(terminal.isCardPresent()); 103 isFalse(terminal.isCardPresent()); 104 105 System.out.println("*** Insert card!"); 106 isTrue(terminal.waitForCardPresent(0)); 107 timer.print(); 108 109 isTrue(terminal.isCardPresent()); 110 isTrue(terminal.isCardPresent()); 111 112 System.out.println("Testing waitForCardPresent() with card already present..."); 113 isTrue(terminal.waitForCardPresent(0)); 114 timer.print(); 115 isTrue(terminal.waitForCardPresent(10000)); 116 timer.print(); 117 isTrue(terminal.waitForCardPresent(100)); 118 timer.print(); 119 isTrue(terminal.waitForCardPresent(10)); 120 timer.print(); 121 122 System.out.println("Testing waitForCardAbsent() timeout..."); 123 isFalse(terminal.waitForCardAbsent(1000)); 124 timer.print(); 125 isFalse(terminal.waitForCardAbsent(100)); 126 timer.print(); 127 isFalse(terminal.waitForCardAbsent(10)); 128 timer.print(); 129 130 System.out.println("*** Remove card!"); 131 isTrue(terminal.waitForCardAbsent(0)); 132 timer.print(); 133 134 isFalse(terminal.isCardPresent()); 135 isFalse(terminal.isCardPresent()); 136 137 System.out.println("OK."); 138 } 139 140 }