2N/A/*
2N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2N/A *
2N/A * This code is free software; you can redistribute it and/or modify it
2N/A * under the terms of the GNU General Public License version 2 only, as
2N/A * published by the Free Software Foundation.
2N/A *
2N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2N/A * version 2 for more details (a copy is included in the LICENSE file that
2N/A * accompanied this code).
2N/A *
2N/A * You should have received a copy of the GNU General Public License version
2N/A * 2 along with this work; if not, write to the Free Software Foundation,
2N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2N/A *
2N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2N/A * or visit www.oracle.com if you need additional information or have any
2N/A * questions.
2N/A */
2N/A
2N/A/**
2N/A * @test
2N/A * @bug 6239117
2N/A * @summary verify that beginExclusive()/endExclusive() works
2N/A * @author Andreas Sterbenz
2N/A * @ignore requires special hardware
2N/A * @run main/manual TestExclusive
2N/A */
2N/A
2N/Aimport java.io.*;
2N/Aimport java.util.*;
2N/A
2N/Aimport javax.smartcardio.*;
2N/A
2N/Apublic class TestExclusive extends Utils {
2N/A
2N/A static volatile boolean exclusive;
2N/A
2N/A static volatile boolean otherOK;
2N/A
2N/A public static void main(String[] args) throws Exception {
2N/A CardTerminal terminal = getTerminal(args);
2N/A
2N/A // establish a connection with the card
2N/A Card card = terminal.connect("T=0");
2N/A System.out.println("card: " + card);
2N/A
2N/A Thread thread = new Thread(new OtherThread(card));
2N/A thread.setDaemon(true);
2N/A thread.start();
2N/A
2N/A card.beginExclusive();
2N/A exclusive = true;
2N/A
2N/A Thread.sleep(1000);
2N/A System.out.println("=1=resuming...");
2N/A
2N/A CardChannel channel = card.getBasicChannel();
2N/A
2N/A System.out.println("=1=Transmitting...");
2N/A transmitTestCommand(channel);
2N/A System.out.println("=1=OK");
2N/A
2N/A try {
2N/A card.beginExclusive();
2N/A } catch (CardException e) {
2N/A System.out.println("=1=OK: " + e);
2N/A }
2N/A
2N/A card.endExclusive();
2N/A
2N/A try {
2N/A card.endExclusive();
} catch (IllegalStateException e) {
System.out.println("=1=OK: " + e);
}
exclusive = false;
Thread.sleep(1000);
// disconnect
card.disconnect(false);
if (otherOK == false) {
throw new Exception("Secondary thread failed");
}
System.out.println("=1=OK.");
}
private static class OtherThread implements Runnable {
private final Card card;
OtherThread(Card card) {
this.card = card;
}
public void run() {
try {
while (exclusive == false) {
Thread.sleep(100);
}
System.out.println("=2=trying endexclusive...");
try {
card.endExclusive();
} catch (IllegalStateException e) {
System.out.println("=2=OK: " + e);
}
System.out.println("=2=trying beginexclusive...");
try {
card.beginExclusive();
} catch (CardException e) {
System.out.println("=2=OK: " + e);
}
System.out.println("=2=trying to transmit...");
CardChannel channel = card.getBasicChannel();
try {
channel.transmit(new CommandAPDU(C1));
} catch (CardException e) {
System.out.println("=2=OK: " + e);
}
while (exclusive) {
Thread.sleep(100);
}
System.out.println("=2=transmitting...");
transmitTestCommand(channel);
System.out.println("=2=OK...");
System.out.println("=2=setting ok");
otherOK = true;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}