|
1 | 1 | /* |
2 | | - * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2007, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
22 | 22 | */ |
23 | 23 |
|
24 | 24 | /* |
| 25 | + * @test |
25 | 26 | * @bug 4634392 |
26 | | - * @summary JDK code doesn't respect contract for equals and hashCode |
| 27 | + * @summary Ensure the GSSName has the correct impl which respects |
| 28 | + * the contract for equals and hashCode across different configurations. |
| 29 | + * @library /test/lib |
27 | 30 | * @author Andrew Fan |
| 31 | + * |
| 32 | + * @run main/othervm -Djava.security.krb5.realm=R -Djava.security.krb5.kdc=127.0.0.1 Krb5NameEquals |
| 33 | + * @run main/othervm -Dsun.security.jgss.native=true Krb5NameEquals |
28 | 34 | */ |
29 | 35 |
|
30 | | -import org.ietf.jgss.*; |
| 36 | +import jtreg.SkippedException; |
| 37 | +import org.ietf.jgss.GSSManager; |
| 38 | +import org.ietf.jgss.GSSName; |
| 39 | +import org.ietf.jgss.Oid; |
31 | 40 |
|
32 | 41 | public class Krb5NameEquals { |
33 | 42 |
|
34 | | - private static String NAME_STR1 = "service@localhost"; |
35 | | - private static String NAME_STR2 = "service2@localhost"; |
| 43 | + private static final String NAME_STR1 = "service@localhost"; |
| 44 | + private static final String NAME_STR2 = "service2@localhost"; |
36 | 45 | private static final Oid MECH; |
37 | 46 |
|
38 | 47 | static { |
39 | | - Oid temp = null; |
40 | 48 | try { |
41 | | - temp = new Oid("1.2.840.113554.1.2.2"); // KRB5 |
| 49 | + MECH = new Oid("1.2.840.113554.1.2.2"); // KRB5 |
42 | 50 | } catch (Exception e) { |
43 | 51 | // should never happen |
| 52 | + throw new RuntimeException("Exception initialising Oid", e); |
44 | 53 | } |
45 | | - MECH = temp; |
46 | 54 | } |
47 | 55 |
|
48 | 56 | public static void main(String[] argv) throws Exception { |
49 | | - GSSManager mgr = GSSManager.getInstance(); |
| 57 | + final GSSManager mgr = GSSManager.getInstance(); |
| 58 | + |
| 59 | + // Checking if native GSS is installed, throwing skip exception if it's not. |
| 60 | + if (Boolean.getBoolean("sun.security.jgss.native")) { |
| 61 | + final var mechs = mgr.getMechs(); |
| 62 | + if (mechs == null || mechs.length == 0) { |
| 63 | + throw new SkippedException("NativeGSS not supported"); |
| 64 | + } |
| 65 | + } |
50 | 66 |
|
51 | | - boolean result = true; |
52 | 67 | // Create GSSName and check their equals(), hashCode() impl |
53 | | - GSSName name1 = mgr.createName(NAME_STR1, |
54 | | - GSSName.NT_HOSTBASED_SERVICE, MECH); |
55 | | - GSSName name2 = mgr.createName(NAME_STR2, |
56 | | - GSSName.NT_HOSTBASED_SERVICE, MECH); |
57 | | - GSSName name3 = mgr.createName(NAME_STR1, |
58 | | - GSSName.NT_HOSTBASED_SERVICE, MECH); |
| 68 | + final GSSName name1 = mgr.createName(NAME_STR1, |
| 69 | + GSSName.NT_HOSTBASED_SERVICE, MECH); |
| 70 | + final GSSName name2 = mgr.createName(NAME_STR2, |
| 71 | + GSSName.NT_HOSTBASED_SERVICE, MECH); |
| 72 | + final GSSName name3 = mgr.createName(NAME_STR1, |
| 73 | + GSSName.NT_HOSTBASED_SERVICE, MECH); |
59 | 74 |
|
60 | | - if (!name1.equals(name1) || !name1.equals(name3) || |
61 | | - !name1.equals((Object) name1) || |
62 | | - !name1.equals((Object) name3)) { |
63 | | - System.out.println("Error: should be the same name"); |
64 | | - result = false; |
| 75 | + if (!name1.equals(name3) || !name1.equals((Object) name3)) { |
| 76 | + throw new RuntimeException("Error: should be the same name"); |
65 | 77 | } else if (name1.hashCode() != name3.hashCode()) { |
66 | | - System.out.println("Error: should have same hash"); |
67 | | - result = false; |
| 78 | + throw new RuntimeException("Error: should have same hash"); |
68 | 79 | } |
69 | 80 |
|
70 | 81 | if (name1.equals(name2) || name1.equals((Object) name2)) { |
71 | | - System.out.println("Error: should be different names"); |
72 | | - result = false; |
| 82 | + throw new RuntimeException("Error: should be different names"); |
73 | 83 | } |
74 | | - if (result) { |
75 | | - System.out.println("Done"); |
76 | | - } else System.exit(1); |
| 84 | + System.out.println("Done"); |
77 | 85 | } |
78 | 86 | } |
0 commit comments