Path: blob/master/test/jdk/sun/security/krb5/tools/KtabZero.java
41154 views
/*1* Copyright (c) 2013, 2019, 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 jdk.test.lib.SecurityTools;24import jdk.test.lib.process.OutputAnalyzer;25import sun.security.krb5.internal.ktab.KeyTab;26import sun.security.krb5.internal.ktab.KeyTabConstants;2728import java.lang.reflect.Field;29import java.nio.file.Files;30import java.nio.file.Path;31import java.nio.file.Paths;3233import static jdk.test.lib.SecurityTools.klist;3435/*36* @test37* @bug 8014196 7002036 704373738* @summary ktab creates a file with zero kt_vno39* @requires os.family == "windows"40* @library /test/lib41* @modules java.security.jgss/sun.security.krb5.internal.ktab:+open42*/43public class KtabZero {4445static final String NAME = "k.tab";4647public static void main(String[] args) throws Exception {4849// 0. Non-existing keytab50Files.deleteIfExists(Paths.get(NAME));51ktab("-l").shouldNotHaveExitValue(0);52klist("-k " + NAME).shouldNotHaveExitValue(0);53check(true);5455// 1. Create with KeyTab56Files.deleteIfExists(Paths.get(NAME));57KeyTab.getInstance(NAME).save();58check(false);5960// 2. Create with the tool61Files.deleteIfExists(Paths.get(NAME));62ktab("-a me@HERE pass").shouldHaveExitValue(0);63ktab("-l").shouldHaveExitValue(0);6465// 7002036: ktab return code changes on a error case66ktab("-hello").shouldNotHaveExitValue(0);67ktab("").shouldNotHaveExitValue(0);68check(false);6970// 3. Invalid keytab71Files.write(Path.of(NAME), "garbage".getBytes());72ktab("-l").shouldNotHaveExitValue(0);73ktab("-a me@HERE pass").shouldNotHaveExitValue(0);74klist("-k " + NAME).shouldNotHaveExitValue(0);75}7677static OutputAnalyzer ktab(String s) throws Exception {78s = ("-k " + NAME + " " + s).trim();79return SecurityTools.ktab(s);80}8182// Checks existence as well as kt-vno83static void check(boolean showBeMissing) throws Exception {84KeyTab kt = KeyTab.getInstance(NAME);85if (kt.isMissing() != showBeMissing) {86throw new Exception("isMissing is not " + showBeMissing);87}88Field f = KeyTab.class.getDeclaredField("kt_vno");89f.setAccessible(true);90if (f.getInt(kt) != KeyTabConstants.KRB5_KT_VNO) {91throw new Exception("kt_vno is " + f.getInt(kt));92}93}94}959697