Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/smartcardio/TestMultiplePresent.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 6239117 644536726* @summary test that CardTerminals.waitForCard() works27* @author Andreas Sterbenz28* @ignore requires special hardware29* @run main/manual TestPresent30*/3132import java.util.List;33import javax.smartcardio.CardTerminal;34import javax.smartcardio.CardTerminals;35import javax.smartcardio.TerminalFactory;36import static javax.smartcardio.CardTerminals.State.*;3738public class TestMultiplePresent {3940public static void main(String[] args) throws Exception {41Utils.setLibrary(args);42TerminalFactory factory = Utils.getTerminalFactory(null);43if (factory == null) {44System.out.println("Skipping the test: " +45"no card terminals available");46return;47}48System.out.println(factory);4950CardTerminals terminals = factory.terminals();51List<CardTerminal> list = terminals.list();52System.out.println("Terminals: " + list);53boolean multipleReaders = true;54if (list.size() < 2) {55if (list.isEmpty()) {56System.out.println("Skipping the test: " +57"no card terminals available");58return;59}60System.out.println("Only one reader present, using simplified test");61multipleReaders = false;62}6364while (true) {65boolean present = false;66for (CardTerminal terminal : list) {67present |= terminal.isCardPresent();68}69if (present == false) {70break;71}72System.out.println("*** Remove all cards!");73Thread.sleep(1000);74}75System.out.println("OK");7677List<CardTerminal> result;7879result = terminals.list(CARD_PRESENT);80if (result.size() != 0) {81throw new Exception("List not empty: " + result);82}8384if (terminals.waitForChange(1000)) {85throw new Exception("no timeout");86}87if (terminals.waitForChange(1000)) {88throw new Exception("no timeout");89}90result = terminals.list(CARD_PRESENT);91if (result.size() != 0) {92throw new Exception("List not empty: " + result);93}9495System.out.println("*** Insert card!");96terminals.waitForChange();9798result = terminals.list(CARD_INSERTION);99System.out.println(result);100if (result.size() != 1) {101throw new Exception("no card present");102}103CardTerminal t1 = result.get(0);104105result = terminals.list(CARD_INSERTION);106System.out.println(result);107if ((result.size() != 1) || (result.get(0) != t1)) {108throw new Exception("no card present");109}110111if (terminals.list(CARD_REMOVAL).size() != 0) {112throw new Exception("List not empty: " + result);113}114if (terminals.list(CARD_REMOVAL).size() != 0) {115throw new Exception("List not empty: " + result);116}117118119if (terminals.waitForChange(1000)) {120throw new Exception("no timeout");121}122123if (terminals.list(CARD_REMOVAL).size() != 0) {124throw new Exception("List not empty: " + result);125}126127if (multipleReaders) {128System.out.println("*** Insert card into other reader!");129terminals.waitForChange();130131result = terminals.list(CARD_INSERTION);132System.out.println(result);133if (result.size() != 1) {134throw new Exception("no card present");135}136CardTerminal t2 = result.get(0);137if (t1.getName().equals(t2.getName())) {138throw new Exception("same terminal");139}140141System.out.println("*** Remove card from 2nd reader!");142terminals.waitForChange();143144if (terminals.list(CARD_INSERTION).size() != 0) {145throw new Exception("List not empty: " + result);146}147result = terminals.list(CARD_REMOVAL);148if ((result.size() != 1) || (result.get(0) != t2)) {149throw new Exception("no or wrong terminal");150}151}152153System.out.println("*** Remove card from 1st reader!");154terminals.waitForChange();155156if (terminals.list(CARD_INSERTION).size() != 0) {157throw new Exception("List not empty: " + result);158}159result = terminals.list(CARD_REMOVAL);160if ((result.size() != 1) || (result.get(0) != t1)) {161throw new Exception("no or wrong terminal");162}163164System.out.println("OK.");165}166167}168169170