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  * @library ../lib/
  33  * @modules jdk.naming.dns/com.sun.jndi.dns
  34  *          java.base/sun.security.util
  35  * @run main ProviderUrlGen
  36  */
  37 
  38 import javax.naming.Context;
  39 import javax.naming.directory.InitialDirContext;
  40 
  41 public class ProviderUrlGen extends DNSTestBase {
  42 
  43     public ProviderUrlGen() {
  44         setLocalServer(false);
  45     }
  46 
  47     public static void main(String[] args) throws Exception {
  48         if (!isPlatformServersAvailable()) {
  49             DNSTestUtils.debug("DNS not configured. There's nothing to test.");
  50             return;
  51         }
  52 
  53         new ProviderUrlGen().run(args);
  54     }
  55 
  56     @Override public void runTest() throws Exception {
  57         initContext();
  58 
  59         String providerUrl = (String) context().getEnvironment()
  60                 .get(Context.PROVIDER_URL);
  61 
  62         if (providerUrl == null) {
  63             throw new RuntimeException("Failed: PROVIDER_URL not set");
  64         }
  65 
  66         DNSTestUtils.debug("PROVIDER_URL = \"" + providerUrl + "\"");
  67 
  68         // We don't know exactly what providerUrl's value should be.
  69         // Check that it is a space-separated list of one or more URLs
  70         // of the form "dns://xxxxxx".
  71 
  72         String[] urls = providerUrl.split(" ");
  73         if (urls.length < 1) {
  74             throw new RuntimeException("Failed:  PROVIDER_URL is empty");
  75         }
  76 
  77         for (String url : urls) {
  78             DNSTestUtils.debug(url);
  79             if (!checkUrl(url)) {
  80                 throw new RuntimeException(
  81                         "Failed:  Unexpected DNS URL: " + url);
  82             }
  83         }
  84     }
  85 
  86     private void initContext() throws Exception {
  87         env().remove(Context.PROVIDER_URL);
  88         setContext(new InitialDirContext(env()));
  89     }
  90 
  91     private static boolean checkUrl(String url) {
  92         return url.startsWith("dns://") && url.length() >= 7;
  93     }
  94 
  95     private static boolean isPlatformServersAvailable() {
  96         try {
  97             java.lang.reflect.Method psa = com.sun.jndi.dns.DnsContextFactory.class
  98                     .getMethod("platformServersAvailable", null);
  99             return (Boolean) psa.invoke(null, null);
 100         } catch (Exception e) {
 101             e.printStackTrace();
 102             return false;
 103         }
 104     }
 105 }