# HG changeset patch # User igerasim # Date 1454081686 -10800 # Fri Jan 29 18:34:46 2016 +0300 # Node ID 8423a04ce4da71c6947c1a1275ede7793d1e60b9 # Parent 7cfb37e4dca5c60c396b8b2023e77fa88be1df8a imported patch 8059212-Modify-smartcardio-manual-regression-tests-so-that-they-do-not-just-fail-if-no-cardreader-found diff --git a/test/sun/security/smartcardio/TestChannel.java b/test/sun/security/smartcardio/TestChannel.java --- a/test/sun/security/smartcardio/TestChannel.java +++ b/test/sun/security/smartcardio/TestChannel.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -47,6 +47,11 @@ public static void main(String[] args) throws Exception { CardTerminal terminal = getTerminal(args); + if (terminal == null) { + System.out.println("Skipping the test: " + + "no card terminals available"); + return; + } // establish a connection with the card Card card = terminal.connect("T=0"); diff --git a/test/sun/security/smartcardio/TestConnect.java b/test/sun/security/smartcardio/TestConnect.java --- a/test/sun/security/smartcardio/TestConnect.java +++ b/test/sun/security/smartcardio/TestConnect.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -40,23 +40,12 @@ public class TestConnect extends Utils { public static void main(String[] args) throws Exception { - TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null, "SunPCSC"); - System.out.println(factory); - - List terminals = factory.terminals().list(); - System.out.println("Terminals: " + terminals); - if (terminals.isEmpty()) { - throw new Exception("No card terminals available"); + CardTerminal terminal = getTerminal(args, "SunPCSC"); + if (terminal == null) { + System.out.println("Skipping the test: " + + "no card terminals available"); + return; } - CardTerminal terminal = terminals.get(0); - - if (terminal.isCardPresent() == false) { - System.out.println("*** Insert card"); - if (terminal.waitForCardPresent(20 * 1000) == false) { - throw new Exception("no card available"); - } - } - System.out.println("card present: " + terminal.isCardPresent()); Card card = terminal.connect("*"); System.out.println("card: " + card); diff --git a/test/sun/security/smartcardio/TestConnectAgain.java b/test/sun/security/smartcardio/TestConnectAgain.java --- a/test/sun/security/smartcardio/TestConnectAgain.java +++ b/test/sun/security/smartcardio/TestConnectAgain.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -40,6 +40,11 @@ public static void main(String[] args) throws Exception { CardTerminal terminal = getTerminal(args); + if (terminal == null) { + System.out.println("Skipping the test: " + + "no card terminals available"); + return; + } Card card = terminal.connect("T=0"); CardChannel channel = card.getBasicChannel(); diff --git a/test/sun/security/smartcardio/TestControl.java b/test/sun/security/smartcardio/TestControl.java --- a/test/sun/security/smartcardio/TestControl.java +++ b/test/sun/security/smartcardio/TestControl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -41,6 +41,11 @@ public static void main(String[] args) throws Exception { CardTerminal terminal = getTerminal(args); + if (terminal == null) { + System.out.println("Skipping the test: " + + "no card terminals available"); + return; + } // establish a connection with the card Card card = terminal.connect("T=0"); diff --git a/test/sun/security/smartcardio/TestDefault.java b/test/sun/security/smartcardio/TestDefault.java --- a/test/sun/security/smartcardio/TestDefault.java +++ b/test/sun/security/smartcardio/TestDefault.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -41,10 +41,12 @@ TerminalFactory factory = TerminalFactory.getDefault(); System.out.println("Type: " + factory.getType()); List terminals = factory.terminals().list(); + if (terminals.isEmpty()) { + System.out.println("Skipping the test: " + + "no card terminals available"); + return; + } System.out.println("Terminals: " + terminals); - if (terminals.isEmpty()) { - throw new Exception("no terminals"); - } System.out.println("OK."); } diff --git a/test/sun/security/smartcardio/TestDirect.java b/test/sun/security/smartcardio/TestDirect.java --- a/test/sun/security/smartcardio/TestDirect.java +++ b/test/sun/security/smartcardio/TestDirect.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -40,10 +40,12 @@ public static void main(String[] args) throws Exception { TerminalFactory terminalFactory = TerminalFactory.getDefault(); List cardTerminals = terminalFactory.terminals().list(); + if (cardTerminals.isEmpty()) { + System.out.println("Skipping the test: " + + "no card terminals available"); + return; + } System.out.println("Terminals: " + cardTerminals); - if (cardTerminals.isEmpty()) { - throw new Exception("No card terminals available"); - } CardTerminal cardTerminal = cardTerminals.get(0); Card card = cardTerminal.connect("DIRECT"); card.disconnect(true); diff --git a/test/sun/security/smartcardio/TestExclusive.java b/test/sun/security/smartcardio/TestExclusive.java --- a/test/sun/security/smartcardio/TestExclusive.java +++ b/test/sun/security/smartcardio/TestExclusive.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -45,6 +45,11 @@ public static void main(String[] args) throws Exception { CardTerminal terminal = getTerminal(args); + if (terminal == null) { + System.out.println("Skipping the test: " + + "no card terminals available"); + return; + } // establish a connection with the card Card card = terminal.connect("T=0"); diff --git a/test/sun/security/smartcardio/TestMultiplePresent.java b/test/sun/security/smartcardio/TestMultiplePresent.java --- a/test/sun/security/smartcardio/TestMultiplePresent.java +++ b/test/sun/security/smartcardio/TestMultiplePresent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -41,7 +41,12 @@ public static void main(String[] args) throws Exception { Utils.setLibrary(args); - TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null); + TerminalFactory factory = Utils.getTerminalFactory(null); + if (factory == null) { + System.out.println("Skipping the test: " + + "no card terminals available"); + return; + } System.out.println(factory); CardTerminals terminals = factory.terminals(); @@ -50,7 +55,9 @@ boolean multipleReaders = true; if (list.size() < 2) { if (list.isEmpty()) { - throw new Exception("no terminals"); + System.out.println("Skipping the test: " + + "no card terminals available"); + return; } System.out.println("Only one reader present, using simplified test"); multipleReaders = false; diff --git a/test/sun/security/smartcardio/TestPresent.java b/test/sun/security/smartcardio/TestPresent.java --- a/test/sun/security/smartcardio/TestPresent.java +++ b/test/sun/security/smartcardio/TestPresent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -35,7 +35,7 @@ import javax.smartcardio.CardTerminal; import javax.smartcardio.TerminalFactory; -public class TestPresent { +public class TestPresent extends Utils { private static class Timer { private long time = System.currentTimeMillis(); @@ -67,15 +67,12 @@ } public static void main(String[] args) throws Exception { - TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null); - System.out.println(factory); - - List terminals = factory.terminals().list(); - System.out.println("Terminals: " + terminals); - if (terminals.isEmpty()) { - throw new Exception("No card terminals available"); + CardTerminal terminal = getTerminal(args); + if (terminal == null) { + System.out.println("Skipping the test: " + + "no card terminals available"); + return; } - CardTerminal terminal = terminals.get(0); while (terminal.isCardPresent()) { System.out.println("*** Remove card!"); diff --git a/test/sun/security/smartcardio/TestTransmit.java b/test/sun/security/smartcardio/TestTransmit.java --- a/test/sun/security/smartcardio/TestTransmit.java +++ b/test/sun/security/smartcardio/TestTransmit.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -49,6 +49,11 @@ public static void main(String[] args) throws Exception { CardTerminal terminal = getTerminal(args); + if (terminal == null) { + System.out.println("Skipping the test: " + + "no card terminals available"); + return; + } Card card = terminal.connect("T=0"); CardChannel channel = card.getBasicChannel(); diff --git a/test/sun/security/smartcardio/Utils.java b/test/sun/security/smartcardio/Utils.java --- a/test/sun/security/smartcardio/Utils.java +++ b/test/sun/security/smartcardio/Utils.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,6 +27,7 @@ import java.io.StringReader; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.List; import javax.smartcardio.CardTerminal; @@ -43,26 +44,59 @@ } } + static TerminalFactory getTerminalFactory(String provName) throws Exception { + try { + TerminalFactory factory = (provName == null) + ? TerminalFactory.getInstance("PC/SC", null) + : TerminalFactory.getInstance("PC/SC", null, provName); + System.out.println(factory); + return factory; + } catch (NoSuchAlgorithmException e) { + Throwable cause = e.getCause(); + if (cause != null && cause.getMessage().startsWith("PC/SC not available")) { + return null; + } + throw e; + } + } + static CardTerminal getTerminal(String[] args) throws Exception { + return getTerminal(args, null); + } + + static CardTerminal getTerminal(String[] args, String provider) throws Exception { setLibrary(args); - TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null); - System.out.println(factory); + try { + TerminalFactory factory = (provider == null) + ? TerminalFactory.getInstance("PC/SC", null) + : TerminalFactory.getInstance("PC/SC", null, provider); + System.out.println(factory); - List terminals = factory.terminals().list(); - System.out.println("Terminals: " + terminals); - if (terminals.isEmpty()) { - throw new Exception("No card terminals available"); + List terminals = factory.terminals().list(); + System.out.println("Terminals: " + terminals); + if (terminals.isEmpty()) { + return null; + } + CardTerminal terminal = terminals.get(0); + + if (terminal.isCardPresent() == false) { + System.out.println("*** Insert card"); + if (terminal.waitForCardPresent(20 * 1000) == false) { + throw new Exception("no card available"); + } + } + System.out.println("card present: " + terminal.isCardPresent()); + + return terminal; + + } catch (NoSuchAlgorithmException e) { + Throwable cause = e.getCause(); + if (cause != null && cause.getMessage().startsWith("PC/SC not available")) { + return null; + } + throw e; } - CardTerminal terminal = terminals.get(0); - - if (terminal.isCardPresent() == false) { - System.out.println("*** Insert card"); - if (terminal.waitForCardPresent(20 * 1000) == false) { - throw new Exception("no card available"); - } - } - return terminal; } static final byte[] C1 = parse("00 A4 04 00 07 A0 00 00 00 62 81 01 00");