1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8043071
27 * @summary Expose session key and KRB_CRED through extended GSS-API
28 */
29
30import javax.security.auth.kerberos.*;
31import java.util.function.Supplier;
32
33public class KerberosNullsAndDestroyTest {
34
35    public static void main(String[] args) throws Exception {
36
37        KerberosPrincipal c = new KerberosPrincipal("me@HERE");
38        KerberosPrincipal s = new KerberosPrincipal("you@THERE");
39
40        // These object constructions should throw NullPointerException
41        checkNPE(() -> new KerberosKey(c, null, 17, 1));
42        checkNPE(() -> new EncryptionKey(null, 17));
43        checkNPE(() -> new KerberosCredMessage(null, s, new byte[1]));
44        checkNPE(() -> new KerberosCredMessage(c, null, new byte[1]));
45        checkNPE(() -> new KerberosCredMessage(c, s, null));
46
47        KerberosKey k1 = new KerberosKey(c, new byte[16], 17, 1);
48        EncryptionKey k2 = new EncryptionKey(new byte[16], 17);
49        KerberosCredMessage m = new KerberosCredMessage(c, s, new byte[1]);
50
51        // These get calls should throw IllegalStateException
52        k1.destroy();
53        checkISE(() -> k1.getAlgorithm());
54        checkISE(() -> k1.getEncoded());
55        checkISE(() -> k1.getFormat());
56        checkISE(() -> k1.getKeyType());
57        checkISE(() -> k1.getPrincipal());
58        checkISE(() -> k1.getVersionNumber());
59
60        k2.destroy();
61        checkISE(() -> k2.getAlgorithm());
62        checkISE(() -> k2.getEncoded());
63        checkISE(() -> k2.getFormat());
64        checkISE(() -> k2.getKeyType());
65
66        m.destroy();
67        checkISE(() -> m.getSender());
68        checkISE(() -> m.getRecipient());
69        checkISE(() -> m.getEncoded());
70    }
71
72    static void checkNPE(Supplier<?> f) throws Exception {
73        check(f, NullPointerException.class);
74    }
75
76    static void checkISE(Supplier<?> f) throws Exception {
77        check(f, IllegalStateException.class);
78    }
79
80    static void check(Supplier<?> f, Class<? extends Exception> type) throws Exception {
81        try {
82            f.get();
83        } catch (Exception e) {
84            if (e.getClass() != type) {
85                throw e;
86            } else {
87                return;
88            }
89        }
90        throw new Exception("Should fail");
91    }
92}
93