aboutsummaryrefslogtreecommitdiff
path: root/test/sun/security/ssl/SessionIdCollisionTest.java
blob: 16582f4e0789b30cfb73bb5db0823d6f7817c4ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (c) 2019, Red Hat Inc. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
import java.lang.reflect.Constructor;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @test
 * @bug 8203190
 * @summary Manual test to verify number of collisions in
 *          sun.security.ssl.SessionId.hashCode()
 * @run main/manual SessionIdCollisionTest 100 20480 10000000
 */

/**
 *
 * Notes:
 * - This is a manual test, not run automatically.
 * - New default value of javax.net.ssl.sessionCacheSize in JDK 12+ is 20480
 * - According to JDK-8210985 24 hours expired cache may exceed several million
 *   entries = > 10_000_000
 *
 * Example usage: java SessionIdCollissionTest 100 20480 10000000
 *
 * Expected outcome of running the test is to see fewer collisions and, more
 * importantly fewer elements in buckets when there are collisions. See:
 * http://mail.openjdk.java.net/pipermail/jdk8u-dev/2019-May/009345.html
 *
 */
public class SessionIdCollisionTest {

    private List<Integer> prepareHashCodes(int total) throws Exception {
        Class<?> sessionIdClass = Class.forName("sun.security.ssl.SessionId");
        Constructor<?> c = sessionIdClass.getDeclaredConstructor(byte[].class);
        c.setAccessible(true);
        // case of rejoinable session ids generates 32 random bytes
        byte[] sessionIdBytes = new byte[32];
        List<Integer> hashCodes = new ArrayList<>();
        for (int i = 0; i < total; i++) {
            SecureRandom random = new SecureRandom();
            random.nextBytes(sessionIdBytes);
            Object sessionId = c.newInstance(sessionIdBytes);
            int hashCode = sessionId.hashCode();
            hashCodes.add(hashCode);
        }
        return hashCodes;
    }

    private void printSummary(boolean withDistribution,
                              List<Integer> hashCodes) throws Exception {
        final int bound = hashCodes.size();
        Collections.sort(hashCodes);
        int collisions = 0;
        Map<Integer, List<Integer>> collCountsReverse = new HashMap<>();
        for (int i = 0; i < bound - 1; i++) {
            int oldval = hashCodes.get(i);
            int nextval = hashCodes.get(i+1);
            if (oldval == nextval) {
                collisions++;
                if (i == bound - 2) { // last elements
                    updateCollCountsReverse(collisions, collCountsReverse, oldval);
                }
            } else {
                updateCollCountsReverse(collisions, collCountsReverse, oldval);
                collisions = 0;
                if (i == bound - 2) { // last elements
                    updateCollCountsReverse(collisions, collCountsReverse, nextval);
                }
            }
        }
        if (withDistribution) {
            System.out.println("---- distribution ----");
        }
        int collCount = 0;
        int maxLength = 0;
        List<Integer> sorted = new ArrayList<>(collCountsReverse.size());
        sorted.addAll(collCountsReverse.keySet());
        Collections.sort(sorted);
        for (Integer coll: sorted) {
            List<Integer> hc = collCountsReverse.get(coll);
            if (withDistribution) {
                System.out.printf("Hashcodes with %02d collisions | " +
                                  "hashCodes: %s\n", coll, hc.toString());
            }
            collCount += coll * hc.size();
            if (coll > maxLength) {
                maxLength = coll;
            }
        }
        if (withDistribution) {
            System.out.println("---- distribution ----");
        }
        System.out.println("Total number of collisions: " + collCount);
        if (collCount > 0) {
            System.out.println("Max length of collision list over all buckets: " +
                                maxLength);
        }
    }

    private void updateCollCountsReverse(int collisions,
            Map<Integer, List<Integer>> reverse, int val) {
        List<Integer> hc = reverse.get(collisions);
        if (hc == null) {
            hc = new ArrayList<>();
            hc.add(val);
            reverse.put(collisions, hc);
        } else {
            hc.add(val);
        }
    }

    public void doCollissionTest(int total) throws Exception {
        System.out.println("Collision test for " + total + " sessions:");
        System.out.println("------------------------------------------------");
        List<Integer> hashcodes = prepareHashCodes(total);
        printSummary(false, hashcodes);
        System.out.println();
    }

    public static void main(String[] args) throws Exception {
        if (args.length < 1) {
            System.err.println("java " + SessionIdCollisionTest.class.getSimpleName() +
                               "<num-sessions> [<num-sessions> ...]");
            System.exit(1);
        }
        SessionIdCollisionTest collTest = new SessionIdCollisionTest();
        for (int i = 0; i < args.length; i++) {
            int total = Integer.parseInt(args[i]);
            collTest.doCollissionTest(total);
        }
    }

}