Repository navigation
Expand file tree
/
Copy pathDraft6455BufferTest.java
More file actions
147 lines (133 loc) · 5.33 KB
/
Copy pathDraft6455BufferTest.java
File metadata and controls
147 lines (133 loc) · 5.33 KB
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
/*
* Copyright (c) 2010-2020 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package org.java_websocket.drafts;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.java_websocket.enums.Opcode;
import org.java_websocket.framing.Framedata;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class Draft6455BufferTest {
static Stream<Arguments> buffers() {
List<Arguments> arguments = new ArrayList<>();
for (boolean direct : new boolean[]{false, true}) {
for (boolean readOnly : new boolean[]{false, true}) {
for (boolean masked : new boolean[]{false, true}) {
for (int length : new int[]{0, 4, 126, 65536}) {
arguments.add(Arguments.of(direct, readOnly, masked, length));
}
}
}
}
return arguments.stream();
}
private static byte[] payload(int length) {
byte[] payload = new byte[length];
for (int i = 0; i < length; i++) {
payload[i] = (byte) i;
}
return payload;
}
private static byte[] wire(byte[] payload, boolean masked) {
int lengthBytes = payload.length <= 125 ? 0 : payload.length <= 65535 ? 2 : 8;
ByteBuffer frame = ByteBuffer.allocate(2 + lengthBytes + (masked ? 4 : 0) + payload.length);
frame.put((byte) 0x82);
int length = lengthBytes == 0 ? payload.length : lengthBytes == 2 ? 126 : 127;
frame.put((byte) (length | (masked ? 0x80 : 0)));
if (lengthBytes == 2) {
frame.putShort((short) payload.length);
} else if (lengthBytes == 8) {
frame.putLong(payload.length);
}
byte[] key = {1, 2, 3, 4};
if (masked) {
frame.put(key);
}
for (int i = 0; i < payload.length; i++) {
frame.put((byte) (payload[i] ^ (masked ? key[i % key.length] : 0)));
}
return frame.array();
}
private static ByteBuffer input(byte[] bytes, int offset, int length, boolean direct,
boolean readOnly) {
ByteBuffer parent = direct ? ByteBuffer.allocateDirect(length + 7)
: ByteBuffer.allocate(length + 7);
parent.position(4);
parent.put(bytes, offset, length);
parent.limit(parent.position());
parent.position(3);
ByteBuffer slice = parent.slice();
slice.position(1);
return readOnly ? slice.asReadOnlyBuffer() : slice;
}
private static void assertFrame(Framedata frame, byte[] expected) {
assertEquals(Opcode.BINARY, frame.getOpcode());
assertEquals(ByteBuffer.wrap(expected), frame.getPayloadData());
}
@ParameterizedTest
@MethodSource("buffers")
void decodeConsecutiveFrames(boolean direct, boolean readOnly, boolean masked, int length)
throws Exception {
byte[] expected = payload(length);
byte[] first = wire(expected, masked);
byte[] second = wire(new byte[]{42}, masked);
ByteBuffer combined = ByteBuffer.allocate(first.length + second.length);
combined.put(first).put(second);
ByteBuffer input = input(combined.array(), 0, combined.capacity(), direct, readOnly);
int limit = input.limit();
List<Framedata> frames = new Draft_6455().translateFrame(input);
assertEquals(2, frames.size());
assertFrame(frames.get(0), expected);
assertFrame(frames.get(1), new byte[]{42});
assertFalse(input.hasRemaining());
assertEquals(limit, input.limit());
}
@ParameterizedTest
@MethodSource("buffers")
void decodeSplitFrames(boolean direct, boolean readOnly, boolean masked, int length)
throws Exception {
byte[] expected = payload(length);
byte[] wire = wire(expected, masked);
Draft_6455 draft = new Draft_6455();
List<Framedata> frames = new ArrayList<>();
int offset = 0;
for (int chunk : new int[]{1, 1, 1, 2, 5, 17, wire.length}) {
int count = Math.min(chunk, wire.length - offset);
ByteBuffer input = input(wire, offset, count, direct, readOnly);
int limit = input.limit();
frames.addAll(draft.translateFrame(input));
assertFalse(input.hasRemaining());
assertEquals(limit, input.limit());
offset += count;
}
assertEquals(1, frames.size());
assertFrame(frames.get(0), expected);
}
}