Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: tests/standalone/src/io/WebSocketProtocolProcessorTest.dart

Issue 10173021: Add web socket protocol processor test (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 #source("../../../../runtime/bin/websocket.dart");
6 #source("../../../../runtime/bin/websocket_impl.dart");
7
8 class WebSocketFrame {
9 WebSocketFrame(int opcode, List<int> data);
10 }
11
12 // Class that when hooked up to the web socket protocol processor will
13 // collect the message and expect it to be equal to the
14 // expectedMessage field when fully received.
15 class WebSocketMessageCollector {
16 WebSocketMessageCollector(_WebSocketProtocolProcessor this.processor,
17 [List<int> this.expectedMessage = null]) {
18 processor.onMessageStart = onMessageStart;
19 processor.onMessageData = onMessageData;
20 processor.onMessageEnd = onMessageEnd;
21 processor.onClosed = onClosed;
22 }
23
24 void onMessageStart(int type) {
25 data = new List<int>();
26 }
27
28 void onMessageData(List<int> buffer, int index, int count) {
29 data.addAll(buffer.getRange(index, count));
30 }
31
32 void onMessageEnd() {
33 messageCount++;
34 Expect.listEquals(expectedMessage, data);
35 data = null;
36 }
37
38 void onClosed(int status, String reason) {
39 closeCount++;
40 }
41
42 void onError(e) {
43 Expect.fail("Unexpected error $e");
44 }
45
46 _WebSocketProtocolProcessor processor;
47 List<int> expectedMessage;
48
49 List<int> data;
50 int messageCount = 0;
51 int closeCount = 0;
52 }
53
54
55 // Web socket constants.
56 final int FRAME_OPCODE_TEXT = 1;
57 final int FRAME_OPCODE_BINARY = 2;
58
59
60 // Function for building a web socket frame.
61 List<int> createFrame(bool fin,
62 int opcode,
63 int maskingKey,
64 List<int> data,
65 int offset,
66 int count) {
67 int frameSize = 2;
68 if (count > 125) frameSize += 2;
69 if (count > 65535) frameSize += 6;
70 frameSize += count;
71 // No masking.
72 assert(maskingKey == null);
73 List<int> frame = new List<int>(frameSize);
74 int frameIndex = 0;
75 frame[frameIndex++] = (fin ? 0x80 : 0x00) | opcode;
76 if (count < 126) {
77 frame[frameIndex++] = count;
78 } else if (count < 65536) {
79 frame[frameIndex++] = 126;
80 frame[frameIndex++] = count >> 8;
81 frame[frameIndex++] = count & 0xFF;
82 } else {
83 frame[frameIndex++] = 127;
84 for (int i = 0; i < 8; i++) {
85 frame[frameIndex++] = count >> ((7 - i) * 8) & 0xFF;
86 }
87 }
88 frame.setRange(frameIndex, count, data, offset);
89 return frame;
90 }
91
92
93 // Test processing messages which are sent in a single frame.
94 void testFullMessages() {
95 // Use the same web socket protocol processor for all frames.
96 _WebSocketProtocolProcessor processor = new _WebSocketProtocolProcessor();
97 WebSocketMessageCollector mc = new WebSocketMessageCollector(processor);
98
99 int messageCount = 0;
100
101 void testMessage(int opcode, List<int> message) {
102 mc.expectedMessage = message;
103 List<int> frame = createFrame(
104 true, opcode, null, message, 0, message.length);
105
106 // Update the processor with one big chunk.
107 messageCount++;
108 processor.update(frame, 0, frame.length);
109 Expect.isNull(mc.data);
110 Expect.equals(0, processor._state);
111
112 // Update the processor one byte at the time.
113 messageCount++;
114 for (int i = 0; i < frame.length; i++) {
115 processor.update(frame, i, 1);
116 }
117 Expect.equals(0, processor._state);
118 Expect.isNull(mc.data);
119
120 // Update the processor two bytes at the time.
121 messageCount++;
122 for (int i = 0; i < frame.length; i += 2) {
123 processor.update(frame, i, i + 1 < frame.length ? 2 : 1);
124 }
125 Expect.equals(0, processor._state);
126 Expect.isNull(mc.data);
127 }
128
129 void runTest(int from, int to, int step) {
130 for (int messageLength = from; messageLength < to; messageLength += step) {
131 List<int> message = new List<int>(messageLength);
132 for (int i = 0; i < messageLength; i++) message[i] = i & 0xFF;
133 testMessage(FRAME_OPCODE_TEXT, message);
134 testMessage(FRAME_OPCODE_BINARY, message);
135 }
136 }
137
138 // Test different message sizes.
139 runTest(0, 10, 1);
140 runTest(120, 130, 1);
141 runTest(0, 1000, 100);
142 runTest(65534, 65537, 1);
143 print("Messages test, messages $messageCount");
144 Expect.equals(messageCount, mc.messageCount);
145 Expect.equals(0, mc.closeCount);
146 }
147
148
149 // Test processing of frames which are split into fragments.
150 void testFragmentedMessages() {
151 // Use the same web socket protocol processor for all frames.
152 _WebSocketProtocolProcessor processor = new _WebSocketProtocolProcessor();
153 WebSocketMessageCollector mc = new WebSocketMessageCollector(processor);
154
155 int messageCount = 0;
156 int frameCount = 0;
157
158 void testFragmentMessage(int opcode, List<int> message, int fragmentSize) {
159 messageCount++;
160 int messageIndex = 0;
161 int remaining = message.length;
162 bool firstFrame = true;
163 bool lastFrame = false;
164 while (!lastFrame) {
165 int payloadSize = Math.min(fragmentSize, remaining);
166 lastFrame = payloadSize == remaining;
167 List<int> frame = createFrame(lastFrame,
168 firstFrame ? opcode : 0x00,
169 null,
170 message,
171 messageIndex,
172 payloadSize);
173 frameCount++;
174 messageIndex += payloadSize;
175 processor.update(frame, 0, frame.length);
176 remaining -= payloadSize;
177 firstFrame = false;
178 }
179 }
180
181 void testMessageFragmentation(int opcode, List<int> message) {
182 mc.expectedMessage = message;
183
184 // Test with fragmenting the message in different fragment sizes.
185 if (message.length <= 10) {
186 for (int i = 1; i < 10; i++) {
187 testFragmentMessage(opcode, message, i);
188 }
189 } else {
190 testFragmentMessage(opcode, message, 1);
191 testFragmentMessage(opcode, message, 10);
192 testFragmentMessage(opcode, message, 100);
193 }
194 }
195
196 void runTest(int from, int to, int step) {
197 for (int messageLength = from; messageLength < to; messageLength += step) {
198 List<int> message = new List<int>(messageLength);
199 for (int i = 0; i < messageLength; i++) message[i] = i & 0xFF;
200 testMessageFragmentation(FRAME_OPCODE_TEXT, message);
201 testMessageFragmentation(FRAME_OPCODE_BINARY, message);
202 }
203 }
204
205 // Test different message sizes.
206 runTest(0, 10, 1);
207 runTest(120, 130, 1);
208 runTest(0, 1000, 100);
209 runTest(65534, 65537, 1);
210 print("Fragment messages test, messages $messageCount, frames $frameCount");
211 Expect.equals(messageCount, mc.messageCount);
212 Expect.equals(0, mc.closeCount);
213 }
214
215 void main() {
216 testFullMessages();
217 testFragmentedMessages();
218 }
OLDNEW
« runtime/bin/websocket_impl.dart ('K') | « runtime/bin/websocket_impl.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698