Path: blob/master/test/jdk/sun/security/krb5/config/Include.java
41155 views
/*1* Copyright (c) 2017, 2018, 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 8029994 8177085 817739826* @summary Support "include" and "includedir" in krb5.conf27* @modules java.security.jgss/sun.security.krb528* @compile -XDignore.symbol.file Include.java29* @run main/othervm Include30*/31import sun.security.krb5.Config;32import sun.security.krb5.KrbException;3334import java.nio.file.Files;35import java.nio.file.Path;36import java.nio.file.Paths;37import java.util.Arrays;3839public class Include {40public static void main(String[] args) throws Exception {4142String krb5Conf = "[section]\nkey="; // Skeleton of a section4344Path conf = Paths.get("krb5.conf"); // base krb5.conf4546Path f = Paths.get("f"); // include47Path f2 = Paths.get("f2"); // f include f248Path d = Paths.get("d"); // includedir49Path dd = Paths.get("d/dd"); // sub dir, ignore50Path ddf = Paths.get("d/dd/ddf"); // file in sub dir, ignore51Path df1 = Paths.get("d/f1"); // one file in dir52Path df2 = Paths.get("d/f2"); // another file53Path df3 = Paths.get("d/f.3"); // third file bad name54Path df4 = Paths.get("d/f4.conf"); // fourth file55Path df5 = Paths.get("d/.f5.conf"); // fifth file is dotfile5657// OK: The base file can be missing58System.setProperty("java.security.krb5.conf", "no-such-file");59tryReload(true);6061System.setProperty("java.security.krb5.conf", conf.toString());6263// Write base file64Files.write(conf,65("include " + f.toAbsolutePath() + "\n" +66"includedir " + d.toAbsolutePath() + "\n" +67krb5Conf + "base").getBytes()68);6970// Error: Neither include nor includedir exists71tryReload(false);7273// Error: Only includedir exists74Files.createDirectory(d);75tryReload(false);7677// Error: Both exists, but include is a cycle78Files.write(f,79("include " + conf.toAbsolutePath() + "\n" +80krb5Conf + "f").getBytes());81tryReload(false);8283// Error: A good include exists, but no includedir yet84Files.delete(d);85Files.write(f, (krb5Conf + "f").getBytes());86tryReload(false);8788// OK: Everything is set89Files.createDirectory(d);90tryReload(true); // Now OK9192// make f include f293Files.write(f,94("include " + f2.toAbsolutePath() + "\n" +95krb5Conf + "f").getBytes());96Files.write(f2, (krb5Conf + "f2").getBytes());97// fx1 and fx2 will be loaded98Files.write(df1, (krb5Conf + "df1").getBytes());99Files.write(df2, (krb5Conf + "df2").getBytes());100// fx3 and fxs (and file inside it) will be ignored101Files.write(df3, (krb5Conf + "df3").getBytes());102Files.createDirectory(dd);103Files.write(ddf, (krb5Conf + "ddf").getBytes());104// fx4 will be loaded105Files.write(df4, (krb5Conf + "df4").getBytes());106// fx5 will be excluded107Files.write(df5, (krb5Conf + "df5").getBytes());108109// OK: All good files read110tryReload(true);111112String[] v = Config.getInstance().getAll("section", "key") .split(" ");113// v will contain f2, f, df[124], and base.114// Order of df[124] is not determined. Sort them first.115Arrays.sort(v, 2, 5);116String longv = Arrays.toString(v);117if (!longv.equals("[f2, f, df1, df2, df4, base]")) {118throw new Exception(longv);119}120121// Error: include file not absolute122Files.write(conf,123("include " + f + "\n" +124"includedir " + d.toAbsolutePath() + "\n" +125krb5Conf + "base").getBytes()126);127tryReload(false);128129// Error: includedir not absolute130Files.write(conf,131("include " + f.toAbsolutePath() + "\n" +132"includedir " + d + "\n" +133krb5Conf + "base").getBytes()134);135tryReload(false);136137// OK: unsupported directive138Files.write(conf,139("module /lib/lib.so\n" +140krb5Conf + "base").getBytes()141);142tryReload(true);143}144145private static void tryReload(boolean expected) throws Exception {146if (expected) {147Config.refresh();148} else {149try {150Config.refresh();151throw new Exception("Should be illegal");152} catch (KrbException ke) {153// OK154}155}156}157}158159160