test/jdk/com/sun/jndi/dns/ConfigTests/AuthTest.java
author xyin
Mon, 22 Oct 2018 13:53:39 +0800
changeset 52205 a562c65c3c74
permissions -rw-r--r--
8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/ Reviewed-by: vtewari, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52205
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     1
/*
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     4
 *
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     8
 *
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    13
 * accompanied this code).
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    14
 *
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    18
 *
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    21
 * questions.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    22
 */
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    23
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    24
import javax.naming.Context;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    25
import javax.naming.NameNotFoundException;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    26
import javax.naming.NamingException;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    27
import javax.naming.directory.Attributes;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    28
import javax.naming.directory.InitialDirContext;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    29
import java.util.Hashtable;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    30
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    31
/*
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    32
 * @test
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    33
 * @bug 8200151
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    34
 * @summary Tests that we can get the attributes of DNS entries for
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    35
 *          authoritative data. And nonauthoritative data by default or
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    36
 *          java.naming.authoritative is set to false, but cannot when
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    37
 *          java.naming.authoritative is set to true.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    38
 * @library ../lib/
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    39
 * @modules java.base/sun.security.util
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    40
 * @run main AuthTest -Dtestname=AuthDefault
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    41
 * @run main AuthTest -Dtestname=AuthFalse
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    42
 * @run main AuthTest -Dtestname=AuthTrue
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    43
 */
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    44
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    45
public class AuthTest extends AuthRecursiveBase {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    46
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    48
        new AuthTest().run(args);
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    49
    }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    50
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    51
    /*
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    52
     * Tests that we can get the attributes of DNS entries for
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    53
     * authoritative data. And nonauthoritative data by default or
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    54
     * java.naming.authoritative is set to false, but cannot when
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    55
     * java.naming.authoritative is set to true.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    56
     */
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    57
    @Override
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    58
    public void runTest() throws Exception {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    59
        String flag = parseFlagFromTestName();
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    60
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    61
        if (flag.equalsIgnoreCase("default")) {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    62
            setContext(new InitialDirContext());
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    63
        } else {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    64
            Hashtable<Object, Object> env = new Hashtable<>();
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    65
            DNSTestUtils.debug("set java.naming.authoritative to " + flag);
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    66
            // java.naming.authoritative is set to true or false
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    67
            env.put(Context.AUTHORITATIVE, flag);
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    68
            setContext(new InitialDirContext(env));
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    69
        }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    70
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    71
        retrieveAndVerifyAuthData();
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    72
        retrieveNonAuthData(Boolean.parseBoolean(flag));
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    73
    }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    74
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    75
    private void retrieveAndVerifyAuthData() throws NamingException {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    76
        // Ensure that auth data retrieval is OK
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    77
        retrieveAndVerifyData(getFqdnUrl(), new String[] { "*" });
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    78
    }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    79
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    80
    /*
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    81
     * If isAuth == true, ensure that nonauth data retrieval cannot be retrieved.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    82
     * If isAuth == false, ensure that nonauth data retrieval is OK, skip
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    83
     * checking attributes for foreign; just successful operation is sufficient.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    84
     */
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    85
    private void retrieveNonAuthData(boolean isAuth) throws NamingException {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    86
        try {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    87
            Attributes retAttrs = context()
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    88
                    .getAttributes(getForeignFqdnUrl(), new String[] { "*" });
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    89
            DNSTestUtils.debug(retAttrs);
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    90
            if (isAuth) {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    91
                throw new RuntimeException(
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    92
                        "Failed: Expecting nonauth entry not found "
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    93
                                + getForeignFqdnUrl());
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    94
            }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    95
        } catch (NameNotFoundException e) {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    96
            if (isAuth) {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    97
                System.out.println("Got expected exception: " + e);
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    98
            } else {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    99
                throw e;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   100
            }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   101
        }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   102
    }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   103
}