8059212: Modify sun/security/smartcardio manual regression tests so that they do not just fail if no cardreader found
authorigerasim
Fri, 12 Feb 2016 00:20:16 +0300
changeset 35778 e2a3c03097de
parent 35777 656b9a28f19e
child 35779 b2eca35d660c
child 35973 eadb1818d0a1
8059212: Modify sun/security/smartcardio manual regression tests so that they do not just fail if no cardreader found Reviewed-by: mullan
jdk/test/sun/security/smartcardio/TestChannel.java
jdk/test/sun/security/smartcardio/TestConnect.java
jdk/test/sun/security/smartcardio/TestConnectAgain.java
jdk/test/sun/security/smartcardio/TestControl.java
jdk/test/sun/security/smartcardio/TestDefault.java
jdk/test/sun/security/smartcardio/TestDirect.java
jdk/test/sun/security/smartcardio/TestExclusive.java
jdk/test/sun/security/smartcardio/TestMultiplePresent.java
jdk/test/sun/security/smartcardio/TestPresent.java
jdk/test/sun/security/smartcardio/TestTransmit.java
jdk/test/sun/security/smartcardio/Utils.java
--- a/jdk/test/sun/security/smartcardio/TestChannel.java	Thu Feb 11 11:19:03 2016 -0500
+++ b/jdk/test/sun/security/smartcardio/TestChannel.java	Fri Feb 12 00:20:16 2016 +0300
@@ -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");
--- a/jdk/test/sun/security/smartcardio/TestConnect.java	Thu Feb 11 11:19:03 2016 -0500
+++ b/jdk/test/sun/security/smartcardio/TestConnect.java	Fri Feb 12 00:20:16 2016 +0300
@@ -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<CardTerminal> 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);
--- a/jdk/test/sun/security/smartcardio/TestConnectAgain.java	Thu Feb 11 11:19:03 2016 -0500
+++ b/jdk/test/sun/security/smartcardio/TestConnectAgain.java	Fri Feb 12 00:20:16 2016 +0300
@@ -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();
--- a/jdk/test/sun/security/smartcardio/TestControl.java	Thu Feb 11 11:19:03 2016 -0500
+++ b/jdk/test/sun/security/smartcardio/TestControl.java	Fri Feb 12 00:20:16 2016 +0300
@@ -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");
--- a/jdk/test/sun/security/smartcardio/TestDefault.java	Thu Feb 11 11:19:03 2016 -0500
+++ b/jdk/test/sun/security/smartcardio/TestDefault.java	Fri Feb 12 00:20:16 2016 +0300
@@ -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<CardTerminal> 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.");
     }
--- a/jdk/test/sun/security/smartcardio/TestDirect.java	Thu Feb 11 11:19:03 2016 -0500
+++ b/jdk/test/sun/security/smartcardio/TestDirect.java	Fri Feb 12 00:20:16 2016 +0300
@@ -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<CardTerminal> 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);
--- a/jdk/test/sun/security/smartcardio/TestExclusive.java	Thu Feb 11 11:19:03 2016 -0500
+++ b/jdk/test/sun/security/smartcardio/TestExclusive.java	Fri Feb 12 00:20:16 2016 +0300
@@ -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");
--- a/jdk/test/sun/security/smartcardio/TestMultiplePresent.java	Thu Feb 11 11:19:03 2016 -0500
+++ b/jdk/test/sun/security/smartcardio/TestMultiplePresent.java	Fri Feb 12 00:20:16 2016 +0300
@@ -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;
--- a/jdk/test/sun/security/smartcardio/TestPresent.java	Thu Feb 11 11:19:03 2016 -0500
+++ b/jdk/test/sun/security/smartcardio/TestPresent.java	Fri Feb 12 00:20:16 2016 +0300
@@ -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<CardTerminal> 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!");
--- a/jdk/test/sun/security/smartcardio/TestTransmit.java	Thu Feb 11 11:19:03 2016 -0500
+++ b/jdk/test/sun/security/smartcardio/TestTransmit.java	Fri Feb 12 00:20:16 2016 +0300
@@ -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();
--- a/jdk/test/sun/security/smartcardio/Utils.java	Thu Feb 11 11:19:03 2016 -0500
+++ b/jdk/test/sun/security/smartcardio/Utils.java	Fri Feb 12 00:20:16 2016 +0300
@@ -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<CardTerminal> terminals = factory.terminals().list();
+            System.out.println("Terminals: " + terminals);
+            if (terminals.isEmpty()) {
+                return null;
+            }
+            CardTerminal terminal = terminals.get(0);
 
-        List<CardTerminal> terminals = factory.terminals().list();
-        System.out.println("Terminals: " + terminals);
-        if (terminals.isEmpty()) {
-            throw new Exception("No card terminals available");
+            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");