backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / packet / test_sctp.py
1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16
17 import inspect
18 import logging
19 import six
20 import struct
21 import unittest
22
23 from nose.tools import eq_
24 from nose.tools import ok_
25 from ryu.lib import addrconv
26 from ryu.lib.packet import packet
27 from ryu.lib.packet import ethernet
28 from ryu.lib.packet import ipv4
29 from ryu.lib.packet import sctp
30 from ryu.ofproto import ether
31 from ryu.ofproto import inet
32
33
34 LOG = logging.getLogger(__name__)
35
36
37 class Test_sctp(unittest.TestCase):
38
39     def setUp(self):
40         self.chunks = []
41         self.csum = 0
42         self.dst_port = 1234
43         self.src_port = 5678
44         self.vtag = 98765432
45
46         self.sc = sctp.sctp(
47             self.src_port, self.dst_port, self.vtag, self.csum,
48             self.chunks)
49
50         self.buf = b'\x16\x2e\x04\xd2\x05\xe3\x0a\x78\x00\x00\x00\x00'
51
52     def setUp_with_data(self):
53         self.unordered = 1
54         self.begin = 1
55         self.end = 1
56         self.length = 16 + 10
57         self.tsn = 12345
58         self.sid = 1
59         self.seq = 0
60         self.payload_id = 0
61         self.payload_data = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a'
62
63         self.data = sctp.chunk_data(
64             unordered=self.unordered, begin=self.begin, end=self.end,
65             tsn=self.tsn, sid=self.sid, payload_data=self.payload_data)
66
67         self.chunks = [self.data]
68
69         self.sc = sctp.sctp(
70             self.src_port, self.dst_port, self.vtag, self.csum,
71             self.chunks)
72
73         self.buf += b'\x00\x07\x00\x1a\x00\x00\x30\x39\x00\x01\x00\x00' + \
74             b'\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a'
75
76     def setUp_with_init(self):
77         self.flags = 0
78         self.length = 20 + 8 + 20 + 8 + 4 + 16 + 16
79         self.init_tag = 123456
80         self.a_rwnd = 9876
81         self.os = 3
82         self.mis = 3
83         self.i_tsn = 123456
84
85         self.p_ipv4 = sctp.param_ipv4('192.168.1.1')
86         self.p_ipv6 = sctp.param_ipv6('fe80::647e:1aff:fec4:8284')
87         self.p_cookie_preserve = sctp.param_cookie_preserve(5000)
88         self.p_ecn = sctp.param_ecn()
89         self.p_host_addr = sctp.param_host_addr(b'test host\x00')
90         self.p_support_type = sctp.param_supported_addr(
91             [sctp.PTYPE_IPV4, sctp.PTYPE_IPV6, sctp.PTYPE_COOKIE_PRESERVE,
92              sctp.PTYPE_ECN, sctp.PTYPE_HOST_ADDR])
93         self.params = [
94             self.p_ipv4, self.p_ipv6, self.p_cookie_preserve,
95             self.p_ecn, self.p_host_addr, self.p_support_type]
96
97         self.init = sctp.chunk_init(
98             init_tag=self.init_tag, a_rwnd=self.a_rwnd, os=self.os,
99             mis=self.mis, i_tsn=self.i_tsn, params=self.params)
100
101         self.chunks = [self.init]
102
103         self.sc = sctp.sctp(
104             self.src_port, self.dst_port, self.vtag, self.csum,
105             self.chunks)
106
107         self.buf += b'\x01\x00\x00\x5c\x00\x01\xe2\x40\x00\x00\x26\x94' + \
108             b'\x00\x03\x00\x03\x00\x01\xe2\x40' + \
109             b'\x00\x05\x00\x08\xc0\xa8\x01\x01' + \
110             b'\x00\x06\x00\x14' + \
111             b'\xfe\x80\x00\x00\x00\x00\x00\x00' + \
112             b'\x64\x7e\x1a\xff\xfe\xc4\x82\x84' + \
113             b'\x00\x09\x00\x08\x00\x00\x13\x88' + \
114             b'\x80\x00\x00\x04' + \
115             b'\x00\x0b\x00\x0e' + \
116             b'\x74\x65\x73\x74\x20\x68\x6f\x73\x74\x00\x00\x00' + \
117             b'\x00\x0c\x00\x0e\x00\x05\x00\x06\x00\x09\x80\x00' + \
118             b'\x00\x0b\x00\x00'
119
120     def setUp_with_init_ack(self):
121         self.flags = 0
122         self.length = 20 + 8 + 8 + 20 + 8 + 4 + 16
123         self.init_tag = 123456
124         self.a_rwnd = 9876
125         self.os = 3
126         self.mis = 3
127         self.i_tsn = 123456
128
129         self.p_state_cookie = sctp.param_state_cookie(b'\x01\x02\x03')
130         self.p_ipv4 = sctp.param_ipv4('192.168.1.1')
131         self.p_ipv6 = sctp.param_ipv6('fe80::647e:1aff:fec4:8284')
132         self.p_unrecognized_param = sctp.param_unrecognized_param(
133             b'\xff\xff\x00\x04')
134         self.p_ecn = sctp.param_ecn()
135         self.p_host_addr = sctp.param_host_addr(b'test host\x00')
136         self.params = [
137             self.p_state_cookie, self.p_ipv4, self.p_ipv6,
138             self.p_unrecognized_param, self.p_ecn, self.p_host_addr]
139
140         self.init_ack = sctp.chunk_init_ack(
141             init_tag=self.init_tag, a_rwnd=self.a_rwnd, os=self.os,
142             mis=self.mis, i_tsn=self.i_tsn, params=self.params)
143
144         self.chunks = [self.init_ack]
145
146         self.sc = sctp.sctp(
147             self.src_port, self.dst_port, self.vtag, self.csum,
148             self.chunks)
149
150         self.buf += b'\x02\x00\x00\x54\x00\x01\xe2\x40\x00\x00\x26\x94' + \
151             b'\x00\x03\x00\x03\x00\x01\xe2\x40' + \
152             b'\x00\x07\x00\x07\x01\x02\x03\x00' + \
153             b'\x00\x05\x00\x08\xc0\xa8\x01\x01' + \
154             b'\x00\x06\x00\x14' + \
155             b'\xfe\x80\x00\x00\x00\x00\x00\x00' + \
156             b'\x64\x7e\x1a\xff\xfe\xc4\x82\x84' + \
157             b'\x00\x08\x00\x08\xff\xff\x00\x04' + \
158             b'\x80\x00\x00\x04' + \
159             b'\x00\x0b\x00\x0e' + \
160             b'\x74\x65\x73\x74\x20\x68\x6f\x73\x74\x00\x00\x00'
161
162     def setUp_with_sack(self):
163         self.flags = 0
164         self.length = 16 + 2 * 2 * 5 + 4 * 5
165         self.tsn_ack = 123456
166         self.a_rwnd = 9876
167         self.gapack_num = 5
168         self.duptsn_num = 5
169         self.gapacks = [[2, 3], [10, 12], [20, 24], [51, 52], [62, 63]]
170         self.duptsns = [123458, 123466, 123476, 123507, 123518]
171
172         self.sack = sctp.chunk_sack(
173             tsn_ack=self.tsn_ack, a_rwnd=self.a_rwnd,
174             gapack_num=self.gapack_num, duptsn_num=self.duptsn_num,
175             gapacks=self.gapacks, duptsns=self.duptsns)
176
177         self.chunks = [self.sack]
178
179         self.sc = sctp.sctp(
180             self.src_port, self.dst_port, self.vtag, self.csum,
181             self.chunks)
182
183         self.buf += b'\x03\x00\x00\x38\x00\x01\xe2\x40' + \
184             b'\x00\x00\x26\x94\x00\x05\x00\x05' + \
185             b'\x00\x02\x00\x03\x00\x0a\x00\x0c\x00\x14\x00\x18' + \
186             b'\x00\x33\x00\x34\x00\x3e\x00\x3f' + \
187             b'\x00\x01\xe2\x42\x00\x01\xe2\x4a\x00\x01\xe2\x54' + \
188             b'\x00\x01\xe2\x73\x00\x01\xe2\x7e'
189
190     def setUp_with_heartbeat(self):
191         self.flags = 0
192         self.length = 4 + 8
193
194         self.p_heartbeat = sctp.param_heartbeat(b'\x01\x02\x03\x04')
195
196         self.heartbeat = sctp.chunk_heartbeat(info=self.p_heartbeat)
197
198         self.chunks = [self.heartbeat]
199
200         self.sc = sctp.sctp(
201             self.src_port, self.dst_port, self.vtag, self.csum,
202             self.chunks)
203
204         self.buf += b'\x04\x00\x00\x0c' + \
205             b'\x00\x01\x00\x08' + \
206             b'\x01\x02\x03\x04'
207
208     def setUp_with_heartbeat_ack(self):
209         self.flags = 0
210         self.length = 4 + 12
211
212         self.p_heartbeat = sctp.param_heartbeat(
213             b'\xff\xee\xdd\xcc\xbb\xaa\x99\x88')
214
215         self.heartbeat_ack = sctp.chunk_heartbeat_ack(info=self.p_heartbeat)
216
217         self.chunks = [self.heartbeat_ack]
218
219         self.sc = sctp.sctp(
220             self.src_port, self.dst_port, self.vtag, self.csum,
221             self.chunks)
222
223         self.buf += b'\x05\x00\x00\x10' + \
224             b'\x00\x01\x00\x0c' + \
225             b'\xff\xee\xdd\xcc\xbb\xaa\x99\x88'
226
227     def setUp_with_abort(self):
228         self.tflag = 0
229         self.length = 4 + 8 + 16 + 8 + 4 + 20 + 8 + 4 + 8 + 8 + 4 + 12 \
230             + 20 + 20
231
232         self.c_invalid_stream_id = sctp.cause_invalid_stream_id(4096)
233         self.c_missing_param = sctp.cause_missing_param(
234             [sctp.PTYPE_IPV4, sctp.PTYPE_IPV6,
235              sctp.PTYPE_COOKIE_PRESERVE, sctp.PTYPE_HOST_ADDR])
236         self.c_stale_cookie = sctp.cause_stale_cookie(b'\x00\x00\x13\x88')
237         self.c_out_of_resource = sctp.cause_out_of_resource()
238         self.c_unresolvable_addr = sctp.cause_unresolvable_addr(
239             sctp.param_host_addr(b'test host\x00'))
240         self.c_unrecognized_chunk = sctp.cause_unrecognized_chunk(
241             b'\xff\x00\x00\x04')
242         self.c_invalid_param = sctp.cause_invalid_param()
243         self.c_unrecognized_param = sctp.cause_unrecognized_param(
244             b'\xff\xff\x00\x04')
245         self.c_no_userdata = sctp.cause_no_userdata(b'\x00\x01\xe2\x40')
246         self.c_cookie_while_shutdown = sctp.cause_cookie_while_shutdown()
247         self.c_restart_with_new_addr = sctp.cause_restart_with_new_addr(
248             sctp.param_ipv4('192.168.1.1'))
249         self.c_user_initiated_abort = sctp.cause_user_initiated_abort(
250             b'Key Interrupt.\x00')
251         self.c_protocol_violation = sctp.cause_protocol_violation(
252             b'Unknown reason.\x00')
253
254         self.causes = [
255             self.c_invalid_stream_id, self.c_missing_param,
256             self.c_stale_cookie, self.c_out_of_resource,
257             self.c_unresolvable_addr, self.c_unrecognized_chunk,
258             self.c_invalid_param, self.c_unrecognized_param,
259             self.c_no_userdata, self.c_cookie_while_shutdown,
260             self.c_restart_with_new_addr, self.c_user_initiated_abort,
261             self.c_protocol_violation]
262
263         self.abort = sctp.chunk_abort(causes=self.causes)
264
265         self.chunks = [self.abort]
266
267         self.sc = sctp.sctp(
268             self.src_port, self.dst_port, self.vtag, self.csum,
269             self.chunks)
270
271         self.buf += b'\x06\x00\x00\x90' + \
272             b'\x00\x01\x00\x08\x10\x00\x00\x00' + \
273             b'\x00\x02\x00\x10\x00\x00\x00\x04' + \
274             b'\x00\x05\x00\x06\x00\x09\x00\x0b' + \
275             b'\x00\x03\x00\x08\x00\x00\x13\x88' + \
276             b'\x00\x04\x00\x04' + \
277             b'\x00\x05\x00\x14' + \
278             b'\x00\x0b\x00\x0e' + \
279             b'\x74\x65\x73\x74\x20\x68\x6f\x73\x74\x00\x00\x00' + \
280             b'\x00\x06\x00\x08\xff\x00\x00\x04' + \
281             b'\x00\x07\x00\x04' + \
282             b'\x00\x08\x00\x08\xff\xff\x00\x04' + \
283             b'\x00\x09\x00\x08\x00\x01\xe2\x40' + \
284             b'\x00\x0a\x00\x04' + \
285             b'\x00\x0b\x00\x0c' + \
286             b'\x00\x05\x00\x08\xc0\xa8\x01\x01' + \
287             b'\x00\x0c\x00\x13' + \
288             b'\x4b\x65\x79\x20\x49\x6e\x74\x65' + \
289             b'\x72\x72\x75\x70\x74\x2e\x00\x00' + \
290             b'\x00\x0d\x00\x14' + \
291             b'\x55\x6e\x6b\x6e\x6f\x77\x6e\x20' + \
292             b'\x72\x65\x61\x73\x6f\x6e\x2e\x00'
293
294     def setUp_with_shutdown(self):
295         self.flags = 0
296         self.length = 8
297         self.tsn_ack = 123456
298
299         self.shutdown = sctp.chunk_shutdown(tsn_ack=self.tsn_ack)
300
301         self.chunks = [self.shutdown]
302
303         self.sc = sctp.sctp(
304             self.src_port, self.dst_port, self.vtag, self.csum,
305             self.chunks)
306
307         self.buf += b'\x07\x00\x00\x08\x00\x01\xe2\x40'
308
309     def setUp_with_shutdown_ack(self):
310         self.flags = 0
311         self.length = 4
312
313         self.shutdown_ack = sctp.chunk_shutdown_ack()
314
315         self.chunks = [self.shutdown_ack]
316
317         self.sc = sctp.sctp(
318             self.src_port, self.dst_port, self.vtag, self.csum,
319             self.chunks)
320
321         self.buf += b'\x08\x00\x00\x04'
322
323     def setUp_with_error(self):
324         self.flags = 0
325         self.length = 4 + 8 + 16 + 8 + 4 + 20 + 8 + 4 + 8 + 8 + 4 + 12 \
326             + 20 + 20
327
328         self.c_invalid_stream_id = sctp.cause_invalid_stream_id(4096)
329         self.c_missing_param = sctp.cause_missing_param(
330             [sctp.PTYPE_IPV4, sctp.PTYPE_IPV6,
331              sctp.PTYPE_COOKIE_PRESERVE, sctp.PTYPE_HOST_ADDR])
332         self.c_stale_cookie = sctp.cause_stale_cookie(b'\x00\x00\x13\x88')
333         self.c_out_of_resource = sctp.cause_out_of_resource()
334         self.c_unresolvable_addr = sctp.cause_unresolvable_addr(
335             sctp.param_host_addr(b'test host\x00'))
336         self.c_unrecognized_chunk = sctp.cause_unrecognized_chunk(
337             b'\xff\x00\x00\x04')
338         self.c_invalid_param = sctp.cause_invalid_param()
339         self.c_unrecognized_param = sctp.cause_unrecognized_param(
340             b'\xff\xff\x00\x04')
341         self.c_no_userdata = sctp.cause_no_userdata(b'\x00\x01\xe2\x40')
342         self.c_cookie_while_shutdown = sctp.cause_cookie_while_shutdown()
343         self.c_restart_with_new_addr = sctp.cause_restart_with_new_addr(
344             sctp.param_ipv4('192.168.1.1'))
345         self.c_user_initiated_abort = sctp.cause_user_initiated_abort(
346             b'Key Interrupt.\x00')
347         self.c_protocol_violation = sctp.cause_protocol_violation(
348             b'Unknown reason.\x00')
349
350         self.causes = [
351             self.c_invalid_stream_id, self.c_missing_param,
352             self.c_stale_cookie, self.c_out_of_resource,
353             self.c_unresolvable_addr, self.c_unrecognized_chunk,
354             self.c_invalid_param, self.c_unrecognized_param,
355             self.c_no_userdata, self.c_cookie_while_shutdown,
356             self.c_restart_with_new_addr, self.c_user_initiated_abort,
357             self.c_protocol_violation]
358
359         self.error = sctp.chunk_error(causes=self.causes)
360
361         self.chunks = [self.error]
362
363         self.sc = sctp.sctp(
364             self.src_port, self.dst_port, self.vtag, self.csum,
365             self.chunks)
366
367         self.buf += b'\x09\x00\x00\x90' + \
368             b'\x00\x01\x00\x08\x10\x00\x00\x00' + \
369             b'\x00\x02\x00\x10\x00\x00\x00\x04' + \
370             b'\x00\x05\x00\x06\x00\x09\x00\x0b' + \
371             b'\x00\x03\x00\x08\x00\x00\x13\x88' + \
372             b'\x00\x04\x00\x04' + \
373             b'\x00\x05\x00\x14' + \
374             b'\x00\x0b\x00\x0e' + \
375             b'\x74\x65\x73\x74\x20\x68\x6f\x73\x74\x00\x00\x00' + \
376             b'\x00\x06\x00\x08\xff\x00\x00\x04' + \
377             b'\x00\x07\x00\x04' + \
378             b'\x00\x08\x00\x08\xff\xff\x00\x04' + \
379             b'\x00\x09\x00\x08\x00\x01\xe2\x40' + \
380             b'\x00\x0a\x00\x04' + \
381             b'\x00\x0b\x00\x0c' + \
382             b'\x00\x05\x00\x08\xc0\xa8\x01\x01' + \
383             b'\x00\x0c\x00\x13' + \
384             b'\x4b\x65\x79\x20\x49\x6e\x74\x65' + \
385             b'\x72\x72\x75\x70\x74\x2e\x00\x00' + \
386             b'\x00\x0d\x00\x14' + \
387             b'\x55\x6e\x6b\x6e\x6f\x77\x6e\x20' + \
388             b'\x72\x65\x61\x73\x6f\x6e\x2e\x00'
389
390     def setUp_with_cookie_echo(self):
391         self.flags = 0
392         self.length = 8
393         self.cookie = b'\x12\x34\x56\x78'
394
395         self.cookie_echo = sctp.chunk_cookie_echo(cookie=self.cookie)
396
397         self.chunks = [self.cookie_echo]
398
399         self.sc = sctp.sctp(
400             self.src_port, self.dst_port, self.vtag, self.csum,
401             self.chunks)
402
403         self.buf += b'\x0a\x00\x00\x08\x12\x34\x56\x78'
404
405     def setUp_with_cookie_ack(self):
406         self.flags = 0
407         self.length = 4
408
409         self.cookie_ack = sctp.chunk_cookie_ack()
410
411         self.chunks = [self.cookie_ack]
412
413         self.sc = sctp.sctp(
414             self.src_port, self.dst_port, self.vtag, self.csum,
415             self.chunks)
416
417         self.buf += b'\x0b\x00\x00\x04'
418
419     def setUp_with_ecn_echo(self):
420         self.flags = 0
421         self.length = 8
422         self.low_tsn = 123456
423
424         self.ecn_echo = sctp.chunk_ecn_echo(low_tsn=self.low_tsn)
425
426         self.chunks = [self.ecn_echo]
427
428         self.sc = sctp.sctp(
429             self.src_port, self.dst_port, self.vtag, self.csum,
430             self.chunks)
431
432         self.buf += b'\x0c\x00\x00\x08\x00\x01\xe2\x40'
433
434     def setUp_with_cwr(self):
435         self.flags = 0
436         self.length = 8
437         self.low_tsn = 123456
438
439         self.cwr = sctp.chunk_cwr(low_tsn=self.low_tsn)
440
441         self.chunks = [self.cwr]
442
443         self.sc = sctp.sctp(
444             self.src_port, self.dst_port, self.vtag, self.csum,
445             self.chunks)
446
447         self.buf += b'\x0d\x00\x00\x08\x00\x01\xe2\x40'
448
449     def setUp_with_shutdown_complete(self):
450         self.tflag = 0
451         self.length = 4
452
453         self.shutdown_complete = sctp.chunk_shutdown_complete()
454
455         self.chunks = [self.shutdown_complete]
456
457         self.sc = sctp.sctp(
458             self.src_port, self.dst_port, self.vtag, self.csum,
459             self.chunks)
460
461         self.buf += b'\x0e\x00\x00\x04'
462
463     def setUp_with_multi_chunks(self):
464         self.s_flags = 0
465         self.s_length = 16
466         self.s_tsn_ack = 123456
467         self.s_a_rwnd = 9876
468         self.s_gapack_num = 0
469         self.s_duptsn_num = 0
470         self.s_gapacks = None
471         self.s_duptsns = None
472
473         self.sack = sctp.chunk_sack(
474             tsn_ack=self.s_tsn_ack, a_rwnd=self.s_a_rwnd)
475
476         self.d1_unordered = 0
477         self.d1_begin = 1
478         self.d1_end = 0
479         self.d1_length = 16 + 10
480         self.d1_tsn = 12345
481         self.d1_sid = 1
482         self.d1_seq = 0
483         self.d1_payload_id = 0
484         self.d1_payload_data = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a'
485
486         self.data1 = sctp.chunk_data(
487             begin=self.d1_begin, tsn=self.d1_tsn, sid=self.d1_sid,
488             payload_data=self.d1_payload_data)
489
490         self.d2_unordered = 0
491         self.d2_begin = 0
492         self.d2_end = 1
493         self.d2_length = 16 + 10
494         self.d2_tsn = 12346
495         self.d2_sid = 1
496         self.d2_seq = 1
497         self.d2_payload_id = 0
498         self.d2_payload_data = b'\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a'
499
500         self.data2 = sctp.chunk_data(
501             end=self.d2_end, tsn=self.d2_tsn, sid=self.d2_sid,
502             seq=self.d2_seq, payload_data=self.d2_payload_data)
503
504         self.chunks = [self.sack, self.data1, self.data2]
505
506         self.sc = sctp.sctp(
507             self.src_port, self.dst_port, self.vtag, self.csum,
508             self.chunks)
509
510         self.buf += b'\x03\x00\x00\x10\x00\x01\xe2\x40' + \
511             b'\x00\x00\x26\x94\x00\x00\x00\x00' + \
512             b'\x00\x02\x00\x1a\x00\x00\x30\x39\x00\x01\x00\x00' + \
513             b'\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a' + \
514             b'\x00\x01\x00\x1a\x00\x00\x30\x3a\x00\x01\x00\x01' + \
515             b'\x00\x00\x00\x00\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a'
516
517     def tearDown(self):
518         pass
519
520     def test_init(self):
521         eq_(self.src_port, self.sc.src_port)
522         eq_(self.dst_port, self.sc.dst_port)
523         eq_(self.vtag, self.sc.vtag)
524         eq_(self.csum, self.sc.csum)
525         eq_(self.chunks, self.sc.chunks)
526
527     def test_init_with_data(self):
528         self.setUp_with_data()
529         self.test_init()
530
531     def test_init_with_init(self):
532         self.setUp_with_init()
533         self.test_init()
534
535     def test_init_with_init_ack(self):
536         self.setUp_with_init_ack()
537         self.test_init()
538
539     def test_init_with_sack(self):
540         self.setUp_with_sack()
541         self.test_init()
542
543     def test_init_with_heartbeat(self):
544         self.setUp_with_heartbeat()
545         self.test_init()
546
547     def test_init_with_heartbeat_ack(self):
548         self.setUp_with_heartbeat_ack()
549         self.test_init()
550
551     def test_init_with_abort(self):
552         self.setUp_with_abort()
553         self.test_init()
554
555     def test_init_with_shutdown(self):
556         self.setUp_with_shutdown()
557         self.test_init()
558
559     def test_init_with_shutdown_ack(self):
560         self.setUp_with_shutdown_ack()
561         self.test_init()
562
563     def test_init_with_error(self):
564         self.setUp_with_error()
565         self.test_init()
566
567     def test_init_with_cookie_echo(self):
568         self.setUp_with_cookie_echo()
569         self.test_init()
570
571     def test_init_with_cookie_ack(self):
572         self.setUp_with_cookie_ack()
573         self.test_init()
574
575     def test_init_with_ecn_echo(self):
576         self.setUp_with_ecn_echo()
577         self.test_init()
578
579     def test_init_with_cwr(self):
580         self.setUp_with_cwr()
581         self.test_init()
582
583     def test_init_with_shutdown_complete(self):
584         self.setUp_with_shutdown_complete()
585         self.test_init()
586
587     def test_init_with_multi_chunks(self):
588         self.setUp_with_multi_chunks()
589         self.test_init()
590
591     def test_parser(self):
592         _res = self.sc.parser(six.binary_type(self.buf))
593         if type(_res) is tuple:
594             res = _res[0]
595         else:
596             res = _res
597         # to calculate the lengths of parameters.
598         self.sc.serialize(None, None)
599
600         eq_(self.src_port, res.src_port)
601         eq_(self.dst_port, res.dst_port)
602         eq_(self.vtag, res.vtag)
603         eq_(self.csum, res.csum)
604         eq_(str(self.chunks), str(res.chunks))
605
606     def test_parser_with_data(self):
607         self.setUp_with_data()
608         self.test_parser()
609
610     def test_parser_with_init(self):
611         self.setUp_with_init()
612         self.test_parser()
613
614     def test_parser_with_init_ack(self):
615         self.setUp_with_init_ack()
616         self.test_parser()
617
618     def test_parser_with_sack(self):
619         self.setUp_with_sack()
620         self.test_parser()
621
622     def test_parser_with_heartbeat(self):
623         self.setUp_with_heartbeat()
624         self.test_parser()
625
626     def test_parser_with_heartbeat_ack(self):
627         self.setUp_with_heartbeat_ack()
628         self.test_parser()
629
630     def test_parser_with_abort(self):
631         self.setUp_with_abort()
632         self.test_parser()
633
634     def test_parser_with_shutdown(self):
635         self.setUp_with_shutdown()
636         self.test_parser()
637
638     def test_parser_with_shutdown_ack(self):
639         self.setUp_with_shutdown_ack()
640         self.test_parser()
641
642     def test_parser_with_error(self):
643         self.setUp_with_error()
644         self.test_parser()
645
646     def test_parser_with_cookie_echo(self):
647         self.setUp_with_cookie_echo()
648         self.test_parser()
649
650     def test_parser_with_cookie_ack(self):
651         self.setUp_with_cookie_ack()
652         self.test_parser()
653
654     def test_parser_with_ecn_echo(self):
655         self.setUp_with_ecn_echo()
656         self.test_parser()
657
658     def test_parser_with_cwr(self):
659         self.setUp_with_cwr()
660         self.test_parser()
661
662     def test_parser_with_shutdown_complete(self):
663         self.setUp_with_shutdown_complete()
664         self.test_parser()
665
666     def test_parser_with_multi_chunks(self):
667         self.setUp_with_multi_chunks()
668         self.test_parser()
669
670     def _test_serialize(self):
671         buf = self.sc.serialize(bytearray(), None)
672         res = struct.unpack_from(sctp.sctp._PACK_STR, buf)
673         eq_(self.src_port, res[0])
674         eq_(self.dst_port, res[1])
675         eq_(self.vtag, res[2])
676         # skip compare checksum
677         # eq_(self.csum, res[3])
678
679         return buf[sctp.sctp._MIN_LEN:]
680
681     def test_serialize(self):
682         self._test_serialize()
683
684     def test_serialize_with_data(self):
685         self.setUp_with_data()
686         buf = self._test_serialize()
687         res = struct.unpack_from(sctp.chunk_data._PACK_STR, buf)
688         eq_(sctp.chunk_data.chunk_type(), res[0])
689         flags = (
690             (self.unordered << 2) |
691             (self.begin << 1) |
692             (self.end << 0))
693         eq_(flags, res[1])
694         eq_(self.length, res[2])
695         eq_(self.tsn, res[3])
696         eq_(self.sid, res[4])
697         eq_(self.seq, res[5])
698         eq_(self.payload_id, res[6])
699         eq_(self.payload_data, buf[sctp.chunk_data._MIN_LEN:])
700
701     def test_serialize_with_init(self):
702         self.setUp_with_init()
703         buf = self._test_serialize()
704         res = struct.unpack_from(sctp.chunk_init._PACK_STR, buf)
705         eq_(sctp.chunk_init.chunk_type(), res[0])
706         eq_(self.flags, res[1])
707         eq_(self.length, res[2])
708         eq_(self.init_tag, res[3])
709         eq_(self.a_rwnd, res[4])
710         eq_(self.os, res[5])
711         eq_(self.mis, res[6])
712         eq_(self.i_tsn, res[7])
713
714         buf = buf[sctp.chunk_init._MIN_LEN:]
715         res1 = struct.unpack_from(sctp.param_ipv4._PACK_STR, buf)
716         eq_(sctp.param_ipv4.param_type(), res1[0])
717         eq_(8, res1[1])
718         eq_('192.168.1.1', addrconv.ipv4.bin_to_text(
719             buf[sctp.param_ipv4._MIN_LEN:sctp.param_ipv4._MIN_LEN + 4]))
720
721         buf = buf[8:]
722         res2 = struct.unpack_from(sctp.param_ipv6._PACK_STR, buf)
723         eq_(sctp.param_ipv6.param_type(), res2[0])
724         eq_(20, res2[1])
725         eq_('fe80::647e:1aff:fec4:8284', addrconv.ipv6.bin_to_text(
726             buf[sctp.param_ipv6._MIN_LEN:sctp.param_ipv6._MIN_LEN + 16]))
727
728         buf = buf[20:]
729         res3 = struct.unpack_from(sctp.param_cookie_preserve._PACK_STR,
730                                   buf)
731         eq_(sctp.param_cookie_preserve.param_type(), res3[0])
732         eq_(8, res3[1])
733         eq_(5000, res3[2])
734
735         buf = buf[8:]
736         res4 = struct.unpack_from(sctp.param_ecn._PACK_STR, buf)
737         eq_(sctp.param_ecn.param_type(), res4[0])
738         eq_(4, res4[1])
739
740         buf = buf[4:]
741         res5 = struct.unpack_from(sctp.param_host_addr._PACK_STR, buf)
742         eq_(sctp.param_host_addr.param_type(), res5[0])
743         eq_(14, res5[1])
744         eq_(b'test host\x00',
745             buf[sctp.param_host_addr._MIN_LEN:
746                 sctp.param_host_addr._MIN_LEN + 10])
747
748         buf = buf[16:]
749         res6 = struct.unpack_from(sctp.param_supported_addr._PACK_STR, buf)
750         res6 = list(res6)
751         eq_(sctp.param_supported_addr.param_type(), res6[0])
752         eq_(14, res6[1])
753         buf = buf[sctp.param_supported_addr._MIN_LEN:]
754         offset = 0
755         tmplist = []
756         while offset < len(buf):
757             (tmp, ) = struct.unpack_from('!H', buf, offset)
758             tmplist.append(tmp)
759             offset += struct.calcsize('!H')
760         res6.extend(tmplist)
761         eq_(sctp.PTYPE_IPV4, res6[2])
762         eq_(sctp.PTYPE_IPV6, res6[3])
763         eq_(sctp.PTYPE_COOKIE_PRESERVE, res6[4])
764         eq_(sctp.PTYPE_ECN, res6[5])
765         eq_(sctp.PTYPE_HOST_ADDR, res6[6])
766
767     def test_serialize_with_init_ack(self):
768         self.setUp_with_init_ack()
769         buf = self._test_serialize()
770         res = struct.unpack_from(sctp.chunk_init_ack._PACK_STR, buf)
771         eq_(sctp.chunk_init_ack.chunk_type(), res[0])
772         eq_(self.flags, res[1])
773         eq_(self.length, res[2])
774         eq_(self.init_tag, res[3])
775         eq_(self.a_rwnd, res[4])
776         eq_(self.os, res[5])
777         eq_(self.mis, res[6])
778         eq_(self.i_tsn, res[7])
779
780         buf = buf[sctp.chunk_init_ack._MIN_LEN:]
781         res1 = struct.unpack_from(sctp.param_state_cookie._PACK_STR, buf)
782         eq_(sctp.param_state_cookie.param_type(), res1[0])
783         eq_(7, res1[1])
784         eq_(b'\x01\x02\x03',
785             buf[sctp.param_state_cookie._MIN_LEN:
786                 sctp.param_state_cookie._MIN_LEN + 3])
787
788         buf = buf[8:]
789         res2 = struct.unpack_from(sctp.param_ipv4._PACK_STR, buf)
790         eq_(sctp.param_ipv4.param_type(), res2[0])
791         eq_(8, res2[1])
792         eq_('192.168.1.1', addrconv.ipv4.bin_to_text(
793             buf[sctp.param_ipv4._MIN_LEN:sctp.param_ipv4._MIN_LEN + 4]))
794
795         buf = buf[8:]
796         res3 = struct.unpack_from(sctp.param_ipv6._PACK_STR, buf)
797         eq_(sctp.param_ipv6.param_type(), res3[0])
798         eq_(20, res3[1])
799         eq_('fe80::647e:1aff:fec4:8284', addrconv.ipv6.bin_to_text(
800             buf[sctp.param_ipv6._MIN_LEN:sctp.param_ipv6._MIN_LEN + 16]))
801
802         buf = buf[20:]
803         res4 = struct.unpack_from(
804             sctp.param_unrecognized_param._PACK_STR, buf)
805         eq_(sctp.param_unrecognized_param.param_type(), res4[0])
806         eq_(8, res4[1])
807         eq_(b'\xff\xff\x00\x04',
808             buf[sctp.param_unrecognized_param._MIN_LEN:
809                 sctp.param_unrecognized_param._MIN_LEN + 4])
810
811         buf = buf[8:]
812         res5 = struct.unpack_from(sctp.param_ecn._PACK_STR, buf)
813         eq_(sctp.param_ecn.param_type(), res5[0])
814         eq_(4, res5[1])
815
816         buf = buf[4:]
817         res6 = struct.unpack_from(sctp.param_host_addr._PACK_STR, buf)
818         eq_(sctp.param_host_addr.param_type(), res6[0])
819         eq_(14, res6[1])
820         eq_(b'test host\x00',
821             buf[sctp.param_host_addr._MIN_LEN:
822                 sctp.param_host_addr._MIN_LEN + 10])
823
824     def test_serialize_with_sack(self):
825         self.setUp_with_sack()
826         buf = self._test_serialize()
827         res = struct.unpack_from(sctp.chunk_sack._PACK_STR, buf)
828         eq_(sctp.chunk_sack.chunk_type(), res[0])
829         eq_(self.flags, res[1])
830         eq_(self.length, res[2])
831         eq_(self.tsn_ack, res[3])
832         eq_(self.a_rwnd, res[4])
833         eq_(self.gapack_num, res[5])
834         eq_(self.duptsn_num, res[6])
835
836         buf = buf[sctp.chunk_sack._MIN_LEN:]
837         gapacks = []
838         for _ in range(self.gapack_num):
839             (gap_s, gap_e) = struct.unpack_from(
840                 sctp.chunk_sack._GAPACK_STR, buf)
841             one = [gap_s, gap_e]
842             gapacks.append(one)
843             buf = buf[sctp.chunk_sack._GAPACK_LEN:]
844         duptsns = []
845         for _ in range(self.duptsn_num):
846             (duptsn, ) = struct.unpack_from(
847                 sctp.chunk_sack._DUPTSN_STR, buf)
848             duptsns.append(duptsn)
849             buf = buf[sctp.chunk_sack._DUPTSN_LEN:]
850         eq_(self.gapacks, gapacks)
851         eq_(self.duptsns, duptsns)
852
853     def test_serialize_with_heartbeat(self):
854         self.setUp_with_heartbeat()
855         buf = self._test_serialize()
856         res = struct.unpack_from(sctp.chunk_heartbeat._PACK_STR, buf)
857         eq_(sctp.chunk_heartbeat.chunk_type(), res[0])
858         eq_(self.flags, res[1])
859         eq_(self.length, res[2])
860
861         buf = buf[sctp.chunk_heartbeat._MIN_LEN:]
862         res1 = struct.unpack_from(sctp.param_heartbeat._PACK_STR, buf)
863         eq_(sctp.param_heartbeat.param_type(), res1[0])
864         eq_(8, res1[1])
865         eq_(b'\x01\x02\x03\x04',
866             buf[sctp.param_heartbeat._MIN_LEN:
867                 sctp.param_heartbeat._MIN_LEN + 4])
868
869     def test_serialize_with_heartbeat_ack(self):
870         self.setUp_with_heartbeat_ack()
871         buf = self._test_serialize()
872         res = struct.unpack_from(sctp.chunk_heartbeat_ack._PACK_STR, buf)
873         eq_(sctp.chunk_heartbeat_ack.chunk_type(), res[0])
874         eq_(self.flags, res[1])
875         eq_(self.length, res[2])
876
877         buf = buf[sctp.chunk_heartbeat_ack._MIN_LEN:]
878         res1 = struct.unpack_from(sctp.param_heartbeat._PACK_STR, buf)
879         eq_(sctp.param_heartbeat.param_type(), res1[0])
880         eq_(12, res1[1])
881         eq_(b'\xff\xee\xdd\xcc\xbb\xaa\x99\x88',
882             buf[sctp.param_heartbeat._MIN_LEN:
883                 sctp.param_heartbeat._MIN_LEN + 8])
884
885     def test_serialize_with_abort(self):
886         self.setUp_with_abort()
887         buf = self._test_serialize()
888         res = struct.unpack_from(sctp.chunk_abort._PACK_STR, buf)
889         eq_(sctp.chunk_abort.chunk_type(), res[0])
890         flags = self.tflag << 0
891         eq_(flags, res[1])
892         eq_(self.length, res[2])
893
894         buf = buf[sctp.chunk_abort._MIN_LEN:]
895         res1 = struct.unpack_from(sctp.cause_invalid_stream_id._PACK_STR, buf)
896         eq_(sctp.cause_invalid_stream_id.cause_code(), res1[0])
897         eq_(8, res1[1])
898         eq_(4096, res1[2])
899
900         buf = buf[8:]
901         res2 = struct.unpack_from(sctp.cause_missing_param._PACK_STR, buf)
902         eq_(sctp.cause_missing_param.cause_code(), res2[0])
903         eq_(16, res2[1])
904         eq_(4, res2[2])
905         types = []
906         for count in range(4):
907             (tmp, ) = struct.unpack_from(
908                 '!H', buf, sctp.cause_missing_param._MIN_LEN + 2 * count)
909             types.append(tmp)
910         eq_(str([sctp.PTYPE_IPV4, sctp.PTYPE_IPV6,
911                  sctp.PTYPE_COOKIE_PRESERVE, sctp.PTYPE_HOST_ADDR]),
912             str(types))
913
914         buf = buf[16:]
915         res3 = struct.unpack_from(sctp.cause_stale_cookie._PACK_STR, buf)
916         eq_(sctp.cause_stale_cookie.cause_code(), res3[0])
917         eq_(8, res3[1])
918         eq_(b'\x00\x00\x13\x88',
919             buf[sctp.cause_stale_cookie._MIN_LEN:
920                 sctp.cause_stale_cookie._MIN_LEN + 4])
921
922         buf = buf[8:]
923         res4 = struct.unpack_from(sctp.cause_out_of_resource._PACK_STR, buf)
924         eq_(sctp.cause_out_of_resource.cause_code(), res4[0])
925         eq_(4, res4[1])
926
927         buf = buf[4:]
928         res5 = struct.unpack_from(
929             sctp.cause_unresolvable_addr._PACK_STR, buf)
930         eq_(sctp.cause_unresolvable_addr.cause_code(), res5[0])
931         eq_(20, res5[1])
932         eq_(b'\x00\x0b\x00\x0e\x74\x65\x73\x74' +
933             b'\x20\x68\x6f\x73\x74\x00\x00\x00',
934             buf[sctp.cause_unresolvable_addr._MIN_LEN:
935                 sctp.cause_unresolvable_addr._MIN_LEN + 16])
936
937         buf = buf[20:]
938         res6 = struct.unpack_from(
939             sctp.cause_unrecognized_chunk._PACK_STR, buf)
940         eq_(sctp.cause_unrecognized_chunk.cause_code(), res6[0])
941         eq_(8, res6[1])
942         eq_(b'\xff\x00\x00\x04',
943             buf[sctp.cause_unrecognized_chunk._MIN_LEN:
944                 sctp.cause_unrecognized_chunk._MIN_LEN + 4])
945
946         buf = buf[8:]
947         res7 = struct.unpack_from(sctp.cause_invalid_param._PACK_STR, buf)
948         eq_(sctp.cause_invalid_param.cause_code(), res7[0])
949         eq_(4, res7[1])
950
951         buf = buf[4:]
952         res8 = struct.unpack_from(
953             sctp.cause_unrecognized_param._PACK_STR, buf)
954         eq_(sctp.cause_unrecognized_param.cause_code(), res8[0])
955         eq_(8, res8[1])
956         eq_(b'\xff\xff\x00\x04',
957             buf[sctp.cause_unrecognized_param._MIN_LEN:
958                 sctp.cause_unrecognized_param._MIN_LEN + 4])
959
960         buf = buf[8:]
961         res9 = struct.unpack_from(sctp.cause_no_userdata._PACK_STR, buf)
962         eq_(sctp.cause_no_userdata.cause_code(), res9[0])
963         eq_(8, res9[1])
964         eq_(b'\x00\x01\xe2\x40',
965             buf[sctp.cause_no_userdata._MIN_LEN:
966                 sctp.cause_no_userdata._MIN_LEN + 4])
967
968         buf = buf[8:]
969         res10 = struct.unpack_from(
970             sctp.cause_cookie_while_shutdown._PACK_STR, buf)
971         eq_(sctp.cause_cookie_while_shutdown.cause_code(), res10[0])
972         eq_(4, res10[1])
973
974         buf = buf[4:]
975         res11 = struct.unpack_from(
976             sctp.cause_restart_with_new_addr._PACK_STR, buf)
977         eq_(sctp.cause_restart_with_new_addr.cause_code(), res11[0])
978         eq_(12, res11[1])
979         eq_(b'\x00\x05\x00\x08\xc0\xa8\x01\x01',
980             buf[sctp.cause_restart_with_new_addr._MIN_LEN:
981                 sctp.cause_restart_with_new_addr._MIN_LEN + 8])
982
983         buf = buf[12:]
984         res12 = struct.unpack_from(
985             sctp.cause_user_initiated_abort._PACK_STR, buf)
986         eq_(sctp.cause_user_initiated_abort.cause_code(), res12[0])
987         eq_(19, res12[1])
988         eq_(b'Key Interrupt.\x00',
989             buf[sctp.cause_user_initiated_abort._MIN_LEN:
990                 sctp.cause_user_initiated_abort._MIN_LEN + 15])
991
992         buf = buf[20:]
993         res13 = struct.unpack_from(
994             sctp.cause_protocol_violation._PACK_STR, buf)
995         eq_(sctp.cause_protocol_violation.cause_code(), res13[0])
996         eq_(20, res13[1])
997         eq_(b'Unknown reason.\x00',
998             buf[sctp.cause_protocol_violation._MIN_LEN:
999                 sctp.cause_protocol_violation._MIN_LEN + 16])
1000
1001     def test_serialize_with_shutdown(self):
1002         self.setUp_with_shutdown()
1003         buf = self._test_serialize()
1004         res = struct.unpack_from(sctp.chunk_shutdown._PACK_STR, buf)
1005         eq_(sctp.chunk_shutdown.chunk_type(), res[0])
1006         eq_(self.flags, res[1])
1007         eq_(self.length, res[2])
1008         eq_(self.tsn_ack, res[3])
1009
1010     def test_serialize_with_shutdown_ack(self):
1011         self.setUp_with_shutdown_ack()
1012         buf = self._test_serialize()
1013         res = struct.unpack_from(sctp.chunk_shutdown_ack._PACK_STR, buf)
1014         eq_(sctp.chunk_shutdown_ack.chunk_type(), res[0])
1015         eq_(self.flags, res[1])
1016         eq_(self.length, res[2])
1017
1018     def test_serialize_with_error(self):
1019         self.setUp_with_error()
1020         buf = self._test_serialize()
1021         res = struct.unpack_from(sctp.chunk_error._PACK_STR, buf)
1022         eq_(sctp.chunk_error.chunk_type(), res[0])
1023         eq_(self.flags, res[1])
1024         eq_(self.length, res[2])
1025
1026         buf = buf[sctp.chunk_error._MIN_LEN:]
1027         res1 = struct.unpack_from(sctp.cause_invalid_stream_id._PACK_STR, buf)
1028         eq_(sctp.cause_invalid_stream_id.cause_code(), res1[0])
1029         eq_(8, res1[1])
1030         eq_(4096, res1[2])
1031
1032         buf = buf[8:]
1033         res2 = struct.unpack_from(sctp.cause_missing_param._PACK_STR, buf)
1034         eq_(sctp.cause_missing_param.cause_code(), res2[0])
1035         eq_(16, res2[1])
1036         eq_(4, res2[2])
1037         types = []
1038         for count in range(4):
1039             (tmp, ) = struct.unpack_from(
1040                 '!H', buf, sctp.cause_missing_param._MIN_LEN + 2 * count)
1041             types.append(tmp)
1042         eq_(str([sctp.PTYPE_IPV4, sctp.PTYPE_IPV6,
1043                  sctp.PTYPE_COOKIE_PRESERVE, sctp.PTYPE_HOST_ADDR]),
1044             str(types))
1045
1046         buf = buf[16:]
1047         res3 = struct.unpack_from(sctp.cause_stale_cookie._PACK_STR, buf)
1048         eq_(sctp.cause_stale_cookie.cause_code(), res3[0])
1049         eq_(8, res3[1])
1050         eq_(b'\x00\x00\x13\x88',
1051             buf[sctp.cause_stale_cookie._MIN_LEN:
1052                 sctp.cause_stale_cookie._MIN_LEN + 4])
1053
1054         buf = buf[8:]
1055         res4 = struct.unpack_from(sctp.cause_out_of_resource._PACK_STR, buf)
1056         eq_(sctp.cause_out_of_resource.cause_code(), res4[0])
1057         eq_(4, res4[1])
1058
1059         buf = buf[4:]
1060         res5 = struct.unpack_from(
1061             sctp.cause_unresolvable_addr._PACK_STR, buf)
1062         eq_(sctp.cause_unresolvable_addr.cause_code(), res5[0])
1063         eq_(20, res5[1])
1064         eq_(b'\x00\x0b\x00\x0e\x74\x65\x73\x74' +
1065             b'\x20\x68\x6f\x73\x74\x00\x00\x00',
1066             buf[sctp.cause_unresolvable_addr._MIN_LEN:
1067                 sctp.cause_unresolvable_addr._MIN_LEN + 16])
1068
1069         buf = buf[20:]
1070         res6 = struct.unpack_from(
1071             sctp.cause_unrecognized_chunk._PACK_STR, buf)
1072         eq_(sctp.cause_unrecognized_chunk.cause_code(), res6[0])
1073         eq_(8, res6[1])
1074         eq_(b'\xff\x00\x00\x04',
1075             buf[sctp.cause_unrecognized_chunk._MIN_LEN:
1076                 sctp.cause_unrecognized_chunk._MIN_LEN + 4])
1077
1078         buf = buf[8:]
1079         res7 = struct.unpack_from(sctp.cause_invalid_param._PACK_STR, buf)
1080         eq_(sctp.cause_invalid_param.cause_code(), res7[0])
1081         eq_(4, res7[1])
1082
1083         buf = buf[4:]
1084         res8 = struct.unpack_from(
1085             sctp.cause_unrecognized_param._PACK_STR, buf)
1086         eq_(sctp.cause_unrecognized_param.cause_code(), res8[0])
1087         eq_(8, res8[1])
1088         eq_(b'\xff\xff\x00\x04',
1089             buf[sctp.cause_unrecognized_param._MIN_LEN:
1090                 sctp.cause_unrecognized_param._MIN_LEN + 4])
1091
1092         buf = buf[8:]
1093         res9 = struct.unpack_from(sctp.cause_no_userdata._PACK_STR, buf)
1094         eq_(sctp.cause_no_userdata.cause_code(), res9[0])
1095         eq_(8, res9[1])
1096         eq_(b'\x00\x01\xe2\x40',
1097             buf[sctp.cause_no_userdata._MIN_LEN:
1098                 sctp.cause_no_userdata._MIN_LEN + 4])
1099
1100         buf = buf[8:]
1101         res10 = struct.unpack_from(
1102             sctp.cause_cookie_while_shutdown._PACK_STR, buf)
1103         eq_(sctp.cause_cookie_while_shutdown.cause_code(), res10[0])
1104         eq_(4, res10[1])
1105
1106         buf = buf[4:]
1107         res11 = struct.unpack_from(
1108             sctp.cause_restart_with_new_addr._PACK_STR, buf)
1109         eq_(sctp.cause_restart_with_new_addr.cause_code(), res11[0])
1110         eq_(12, res11[1])
1111         eq_(b'\x00\x05\x00\x08\xc0\xa8\x01\x01',
1112             buf[sctp.cause_restart_with_new_addr._MIN_LEN:
1113                 sctp.cause_restart_with_new_addr._MIN_LEN + 8])
1114
1115         buf = buf[12:]
1116         res12 = struct.unpack_from(
1117             sctp.cause_user_initiated_abort._PACK_STR, buf)
1118         eq_(sctp.cause_user_initiated_abort.cause_code(), res12[0])
1119         eq_(19, res12[1])
1120         eq_(b'Key Interrupt.\x00',
1121             buf[sctp.cause_user_initiated_abort._MIN_LEN:
1122                 sctp.cause_user_initiated_abort._MIN_LEN + 15])
1123
1124         buf = buf[20:]
1125         res13 = struct.unpack_from(
1126             sctp.cause_protocol_violation._PACK_STR, buf)
1127         eq_(sctp.cause_protocol_violation.cause_code(), res13[0])
1128         eq_(20, res13[1])
1129         eq_(b'Unknown reason.\x00',
1130             buf[sctp.cause_protocol_violation._MIN_LEN:
1131                 sctp.cause_protocol_violation._MIN_LEN + 16])
1132
1133     def test_serialize_with_cookie_echo(self):
1134         self.setUp_with_cookie_echo()
1135         buf = self._test_serialize()
1136         res = struct.unpack_from(sctp.chunk_cookie_echo._PACK_STR, buf)
1137         eq_(sctp.chunk_cookie_echo.chunk_type(), res[0])
1138         eq_(self.flags, res[1])
1139         eq_(self.length, res[2])
1140         eq_(self.cookie,
1141             buf[sctp.chunk_cookie_echo._MIN_LEN:
1142                 sctp.chunk_cookie_echo._MIN_LEN + 4])
1143
1144     def test_serialize_with_cookie_ack(self):
1145         self.setUp_with_cookie_ack()
1146         buf = self._test_serialize()
1147         res = struct.unpack_from(sctp.chunk_cookie_ack._PACK_STR, buf)
1148         eq_(sctp.chunk_cookie_ack.chunk_type(), res[0])
1149         eq_(self.flags, res[1])
1150         eq_(self.length, res[2])
1151
1152     def test_serialize_with_ecn_echo(self):
1153         self.setUp_with_ecn_echo()
1154         buf = self._test_serialize()
1155         res = struct.unpack_from(sctp.chunk_ecn_echo._PACK_STR, buf)
1156         eq_(sctp.chunk_ecn_echo.chunk_type(), res[0])
1157         eq_(self.flags, res[1])
1158         eq_(self.length, res[2])
1159         eq_(self.low_tsn, res[3])
1160
1161     def test_serialize_with_cwr(self):
1162         self.setUp_with_cwr()
1163         buf = self._test_serialize()
1164         res = struct.unpack_from(sctp.chunk_cwr._PACK_STR, buf)
1165         eq_(sctp.chunk_cwr.chunk_type(), res[0])
1166         eq_(self.flags, res[1])
1167         eq_(self.length, res[2])
1168         eq_(self.low_tsn, res[3])
1169
1170     def test_serialize_with_shutdown_complete(self):
1171         self.setUp_with_shutdown_complete()
1172         buf = self._test_serialize()
1173         res = struct.unpack_from(
1174             sctp.chunk_shutdown_complete._PACK_STR, buf)
1175         eq_(sctp.chunk_shutdown_complete.chunk_type(), res[0])
1176         flags = self.tflag << 0
1177         eq_(flags, res[1])
1178         eq_(self.length, res[2])
1179
1180     def test_serialize_with_multi_chunks(self):
1181         self.setUp_with_multi_chunks()
1182         buf = self._test_serialize()
1183         res = struct.unpack_from(sctp.chunk_sack._PACK_STR, buf)
1184         eq_(sctp.chunk_sack.chunk_type(), res[0])
1185         eq_(self.s_flags, res[1])
1186         eq_(self.s_length, res[2])
1187         eq_(self.s_tsn_ack, res[3])
1188         eq_(self.s_a_rwnd, res[4])
1189         eq_(self.s_gapack_num, res[5])
1190         eq_(self.s_duptsn_num, res[6])
1191
1192         buf = buf[self.s_length:]
1193         res = struct.unpack_from(sctp.chunk_data._PACK_STR, buf)
1194         eq_(sctp.chunk_data.chunk_type(), res[0])
1195         d1_flags = (
1196             (self.d1_unordered << 2) |
1197             (self.d1_begin << 1) |
1198             (self.d1_end << 0))
1199         eq_(d1_flags, res[1])
1200         eq_(self.d1_length, res[2])
1201         eq_(self.d1_tsn, res[3])
1202         eq_(self.d1_sid, res[4])
1203         eq_(self.d1_seq, res[5])
1204         eq_(self.d1_payload_id, res[6])
1205         eq_(self.d1_payload_data,
1206             buf[sctp.chunk_data._MIN_LEN:
1207                 sctp.chunk_data._MIN_LEN + 10])
1208
1209         buf = buf[self.d1_length:]
1210         res = struct.unpack_from(sctp.chunk_data._PACK_STR, buf)
1211         eq_(sctp.chunk_data.chunk_type(), res[0])
1212         d2_flags = (
1213             (self.d2_unordered << 2) |
1214             (self.d2_begin << 1) |
1215             (self.d2_end << 0))
1216         eq_(d2_flags, res[1])
1217         eq_(self.d2_length, res[2])
1218         eq_(self.d2_tsn, res[3])
1219         eq_(self.d2_sid, res[4])
1220         eq_(self.d2_seq, res[5])
1221         eq_(self.d2_payload_id, res[6])
1222         eq_(self.d2_payload_data,
1223             buf[sctp.chunk_data._MIN_LEN:
1224                 sctp.chunk_data._MIN_LEN + 10])
1225
1226     def test_build_sctp(self):
1227         eth = ethernet.ethernet('00:aa:aa:aa:aa:aa', '00:bb:bb:bb:bb:bb',
1228                                 ether.ETH_TYPE_IP)
1229         ip4 = ipv4.ipv4(4, 5, 16, 0, 0, 2, 0, 64, inet.IPPROTO_SCTP, 0,
1230                         '192.168.1.1', '10.144.1.1')
1231         pkt = eth / ip4 / self.sc
1232
1233         eth = pkt.get_protocol(ethernet.ethernet)
1234         ok_(eth)
1235         eq_(eth.ethertype, ether.ETH_TYPE_IP)
1236
1237         ip4 = pkt.get_protocol(ipv4.ipv4)
1238         ok_(ip4)
1239         eq_(ip4.proto, inet.IPPROTO_SCTP)
1240
1241         sc = pkt.get_protocol(sctp.sctp)
1242         ok_(sc)
1243         eq_(sc, self.sc)
1244
1245     def test_build_sctp_with_data(self):
1246         self.setUp_with_data()
1247         self.test_build_sctp()
1248
1249     def test_build_sctp_with_init(self):
1250         self.setUp_with_init()
1251         self.test_build_sctp()
1252
1253     def test_build_sctp_with_init_ack(self):
1254         self.setUp_with_init_ack()
1255         self.test_build_sctp()
1256
1257     def test_build_sctp_with_sack(self):
1258         self.setUp_with_sack()
1259         self.test_build_sctp()
1260
1261     def test_build_sctp_with_heartbeat(self):
1262         self.setUp_with_heartbeat()
1263         self.test_build_sctp()
1264
1265     def test_build_sctp_with_heartbeat_ack(self):
1266         self.setUp_with_heartbeat_ack()
1267         self.test_build_sctp()
1268
1269     def test_build_sctp_with_abort(self):
1270         self.setUp_with_abort()
1271         self.test_build_sctp()
1272
1273     def test_build_sctp_with_shutdown(self):
1274         self.setUp_with_shutdown()
1275         self.test_build_sctp()
1276
1277     def test_build_sctp_with_shutdown_ack(self):
1278         self.setUp_with_shutdown_ack()
1279         self.test_build_sctp()
1280
1281     def test_build_sctp_with_error(self):
1282         self.setUp_with_error()
1283         self.test_build_sctp()
1284
1285     def test_build_sctp_with_cookie_echo(self):
1286         self.setUp_with_cookie_echo()
1287         self.test_build_sctp()
1288
1289     def test_build_sctp_with_cookie_ack(self):
1290         self.setUp_with_cookie_ack()
1291         self.test_build_sctp()
1292
1293     def test_build_sctp_with_ecn_echo(self):
1294         self.setUp_with_ecn_echo()
1295         self.test_build_sctp()
1296
1297     def test_build_sctp_with_cwr(self):
1298         self.setUp_with_cwr()
1299         self.test_build_sctp()
1300
1301     def test_build_sctp_with_shutdown_complete(self):
1302         self.setUp_with_shutdown_complete()
1303         self.test_build_sctp()
1304
1305     def tset_build_sctp_with_multi_chunks(self):
1306         self.setUp_with_multi_chunks()
1307         self.test_build_sctp()
1308
1309     def test_to_string(self):
1310         sctp_values = {'src_port': self.src_port,
1311                        'dst_port': self.dst_port,
1312                        'vtag': self.vtag,
1313                        'csum': self.csum,
1314                        'chunks': self.chunks}
1315         _sctp_str = ','.join(['%s=%s' % (k, sctp_values[k])
1316                               for k, _ in inspect.getmembers(self.sc)
1317                               if k in sctp_values])
1318         sctp_str = '%s(%s)' % (sctp.sctp.__name__, _sctp_str)
1319
1320         eq_(str(self.sc), sctp_str)
1321         eq_(repr(self.sc), sctp_str)
1322
1323     def test_to_string_with_data(self):
1324         self.setUp_with_data()
1325         self.test_to_string()
1326
1327     def test_to_string_with_init(self):
1328         self.setUp_with_init()
1329         self.test_to_string()
1330
1331     def test_to_string_with_init_ack(self):
1332         self.setUp_with_init_ack()
1333         self.test_to_string()
1334
1335     def test_to_string_with_sack(self):
1336         self.setUp_with_sack()
1337         self.test_to_string()
1338
1339     def test_to_string_with_heartbeat(self):
1340         self.setUp_with_heartbeat()
1341         self.test_to_string()
1342
1343     def test_to_string_with_heartbeat_ack(self):
1344         self.setUp_with_heartbeat_ack()
1345         self.test_to_string()
1346
1347     def test_to_string_with_abort(self):
1348         self.setUp_with_abort()
1349         self.test_to_string()
1350
1351     def test_to_string_with_shutdown(self):
1352         self.setUp_with_shutdown()
1353         self.test_to_string()
1354
1355     def test_to_string_with_shutdown_ack(self):
1356         self.setUp_with_shutdown_ack()
1357         self.test_to_string()
1358
1359     def test_to_string_with_error(self):
1360         self.setUp_with_error()
1361         self.test_to_string()
1362
1363     def test_to_string_with_cookie_echo(self):
1364         self.setUp_with_cookie_echo()
1365         self.test_to_string()
1366
1367     def test_to_string_with_cookie_ack(self):
1368         self.setUp_with_cookie_ack()
1369         self.test_to_string()
1370
1371     def test_to_string_with_ecn_echo(self):
1372         self.setUp_with_ecn_echo()
1373         self.test_to_string()
1374
1375     def test_to_string_with_cwr(self):
1376         self.setUp_with_cwr()
1377         self.test_to_string()
1378
1379     def test_to_string_with_shutdown_complete(self):
1380         self.setUp_with_shutdown_complete()
1381         self.test_to_string()
1382
1383     def test_to_string_with_multi_chunks(self):
1384         self.setUp_with_multi_chunks()
1385         self.test_to_string()
1386
1387     def test_json(self):
1388         jsondict = self.sc.to_jsondict()
1389         sc = sctp.sctp.from_jsondict(jsondict['sctp'])
1390         eq_(str(self.sc), str(sc))
1391
1392     def test_json_with_data(self):
1393         self.setUp_with_data()
1394         self.test_json()
1395
1396     def test_json_with_init(self):
1397         self.setUp_with_init()
1398         self.test_json()
1399
1400     def test_json_with_init_ack(self):
1401         self.setUp_with_init_ack()
1402         self.test_json()
1403
1404     def test_json_with_sack(self):
1405         self.setUp_with_sack()
1406         self.test_json()
1407
1408     def test_json_with_heartbeat(self):
1409         self.setUp_with_heartbeat()
1410         self.test_json()
1411
1412     def test_json_with_heartbeat_ack(self):
1413         self.setUp_with_heartbeat_ack()
1414         self.test_json()
1415
1416     def test_json_with_abort(self):
1417         self.setUp_with_abort()
1418         self.test_json()
1419
1420     def test_json_with_shutdown(self):
1421         self.setUp_with_shutdown()
1422         self.test_json()
1423
1424     def test_json_with_shutdown_ack(self):
1425         self.setUp_with_shutdown_ack()
1426         self.test_json()
1427
1428     def test_json_with_error(self):
1429         self.setUp_with_error()
1430         self.test_json()
1431
1432     def test_json_with_cookie_echo(self):
1433         self.setUp_with_cookie_echo()
1434         self.test_json()
1435
1436     def test_json_with_cookie_ack(self):
1437         self.setUp_with_cookie_ack()
1438         self.test_json()
1439
1440     def test_json_with_ecn_echo(self):
1441         self.setUp_with_ecn_echo()
1442         self.test_json()
1443
1444     def test_json_with_cwr(self):
1445         self.setUp_with_cwr()
1446         self.test_json()
1447
1448     def test_json_with_shutdown_complete(self):
1449         self.setUp_with_shutdown_complete()
1450         self.test_json()
1451
1452     def test_json_with_multi_chunks(self):
1453         self.setUp_with_multi_chunks()
1454         self.test_json()