Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/smartcardio/TestPresent.java
38840 views
/*1* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 6293769 629452726* @summary test that the isCardPresent()/waitForX() APIs work correctly27* @author Andreas Sterbenz28* @ignore requires special hardware29* @run main/manual TestPresent30*/3132import java.util.List;33import javax.smartcardio.CardTerminal;34import javax.smartcardio.TerminalFactory;3536public class TestPresent extends Utils {3738private static class Timer {39private long time = System.currentTimeMillis();40long update() {41long t = System.currentTimeMillis();42long diff = t - time;43time = t;44return diff;45}46long print() {47long t = update();48System.out.println("Elapsed time: " + t + " ms.");49return t;50}51}5253private static boolean isFalse(boolean b) throws Exception {54if (b) {55throw new Exception("not false");56}57return b;58}5960private static boolean isTrue(boolean b) throws Exception {61if (!b) {62throw new Exception("not true");63}64return b;65}6667public static void main(String[] args) throws Exception {68CardTerminal terminal = getTerminal(args);69if (terminal == null) {70System.out.println("Skipping the test: " +71"no card terminals available");72return;73}7475while (terminal.isCardPresent()) {76System.out.println("*** Remove card!");77Thread.sleep(1000);78}7980Timer timer = new Timer();8182System.out.println("Testing waitForCardAbsent() with card already absent...");83isTrue(terminal.waitForCardAbsent(10));84timer.print();85isTrue(terminal.waitForCardAbsent(100));86timer.print();87isTrue(terminal.waitForCardAbsent(10000));88timer.print();89isTrue(terminal.waitForCardAbsent(0));90timer.print();9192System.out.println("Testing waitForCardPresent() timeout...");93isFalse(terminal.waitForCardPresent(10));94timer.print();95isFalse(terminal.waitForCardPresent(100));96timer.print();97isFalse(terminal.waitForCardPresent(1000));98timer.print();99100isFalse(terminal.isCardPresent());101isFalse(terminal.isCardPresent());102103System.out.println("*** Insert card!");104isTrue(terminal.waitForCardPresent(0));105timer.print();106107isTrue(terminal.isCardPresent());108isTrue(terminal.isCardPresent());109110System.out.println("Testing waitForCardPresent() with card already present...");111isTrue(terminal.waitForCardPresent(0));112timer.print();113isTrue(terminal.waitForCardPresent(10000));114timer.print();115isTrue(terminal.waitForCardPresent(100));116timer.print();117isTrue(terminal.waitForCardPresent(10));118timer.print();119120System.out.println("Testing waitForCardAbsent() timeout...");121isFalse(terminal.waitForCardAbsent(1000));122timer.print();123isFalse(terminal.waitForCardAbsent(100));124timer.print();125isFalse(terminal.waitForCardAbsent(10));126timer.print();127128System.out.println("*** Remove card!");129isTrue(terminal.waitForCardAbsent(0));130timer.print();131132isFalse(terminal.isCardPresent());133isFalse(terminal.isCardPresent());134135System.out.println("OK.");136}137138}139140141