Path: blob/master/test/jdk/sun/security/krb5/IPv6.java
41149 views
/*1* Copyright (c) 2009, 2016, 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*/2223/*24* @test25* @bug 6877357 688516626* @run main/othervm IPv627* @modules jdk.security.auth28* @summary IPv6 address does not work29*/3031import com.sun.security.auth.module.Krb5LoginModule;32import java.io.BufferedReader;33import java.io.ByteArrayOutputStream;34import java.io.FileOutputStream;35import java.io.PrintStream;36import java.io.StringReader;37import java.util.HashMap;38import java.util.Map;39import java.util.regex.Matcher;40import java.util.regex.Pattern;41import javax.security.auth.Subject;4243public class IPv6 {4445public static void main(String[] args) throws Exception {4647String[][] kdcs = {48{"simple.host", null}, // These are legal settings49{"simple.host", ""},50{"simple.host", "8080"},51{"0.0.0.1", null},52{"0.0.0.1", ""},53{"0.0.0.1", "8080"},54{"1::1", null},55{"[1::1]", null},56{"[1::1]", ""},57{"[1::1]", "8080"},58{"[1::1", null}, // Two illegal settings59{"[1::1]abc", null},60};61// Prepares a krb5.conf with every kind of KDC settings62PrintStream out = new PrintStream(new FileOutputStream("ipv6.conf"));63out.println("[libdefaults]");64out.println("default_realm = V6");65out.println("kdc_timeout = 1");66out.println("[realms]");67out.println("V6 = {");68for (String[] hp: kdcs) {69if (hp[1] != null) out.println(" kdc = "+hp[0]+":"+hp[1]);70else out.println(" kdc = " + hp[0]);71}72out.println("}");73out.close();7475System.setProperty("sun.security.krb5.debug", "true");76System.setProperty("java.security.krb5.conf", "ipv6.conf");7778ByteArrayOutputStream bo = new ByteArrayOutputStream();79PrintStream po = new PrintStream(bo);80PrintStream oldout = System.out;81System.setOut(po);8283try {84Subject subject = new Subject();85Krb5LoginModule krb5 = new Krb5LoginModule();86Map<String, String> map = new HashMap<>();87Map<String, Object> shared = new HashMap<>();8889map.put("debug", "true");90map.put("doNotPrompt", "true");91map.put("useTicketCache", "false");92map.put("useFirstPass", "true");93shared.put("javax.security.auth.login.name", "any");94shared.put("javax.security.auth.login.password", "any".toCharArray());95krb5.initialize(subject, null, shared, map);96krb5.login();97} catch (Exception e) {98// Ignore99}100101po.flush();102103System.setOut(oldout);104BufferedReader br = new BufferedReader(new StringReader(105new String(bo.toByteArray())));106int cc = 0;107Pattern r = Pattern.compile(".*KrbKdcReq send: kdc=(.*) UDP:(\\d+),.*");108String line;109while ((line = br.readLine()) != null) {110Matcher m = r.matcher(line.subSequence(0, line.length()));111if (m.matches()) {112System.out.println("------------------");113System.out.println(line);114String h = m.group(1), p = m.group(2);115String eh = kdcs[cc][0], ep = kdcs[cc][1];116if (eh.charAt(0) == '[') {117eh = eh.substring(1, eh.length()-1);118}119System.out.println("Expected: " + eh + " : " + ep);120System.out.println("Actual: " + h + " : " + p);121if (!eh.equals(h) ||122(ep == null || ep.length() == 0) && !p.equals("88") ||123(ep != null && ep.length() > 0) && !p.equals(ep)) {124throw new Exception("Mismatch");125}126cc++;127}128}129if (cc != kdcs.length - 2) { // 2 illegal settings at the end130throw new Exception("Not traversed");131}132}133}134135136