Path: blob/master/test/jdk/sun/security/smartcardio/TestExclusive.java
41149 views
/*1* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 623911726* @summary verify that beginExclusive()/endExclusive() works27* @author Andreas Sterbenz28* @modules java.smartcardio/javax.smartcardio29* @run main/manual TestExclusive30*/3132// This test requires special hardware.3334import javax.smartcardio.Card;35import javax.smartcardio.CardChannel;36import javax.smartcardio.CardException;37import javax.smartcardio.CardTerminal;38import javax.smartcardio.CommandAPDU;3940public class TestExclusive extends Utils {4142static volatile boolean exclusive;4344static volatile boolean otherOK;4546public static void main(String[] args) throws Exception {47CardTerminal terminal = getTerminal(args);48if (terminal == null) {49System.out.println("Skipping the test: " +50"no card terminals available");51return;52}5354// establish a connection with the card55Card card = terminal.connect("T=0");56System.out.println("card: " + card);5758Thread thread = new Thread(new OtherThread(card));59thread.setDaemon(true);60thread.start();6162card.beginExclusive();63exclusive = true;6465Thread.sleep(1000);66System.out.println("=1=resuming...");6768CardChannel channel = card.getBasicChannel();6970System.out.println("=1=Transmitting...");71transmitTestCommand(channel);72System.out.println("=1=OK");7374try {75card.beginExclusive();76} catch (CardException e) {77System.out.println("=1=OK: " + e);78}7980card.endExclusive();8182try {83card.endExclusive();84} catch (IllegalStateException e) {85System.out.println("=1=OK: " + e);86}8788exclusive = false;8990Thread.sleep(1000);9192// disconnect93card.disconnect(true);9495if (! otherOK) {96throw new Exception("Secondary thread failed");97}9899System.out.println("=1=OK.");100}101102private static class OtherThread implements Runnable {103private final Card card;104OtherThread(Card card) {105this.card = card;106}107108public void run() {109try {110while (exclusive == false) {111Thread.sleep(100);112}113114System.out.println("=2=trying endexclusive...");115try {116card.endExclusive();117} catch (IllegalStateException e) {118System.out.println("=2=OK: " + e);119}120121System.out.println("=2=trying beginexclusive...");122try {123card.beginExclusive();124} catch (CardException e) {125System.out.println("=2=OK: " + e);126}127128System.out.println("=2=trying to transmit...");129CardChannel channel = card.getBasicChannel();130try {131channel.transmit(new CommandAPDU(C1));132} catch (CardException e) {133System.out.println("=2=OK: " + e);134}135136while (exclusive) {137Thread.sleep(100);138}139140System.out.println("=2=transmitting...");141transmitTestCommand(channel);142System.out.println("=2=OK...");143144System.out.println("=2=setting ok");145otherOK = true;146} catch (Exception e) {147e.printStackTrace();148}149}150}151152}153154155