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 6239117
  27  * @summary test connect works correctly if called multiple times/card removed
  28  * @author Andreas Sterbenz
  29  * @modules java.smartcardio/javax.smartcardio
  30  * @run main/manual TestConnectAgain
  31  */
  32 
  33 // This test requires special hardware.
  34 
  35 import javax.smartcardio.Card;
  36 import javax.smartcardio.CardException;
  37 import javax.smartcardio.CardChannel;
  38 import javax.smartcardio.CardTerminal;
  39 
  40 public class TestConnectAgain extends Utils {
  41 
  42     public static void main(String[] args) throws Exception {
  43         CardTerminal terminal = getTerminal(args);
  44         if (terminal == null) {
  45             System.out.println("Skipping the test: " +
  46                     "no card terminals available");
  47             return;
  48         }
  49 
  50         Card card = terminal.connect("T=0");
  51         CardChannel channel = card.getBasicChannel();
  52         transmitTestCommand(channel);
  53 
  54         Card card2 = terminal.connect("*");
  55         if (card != card2) {
  56             throw new Exception("Different card object");
  57         }
  58         card2 = terminal.connect("T=0");
  59         if (card != card2) {
  60             throw new Exception("Different card object");
  61         }
  62 
  63         System.out.println("Remove card!");
  64         terminal.waitForCardAbsent(0);
  65 
  66         try {
  67             transmitTestCommand(channel);
  68             throw new Exception();
  69         } catch (CardException e) {
  70             System.out.println("OK: " + e);
  71         }
  72 
  73         System.out.println("Insert card!");
  74         terminal.waitForCardPresent(0);
  75 
  76         try {
  77             transmitTestCommand(channel);
  78             throw new Exception();
  79         } catch (IllegalStateException e) {
  80             System.out.println("OK: " + e);
  81         }
  82 
  83         card = terminal.connect("*");
  84         if (card == card2) {
  85             throw new Exception("Old card object");
  86         }
  87 
  88         try {
  89             transmitTestCommand(channel);
  90             throw new Exception();
  91         } catch (IllegalStateException e) {
  92             System.out.println("OK: " + e);
  93         }
  94 
  95         channel = card.getBasicChannel();
  96         transmitTestCommand(channel);
  97 
  98         card2 = terminal.connect("*");
  99         if (card != card2) {
 100             throw new Exception("Different card object");
 101         }
 102 
 103         // disconnect
 104         card.disconnect(true);
 105 
 106         System.out.println("OK.");
 107     }
 108 
 109 }