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