Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6fae67b5ba61c86daa48557308d6b0d65d18f9fb (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
//
//  ========================================================================
//  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.hamcrest.Matchers.greaterThan;
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.junit.Before;
import org.junit.Test;

public class WebSocketParserD00Test
{
    private ByteArrayBuffer _in;
    private Handler _handler;
    private WebSocketParser _parser;

    @Before
    public void setUp() throws Exception
    {
        WebSocketBuffers buffers = new WebSocketBuffers(1024);
        ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
        _handler = new Handler();
        _parser=new WebSocketParserD00(buffers, endPoint,_handler);
        _in = new ByteArrayBuffer(2048);
        endPoint.setIn(_in);
    }

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

    @Test
    public void testOneUtf8() throws Exception
    {
        _in.put((byte)0x00);
        _in.put("Hello World".getBytes(StringUtil.__UTF8));
        _in.put((byte)0xff);

        int filled =_parser.parseNext();

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

    @Test
    public void testTwoUtf8() throws Exception
    {
        _in.put((byte)0x00);
        _in.put("Hello World".getBytes(StringUtil.__UTF8));
        _in.put((byte)0xff);
        _in.put((byte)0x00);
        _in.put("Hell\uFF4F W\uFF4Frld".getBytes(StringUtil.__UTF8));
        _in.put((byte)0xff);

        int filled =_parser.parseNext();

        assertThat(filled,greaterThan(0));
        assertEquals("Hello World",_handler._data.get(0));
        assertFalse(_parser.isBufferEmpty());
        assertFalse(_parser.getBuffer()==null);

        filled =_parser.parseNext();

        assertThat(filled,greaterThan(0));
        assertEquals("Hell\uFF4f W\uFF4Frld",_handler._data.get(1));
        assertTrue(_parser.isBufferEmpty());
        assertTrue(_parser.getBuffer()==null);
    }

    @Test
    public void testOneBinary() throws Exception
    {
        _in.put((byte)0x80);
        _in.put((byte)11);
        _in.put("Hello World".getBytes(StringUtil.__UTF8));

        int filled =_parser.parseNext();

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

    @Test
    public void testTwoBinary() throws Exception
    {
        _in.put((byte)0x80);
        _in.put((byte)11);
        _in.put("Hello World".getBytes(StringUtil.__UTF8));

        byte[] data = new byte[150];
        for (int i=0;i<data.length;i++)
            data[i]=(byte)('0'+(i%10));

        _in.put((byte)0x80);
        _in.put((byte)(0x80|(data.length>>7)));
        _in.put((byte)(data.length&0x7f));
        _in.put(data);

        int filled =_parser.parseNext();
        assertThat(filled,greaterThan(0));
        assertEquals("Hello World",_handler._data.get(0));
        assertFalse(_parser.isBufferEmpty());
        assertFalse(_parser.getBuffer()==null);

        filled =_parser.parseNext();
        assertThat(filled,greaterThan(0));
        String got=_handler._data.get(1);
        assertEquals(data.length,got.length());
        assertTrue(got.startsWith("012345678901234567890123"));
        assertTrue(_parser.isBufferEmpty());
        assertTrue(_parser.getBuffer()==null);
    }


    private class Handler implements WebSocketParser.FrameHandler
    {
        public List<String> _data = new ArrayList<String>();

        public void onFrame(byte flags, byte opcode, Buffer buffer)
        {
            _data.add(buffer.toString(StringUtil.__UTF8));
        }

        public void close(int code,String message)
        {
        }
    }
}

Back to the top