Path: blob/master/test/jdk/sun/security/smartcardio/TestMultiplePresent.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 6239117 644536726* @summary test that CardTerminals.waitForCard() works27* @author Andreas Sterbenz28* @modules java.smartcardio/javax.smartcardio29* @run main/manual TestMultiplePresent30*/3132// This test requires special hardware.3334import java.util.List;35import javax.smartcardio.CardTerminal;36import javax.smartcardio.CardTerminals;37import javax.smartcardio.TerminalFactory;38import static javax.smartcardio.CardTerminals.State.*;3940public class TestMultiplePresent {4142public static void main(String[] args) throws Exception {43Utils.setLibrary(args);44TerminalFactory factory = Utils.getTerminalFactory(null);45if (factory == null) {46System.out.println("Skipping the test: " +47"no card terminals available");48return;49}50System.out.println(factory);5152CardTerminals terminals = factory.terminals();53List<CardTerminal> list = terminals.list();54System.out.println("Terminals: " + list);55boolean multipleReaders = true;56if (list.size() < 2) {57if (list.isEmpty()) {58System.out.println("Skipping the test: " +59"no card terminals available");60return;61}62System.out.println("Only one reader present, using simplified test");63multipleReaders = false;64}6566while (true) {67boolean present = false;68for (CardTerminal terminal : list) {69present |= terminal.isCardPresent();70}71if (present == false) {72break;73}74System.out.println("*** Remove all cards!");75Thread.sleep(1000);76}77System.out.println("OK");7879List<CardTerminal> result;8081result = terminals.list(CARD_PRESENT);82if (result.size() != 0) {83throw new Exception("List not empty: " + result);84}8586if (terminals.waitForChange(1000)) {87throw new Exception("no timeout");88}89if (terminals.waitForChange(1000)) {90throw new Exception("no timeout");91}92result = terminals.list(CARD_PRESENT);93if (result.size() != 0) {94throw new Exception("List not empty: " + result);95}9697System.out.println("*** Insert card!");98terminals.waitForChange();99100result = terminals.list(CARD_INSERTION);101System.out.println(result);102if (result.size() != 1) {103throw new Exception("no card present");104}105CardTerminal t1 = result.get(0);106107result = terminals.list(CARD_INSERTION);108System.out.println(result);109if ((result.size() != 1) || (result.get(0) != t1)) {110throw new Exception("no card present");111}112113if (terminals.list(CARD_REMOVAL).size() != 0) {114throw new Exception("List not empty: " + result);115}116if (terminals.list(CARD_REMOVAL).size() != 0) {117throw new Exception("List not empty: " + result);118}119120121if (terminals.waitForChange(1000)) {122throw new Exception("no timeout");123}124125if (terminals.list(CARD_REMOVAL).size() != 0) {126throw new Exception("List not empty: " + result);127}128129if (multipleReaders) {130System.out.println("*** Insert card into other reader!");131terminals.waitForChange();132133result = terminals.list(CARD_INSERTION);134System.out.println(result);135if (result.size() != 1) {136throw new Exception("no card present");137}138CardTerminal t2 = result.get(0);139if (t1.getName().equals(t2.getName())) {140throw new Exception("same terminal");141}142143System.out.println("*** Remove card from 2nd reader!");144terminals.waitForChange();145146if (terminals.list(CARD_INSERTION).size() != 0) {147throw new Exception("List not empty: " + result);148}149result = terminals.list(CARD_REMOVAL);150if ((result.size() != 1) || (result.get(0) != t2)) {151throw new Exception("no or wrong terminal");152}153}154155System.out.println("*** Remove card from 1st reader!");156terminals.waitForChange();157158if (terminals.list(CARD_INSERTION).size() != 0) {159throw new Exception("List not empty: " + result);160}161result = terminals.list(CARD_REMOVAL);162if ((result.size() != 1) || (result.get(0) != t1)) {163throw new Exception("no or wrong terminal");164}165166System.out.println("OK.");167}168169}170171172