6112N/A/*
6112N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6112N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6112N/A *
6112N/A * This code is free software; you can redistribute it and/or modify it
6112N/A * under the terms of the GNU General Public License version 2 only, as
6112N/A * published by the Free Software Foundation.
6112N/A *
6112N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6112N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6112N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6112N/A * version 2 for more details (a copy is included in the LICENSE file that
6112N/A * accompanied this code).
6112N/A *
6112N/A * You should have received a copy of the GNU General Public License version
6112N/A * 2 along with this work; if not, write to the Free Software Foundation,
6112N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6112N/A *
6112N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6112N/A * or visit www.oracle.com if you need additional information or have any
6112N/A * questions.
6112N/A */
6112N/A
6112N/Aimport sun.security.krb5.internal.ktab.KeyTab;
6112N/Aimport sun.security.krb5.internal.ktab.KeyTabConstants;
6112N/A
6112N/Aimport java.io.File;
6112N/Aimport java.lang.reflect.Field;
6112N/Aimport java.nio.file.Files;
6112N/Aimport java.nio.file.Paths;
6112N/A
6112N/A/*
6112N/A * @test
6112N/A * @bug 8014196
6112N/A * @summary ktab creates a file with zero kt_vno
6112N/A */
6112N/Apublic class KtabZero {
6112N/A
6112N/A static final String NAME = "k.tab";
6112N/A
6112N/A public static void main(String[] args) throws Exception {
6112N/A
6112N/A // 0. Non-existing keytab
6112N/A Files.deleteIfExists(Paths.get(NAME));
6112N/A check(true);
6112N/A
6112N/A // 1. Create with KeyTab
6112N/A Files.deleteIfExists(Paths.get(NAME));
6112N/A KeyTab.getInstance(NAME).save();
6112N/A check(false);
6112N/A
6112N/A // 2. Create with the tool
6112N/A Files.deleteIfExists(Paths.get(NAME));
6112N/A try {
6112N/A Class ktab = Class.forName("sun.security.krb5.internal.tools.Ktab");
6112N/A ktab.getDeclaredMethod("main", String[].class).invoke(null,
6112N/A (Object)(("-k " + NAME + " -a me@HERE pass").split(" ")));
6112N/A } catch (ClassNotFoundException cnfe) {
6112N/A // Only Windows has ktab tool
6112N/A System.out.println("No ktab tool here. Ignored.");
6112N/A return;
6112N/A }
6112N/A check(false);
6112N/A }
6112N/A
6112N/A // Checks existence as well as kt-vno
6112N/A static void check(boolean showBeMissing) throws Exception {
6112N/A KeyTab kt = KeyTab.getInstance(NAME);
6112N/A if (kt.isMissing() != showBeMissing) {
6112N/A throw new Exception("isMissing is not " + showBeMissing);
6112N/A }
6112N/A Field f = KeyTab.class.getDeclaredField("kt_vno");
6112N/A f.setAccessible(true);
6112N/A if (f.getInt(kt) != KeyTabConstants.KRB5_KT_VNO) {
6112N/A throw new Exception("kt_vno is " + f.getInt(kt));
6112N/A }
6112N/A }
6112N/A}