jdk/test/sun/security/smartcardio/TestExclusive.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005 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
       
    27  * @summary verify that beginExclusive()/endExclusive() works
       
    28  * @author Andreas Sterbenz
       
    29  * @ignore requires special hardware
       
    30  * @run main/manual TestExclusive
       
    31  */
       
    32 
       
    33 import java.io.*;
       
    34 import java.util.*;
       
    35 
       
    36 import javax.smartcardio.*;
       
    37 
       
    38 public class TestExclusive extends Utils {
       
    39 
       
    40     static volatile boolean exclusive;
       
    41 
       
    42     static volatile boolean otherOK;
       
    43 
       
    44     public static void main(String[] args) throws Exception {
       
    45         CardTerminal terminal = getTerminal(args);
       
    46 
       
    47         // establish a connection with the card
       
    48         Card card = terminal.connect("T=0");
       
    49         System.out.println("card: " + card);
       
    50 
       
    51         Thread thread = new Thread(new OtherThread(card));
       
    52         thread.setDaemon(true);
       
    53         thread.start();
       
    54 
       
    55         card.beginExclusive();
       
    56         exclusive = true;
       
    57 
       
    58         Thread.sleep(1000);
       
    59         System.out.println("=1=resuming...");
       
    60 
       
    61         CardChannel channel = card.getBasicChannel();
       
    62 
       
    63         System.out.println("=1=Transmitting...");
       
    64         transmitTestCommand(channel);
       
    65         System.out.println("=1=OK");
       
    66 
       
    67         try {
       
    68             card.beginExclusive();
       
    69         } catch (CardException e) {
       
    70             System.out.println("=1=OK: " + e);
       
    71         }
       
    72 
       
    73         card.endExclusive();
       
    74 
       
    75         try {
       
    76             card.endExclusive();
       
    77         } catch (IllegalStateException e) {
       
    78             System.out.println("=1=OK: " + e);
       
    79         }
       
    80 
       
    81         exclusive = false;
       
    82 
       
    83         Thread.sleep(1000);
       
    84 
       
    85         // disconnect
       
    86         card.disconnect(false);
       
    87 
       
    88         if (otherOK == false) {
       
    89             throw new Exception("Secondary thread failed");
       
    90         }
       
    91 
       
    92         System.out.println("=1=OK.");
       
    93     }
       
    94 
       
    95     private static class OtherThread implements Runnable {
       
    96         private final Card card;
       
    97         OtherThread(Card card) {
       
    98             this.card = card;
       
    99         }
       
   100 
       
   101         public void run() {
       
   102             try {
       
   103                 while (exclusive == false) {
       
   104                     Thread.sleep(100);
       
   105                 }
       
   106 
       
   107                 System.out.println("=2=trying endexclusive...");
       
   108                 try {
       
   109                     card.endExclusive();
       
   110                 } catch (IllegalStateException e) {
       
   111                     System.out.println("=2=OK: " + e);
       
   112                 }
       
   113 
       
   114                 System.out.println("=2=trying beginexclusive...");
       
   115                 try {
       
   116                     card.beginExclusive();
       
   117                 } catch (CardException e) {
       
   118                     System.out.println("=2=OK: " + e);
       
   119                 }
       
   120 
       
   121                 System.out.println("=2=trying to transmit...");
       
   122                 CardChannel channel = card.getBasicChannel();
       
   123                 try {
       
   124                     channel.transmit(new CommandAPDU(C1));
       
   125                 } catch (CardException e) {
       
   126                     System.out.println("=2=OK: " + e);
       
   127                 }
       
   128 
       
   129                 while (exclusive) {
       
   130                     Thread.sleep(100);
       
   131                 }
       
   132 
       
   133                 System.out.println("=2=transmitting...");
       
   134                 transmitTestCommand(channel);
       
   135                 System.out.println("=2=OK...");
       
   136 
       
   137                 System.out.println("=2=setting ok");
       
   138                 otherOK = true;
       
   139             } catch (Exception e) {
       
   140                 e.printStackTrace();
       
   141             }
       
   142         }
       
   143     }
       
   144 
       
   145 }