Path: blob/master/test/jdk/sun/security/smartcardio/TestPresent.java
41149 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* @modules java.smartcardio/javax.smartcardio29* @run main/manual TestPresent30*/3132// This test requires special hardware.3334import java.util.List;35import javax.smartcardio.CardTerminal;36import javax.smartcardio.TerminalFactory;3738public class TestPresent extends Utils {3940private static class Timer {41private long time = System.currentTimeMillis();42long update() {43long t = System.currentTimeMillis();44long diff = t - time;45time = t;46return diff;47}48long print() {49long t = update();50System.out.println("Elapsed time: " + t + " ms.");51return t;52}53}5455private static boolean isFalse(boolean b) throws Exception {56if (b) {57throw new Exception("not false");58}59return b;60}6162private static boolean isTrue(boolean b) throws Exception {63if (!b) {64throw new Exception("not true");65}66return b;67}6869public static void main(String[] args) throws Exception {70CardTerminal terminal = getTerminal(args);71if (terminal == null) {72System.out.println("Skipping the test: " +73"no card terminals available");74return;75}7677while (terminal.isCardPresent()) {78System.out.println("*** Remove card!");79Thread.sleep(1000);80}8182Timer timer = new Timer();8384System.out.println("Testing waitForCardAbsent() with card already absent...");85isTrue(terminal.waitForCardAbsent(10));86timer.print();87isTrue(terminal.waitForCardAbsent(100));88timer.print();89isTrue(terminal.waitForCardAbsent(10000));90timer.print();91isTrue(terminal.waitForCardAbsent(0));92timer.print();9394System.out.println("Testing waitForCardPresent() timeout...");95isFalse(terminal.waitForCardPresent(10));96timer.print();97isFalse(terminal.waitForCardPresent(100));98timer.print();99isFalse(terminal.waitForCardPresent(1000));100timer.print();101102isFalse(terminal.isCardPresent());103isFalse(terminal.isCardPresent());104105System.out.println("*** Insert card!");106isTrue(terminal.waitForCardPresent(0));107timer.print();108109isTrue(terminal.isCardPresent());110isTrue(terminal.isCardPresent());111112System.out.println("Testing waitForCardPresent() with card already present...");113isTrue(terminal.waitForCardPresent(0));114timer.print();115isTrue(terminal.waitForCardPresent(10000));116timer.print();117isTrue(terminal.waitForCardPresent(100));118timer.print();119isTrue(terminal.waitForCardPresent(10));120timer.print();121122System.out.println("Testing waitForCardAbsent() timeout...");123isFalse(terminal.waitForCardAbsent(1000));124timer.print();125isFalse(terminal.waitForCardAbsent(100));126timer.print();127isFalse(terminal.waitForCardAbsent(10));128timer.print();129130System.out.println("*** Remove card!");131isTrue(terminal.waitForCardAbsent(0));132timer.print();133134isFalse(terminal.isCardPresent());135isFalse(terminal.isCardPresent());136137System.out.println("OK.");138}139140}141142143