Path: blob/master/test/jdk/com/sun/jndi/dns/ConfigTests/AuthTest.java
41155 views
/*1* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.naming.Context;24import javax.naming.NameNotFoundException;25import javax.naming.NamingException;26import javax.naming.directory.Attributes;27import javax.naming.directory.InitialDirContext;28import java.util.Hashtable;2930/*31* @test32* @bug 820015133* @summary Tests that we can get the attributes of DNS entries for34* authoritative data. And nonauthoritative data by default or35* java.naming.authoritative is set to false, but cannot when36* java.naming.authoritative is set to true.37* @library ../lib/38* @modules java.base/sun.security.util39* @run main AuthTest -Dtestname=AuthDefault40* @run main AuthTest -Dtestname=AuthFalse41* @run main AuthTest -Dtestname=AuthTrue42*/4344public class AuthTest extends AuthRecursiveBase {4546public static void main(String[] args) throws Exception {47new AuthTest().run(args);48}4950/*51* Tests that we can get the attributes of DNS entries for52* authoritative data. And nonauthoritative data by default or53* java.naming.authoritative is set to false, but cannot when54* java.naming.authoritative is set to true.55*/56@Override57public void runTest() throws Exception {58String flag = parseFlagFromTestName();5960if (flag.equalsIgnoreCase("default")) {61setContext(new InitialDirContext());62} else {63Hashtable<Object, Object> env = new Hashtable<>();64DNSTestUtils.debug("set java.naming.authoritative to " + flag);65// java.naming.authoritative is set to true or false66env.put(Context.AUTHORITATIVE, flag);67setContext(new InitialDirContext(env));68}6970retrieveAndVerifyAuthData();71retrieveNonAuthData(Boolean.parseBoolean(flag));72}7374private void retrieveAndVerifyAuthData() throws NamingException {75// Ensure that auth data retrieval is OK76retrieveAndVerifyData(getFqdnUrl(), new String[] { "*" });77}7879/*80* If isAuth == true, ensure that nonauth data retrieval cannot be retrieved.81* If isAuth == false, ensure that nonauth data retrieval is OK, skip82* checking attributes for foreign; just successful operation is sufficient.83*/84private void retrieveNonAuthData(boolean isAuth) throws NamingException {85try {86Attributes retAttrs = context()87.getAttributes(getForeignFqdnUrl(), new String[] { "*" });88DNSTestUtils.debug(retAttrs);89if (isAuth) {90throw new RuntimeException(91"Failed: Expecting nonauth entry not found "92+ getForeignFqdnUrl());93}94} catch (NameNotFoundException e) {95if (isAuth) {96System.out.println("Got expected exception: " + e);97} else {98throw e;99}100}101}102}103104105