jdk/test/sun/security/smartcardio/TestMultiplePresent.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 6239117 6445367
       
    27  * @summary test that CardTerminals.waitForCard() works
       
    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 import static javax.smartcardio.CardTerminals.State.*;
       
    38 
       
    39 public class TestMultiplePresent {
       
    40 
       
    41     public static void main(String[] args) throws Exception {
       
    42         Utils.setLibrary(args);
       
    43         TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
       
    44         System.out.println(factory);
       
    45 
       
    46         CardTerminals terminals = factory.terminals();
       
    47         List<CardTerminal> list = terminals.list();
       
    48         System.out.println("Terminals: " + list);
       
    49         boolean multipleReaders = true;
       
    50         if (list.size() < 2) {
       
    51             if (list.isEmpty()) {
       
    52                 throw new Exception("no terminals");
       
    53             }
       
    54             System.out.println("Only one reader present, using simplified test");
       
    55             multipleReaders = false;
       
    56         }
       
    57 
       
    58         while (true) {
       
    59             boolean present = false;
       
    60             for (CardTerminal terminal : list) {
       
    61                 present |= terminal.isCardPresent();
       
    62             }
       
    63             if (present == false) {
       
    64                 break;
       
    65             }
       
    66             System.out.println("*** Remove all cards!");
       
    67             Thread.sleep(1000);
       
    68         }
       
    69         System.out.println("OK");
       
    70 
       
    71         List<CardTerminal> result;
       
    72 
       
    73         result = terminals.list(CARD_PRESENT);
       
    74         if (result.size() != 0) {
       
    75             throw new Exception("List not empty: " + result);
       
    76         }
       
    77 
       
    78         if (terminals.waitForChange(1000)) {
       
    79             throw new Exception("no timeout");
       
    80         }
       
    81         if (terminals.waitForChange(1000)) {
       
    82             throw new Exception("no timeout");
       
    83         }
       
    84         result = terminals.list(CARD_PRESENT);
       
    85         if (result.size() != 0) {
       
    86             throw new Exception("List not empty: " + result);
       
    87         }
       
    88 
       
    89         System.out.println("*** Insert card!");
       
    90         terminals.waitForChange();
       
    91 
       
    92         result = terminals.list(CARD_INSERTION);
       
    93         System.out.println(result);
       
    94         if (result.size() != 1) {
       
    95             throw new Exception("no card present");
       
    96         }
       
    97         CardTerminal t1 = result.get(0);
       
    98 
       
    99         result = terminals.list(CARD_INSERTION);
       
   100         System.out.println(result);
       
   101         if ((result.size() != 1) || (result.get(0) != t1)) {
       
   102             throw new Exception("no card present");
       
   103         }
       
   104 
       
   105         if (terminals.list(CARD_REMOVAL).size() != 0) {
       
   106             throw new Exception("List not empty: " + result);
       
   107         }
       
   108         if (terminals.list(CARD_REMOVAL).size() != 0) {
       
   109             throw new Exception("List not empty: " + result);
       
   110         }
       
   111 
       
   112 
       
   113         if (terminals.waitForChange(1000)) {
       
   114             throw new Exception("no timeout");
       
   115         }
       
   116 
       
   117         if (terminals.list(CARD_REMOVAL).size() != 0) {
       
   118             throw new Exception("List not empty: " + result);
       
   119         }
       
   120 
       
   121         if (multipleReaders) {
       
   122             System.out.println("*** Insert card into other reader!");
       
   123             terminals.waitForChange();
       
   124 
       
   125             result = terminals.list(CARD_INSERTION);
       
   126             System.out.println(result);
       
   127             if (result.size() != 1) {
       
   128                 throw new Exception("no card present");
       
   129             }
       
   130             CardTerminal t2 = result.get(0);
       
   131             if (t1.getName().equals(t2.getName())) {
       
   132                 throw new Exception("same terminal");
       
   133             }
       
   134 
       
   135             System.out.println("*** Remove card from 2nd reader!");
       
   136             terminals.waitForChange();
       
   137 
       
   138             if (terminals.list(CARD_INSERTION).size() != 0) {
       
   139                 throw new Exception("List not empty: " + result);
       
   140             }
       
   141             result = terminals.list(CARD_REMOVAL);
       
   142             if ((result.size() != 1) || (result.get(0) != t2)) {
       
   143                 throw new Exception("no or wrong terminal");
       
   144             }
       
   145         }
       
   146 
       
   147         System.out.println("*** Remove card from 1st reader!");
       
   148         terminals.waitForChange();
       
   149 
       
   150         if (terminals.list(CARD_INSERTION).size() != 0) {
       
   151             throw new Exception("List not empty: " + result);
       
   152         }
       
   153         result = terminals.list(CARD_REMOVAL);
       
   154         if ((result.size() != 1) || (result.get(0) != t1)) {
       
   155             throw new Exception("no or wrong terminal");
       
   156         }
       
   157 
       
   158         System.out.println("OK.");
       
   159     }
       
   160 
       
   161 }