Path: blob/master/test/jdk/sun/security/krb5/name/Constructors.java
41142 views
/*1* Copyright (c) 2012, 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*/22/*23* @test24* @bug 696625925* @summary Make PrincipalName and Realm immutable26* @modules java.security.jgss/sun.security.krb527* @run main/othervm Constructors28*/2930import java.util.Arrays;31import sun.security.krb5.*;3233public class Constructors {34public static void main(String[] args) throws Exception {3536int type;37boolean testNoDefaultDomain;3839// Part 1: on format4041// Good ones42type = PrincipalName.KRB_NT_UNKNOWN;43checkName("a", type, "R", "R", false, "a");44checkName("a@R2", type, "R", "R", false, "a");45checkName("a/b", type, "R", "R", false, "a", "b");46checkName("a/b@R2", type, "R", "R", false, "a", "b");47checkName("a/b/c", type, "R", "R", false, "a", "b", "c");48checkName("a/b/c@R2", type, "R", "R", false, "a", "b", "c");49// Weird ones50checkName("a\\/b", type, "R", "R", false, "a/b");51checkName("a\\/b\\/c", type, "R", "R", false, "a/b/c");52checkName("a\\/b\\@R2", type, "R", "R", false, "a/b@R2");53// Bad ones54checkName("a", type, "", null, false);55checkName("a/", type, "R", null, false);56checkName("/a", type, "R", null, false);57checkName("a//b", type, "R", null, false);58checkName("a@", type, null, null, false);59type = PrincipalName.KRB_NT_SRV_HST;6061// Part 2: on realm choices6263// When there is no default realm64System.setProperty("java.security.krb5.conf",65System.getProperty("test.src", ".") + "/empty.conf");66Config.refresh();6768// A Windows client login to AD always has a default realm69try {70Realm r = Realm.getDefault();71System.out.println("testNoDefaultDomain = false. Realm is " + r);72testNoDefaultDomain = false;73} catch (RealmException re) {74// Great. This is what we expected75testNoDefaultDomain = true;76}7778if (testNoDefaultDomain) {79type = PrincipalName.KRB_NT_UNKNOWN;80checkName("a", type, "R1", "R1", false, "a"); // arg81checkName("a@R1", type, null, "R1", false, "a"); // or r in name82checkName("a@R2", type, "R1", "R1", false, "a"); // arg over r83checkName("a", type, null, null, false); // fail if none84checkName("a/b@R1", type, null, "R1", false, "a", "b");85type = PrincipalName.KRB_NT_SRV_HST;86// Let's pray "b.h" won't be canonicalized87checkName("a/b.h", type, "R1", "R1", false, "a", "b.h"); // arg88checkName("a/b.h@R1", type, null, "R1", false, "a", "b.h"); // or r in name89checkName("a/b.h@R1", type, "R2", "R2", false, "a", "b.h"); // arg over r90checkName("a/b.h", type, null, null, false); // fail if none91}9293// When there is default realm94System.setProperty("java.security.krb5.conf",95System.getProperty("test.src", ".") + "/krb5.conf");96Config.refresh();9798type = PrincipalName.KRB_NT_UNKNOWN;99checkName("a", type, "R1", "R1", false, "a"); // arg100checkName("a@R1", type, null, "R1", false, "a"); // or r in name101checkName("a@R2", type, "R1", "R1", false, "a"); // arg over r102checkName("a", type, null, "R", true, "a"); // default103checkName("a/b", type, null, "R", true, "a", "b");104type = PrincipalName.KRB_NT_SRV_HST;105checkName("a/b.h3", type, "R1", "R1", false, "a", "b.h3"); // arg106checkName("a/b.h@R1", type, null, "R1", false, "a", "b.h"); // or r in name107checkName("a/b.h3@R2", type, "R1", "R1", false, "a", "b.h3"); // arg over r108checkName("a/b.h2", type, "R1", "R1", false, "a", "b.h2"); // arg over map109checkName("a/b.h2@R1", type, null, "R1", false, "a", "b.h2"); // r over map110checkName("a/b.h2", type, null, "R2", true, "a", "b.h2"); // map111checkName("a/b.h", type, null, "R", true, "a", "b.h"); // default112}113114// Check if the creation matches the expected output.115// Note: realm == null means creation failure116static void checkName(String n, int t, String s,117String realm, boolean deduced, String... parts)118throws Exception {119PrincipalName pn = null;120try {121pn = new PrincipalName(n, t, s);122} catch (Exception e) {123if (realm == null) {124return; // This is expected125} else {126throw e;127}128}129if (!pn.getRealmAsString().equals(realm)130|| !Arrays.equals(pn.getNameStrings(), parts)) {131throw new Exception(pn.toString() + " vs "132+ Arrays.toString(parts) + "@" + realm);133}134if (deduced != pn.isRealmDeduced()) {135throw new Exception("pn.realmDeduced is " + pn.isRealmDeduced());136}137}138}139140141