1 /*
   2  * Copyright (c) 2002, 2018, 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 8208279
  27  * @summary If DNS name resolution is configured normally for SWAN on the local
  28  *          machine, construct a context using the configured servers and verify
  29  *          that a PROVIDER_URL property is generated for the context.
  30  *          If DNS is not configured, or if JDK version is earlier than 1.4.1,
  31  *          then this test is a no-op (it passes without doing anything).
  32  * @modules jdk.naming.dns/com.sun.jndi.dns
  33  *          java.base/sun.security.util
  34  * @library ../lib/
  35  * @run main ProviderUrlGen
  36  */
  37 
  38 import javax.naming.Context;
  39 import javax.naming.directory.DirContext;
  40 import javax.naming.directory.InitialDirContext;
  41 import java.util.Hashtable;
  42 
  43 public class ProviderUrlGen {
  44 
  45     public static void main(String[] args) throws Exception {
  46         Hashtable<Object, Object> env;
  47 
  48         // initialize test
  49         env = DNSTestUtils.initEnv(false, ProviderUrlGen.class.getName(), args);
  50 
  51         env.remove(Context.PROVIDER_URL);
  52 
  53         DirContext ctx = null;
  54 
  55         try {
  56             if (!isPlatformServersAvailable()) {
  57                 System.out.println(
  58                         "DNS not configured.  There's nothing to test.");
  59                 return;
  60             }
  61 
  62             ctx = new InitialDirContext(env);
  63 
  64             String providerUrl = (String) ctx.getEnvironment()
  65                     .get(Context.PROVIDER_URL);
  66 
  67             if (providerUrl == null) {
  68                 throw new RuntimeException("Failed:  PROVIDER_URL not set");
  69             }
  70 
  71             DNSTestUtils.debug("PROVIDER_URL = \"" + providerUrl + "\"");
  72 
  73             // We don't know exactly what providerUrl's value should be.
  74             // Check that it is a space-separated list of one or more URLs
  75             // of the form "dns://xxxxxx".
  76 
  77             String[] urls = providerUrl.split(" ");
  78             if (urls.length < 1) {
  79                 throw new RuntimeException("Failed:  PROVIDER_URL is empty");
  80             }
  81 
  82             for (String url : urls) {
  83                 DNSTestUtils.debug(url);
  84                 if (!checkUrl(url)) {
  85                     throw new RuntimeException(
  86                             "Failed:  Unexpected DNS URL: " + url);
  87                 }
  88             }
  89 
  90         } finally {
  91             DNSTestUtils.cleanup(ctx);
  92         }
  93     }
  94 
  95     private static boolean checkUrl(String url) {
  96         return url.startsWith("dns://") && url.length() >= 7;
  97     }
  98 
  99     private static boolean isPlatformServersAvailable() {
 100         try {
 101             java.lang.reflect.Method psa = com.sun.jndi.dns.DnsContextFactory.class
 102                     .getMethod("platformServersAvailable", null);
 103             return (Boolean) psa.invoke(null, null);
 104         } catch (Exception e) {
 105             e.printStackTrace();
 106             return false;
 107         }
 108     }
 109 }