Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7d1de4b54c074997e872ccb6d9bd5d44f0e3c61c (plain) (blame)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
//  ========================================================================
//  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.websocket;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jetty.http.HttpHeaderValues;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.BufferCache.CachedBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.Utf8StringBuilder;
import org.junit.Before;
import org.junit.Test;

public class WebSocketParserD08Test
{
    private MaskedByteArrayBuffer _in;
    private Handler _handler;
    private WebSocketParserD08 _parser;
    private byte[] _mask = new byte[] {(byte)0x00,(byte)0xF0,(byte)0x0F,(byte)0xFF};
    private int _m;

    class MaskedByteArrayBuffer extends ByteArrayBuffer
    {
        MaskedByteArrayBuffer()
        {
            super(4096);
        }
        
        public void sendMask()
        {
            super.poke(putIndex(),_mask,0,4);
            super.setPutIndex(putIndex()+4);
            _m=0;
        }

        @Override
        public int put(Buffer src)
        {
            return put(src.asArray(),0,src.length());
        }

        public void putUnmasked(byte b)
        {
            super.put(b);
        }
        
        @Override
        public void put(byte b)
        {
            super.put((byte)(b^_mask[_m++%4]));
        }

        @Override
        public int put(byte[] b, int offset, int length)
        {
            byte[] mb = new byte[b.length];
            final int end=offset+length;
            for (int i=offset;i<end;i++)
            {
                mb[i]=(byte)(b[i]^_mask[_m++%4]);
            }
            return super.put(mb,offset,length);
        }

        @Override
        public int put(byte[] b)
        {
            return put(b,0,b.length);
        }
        
    };
    
    
    @Before
    public void setUp() throws Exception
    {
        WebSocketBuffers buffers = new WebSocketBuffers(1024);
        ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
        endPoint.setNonBlocking(true);
        _handler = new Handler();
        _parser=new WebSocketParserD08(buffers, endPoint,_handler,true);
        _parser.setFakeFragments(false);
        _in = new MaskedByteArrayBuffer();
        
        endPoint.setIn(_in);
    }

    @Test
    public void testCache() throws Exception
    {
        assertEquals(HttpHeaderValues.UPGRADE_ORDINAL ,((CachedBuffer)HttpHeaderValues.CACHE.lookup("Upgrade")).getOrdinal());
    }

    @Test
    public void testFlagsOppcode() throws Exception
    {
        _in.putUnmasked((byte)0xff);
        _in.putUnmasked((byte)0x80);
        _in.sendMask();

        int progress =_parser.parseNext();

        assertTrue(progress>0);
        assertEquals(0xf,_handler._flags);
        assertEquals(0xf,_handler._opcode);
        assertTrue(_parser.isBufferEmpty());
        _parser.returnBuffer();
        assertTrue(_parser.getBuffer()==null);
    }
    
    @Test
    public void testShortText() throws Exception
    {
        _in.putUnmasked((byte)0x81);
        _in.putUnmasked((byte)(0x80|11));
        _in.sendMask();
        _in.put("Hello World".getBytes(StringUtil.__UTF8));
        // System.err.println("tosend="+TypeUtil.toHexString(_in.asArray()));

        int progress =_parser.parseNext();

        assertEquals(18,progress);
        assertEquals("Hello World",_handler._data.get(0));
        assertEquals(0x8,_handler._flags);
        assertEquals(0x1,_handler._opcode);
        assertTrue(_parser.isBufferEmpty());
        _parser.returnBuffer();
        assertTrue(_parser.getBuffer()==null);
    }
    
    @Test
    public void testShortUtf8() throws Exception
    {
        String string = "Hell\uFF4f W\uFF4Frld";
        byte[] bytes = string.getBytes("UTF-8");

        _in.putUnmasked((byte)0x81);
        _in.putUnmasked((byte)(0x80|bytes.length));
        _in.sendMask();
        _in.put(bytes);

        int progress =_parser.parseNext();

        assertEquals(bytes.length+7,progress);
        assertEquals(string,_handler._data.get(0));
        assertEquals(0x8,_handler._flags);
        assertEquals(0x1,_handler._opcode);
        _parser.returnBuffer();
        assertTrue(_parser.isBufferEmpty());
        assertTrue(_parser.getBuffer()==null);
    }
    
    @Test
    public void testMediumText() throws Exception
    {
        String string = "Hell\uFF4f Medium W\uFF4Frld ";
        for (int i=0;i<4;i++)
            string = string+string;
        string += ". The end.";
        
        byte[] bytes = string.getBytes(StringUtil.__UTF8);
        
        _in.putUnmasked((byte)0x81);
        _in.putUnmasked((byte)(0x80|0x7E));
        _in.putUnmasked((byte)(bytes.length>>8));
        _in.putUnmasked((byte)(bytes.length&0xff));
        _in.sendMask();
        _in.put(bytes);

        int progress =_parser.parseNext();

        assertEquals(bytes.length+9,progress);
        assertEquals(string,_handler._data.get(0));
        assertEquals(0x8,_handler._flags);
        assertEquals(0x1,_handler._opcode);
        _parser.returnBuffer();
        assertTrue(_parser.isBufferEmpty());
        assertTrue(_parser.getBuffer()==null);
    }
    
    @Test
    public void testLongText() throws Exception
    {
        WebSocketBuffers buffers = new WebSocketBuffers(0x20000);
        ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
        WebSocketParserD08 parser=new WebSocketParserD08(buffers, endPoint,_handler,false);
        ByteArrayBuffer in = new ByteArrayBuffer(0x20000);
        endPoint.setIn(in);
        
        String string = "Hell\uFF4f Big W\uFF4Frld ";
        for (int i=0;i<12;i++)
            string = string+string;
        string += ". The end.";
        
        byte[] bytes = string.getBytes("UTF-8");

        _in.sendMask();
        in.put((byte)0x84);
        in.put((byte)0x7F);
        in.put((byte)0x00);
        in.put((byte)0x00);
        in.put((byte)0x00);
        in.put((byte)0x00);
        in.put((byte)0x00);
        in.put((byte)(bytes.length>>16));
        in.put((byte)((bytes.length>>8)&0xff));
        in.put((byte)(bytes.length&0xff));
        in.put(bytes);

        int progress =parser.parseNext();
        parser.returnBuffer();

        assertEquals(bytes.length+11,progress);
        assertEquals(string,_handler._data.get(0));
        assertTrue(parser.isBufferEmpty());
        assertTrue(parser.getBuffer()==null);
    }

    @Test
    public void testShortFragmentTest() throws Exception
    {
        _in.putUnmasked((byte)0x01);
        _in.putUnmasked((byte)0x86);
        _in.sendMask();
        _in.put("Hello ".getBytes(StringUtil.__UTF8));
        _in.putUnmasked((byte)0x80);
        _in.putUnmasked((byte)0x85);
        _in.sendMask();
        _in.put("World".getBytes(StringUtil.__UTF8));

        int progress =_parser.parseNext();

        assertEquals(24,progress);
        assertEquals(0,_handler._data.size());
        assertFalse(_parser.isBufferEmpty());
        assertFalse(_parser.getBuffer()==null);

        progress =_parser.parseNext();
        _parser.returnBuffer();

        assertEquals(1,progress);
        assertEquals("Hello World",_handler._data.get(0));
        assertTrue(_parser.isBufferEmpty());
        assertTrue(_parser.getBuffer()==null);
    }

    @Test
    public void testFrameTooLarge() throws Exception
    {
        // Buffers are only 1024, so this frame is too large
        _parser.setFakeFragments(false);
        
        _in.putUnmasked((byte)0x81);
        _in.putUnmasked((byte)(0x80|0x7E));
        _in.putUnmasked((byte)(2048>>8));
        _in.putUnmasked((byte)(2048&0xff));
        _in.sendMask();

        int progress =_parser.parseNext();

        assertTrue(progress>0);
       
        assertEquals(WebSocketConnectionD08.CLOSE_BADDATA,_handler._code);
        for (int i=0;i<2048;i++)
            _in.put((byte)'a');
        progress =_parser.parseNext();

        assertEquals(2048,progress);
        assertEquals(0,_handler._data.size());
        assertEquals(0,_handler._utf8.length());
        
        _handler._code=0;
        _handler._message=null;

        _in.putUnmasked((byte)0x81);
        _in.putUnmasked((byte)0xFE);
        _in.putUnmasked((byte)(1024>>8));
        _in.putUnmasked((byte)(1024&0xff));
        _in.sendMask();
        for (int i=0;i<1024;i++)
            _in.put((byte)'a');

        progress =_parser.parseNext();
        assertTrue(progress>0);
        assertEquals(1,_handler._data.size());
        assertEquals(1024,_handler._data.get(0).length());
    }

    @Test
    public void testFakeFragement() throws Exception
    {
        // Buffers are only 1024, so this frame will be fake fragmented
        _parser.setFakeFragments(true);
        
        _in.putUnmasked((byte)0x81);
        _in.putUnmasked((byte)(0x80|0x7E));
        _in.putUnmasked((byte)(2048>>8));
        _in.putUnmasked((byte)(2048&0xff));
        _in.sendMask();
        for (int i=0;i<2048;i++)
            _in.put((byte)('a'+i%26));
        
        int progress =_parser.parseNext();
        assertTrue(progress>0);

        assertEquals(2,_handler._frames);
        assertEquals(WebSocketConnectionD08.OP_CONTINUATION,_handler._opcode);
        assertEquals(1,_handler._data.size());
        String mesg=_handler._data.remove(0);

        assertEquals(2048,mesg.length());

        for (int i=0;i<2048;i++)
            assertEquals(('a'+i%26),mesg.charAt(i));
    }

    private class Handler implements WebSocketParser.FrameHandler
    {
        Utf8StringBuilder _utf8 = new Utf8StringBuilder();
        public List<String> _data = new ArrayList<String>();
        private byte _flags;
        private byte _opcode;
        int _code;
        String _message;
        int _frames;

        public void onFrame(byte flags, byte opcode, Buffer buffer)
        {
            _frames++;
            _flags=flags;
            _opcode=opcode;
            if ((flags&0x8)==0)
                _utf8.append(buffer.array(),buffer.getIndex(),buffer.length());
            else if (_utf8.length()==0)
                _data.add(buffer.toString("utf-8"));
            else
            {
                _utf8.append(buffer.array(),buffer.getIndex(),buffer.length());
                _data.add(_utf8.toString());
                _utf8.reset();
            }
        }

        public void close(int code,String message)
        {
            _code=code;
            _message=message;
        }
    }
}

Back to the top