1/*
2 * Copyright (c) 2016, 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// SunJSSE does not support dynamic system properties, no way to re-use
25// system properties in samevm/agentvm mode.
26
27/*
28 * @test
29 * @bug 8161086
30 * @summary DTLS handshaking fails if some messages were lost
31 * @modules java.base/sun.security.util
32 * @build DTLSOverDatagram
33 *
34 * @run main/othervm PacketLossRetransmission client 0 hello_request
35 * @run main/othervm PacketLossRetransmission client 1 client_hello
36 * @run main/othervm PacketLossRetransmission client 2 server_hello
37 * @run main/othervm PacketLossRetransmission client 3 hello_verify_request
38 * @run main/othervm PacketLossRetransmission client 4 new_session_ticket
39 * @run main/othervm PacketLossRetransmission client 11 certificate
40 * @run main/othervm PacketLossRetransmission client 12 server_key_exchange
41 * @run main/othervm PacketLossRetransmission client 13 certificate_request
42 * @run main/othervm PacketLossRetransmission client 14 server_hello_done
43 * @run main/othervm PacketLossRetransmission client 15 certificate_verify
44 * @run main/othervm PacketLossRetransmission client 16 client_key_exchange
45 * @run main/othervm PacketLossRetransmission client 20 finished
46 * @run main/othervm PacketLossRetransmission client 21 certificate_url
47 * @run main/othervm PacketLossRetransmission client 22 certificate_status
48 * @run main/othervm PacketLossRetransmission client 23 supplemental_data
49 * @run main/othervm PacketLossRetransmission client -1 change_cipher_spec
50 * @run main/othervm PacketLossRetransmission server 0 hello_request
51 * @run main/othervm PacketLossRetransmission server 1 client_hello
52 * @run main/othervm PacketLossRetransmission server 2 server_hello
53 * @run main/othervm PacketLossRetransmission server 3 hello_verify_request
54 * @run main/othervm PacketLossRetransmission server 4 new_session_ticket
55 * @run main/othervm PacketLossRetransmission server 11 certificate
56 * @run main/othervm PacketLossRetransmission server 12 server_key_exchange
57 * @run main/othervm PacketLossRetransmission server 13 certificate_request
58 * @run main/othervm PacketLossRetransmission server 14 server_hello_done
59 * @run main/othervm PacketLossRetransmission server 15 certificate_verify
60 * @run main/othervm PacketLossRetransmission server 16 client_key_exchange
61 * @run main/othervm PacketLossRetransmission server 20 finished
62 * @run main/othervm PacketLossRetransmission server 21 certificate_url
63 * @run main/othervm PacketLossRetransmission server 22 certificate_status
64 * @run main/othervm PacketLossRetransmission server 23 supplemental_data
65 * @run main/othervm PacketLossRetransmission server -1 change_cipher_spec
66 */
67
68import java.util.List;
69import java.util.ArrayList;
70import java.net.DatagramPacket;
71import java.net.SocketAddress;
72import javax.net.ssl.SSLEngine;
73
74/**
75 * Test that DTLS implementation is able to do retransmission internally
76 * automatically if packet get lost.
77 */
78public class PacketLossRetransmission extends DTLSOverDatagram {
79    private static boolean isClient;
80    private static byte handshakeType;
81
82    private boolean needPacketLoss = true;
83
84    public static void main(String[] args) throws Exception {
85        isClient = args[0].equals("client");
86        handshakeType = Byte.valueOf(args[1]);
87
88        PacketLossRetransmission testCase = new PacketLossRetransmission();
89        testCase.runTest(testCase);
90    }
91
92    @Override
93    boolean produceHandshakePackets(SSLEngine engine, SocketAddress socketAddr,
94            String side, List<DatagramPacket> packets) throws Exception {
95
96        boolean finished = super.produceHandshakePackets(
97                engine, socketAddr, side, packets);
98
99        if (needPacketLoss && (!(isClient ^ engine.getUseClientMode()))) {
100            DatagramPacket packet = getPacket(packets, handshakeType);
101            if (packet != null) {
102                needPacketLoss = false;
103
104                System.out.println("Loss a packet of handshake messahe");
105                packets.remove(packet);
106            }
107        }
108
109        return finished;
110    }
111}
112