1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 7191662
  27  * @summary Ensure non-java.base providers can be found by ServiceLoader
  28  * @author Valerie Peng
  29  */
  30 
  31 import java.util.*;
  32 import java.security.*;
  33 
  34 public class DefaultProviderList {
  35 
  36     public static void main(String[] args) throws Exception {
  37         Provider[] defaultProvs = Security.getProviders();
  38         System.out.println("Providers: " + Arrays.asList(defaultProvs));
  39         System.out.println();
  40 
  41         ServiceLoader<Provider> sl = ServiceLoader.load(Provider.class);
  42         boolean failed = false;
  43         for (Provider p : defaultProvs) {
  44             String pName = p.getName();
  45             // only providers outside java.base are loaded by ServiceLoader
  46             if (pName.equals("SUN") || pName.equals("SunRsaSign") ||
  47                 pName.equals("SunJCE") || pName.equals("SunJSSE")) {
  48                 System.out.println("Skip test for provider " + pName);
  49                 continue;
  50             }
  51             String pClassName = p.getClass().getName();
  52             String pArg = p.getArgument();
  53             // Should be able to find each one through ServiceLoader
  54             Iterator<Provider> provIter = sl.iterator();
  55             boolean found = false;
  56             while (provIter.hasNext()) {
  57                 Provider pFromSL = provIter.next();
  58                 if (pFromSL.getClass().getName().equals(pClassName)) {
  59                     if (pArg == null || pArg.length() == 0) {
  60                         // no further config needed, we found a match
  61                         found = (pFromSL.getName().equals(pName));
  62                         break;
  63                     } else {
  64                         // need to configure w/ same args
  65                         Provider p2 = pFromSL.configure(pArg);
  66                         found = (p2.getName().equals(pName));
  67                     }
  68                 }
  69             }
  70             System.out.println("Found " + pName + " = " + found);
  71             if (!found) {
  72                 failed = true;
  73                 System.out.println("Error: no provider " + pName +
  74                     " found");
  75             }
  76         }
  77         if (!failed) {
  78             System.out.println("Test Passed");
  79         } else {
  80             throw new Exception("One or more provider not loaded by SL");
  81         }
  82     }
  83 }