jdk/test/sun/security/krb5/IPv6.java
changeset 3864 21eb0b487d1f
child 3944 e098e010b520
equal deleted inserted replaced
3863:8e0f58b1c072 3864:21eb0b487d1f
       
     1 /*
       
     2  * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6877357
       
    27  * @summary IPv6 address does not work
       
    28  */
       
    29 
       
    30 import com.sun.security.auth.module.Krb5LoginModule;
       
    31 import java.io.*;
       
    32 import java.util.HashMap;
       
    33 import java.util.Map;
       
    34 import java.util.regex.Matcher;
       
    35 import java.util.regex.Pattern;
       
    36 import javax.security.auth.Subject;
       
    37 
       
    38 public class IPv6 {
       
    39 
       
    40     public static void main(String[] args) throws Exception {
       
    41 
       
    42         String[][] kdcs = {
       
    43                 {"simple.host", null},  // These are legal settings
       
    44                 {"simple.host", ""},
       
    45                 {"simple.host", "8080"},
       
    46                 {"0.0.0.1", null},
       
    47                 {"0.0.0.1", ""},
       
    48                 {"0.0.0.1", "8080"},
       
    49                 {"1::1", null},
       
    50                 {"[1::1]", null},
       
    51                 {"[1::1]", ""},
       
    52                 {"[1::1]", "8080"},
       
    53                 {"[1::1", null},        // Two illegal settings
       
    54                 {"[1::1]abc", null},
       
    55         };
       
    56         // Prepares a krb5.conf with every kind of KDC settings
       
    57         PrintStream out = new PrintStream(new FileOutputStream("ipv6.conf"));
       
    58         out.println("[libdefaults]");
       
    59         out.println("default_realm = V6");
       
    60         out.println("[realms]");
       
    61         out.println("V6 = {");
       
    62         for (String[] hp: kdcs) {
       
    63             if (hp[1] != null) out.println("    kdc = "+hp[0]+":"+hp[1]);
       
    64             else out.println("    kdc = " + hp[0]);
       
    65         }
       
    66         out.println("}");
       
    67         out.close();
       
    68 
       
    69         System.setProperty("sun.security.krb5.debug", "true");
       
    70         System.setProperty("java.security.krb5.conf", "ipv6.conf");
       
    71 
       
    72         ByteArrayOutputStream bo = new ByteArrayOutputStream();
       
    73         PrintStream po = new PrintStream(bo);
       
    74         PrintStream oldout = System.out;
       
    75         System.setOut(po);
       
    76 
       
    77         try {
       
    78             Subject subject = new Subject();
       
    79             Krb5LoginModule krb5 = new Krb5LoginModule();
       
    80             Map<String, String> map = new HashMap<String, String>();
       
    81             Map<String, Object> shared = new HashMap<String, Object>();
       
    82 
       
    83             map.put("debug", "true");
       
    84             map.put("doNotPrompt", "true");
       
    85             map.put("useTicketCache", "false");
       
    86             map.put("useFirstPass", "true");
       
    87             shared.put("javax.security.auth.login.name", "any");
       
    88             shared.put("javax.security.auth.login.password", "any".toCharArray());
       
    89             krb5.initialize(subject, null, shared, map);
       
    90             krb5.login();
       
    91         } catch (Exception e) {
       
    92             // Ignore
       
    93         }
       
    94 
       
    95         po.flush();
       
    96 
       
    97         System.setOut(oldout);
       
    98         String[] lines = new String(bo.toByteArray()).split("\n");
       
    99         int cc = 0;
       
   100         Pattern r = Pattern.compile(".*KrbKdcReq send: kdc=(.*) UDP:(\\d+),.*");
       
   101         for (String line: lines) {
       
   102             Matcher m = r.matcher(line.subSequence(0, line.length()));
       
   103             if (m.matches()) {
       
   104                 System.out.println("------------------");
       
   105                 System.out.println(line);
       
   106                 String h = m.group(1), p = m.group(2);
       
   107                 String eh = kdcs[cc][0], ep = kdcs[cc][1];
       
   108                 if (eh.charAt(0) == '[') {
       
   109                     eh = eh.substring(1, eh.length()-1);
       
   110                 }
       
   111                 System.out.println("Expected: " + eh + " : " + ep);
       
   112                 System.out.println("Actual: " + h + " : " + p);
       
   113                 if (!eh.equals(h) ||
       
   114                         (ep == null || ep.length() == 0) && !p.equals("88") ||
       
   115                         (ep != null && ep.length() > 0) && !p.equals(ep)) {
       
   116                     throw new Exception("Mismatch");
       
   117                 }
       
   118                 cc++;
       
   119             }
       
   120         }
       
   121         if (cc != kdcs.length - 2) {    // 2 illegal settings at the end
       
   122             throw new Exception("Not traversed");
       
   123         }
       
   124     }
       
   125 }
       
   126 
       
   127