1 /*
   2  * Copyright (c) 2012, 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  * @test
  25  * @bug 6966259
  26  * @summary Make PrincipalName and Realm immutable
  27  * @run main/othervm Constructors
  28  */
  29 
  30 import java.util.Arrays;
  31 import sun.security.krb5.*;
  32 
  33 public class Constructors {
  34     public static void main(String[] args) throws Exception {
  35 
  36         int type;
  37         boolean testNoDefaultDomain;
  38 
  39         // Part 1: on format
  40 
  41         // Good ones
  42         type = PrincipalName.KRB_NT_UNKNOWN;
  43         checkName("a", type, "R", "R", "a");
  44         checkName("a@R2", type, "R", "R", "a");
  45         checkName("a/b", type, "R", "R", "a", "b");
  46         checkName("a/b@R2", type, "R", "R", "a", "b");
  47         checkName("a/b/c", type, "R", "R", "a", "b", "c");
  48         checkName("a/b/c@R2", type, "R", "R", "a", "b", "c");
  49         // Weird ones
  50         checkName("a\\/b", type, "R", "R", "a/b");
  51         checkName("a\\/b\\/c", type, "R", "R", "a/b/c");
  52         checkName("a\\/b\\@R2", type, "R", "R", "a/b@R2");
  53         // Bad ones
  54         checkName("a", type, "", null);
  55         checkName("a/", type, "R", null);
  56         checkName("/a", type, "R", null);
  57         checkName("a//b", type, "R", null);
  58         checkName("a@", type, null, null);
  59         type = PrincipalName.KRB_NT_SRV_HST;
  60 
  61         // Part 2: on realm choices
  62 
  63         // When there is no default realm
  64         System.setProperty("java.security.krb5.conf",
  65                 System.getProperty("test.src", ".") + "/empty.conf");
  66         Config.refresh();
  67 
  68         // A Windows client login to AD always has a default realm
  69         try {
  70             Realm r = Realm.getDefault();
  71             System.out.println("testNoDefaultDomain = false. Realm is " + r);
  72             testNoDefaultDomain = false;
  73         } catch (RealmException re) {
  74             // Great. This is what we expected
  75             testNoDefaultDomain = true;
  76         }
  77 
  78         if (testNoDefaultDomain) {
  79             type = PrincipalName.KRB_NT_UNKNOWN;
  80             checkName("a", type, "R1", "R1", "a");      // arg
  81             checkName("a@R1", type, null, "R1", "a");   // or r in name
  82             checkName("a@R2", type, "R1", "R1", "a");   // arg over r
  83             checkName("a", type, null, null);      // fail if none
  84             checkName("a/b@R1", type, null, "R1", "a", "b");
  85             type = PrincipalName.KRB_NT_SRV_HST;
  86             // Let's pray "b.h" won't be canonicalized
  87             checkName("a/b.h", type, "R1", "R1", "a", "b.h");    // arg
  88             checkName("a/b.h@R1", type, null, "R1", "a", "b.h"); // or r in name
  89             checkName("a/b.h@R1", type, "R2", "R2", "a", "b.h"); // arg over r
  90             checkName("a/b.h", type, null, null);    // fail if none
  91         }
  92 
  93         // When there is default realm
  94         System.setProperty("java.security.krb5.conf",
  95                 System.getProperty("test.src", ".") + "/krb5.conf");
  96         Config.refresh();
  97 
  98         type = PrincipalName.KRB_NT_UNKNOWN;
  99         checkName("a", type, "R1", "R1", "a");      // arg
 100         checkName("a@R1", type, null, "R1", "a");   // or r in name
 101         checkName("a@R2", type, "R1", "R1", "a");   // arg over r
 102         checkName("a", type, null, "R", "a");       // default
 103         checkName("a/b", type, null, "R", "a", "b");
 104         type = PrincipalName.KRB_NT_SRV_HST;
 105         checkName("a/b.h3", type, "R1", "R1", "a", "b.h3");     // arg
 106         checkName("a/b.h@R1", type, null, "R1", "a", "b.h");    // or r in name
 107         checkName("a/b.h3@R2", type, "R1", "R1", "a", "b.h3");  // arg over r
 108         checkName("a/b.h2", type, "R1", "R1", "a", "b.h2");     // arg over map
 109         checkName("a/b.h2@R1", type, null, "R1", "a", "b.h2");  // r over map
 110         checkName("a/b.h2", type, null, "R2", "a", "b.h2");     // map
 111         checkName("a/b.h", type, null, "R", "a", "b.h");        // default
 112     }
 113 
 114     // Check if the creation matches the expected output.
 115     // Note: realm == null means creation failure
 116     static void checkName(String n, int t, String s,
 117             String realm, String... parts)
 118             throws Exception {
 119         PrincipalName pn = null;
 120         try {
 121             pn = new PrincipalName(n, t, s);
 122         } catch (Exception e) {
 123             if (realm == null) {
 124                 return; // This is expected
 125             } else {
 126                 throw e;
 127             }
 128         }
 129         if (!pn.getRealmAsString().equals(realm)
 130                 || !Arrays.equals(pn.getNameStrings(), parts)) {
 131             throw new Exception(pn.toString() + " vs "
 132                     + Arrays.toString(parts) + "@" + realm);
 133         }
 134     }
 135 }