Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/smartcardio/TestExclusive.java
38840 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* @ignore requires special hardware29* @run main/manual TestExclusive30*/3132import javax.smartcardio.Card;33import javax.smartcardio.CardChannel;34import javax.smartcardio.CardException;35import javax.smartcardio.CardTerminal;36import javax.smartcardio.CommandAPDU;3738public class TestExclusive extends Utils {3940static volatile boolean exclusive;4142static volatile boolean otherOK;4344public static void main(String[] args) throws Exception {45CardTerminal terminal = getTerminal(args);46if (terminal == null) {47System.out.println("Skipping the test: " +48"no card terminals available");49return;50}5152// establish a connection with the card53Card card = terminal.connect("T=0");54System.out.println("card: " + card);5556Thread thread = new Thread(new OtherThread(card));57thread.setDaemon(true);58thread.start();5960card.beginExclusive();61exclusive = true;6263Thread.sleep(1000);64System.out.println("=1=resuming...");6566CardChannel channel = card.getBasicChannel();6768System.out.println("=1=Transmitting...");69transmitTestCommand(channel);70System.out.println("=1=OK");7172try {73card.beginExclusive();74} catch (CardException e) {75System.out.println("=1=OK: " + e);76}7778card.endExclusive();7980try {81card.endExclusive();82} catch (IllegalStateException e) {83System.out.println("=1=OK: " + e);84}8586exclusive = false;8788Thread.sleep(1000);8990// disconnect91card.disconnect(true);9293if (! otherOK) {94throw new Exception("Secondary thread failed");95}9697System.out.println("=1=OK.");98}99100private static class OtherThread implements Runnable {101private final Card card;102OtherThread(Card card) {103this.card = card;104}105106public void run() {107try {108while (exclusive == false) {109Thread.sleep(100);110}111112System.out.println("=2=trying endexclusive...");113try {114card.endExclusive();115} catch (IllegalStateException e) {116System.out.println("=2=OK: " + e);117}118119System.out.println("=2=trying beginexclusive...");120try {121card.beginExclusive();122} catch (CardException e) {123System.out.println("=2=OK: " + e);124}125126System.out.println("=2=trying to transmit...");127CardChannel channel = card.getBasicChannel();128try {129channel.transmit(new CommandAPDU(C1));130} catch (CardException e) {131System.out.println("=2=OK: " + e);132}133134while (exclusive) {135Thread.sleep(100);136}137138System.out.println("=2=transmitting...");139transmitTestCommand(channel);140System.out.println("=2=OK...");141142System.out.println("=2=setting ok");143otherOK = true;144} catch (Exception e) {145e.printStackTrace();146}147}148}149150}151152153