File: framing_spec.rb

package info (click to toggle)
ruby-em-websocket 0.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 416 kB
  • sloc: ruby: 3,137; makefile: 5
file content (298 lines) | stat: -rw-r--r-- 8,090 bytes parent folder | download | duplicates (2)
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
# encoding: BINARY

require 'helper'

describe EM::WebSocket::Framing03 do
  class FramingContainer
    include EM::WebSocket::Framing03
    
    def initialize
      @connection = Object.new
      def @connection.max_frame_size
        1000000
      end
    end

    def <<(data)
      @data << data
      process_data
    end
    
    def debug(*args); end
  end
  
  before :each do
    @f = FramingContainer.new
    @f.initialize_framing
  end
  
  describe "basic examples" do
    it "connection close" do
      @f.should_receive(:message).with(:close, '', '')
      @f << 0b00000001
      @f << 0b00000000
    end
    
    it "ping" do
      @f.should_receive(:message).with(:ping, '', '')
      @f << 0b00000010
      @f << 0b00000000
    end
    
    it "pong" do
      @f.should_receive(:message).with(:pong, '', '')
      @f << 0b00000011
      @f << 0b00000000
    end
    
    it "text" do
      @f.should_receive(:message).with(:text, '', 'foo')
      @f << 0b00000100
      @f << 0b00000011
      @f << 'foo'
    end
    
    it "Text in two frames" do
      @f.should_receive(:message).with(:text, '', 'hello world')
      @f << 0b10000100
      @f << 0b00000110
      @f << "hello "
      @f << 0b00000000
      @f << 0b00000101
      @f << "world"
    end
    
    it "2 byte extended payload length text frame" do
      data = 'a' * 256
      @f.should_receive(:message).with(:text, '', data)
      @f << 0b00000100 # Single frame, text
      @f << 0b01111110 # Length 126 (so read 2 bytes)
      @f << 0b00000001 # Two bytes in network byte order (256)
      @f << 0b00000000
      @f << data
    end
  end
  
  # These examples are straight from the spec
  # http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-03#section-4.6
  describe "examples from the spec" do
    it "a single-frame text message" do
      @f.should_receive(:message).with(:text, '', 'Hello')
      @f << "\x04\x05Hello"
    end
    
    it "a fragmented text message" do
      @f.should_receive(:message).with(:text, '', 'Hello')
      @f << "\x84\x03Hel"
      @f << "\x00\x02lo"
    end
    
    it "Ping request and response" do
      @f.should_receive(:message).with(:ping, '', 'Hello')
      @f << "\x02\x05Hello"
    end
    
    it "256 bytes binary message in a single frame" do
      data = "a"*256
      @f.should_receive(:message).with(:binary, '', data)
      @f << "\x05\x7E\x01\x00" + data
    end
    
    it "64KiB binary message in a single frame" do
      data = "a"*65536
      @f.should_receive(:message).with(:binary, '', data)
      @f << "\x05\x7F\x00\x00\x00\x00\x00\x01\x00\x00" + data
    end
  end

  describe "other tests" do
    it "should accept a fragmented unmasked text message in 3 frames" do
      @f.should_receive(:message).with(:text, '', 'Hello world')
      @f << "\x84\x03Hel"
      @f << "\x80\x02lo"
      @f << "\x00\x06 world"
    end
  end

  describe "error cases" do
    it "should raise an exception on continuation frame without preceeding more frame" do
      lambda {
        @f << 0b00000000 # Single frame, continuation
        @f << 0b00000001 # Length 1
        @f << 'f'
      }.should raise_error(EM::WebSocket::WebSocketError, 'Continuation frame not expected')
    end
  end
end

# These examples are straight from the spec
# http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-03#section-4.6
describe EM::WebSocket::Framing04 do
  class FramingContainer04
    include EM::WebSocket::Framing04

    def initialize
      @connection = Object.new
      def @connection.max_frame_size
        1000000
      end
    end

    def <<(data)
      @data << data
      process_data
    end

    def debug(*args); end
  end

  before :each do
    @f = FramingContainer04.new
    @f.initialize_framing
  end

  describe "examples from the spec" do
    it "a single-frame text message" do
      @f.should_receive(:message).with(:text, '', 'Hello')
      @f << "\x84\x05\x48\x65\x6c\x6c\x6f" # "\x84\x05Hello"
    end

    it "a fragmented text message" do
      @f.should_receive(:message).with(:text, '', 'Hello')
      @f << "\x04\x03Hel"
      @f << "\x80\x02lo"
    end

    it "Ping request" do
      @f.should_receive(:message).with(:ping, '', 'Hello')
      @f << "\x82\x05Hello"
    end

    it "a pong response" do
      @f.should_receive(:message).with(:pong, '', 'Hello')
      @f << "\x83\x05Hello"
    end

    it "256 bytes binary message in a single frame" do
      data = "a"*256
      @f.should_receive(:message).with(:binary, '', data)
      @f << "\x85\x7E\x01\x00" + data
    end

    it "64KiB binary message in a single frame" do
      data = "a"*65536
      @f.should_receive(:message).with(:binary, '', data)
      @f << "\x85\x7F\x00\x00\x00\x00\x00\x01\x00\x00" + data
    end
  end

  describe "other tests" do
    it "should accept a fragmented unmasked text message in 3 frames" do
      @f.should_receive(:message).with(:text, '', 'Hello world')
      @f << "\x04\x03Hel"
      @f << "\x00\x02lo"
      @f << "\x80\x06 world"
    end
  end
end

describe EM::WebSocket::Framing07 do
  class FramingContainer07
    include EM::WebSocket::Framing07

    def initialize
      @connection = Object.new
      def @connection.max_frame_size
        1000000
      end
    end

    def <<(data)
      @data << data
      process_data
    end

    def debug(*args); end
  end

  before :each do
    @f = FramingContainer07.new
    @f.initialize_framing
  end

  # These examples are straight from the spec
  # http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07#section-4.6
  describe "examples from the spec" do
    it "a single-frame unmakedtext message" do
      @f.should_receive(:message).with(:text, '', 'Hello')
      @f << "\x81\x05\x48\x65\x6c\x6c\x6f" # "\x84\x05Hello"
    end

    it "a single-frame masked text message" do
      @f.should_receive(:message).with(:text, '', 'Hello')
      @f << "\x81\x85\x37\xfa\x21\x3d\x7f\x9f\x4d\x51\x58" # "\x84\x05Hello"
    end

    it "a fragmented unmasked text message" do
      @f.should_receive(:message).with(:text, '', 'Hello')
      @f << "\x01\x03Hel"
      @f << "\x80\x02lo"
    end

    it "Ping request" do
      @f.should_receive(:message).with(:ping, '', 'Hello')
      @f << "\x89\x05Hello"
    end

    it "a pong response" do
      @f.should_receive(:message).with(:pong, '', 'Hello')
      @f << "\x8a\x05Hello"
    end

    it "256 bytes binary message in a single unmasked frame" do
      data = "a"*256
      @f.should_receive(:message).with(:binary, '', data)
      @f << "\x82\x7E\x01\x00" + data
    end

    it "64KiB binary message in a single unmasked frame" do
      data = "a"*65536
      @f.should_receive(:message).with(:binary, '', data)
      @f << "\x82\x7F\x00\x00\x00\x00\x00\x01\x00\x00" + data
    end
  end

  describe "other tests" do
    it "should raise a WSProtocolError if an invalid frame type is requested" do
      lambda {
        # Opcode 3 is not supported by this draft
        @f << "\x83\x05Hello"
      }.should raise_error(EventMachine::WebSocket::WSProtocolError, "Unknown opcode 3")
    end

    it "should accept a fragmented unmasked text message in 3 frames" do
      @f.should_receive(:message).with(:text, '', 'Hello world')
      @f << "\x01\x03Hel"
      @f << "\x00\x02lo"
      @f << "\x80\x06 world"
    end

    it "should raise if non-fin frame is followed by a non-continuation data frame (continuation frame would be expected)" do
      lambda {
        @f << 0b00000001 # Not fin, text
        @f << 0b00000001 # Length 1
        @f << 'f'
        @f << 0b10000001 # fin, text (continutation expected)
        @f << 0b00000001 # Length 1
        @f << 'b'
      }.should raise_error(EM::WebSocket::WebSocketError, 'Continuation frame expected')
    end

    it "should raise on non-fin control frames (control frames must not be fragmented)" do
      lambda {
        @f << 0b00001010 # Not fin, pong (opcode 10)
        @f << 0b00000000 # Length 1
      }.should raise_error(EM::WebSocket::WebSocketError, 'Control frames must not be fragmented')
    end
  end
end