Path: blob/master/test/jdk/sun/security/krb5/tools/KtabCheck.java
41154 views
/*1* Copyright (c) 2010, 2021, 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*/2223import java.io.File;24import java.nio.file.Files;25import java.nio.file.Path;26import java.util.Arrays;27import java.util.HashSet;28import java.util.Set;2930import jdk.test.lib.SecurityTools;31import sun.security.krb5.internal.ktab.KeyTab;32import sun.security.krb5.internal.ktab.KeyTabEntry;3334/*35* @test36* @bug 6950546 813934837* @summary "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"38* @requires os.family == "windows"39* @library /test/lib40* @modules java.security.jgss/sun.security.krb5.internal.ktab41* java.security.jgss/sun.security.krb542*/43public class KtabCheck {4445private static final String KEYTAB = "ktab.tmp";4647public static void main(String[] args) throws Exception {4849Files.deleteIfExists(Path.of(KEYTAB));5051// This test uses a krb5.conf file (onlythree.conf) in which52// only 3 etypes in the default_tkt_enctypes setting are enabled53// by default: aes128-cts(17), aes256-cts(18), and aes128-sha2(19).5455ktab("-a me mine");56check(1,17,1,18,1,19);57ktab("-a me mine -n 0");58check(0,17,0,18,0,19);59ktab("-a me mine -n 1 -append");60check(0,17,0,18,0,19,1,17,1,18,1,19);61ktab("-a me mine -append");62check(0,17,0,18,0,19,1,17,1,18,1,19,2,17,2,18,2,19);63ktab("-a me mine");64check(3,17,3,18,3,19);65ktab("-a me mine -n 4 -append");66check(3,17,3,18,3,19,4,17,4,18,4,19);67ktab("-a me mine -n 5 -append");68check(3,17,3,18,3,19,4,17,4,18,4,19,5,17,5,18,5,19);69ktab("-a me mine -n 6 -append");70check(3,17,3,18,3,19,4,17,4,18,4,19,5,17,5,18,5,19,6,17,6,18,6,19);71ktab("-d me 3");72check(4,17,4,18,4,19,5,17,5,18,5,19,6,17,6,18,6,19);73ktab("-d me -e 17 6");74check(4,17,4,18,4,19,5,17,5,18,5,19,6,18,6,19);75ktab("-d me -e 19 6");76check(4,17,4,18,4,19,5,17,5,18,5,19,6,18);77ktab("-d me -e 17 5");78check(4,17,4,18,4,19,5,18,5,19,6,18);79ktab("-d me old");80check(4,17,5,19,6,18);81try {82ktab("-d me old");83throw new Exception("Should fail");84} catch (Exception e) {85// no-op86}87check(4,17,5,19,6,18);88ktab("-d me");89check();90}9192static void ktab(String s) throws Exception {93File conf = new File(System.getProperty("test.src"), "onlythree.conf");94SecurityTools.ktab("-J-Djava.security.krb5.conf=" + conf95+ " -k " + KEYTAB + " -f " + s).shouldHaveExitValue(0);96}9798/**99* Checks if a keytab contains exactly the keys (kvno and etype)100* @param args kvno etype...101*/102static void check(int... args) throws Exception {103System.out.println("Checking " + Arrays.toString(args));104KeyTab ktab = KeyTab.getInstance(KEYTAB);105Set<String> expected = new HashSet<>();106for (int i = 0; i < args.length; i += 2) {107expected.add(args[i] + ":" + args[i + 1]);108}109for (KeyTabEntry e: ktab.getEntries()) {110// KVNO and etype111String vne = e.getKey().getKeyVersionNumber() + ":" +112e.getKey().getEType();113if (!expected.contains(vne)) {114throw new Exception("No " + vne + " in expected");115}116expected.remove(vne);117}118if (!expected.isEmpty()) {119throw new Exception("Extra elements in expected");120}121}122}123124125