jdk/test/sun/security/smartcardio/TestPresent.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any 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  * @ignore requires special hardware
       
    30  * @run main/manual TestPresent
       
    31  */
       
    32 
       
    33 import java.io.*;
       
    34 import java.util.*;
       
    35 
       
    36 import javax.smartcardio.*;
       
    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));
       
    99         timer.print();
       
   100         isFalse(terminal.waitForCardPresent(100));
       
   101         timer.print();
       
   102         isFalse(terminal.waitForCardPresent(1000));
       
   103         timer.print();
       
   104 
       
   105         isFalse(terminal.isCardPresent());
       
   106         isFalse(terminal.isCardPresent());
       
   107 
       
   108         System.out.println("*** Insert card!");
       
   109         isTrue(terminal.waitForCardPresent(0));
       
   110         timer.print();
       
   111 
       
   112         isTrue(terminal.isCardPresent());
       
   113         isTrue(terminal.isCardPresent());
       
   114 
       
   115         System.out.println("Testing waitForCardPresent() with card already present...");
       
   116         isTrue(terminal.waitForCardPresent(0));
       
   117         timer.print();
       
   118         isTrue(terminal.waitForCardPresent(10000));
       
   119         timer.print();
       
   120         isTrue(terminal.waitForCardPresent(100));
       
   121         timer.print();
       
   122         isTrue(terminal.waitForCardPresent(10));
       
   123         timer.print();
       
   124 
       
   125         System.out.println("Testing waitForCardAbsent() timeout...");
       
   126         isFalse(terminal.waitForCardAbsent(1000));
       
   127         timer.print();
       
   128         isFalse(terminal.waitForCardAbsent(100));
       
   129         timer.print();
       
   130         isFalse(terminal.waitForCardAbsent(10));
       
   131         timer.print();
       
   132 
       
   133         System.out.println("*** Remove card!");
       
   134         isTrue(terminal.waitForCardAbsent(0));
       
   135         timer.print();
       
   136 
       
   137         isFalse(terminal.isCardPresent());
       
   138         isFalse(terminal.isCardPresent());
       
   139 
       
   140         System.out.println("OK.");
       
   141     }
       
   142 
       
   143 }