File: AutomaticPong.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (228 lines) | stat: -rw-r--r-- 8,787 bytes parent folder | download | duplicates (12)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. 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.
 */

/*
 * @test
 * @build DummyWebSocketServer
 * @run testng/othervm
 *      -Djdk.internal.httpclient.websocket.debug=true
 *       AutomaticPong
 */
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.http.WebSocket;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static java.net.http.HttpClient.newHttpClient;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

public class AutomaticPong {
    /*
     * The sendClose method has been invoked and a Ping comes from the server.
     * Naturally, the client cannot reply with a Pong (the output has been
     * closed). However, this MUST not be treated as an error.
     * At this stage the server either has received or pretty soon will receive
     * the Close message sent by the sendClose. Thus, the server will know the
     * client cannot send further messages and it's up to the server to decide
     * how to react on the corresponding Pong not being received.
     */
    @Test
    public void sendCloseThenAutomaticPong() throws IOException {
        int[] bytes = {
                0x89, 0x00,                                     // ping
                0x89, 0x06, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x3f, // ping hello?
                0x88, 0x00,                                     // close
        };
        try (var server = Support.serverWithCannedData(bytes)) {
            server.open();
            MockListener listener = new MockListener() {
                @Override
                protected void onOpen0(WebSocket webSocket) {
                    /* request nothing */
                }
            };
            var webSocket = newHttpClient()
                    .newWebSocketBuilder()
                    .buildAsync(server.getURI(), listener)
                    .join();
            try {
                webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok").join();
                // now request all messages available
                webSocket.request(Long.MAX_VALUE);
                List<MockListener.Invocation> actual = listener.invocations();
                ByteBuffer hello = ByteBuffer.wrap("hello?".getBytes(StandardCharsets.UTF_8));
                ByteBuffer empty = ByteBuffer.allocate(0);
                List<MockListener.Invocation> expected = List.of(
                        MockListener.Invocation.onOpen(webSocket),
                        MockListener.Invocation.onPing(webSocket, empty),
                        MockListener.Invocation.onPing(webSocket, hello),
                        MockListener.Invocation.onClose(webSocket, 1005, "")
                );
                assertEquals(actual, expected);
            } finally {
                webSocket.abort();
            }
        }
    }

    /*
     * The server sends a number of contiguous Ping messages. The client replies
     * to these messages automatically. According to RFC 6455 a WebSocket client
     * is free to reply only to the most recent Pings.
     *
     * What is checked here is that:
     *
     *     a) the order of Pong replies corresponds to the Pings received,
     *     b) the last Pong corresponds to the last Ping
     *     c) there are no unrelated Pongs
     */
    @Test(dataProvider = "nPings")
    public void automaticPongs(int nPings) throws Exception {
        // big enough to not bother with resize
        ByteBuffer buffer = ByteBuffer.allocate(65536);
        Frame.HeaderWriter w = new Frame.HeaderWriter();
        for (int i = 0; i < nPings; i++) {
            w.fin(true)
             .opcode(Frame.Opcode.PING)
             .noMask()
             .payloadLen(4)    // the length of the number of the Ping (int)
             .write(buffer);
            buffer.putInt(i);  // the number of the Ping (int)
        }
        w.fin(true)
         .opcode(Frame.Opcode.CLOSE)
         .noMask()
         .payloadLen(2)
        .write(buffer);
        buffer.putChar((char) 1000);
        buffer.flip();
        try (var server = Support.serverWithCannedData(buffer.array())) {
            server.open();
            MockListener listener = new MockListener();
            var webSocket = newHttpClient()
                    .newWebSocketBuilder()
                    .buildAsync(server.getURI(), listener)
                    .join();
            try {
                List<MockListener.Invocation> inv = listener.invocations();
                assertEquals(inv.size(), nPings + 2); // n * onPing + onOpen + onClose

                ByteBuffer data = server.read();
                Frame.Reader reader = new Frame.Reader();

                Frame.Consumer consumer = new Frame.Consumer() {

                    ByteBuffer number = ByteBuffer.allocate(4);
                    Frame.Masker masker = new Frame.Masker();
                    int i = -1;
                    boolean closed;

                    @Override
                    public void fin(boolean value) {
                        assertTrue(value);
                    }

                    @Override
                    public void rsv1(boolean value) {
                        assertFalse(value);
                    }

                    @Override
                    public void rsv2(boolean value) {
                        assertFalse(value);
                    }

                    @Override
                    public void rsv3(boolean value) {
                        assertFalse(value);
                    }

                    @Override
                    public void opcode(Frame.Opcode value) {
                        if (value == Frame.Opcode.CLOSE) {
                            closed = true;
                            return;
                        }
                        assertEquals(value, Frame.Opcode.PONG);
                    }

                    @Override
                    public void mask(boolean value) {
                        assertTrue(value);
                    }

                    @Override
                    public void payloadLen(long value) {
                        if (!closed)
                            assertEquals(value, 4);
                    }

                    @Override
                    public void maskingKey(int value) {
                        masker.mask(value);
                    }

                    @Override
                    public void payloadData(ByteBuffer src) {
                        masker.transferMasking(src, number);
                        if (closed) {
                            return;
                        }
                        number.flip();
                        int n = number.getInt();
                        System.out.printf("pong number=%s%n", n);
                        number.clear();
                        // a Pong with the number less than the maximum of Pongs already
                        // received MUST never be received
                        if (i >= n) {
                            fail(String.format("i=%s, n=%s", i, n));
                        }
                        i = n;
                    }

                    @Override
                    public void endFrame() {
                    }
                };
                while (data.hasRemaining()) {
                    reader.readFrame(data, consumer);
                }
            } finally {
                webSocket.abort();
            }
        }
    }


    @DataProvider(name = "nPings")
    public Object[][] nPings() {
        return new Object[][]{{1}, {2}, {4}, {8}, {9}, {256}};
    }
}