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  * @modules java.smartcardio/javax.smartcardio
  30  * @run main/manual TestPresent
  31  */
  32 
  33 // This test requires special hardware.
  34 
  35 import java.util.List;
  36 import javax.smartcardio.CardTerminal;
  37 import javax.smartcardio.TerminalFactory;
  38 
  39 public class TestPresent extends Utils {
  40 
  41     private static class Timer {
  42         private long time = System.currentTimeMillis();
  43         long update() {
  44             long t = System.currentTimeMillis();
  45             long diff = t - time;
  46             time = t;
  47             return diff;
  48         }
  49         long print() {
  50             long t = update();
  51             System.out.println("Elapsed time: " + t + " ms.");
  52             return t;
  53         }
  54     }
  55 
  56     private static boolean isFalse(boolean b) throws Exception {
  57         if (b) {
  58             throw new Exception("not false");
  59         }
  60         return b;
  61     }
  62 
  63     private static boolean isTrue(boolean b) throws Exception {
  64         if (!b) {
  65             throw new Exception("not true");
  66         }
  67         return b;
  68     }
  69 
  70     public static void main(String[] args) throws Exception {
  71         CardTerminal terminal = getTerminal(args);
  72         if (terminal == null) {
  73             System.out.println("Skipping the test: " +
  74                     "no card terminals available");
  75             return;
  76         }
  77 
  78         while (terminal.isCardPresent()) {
  79             System.out.println("*** Remove card!");
  80             Thread.sleep(1000);
  81         }
  82 
  83         Timer timer = new Timer();
  84 
  85         System.out.println("Testing waitForCardAbsent() with card already absent...");
  86         isTrue(terminal.waitForCardAbsent(10));
  87         timer.print();
  88         isTrue(terminal.waitForCardAbsent(100));
  89         timer.print();
  90         isTrue(terminal.waitForCardAbsent(10000));
  91         timer.print();
  92         isTrue(terminal.waitForCardAbsent(0));
  93         timer.print();
  94 
  95         System.out.println("Testing waitForCardPresent() timeout...");
  96         isFalse(terminal.waitForCardPresent(10));
  97         timer.print();
  98         isFalse(terminal.waitForCardPresent(100));
  99         timer.print();
 100         isFalse(terminal.waitForCardPresent(1000));
 101         timer.print();
 102 
 103         isFalse(terminal.isCardPresent());
 104         isFalse(terminal.isCardPresent());
 105 
 106         System.out.println("*** Insert card!");
 107         isTrue(terminal.waitForCardPresent(0));
 108         timer.print();
 109 
 110         isTrue(terminal.isCardPresent());
 111         isTrue(terminal.isCardPresent());
 112 
 113         System.out.println("Testing waitForCardPresent() with card already present...");
 114         isTrue(terminal.waitForCardPresent(0));
 115         timer.print();
 116         isTrue(terminal.waitForCardPresent(10000));
 117         timer.print();
 118         isTrue(terminal.waitForCardPresent(100));
 119         timer.print();
 120         isTrue(terminal.waitForCardPresent(10));
 121         timer.print();
 122 
 123         System.out.println("Testing waitForCardAbsent() timeout...");
 124         isFalse(terminal.waitForCardAbsent(1000));
 125         timer.print();
 126         isFalse(terminal.waitForCardAbsent(100));
 127         timer.print();
 128         isFalse(terminal.waitForCardAbsent(10));
 129         timer.print();
 130 
 131         System.out.println("*** Remove card!");
 132         isTrue(terminal.waitForCardAbsent(0));
 133         timer.print();
 134 
 135         isFalse(terminal.isCardPresent());
 136         isFalse(terminal.isCardPresent());
 137 
 138         System.out.println("OK.");
 139     }
 140 
 141 }