J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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 6309280 |
| 27 | * @summary test connect() works |
| 28 | * @author Andreas Sterbenz |
| 29 | * @ignore requires special hardware |
| 30 | * @run main/manual TestConnect |
| 31 | */ |
| 32 | |
| 33 | import java.io.*; |
| 34 | import java.util.*; |
| 35 | |
| 36 | import javax.smartcardio.*; |
| 37 | |
| 38 | public class TestConnect extends Utils { |
| 39 | |
| 40 | public static void main(String[] args) throws Exception { |
| 41 | TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null, "SunPCSC"); |
| 42 | System.out.println(factory); |
| 43 | |
| 44 | List<CardTerminal> terminals = factory.terminals().list(); |
| 45 | System.out.println("Terminals: " + terminals); |
| 46 | if (terminals.isEmpty()) { |
| 47 | throw new Exception("No card terminals available"); |
| 48 | } |
| 49 | CardTerminal terminal = terminals.get(0); |
| 50 | |
| 51 | if (terminal.isCardPresent() == false) { |
| 52 | System.out.println("*** Insert card"); |
| 53 | if (terminal.waitForCardPresent(20 * 1000) == false) { |
| 54 | throw new Exception("no card available"); |
| 55 | } |
| 56 | } |
| 57 | System.out.println("card present: " + terminal.isCardPresent()); |
| 58 | |
| 59 | Card card = terminal.connect("*"); |
| 60 | System.out.println("card: " + card); |
| 61 | if (card.getProtocol().equals("T=0") == false) { |
| 62 | throw new Exception("Not T=0 protocol"); |
| 63 | } |
| 64 | transmit(card); |
| 65 | card.disconnect(false); |
| 66 | |
| 67 | try { |
| 68 | transmit(card); |
| 69 | throw new Exception("transmitted to disconnected card"); |
| 70 | } catch (IllegalStateException e) { |
| 71 | System.out.println("OK: " + e); |
| 72 | } |
| 73 | |
| 74 | /* ignore: Solaris bug |
| 75 | try { |
| 76 | card = terminal.connect("T=1"); |
| 77 | System.out.println(card); |
| 78 | throw new Exception("connected via T=1"); |
| 79 | } catch (CardException e) { |
| 80 | System.out.println("OK: " + e); |
| 81 | } |
| 82 | */ |
| 83 | |
| 84 | try { |
| 85 | card = terminal.connect("T=Foo"); |
| 86 | System.out.println(card); |
| 87 | throw new Exception("connected via T=Foo"); |
| 88 | } catch (IllegalArgumentException e) { |
| 89 | System.out.println("OK: " + e); |
| 90 | } |
| 91 | |
| 92 | card = terminal.connect("T=0"); |
| 93 | System.out.println(card); |
| 94 | if (card.getProtocol().equals("T=0") == false) { |
| 95 | throw new Exception("Not T=0 protocol"); |
| 96 | } |
| 97 | transmit(card); |
| 98 | card.disconnect(true); |
| 99 | |
| 100 | card = terminal.connect("*"); |
| 101 | System.out.println("card: " + card); |
| 102 | if (card.getProtocol().equals("T=0") == false) { |
| 103 | throw new Exception("Not T=0 protocol"); |
| 104 | } |
| 105 | transmit(card); |
| 106 | card.disconnect(true); |
| 107 | card.disconnect(true); |
| 108 | |
| 109 | System.out.println("OK."); |
| 110 | } |
| 111 | |
| 112 | private static void transmit(Card card) throws Exception { |
| 113 | CardChannel channel = card.getBasicChannel(); |
| 114 | System.out.println("Transmitting..."); |
| 115 | transmitTestCommand(channel); |
| 116 | } |
| 117 | |
| 118 | } |