test/jdk/com/sun/jndi/dns/ConfigTests/AuthTest.java
changeset 52205 a562c65c3c74
equal deleted inserted replaced
52204:cf3fafc740bb 52205:a562c65c3c74
       
     1 /*
       
     2  * Copyright (c) 2000, 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 import javax.naming.Context;
       
    25 import javax.naming.NameNotFoundException;
       
    26 import javax.naming.NamingException;
       
    27 import javax.naming.directory.Attributes;
       
    28 import javax.naming.directory.InitialDirContext;
       
    29 import java.util.Hashtable;
       
    30 
       
    31 /*
       
    32  * @test
       
    33  * @bug 8200151
       
    34  * @summary Tests that we can get the attributes of DNS entries for
       
    35  *          authoritative data. And nonauthoritative data by default or
       
    36  *          java.naming.authoritative is set to false, but cannot when
       
    37  *          java.naming.authoritative is set to true.
       
    38  * @library ../lib/
       
    39  * @modules java.base/sun.security.util
       
    40  * @run main AuthTest -Dtestname=AuthDefault
       
    41  * @run main AuthTest -Dtestname=AuthFalse
       
    42  * @run main AuthTest -Dtestname=AuthTrue
       
    43  */
       
    44 
       
    45 public class AuthTest extends AuthRecursiveBase {
       
    46 
       
    47     public static void main(String[] args) throws Exception {
       
    48         new AuthTest().run(args);
       
    49     }
       
    50 
       
    51     /*
       
    52      * Tests that we can get the attributes of DNS entries for
       
    53      * authoritative data. And nonauthoritative data by default or
       
    54      * java.naming.authoritative is set to false, but cannot when
       
    55      * java.naming.authoritative is set to true.
       
    56      */
       
    57     @Override
       
    58     public void runTest() throws Exception {
       
    59         String flag = parseFlagFromTestName();
       
    60 
       
    61         if (flag.equalsIgnoreCase("default")) {
       
    62             setContext(new InitialDirContext());
       
    63         } else {
       
    64             Hashtable<Object, Object> env = new Hashtable<>();
       
    65             DNSTestUtils.debug("set java.naming.authoritative to " + flag);
       
    66             // java.naming.authoritative is set to true or false
       
    67             env.put(Context.AUTHORITATIVE, flag);
       
    68             setContext(new InitialDirContext(env));
       
    69         }
       
    70 
       
    71         retrieveAndVerifyAuthData();
       
    72         retrieveNonAuthData(Boolean.parseBoolean(flag));
       
    73     }
       
    74 
       
    75     private void retrieveAndVerifyAuthData() throws NamingException {
       
    76         // Ensure that auth data retrieval is OK
       
    77         retrieveAndVerifyData(getFqdnUrl(), new String[] { "*" });
       
    78     }
       
    79 
       
    80     /*
       
    81      * If isAuth == true, ensure that nonauth data retrieval cannot be retrieved.
       
    82      * If isAuth == false, ensure that nonauth data retrieval is OK, skip
       
    83      * checking attributes for foreign; just successful operation is sufficient.
       
    84      */
       
    85     private void retrieveNonAuthData(boolean isAuth) throws NamingException {
       
    86         try {
       
    87             Attributes retAttrs = context()
       
    88                     .getAttributes(getForeignFqdnUrl(), new String[] { "*" });
       
    89             DNSTestUtils.debug(retAttrs);
       
    90             if (isAuth) {
       
    91                 throw new RuntimeException(
       
    92                         "Failed: Expecting nonauth entry not found "
       
    93                                 + getForeignFqdnUrl());
       
    94             }
       
    95         } catch (NameNotFoundException e) {
       
    96             if (isAuth) {
       
    97                 System.out.println("Got expected exception: " + e);
       
    98             } else {
       
    99                 throw e;
       
   100             }
       
   101         }
       
   102     }
       
   103 }