Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/krb5/name/Constructors.java
38853 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* @run main/othervm Constructors27*/2829import java.util.Arrays;30import sun.security.krb5.*;3132public class Constructors {33public static void main(String[] args) throws Exception {3435int type;36boolean testNoDefaultDomain;3738// Part 1: on format3940// Good ones41type = PrincipalName.KRB_NT_UNKNOWN;42checkName("a", type, "R", "R", false, "a");43checkName("a@R2", type, "R", "R", false, "a");44checkName("a/b", type, "R", "R", false, "a", "b");45checkName("a/b@R2", type, "R", "R", false, "a", "b");46checkName("a/b/c", type, "R", "R", false, "a", "b", "c");47checkName("a/b/c@R2", type, "R", "R", false, "a", "b", "c");48// Weird ones49checkName("a\\/b", type, "R", "R", false, "a/b");50checkName("a\\/b\\/c", type, "R", "R", false, "a/b/c");51checkName("a\\/b\\@R2", type, "R", "R", false, "a/b@R2");52// Bad ones53checkName("a", type, "", null, false);54checkName("a/", type, "R", null, false);55checkName("/a", type, "R", null, false);56checkName("a//b", type, "R", null, false);57checkName("a@", type, null, null, false);58type = PrincipalName.KRB_NT_SRV_HST;5960// Part 2: on realm choices6162// When there is no default realm63System.setProperty("java.security.krb5.conf",64System.getProperty("test.src", ".") + "/empty.conf");65Config.refresh();6667// A Windows client login to AD always has a default realm68try {69Realm r = Realm.getDefault();70System.out.println("testNoDefaultDomain = false. Realm is " + r);71testNoDefaultDomain = false;72} catch (RealmException re) {73// Great. This is what we expected74testNoDefaultDomain = true;75}7677if (testNoDefaultDomain) {78type = PrincipalName.KRB_NT_UNKNOWN;79checkName("a", type, "R1", "R1", false, "a"); // arg80checkName("a@R1", type, null, "R1", false, "a"); // or r in name81checkName("a@R2", type, "R1", "R1", false, "a"); // arg over r82checkName("a", type, null, null, false); // fail if none83checkName("a/b@R1", type, null, "R1", false, "a", "b");84type = PrincipalName.KRB_NT_SRV_HST;85// Let's pray "b.h" won't be canonicalized86checkName("a/b.h", type, "R1", "R1", false, "a", "b.h"); // arg87checkName("a/b.h@R1", type, null, "R1", false, "a", "b.h"); // or r in name88checkName("a/b.h@R1", type, "R2", "R2", false, "a", "b.h"); // arg over r89checkName("a/b.h", type, null, null, false); // fail if none90}9192// When there is default realm93System.setProperty("java.security.krb5.conf",94System.getProperty("test.src", ".") + "/krb5.conf");95Config.refresh();9697type = PrincipalName.KRB_NT_UNKNOWN;98checkName("a", type, "R1", "R1", false, "a"); // arg99checkName("a@R1", type, null, "R1", false, "a"); // or r in name100checkName("a@R2", type, "R1", "R1", false, "a"); // arg over r101checkName("a", type, null, "R", true, "a"); // default102checkName("a/b", type, null, "R", true, "a", "b");103type = PrincipalName.KRB_NT_SRV_HST;104checkName("a/b.h3", type, "R1", "R1", false, "a", "b.h3"); // arg105checkName("a/b.h@R1", type, null, "R1", false, "a", "b.h"); // or r in name106checkName("a/b.h3@R2", type, "R1", "R1", false, "a", "b.h3"); // arg over r107checkName("a/b.h2", type, "R1", "R1", false, "a", "b.h2"); // arg over map108checkName("a/b.h2@R1", type, null, "R1", false, "a", "b.h2"); // r over map109checkName("a/b.h2", type, null, "R2", true, "a", "b.h2"); // map110checkName("a/b.h", type, null, "R", true, "a", "b.h"); // default111}112113// Check if the creation matches the expected output.114// Note: realm == null means creation failure115static void checkName(String n, int t, String s,116String realm, boolean deduced, String... parts)117throws Exception {118PrincipalName pn = null;119try {120pn = new PrincipalName(n, t, s);121} catch (Exception e) {122if (realm == null) {123return; // This is expected124} else {125throw e;126}127}128if (!pn.getRealmAsString().equals(realm)129|| !Arrays.equals(pn.getNameStrings(), parts)) {130throw new Exception(pn.toString() + " vs "131+ Arrays.toString(parts) + "@" + realm);132}133if (deduced != pn.isRealmDeduced()) {134throw new Exception("pn.realmDeduced is " + pn.isRealmDeduced());135}136}137}138139140