• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# MIT License
2
3# Copyright (c) 2018 Jose Amores
4
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22
23# This file is part of Scapy
24# See http://www.secdev.org/projects/scapy for more information
25# Copyright (C) Sebastian Baar <sebastian.baar@gmx.de>
26# This program is published under a GPLv2 license
27
28##########
29##########
30
31
32+ Basic operations
33
34= Load module
35load_contrib("automotive.someip", globals_dict=globals())
36
37+ SOME/IP operation
38
39= Basic build
40p = SOMEIP()
41pstr = bytes(p)
42binstr = b"\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x01\x01\x00\x00"
43assert pstr == binstr
44
45= Build with empty payload
46p.payload = Raw(b"")
47pstr = bytes(p)
48binstr = b"\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x01\x01\x00\x00"
49assert pstr == binstr
50
51= Build with non-empty payload
52p.payload = Raw(b"\xde\xad\xbe\xef")
53pstr = bytes(p)
54binstr = b"\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x01\x01\x00\x00\xde\xad\xbe\xef"
55assert pstr == binstr
56
57= Dissect EVENT_ID packet
58p = SOMEIP(b"\x11\x11\x81\x11\x00\x00\x00\x08\x33\x33\x44\x44\x02\x03\x04\x05")
59
60assert p.srv_id == 0x1111
61assert p.sub_id == 0x8111
62assert p.client_id == 0x3333
63assert p.session_id == 0x4444
64assert p.proto_ver == 0x02
65assert p.iface_ver == 0x03
66assert p.msg_type == 0x04
67assert p.retcode == 0x05
68
69
70= Dissect METHOD_ID packet
71p = SOMEIP(b"\x11\x11\x01\x11\x00\x00\x00\x08\x33\x33\x44\x44\x02\x03\x04\x05")
72
73assert p.srv_id == 0x1111
74assert p.sub_id == 0x0111
75assert p.client_id == 0x3333
76assert p.session_id == 0x4444
77assert p.proto_ver == 0x02
78assert p.iface_ver == 0x03
79assert p.msg_type == 0x04
80assert p.retcode == 0x05
81
82+ SOME/IP-TP operation
83
84= Build TP
85p = SOMEIP()
86p.msg_type = 0x20
87
88pstr = bytes(p)
89print(pstr)
90binstr = b'\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x01\x01\x20\x00\x00\x00\x00\x00'
91assert pstr == binstr
92
93p.more_seg = 1
94pstr = bytes(p)
95binstr = b'\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x01\x01\x20\x00\x00\x00\x00\x01'
96assert pstr == binstr
97
98p.msg_type = 0x00
99pstr = bytes(p)
100binstr = b'\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x01\x01\x00\x00'
101assert pstr == binstr
102
103= Dissect TP
104p = SOMEIP(b'\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x01\x01\x21\x00\x00\x00\x00\x01')
105
106assert p.msg_type == 0x21
107assert p.more_seg == 1
108assert p.len == 12
109
110p.msg_type = 0x00
111
112pstr = bytes(p)
113binstr = b"\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x01\x01\x00\x00"
114assert pstr == binstr
115
116= Build TP fragmented
117p = SOMEIP()
118p.msg_type = 0x20
119p.add_payload(Raw("A"*1400))
120
121f = p.fragment()
122
123assert f[0].len == 1404
124assert f[1].len == 20
125assert f[0].payload == Raw("A"*1392)
126assert f[1].payload == Raw("A"*8)
127assert f[0].more_seg == 1
128assert f[1].more_seg == 0
129
130+ SD Entry Service
131
132= Check packet length on empty build
133p = SDEntry_Service()
134assert len(bytes(p)) == SDENTRY_OVERALL_LEN
135
136= Build 1
137p = SDEntry_Service(type = SDENTRY_TYPE_SRV_OFFERSERVICE,
138				    index_1 = 0x11, index_2 = 0x22, srv_id = 0x3333,
139					inst_id = 0x4444, major_ver = 0x55,
140					ttl = 0x666666, minor_ver = 0xdeadbeef)
141p_str = bytes(p)
142bin_str = b"\x01\x11\x22\x00\x33\x33\x44\x44\x55\x66\x66\x66\xde\xad\xbe\xef"
143assert p_str == bin_str
144assert len(p_str) == SDENTRY_OVERALL_LEN
145
146= Build 2
147p = SDEntry_Service(n_opt_1 = 0xf1, n_opt_2 = 0xf2)
148p_str = bytes(p)
149bin_str = b"\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
150assert p_str == bin_str
151assert len(p_str) == SDENTRY_OVERALL_LEN
152
153= Dissect
154p = SDEntry_Service(
155    b"\x01\x22\x33\x00\x44\x44\x55\x55\x66\x77\x77\x77\xde\xad\xbe\xef")
156assert p.type == SDENTRY_TYPE_SRV_OFFERSERVICE
157assert p.index_1 == 0x22
158assert p.index_2 == 0x33
159assert p.srv_id == 0x4444
160assert p.inst_id == 0x5555
161assert p.major_ver == 0x66
162assert p.ttl == 0x777777
163assert p.minor_ver == 0xdeadbeef
164
165+ SD Entry Eventgroup
166
167= Check packet length on empty build
168p = SDEntry_EventGroup()
169assert len(bytes(p)) == SDENTRY_OVERALL_LEN
170
171= Build
172p = SDEntry_EventGroup(index_1 = 0x11, index_2 = 0x22, srv_id = 0x3333,
173			           inst_id = 0x4444, major_ver = 0x55, ttl = 0x666666,
174					   cnt = 0x7, eventgroup_id = 0x8888)
175p_str = bytes(p)
176bin_str = b"\x06\x11\x22\x00\x33\x33\x44\x44\x55\x66\x66\x66\x00\x07\x88\x88"
177assert p_str == bin_str
178assert len(bytes(p)) == SDENTRY_OVERALL_LEN
179
180= Dissect
181p = SDEntry_EventGroup(
182    b"\x06\x11\x22\x00\x33\x33\x44\x44\x55\x66\x66\x66\x00\x07\x88\x88")
183assert p.index_1 == 0x11
184assert p.index_2 == 0x22
185assert p.srv_id == 0x3333
186assert p.inst_id == 0x4444
187assert p.major_ver == 0x55
188assert p.ttl == 0x666666
189assert p.cnt == 0x7
190assert p.eventgroup_id == 0x8888
191
192+ SD Flags
193
194= Build and check flags
195p = SD()
196p.flags = "REBOOT"
197assert p.flags == 0x80
198
199p.flags = ""
200assert p.flags == 0x00
201
202p.flags = "UNICAST"
203assert p.flags == 0x40
204
205p.flags = ""
206assert p.flags == 0x00
207
208p.flags = "EXPLICIT_INITIAL_DATA_CONTROL"
209assert p.flags == 0x20
210
211p.flags = ""
212assert p.flags == 0x00
213
214p.flags = "REBOOT+UNICAST+EXPLICIT_INITIAL_DATA_CONTROL"
215assert p.flags == 0xe0
216
217+ SD Get SOME/IP Packet
218
219= Build empty
220p = SOMEIP() / SD()
221assert len(bytes(p)) == SOMEIP._OVERALL_LEN_NOPAYLOAD + 12
222
223= Verify constants against spec TR_SOMEIP_00250
224assert SD.SOMEIP_MSGID_SRVID == 0xffff
225assert SD.SOMEIP_MSGID_SUBID == 0x8100
226assert SD.SOMEIP_CLIENT_ID == 0x0000
227assert SD.SOMEIP_MINIMUM_SESSION_ID == 0x0001
228assert SD.SOMEIP_PROTO_VER == 0x01
229assert SD.SOMEIP_IFACE_VER == 0x01
230assert SD.SOMEIP_MSG_TYPE == 0x02
231assert SD.SOMEIP_RETCODE == 0x00
232
233= check that values are bound
234assert p[SOMEIP].srv_id == SD.SOMEIP_MSGID_SRVID
235assert p[SOMEIP].sub_id == SD.SOMEIP_MSGID_SUBID
236assert p[SOMEIP].client_id == SD.SOMEIP_CLIENT_ID
237assert p[SOMEIP].session_id != 0x0000
238assert p[SOMEIP].session_id >= SD.SOMEIP_MINIMUM_SESSION_ID
239assert p[SOMEIP].proto_ver == SD.SOMEIP_PROTO_VER
240assert p[SOMEIP].iface_ver == SD.SOMEIP_IFACE_VER
241assert p[SOMEIP].msg_type == SD.SOMEIP_MSG_TYPE
242assert p[SOMEIP].retcode == SD.SOMEIP_RETCODE
243
244# FIXME: Service Discovery messages shell be transported over UDP
245# (TR_SOMEIP_00248)
246# FIXME: The port 30490 (UDP and TCP as well) shall be only used for SOME/IP-SD
247# and not used for applications communicating over SOME/IP (TR_SOMEIP_00020)
248
249+ SD
250
251= Check length of package without entries nor options
252p = SD()
253assert len(bytes(p)) == 12
254
255= Check entries to array and size check
256p.set_entryArray([SDEntry_Service(), SDEntry_EventGroup()])
257assert struct.unpack("!L", bytes(p)[4:8])[0] == 32
258
259p.set_entryArray([])
260assert struct.unpack("!L", bytes(p)[4:8])[0] == 0
261
262= Check Options to array and size check
263p.set_optionArray([SDOption_IP4_EndPoint(), SDOption_IP4_EndPoint()])
264assert struct.unpack("!L", bytes(p)[8:12])[0] == 24
265
266p.set_optionArray([])
267assert struct.unpack("!L", bytes(p)[8:12])[0] == 0
268
269= Check Entries & Options to array and size check
270p.set_entryArray([SDEntry_Service(), SDEntry_EventGroup()])
271p.set_optionArray([SDOption_IP4_EndPoint(), SDOption_IP4_EndPoint()])
272
273assert struct.unpack("!L", bytes(p)[4:8])[0] == 32
274assert struct.unpack("!L", bytes(p)[40:44])[0] == 24
275
276
277+ Git issue 2348: SOME/IP-SD Entry-Array is broken by building it from RAW
278
279= Single SD entry
280# offer service
281ea1 = SDEntry_Service()
282ea1.type = 1
283ea1.srv_id = 0x1234
284ea1.inst_id = 0x5678
285ea1.ttl = 0x333333
286
287# subscribe eventgroup
288ea2 = SDEntry_EventGroup()
289ea2.type = 0x6
290ea2.srv_id = 0x8765
291ea2.inst_id = 0x4321
292ea2.ttl = 0x222222
293ea2.eventgroup_id = 0x1357
294
295sd1 = SD()
296sd1.set_entryArray([ea1])
297# this is computed on build, but we need it sooner for the assert
298sd1.len_entry_array = 16
299sd1.len_option_array = 0
300
301assert sd1.show(dump=True) == SD(sd1.build()).show(dump=True)
302
303= Double SD entry
304sd2 = SD()
305sd2.set_entryArray([ea2,ea1])
306# this is computed on build, but we need it sooner for the assert
307sd2.len_entry_array = 32
308sd2.len_option_array = 0
309
310assert sd2.show(dump=True) == SD(sd2.build()).show(dump=True)
311
312= Flipped double SD entry
313# flip the order
314sd2.set_entryArray([ea1,ea2])
315assert sd2.show(dump=True) == SD(sd2.build()).show(dump=True)
316
317
318
319
320
321+ SD Options (individual)
322= Verifying constants against spec
323assert SDOPTION_CFG_TYPE == 0x01
324assert SDOPTION_LOADBALANCE_TYPE == 0x02
325assert SDOPTION_LOADBALANCE_LEN == 0x05
326assert SDOPTION_IP4_ENDPOINT_TYPE == 0x04
327assert SDOPTION_IP4_ENDPOINT_LEN == 0x0009
328assert SDOPTION_IP4_MCAST_TYPE == 0x14
329assert SDOPTION_IP4_MCAST_LEN == 0x0009
330assert SDOPTION_IP4_SDENDPOINT_TYPE == 0x24
331assert SDOPTION_IP4_SDENDPOINT_LEN == 0x0009
332assert SDOPTION_IP6_ENDPOINT_TYPE == 0x06
333assert SDOPTION_IP6_ENDPOINT_LEN == 0x0015
334assert SDOPTION_IP6_MCAST_TYPE == 0x16
335assert SDOPTION_IP6_MCAST_LEN == 0x0015
336assert SDOPTION_IP6_SDENDPOINT_TYPE == 0x26
337assert SDOPTION_IP6_SDENDPOINT_LEN == 0x0015
338
339### SDOption_Config
340= SDOption_Config: Verify make_string() method from dict
341data = { "hello": "world" }
342out = SDOption_Config.make_string(data)
343assert out == b"\x0bhello=world\x00"
344
345= SDOption_Config: Verify make_string() method from list
346data = [
347	("x", "y"),
348	("abc", "def"),
349	("123", "456")
350]
351out = SDOption_Config.make_string(data)
352assert out == b"\x03x=y\x07abc=def\x07123=456\x00"
353
354= SDOption_Config: Build and dissect empty
355opt = SDOption_Config()
356optraw = opt.build()
357assert optraw == b"\x00\x02\x01\x00\x00"
358
359opt = SDOption_Config(optraw)
360assert opt.len == 0x2
361assert opt.type == SDOPTION_CFG_TYPE
362assert opt.res_hdr == 0x0
363assert opt.cfg_str == b"\x00"
364
365= SDOption_Config: Build and dissect spec example
366tststr = b"\x05abc=x\x07def=123\x00"
367opt = SDOption_Config(cfg_str=tststr)
368optraw = opt.build()
369assert optraw == b"\x00\x10\x01\x00" + tststr
370
371opt = SDOption_Config(optraw)
372assert opt.len == 0x10
373assert opt.type == SDOPTION_CFG_TYPE
374assert opt.res_hdr == 0x00
375assert opt.cfg_str == tststr
376
377= SDOption_Config: Build and dissect fully populated
378tststr = b"abcdefghijklmnopqrstuvwxyz"
379opt = SDOption_Config(len=0x1234, type=0x56, res_hdr=0x78, cfg_str=tststr)
380optraw = opt.build()
381assert optraw == b"\x12\x34\x56\x78" + tststr
382
383opt = SDOption_Config(optraw)
384assert opt.len == 0x1234
385assert opt.type == 0x56
386assert opt.res_hdr == 0x78
387assert opt.cfg_str == tststr
388
389
390### SDOption_LoadBalance
391= SDOption_LoadBalance: Build and dissect empty
392opt = SDOption_LoadBalance()
393optraw = opt.build()
394assert optraw == b"\x00\x05\x02\x00\x00\x00\x00\x00"
395
396opt = SDOption_LoadBalance(optraw)
397assert opt.len == SDOPTION_LOADBALANCE_LEN
398assert opt.type == SDOPTION_LOADBALANCE_TYPE
399assert opt.res_hdr == 0x0
400assert opt.priority == 0x0
401assert opt.weight == 0x0
402
403= SDOption_LoadBalance: Build and dissect example
404opt = SDOption_LoadBalance(priority=0x1234, weight=0x5678)
405optraw = opt.build()
406assert optraw == b"\x00\x05\x02\x00\x12\x34\x56\x78"
407
408opt = SDOption_LoadBalance(optraw)
409assert opt.len == SDOPTION_LOADBALANCE_LEN
410assert opt.type == SDOPTION_LOADBALANCE_TYPE
411assert opt.res_hdr == 0x00
412assert opt.priority == 0x1234
413assert opt.weight == 0x5678
414
415= SDOption_LoadBalance: Build and dissect fully populated
416opt = SDOption_LoadBalance(len=0x1234, type=0x56, res_hdr=0x78, priority=0x9abc, weight=0xdef0)
417optraw = opt.build()
418assert optraw == b"\x12\x34\x56\x78\x9a\xbc\xde\xf0"
419
420opt = SDOption_LoadBalance(optraw)
421assert opt.len == 0x1234
422assert opt.type == 0x56
423assert opt.res_hdr == 0x78
424assert opt.priority == 0x9abc
425assert opt.weight == 0xdef0
426
427
428### SDOption_IP4_EndPoint
429= SDOption_IP4_EndPoint: Build and dissect empty
430opt = SDOption_IP4_EndPoint()
431optraw = opt.build()
432assert optraw == b"\x00\x09\x04\x00\x00\x00\x00\x00\x00\x11\x00\x00"
433
434opt = SDOption_IP4_EndPoint(optraw)
435assert opt.len == SDOPTION_IP4_ENDPOINT_LEN
436assert opt.type == SDOPTION_IP4_ENDPOINT_TYPE
437assert opt.res_hdr == 0x0
438assert opt.addr == "0.0.0.0"
439assert opt.res_tail == 0x0
440assert opt.l4_proto == 0x11
441assert opt.port == 0x0
442
443
444= SDOption_IP4_EndPoint: Build and dissect example
445opt = SDOption_IP4_EndPoint(addr = "192.168.123.45", l4_proto = "TCP", port = 0x1234)
446optraw = opt.build()
447assert optraw == b"\x00\x09\x04\x00\xc0\xa8\x7b\x2d\x00\x06\x12\x34"
448
449opt = SDOption_IP4_EndPoint(optraw)
450assert opt.len == SDOPTION_IP4_ENDPOINT_LEN
451assert opt.type == SDOPTION_IP4_ENDPOINT_TYPE
452assert opt.res_hdr == 0x00
453assert opt.addr == "192.168.123.45"
454assert opt.res_tail == 0x0
455assert opt.l4_proto == 0x06
456assert opt.port == 0x1234
457
458= SDOption_IP4_EndPoint: Build and dissect fully populated
459opt = SDOption_IP4_EndPoint(len=0x1234, type=0x56, res_hdr=0x78, addr = "11.22.33.44", res_tail = 0x9a, l4_proto = 0xbc, port = 0xdef0)
460optraw = opt.build()
461assert optraw == b"\x12\x34\x56\x78\x0b\x16\x21\x2c\x9a\xbc\xde\xf0"
462
463opt = SDOption_IP4_EndPoint(optraw)
464assert opt.len == 0x1234
465assert opt.type == 0x56
466assert opt.res_hdr == 0x78
467assert opt.addr == "11.22.33.44"
468assert opt.res_tail == 0x9a
469assert opt.l4_proto == 0xbc
470assert opt.port == 0xdef0
471
472
473### SDOption_IP4_Multicast
474= SDOption_IP4_Multicast: Build and dissect empty
475opt = SDOption_IP4_Multicast()
476optraw = opt.build()
477assert optraw == b"\x00\x09\x14\x00\x00\x00\x00\x00\x00\x11\x00\x00"
478
479opt = SDOption_IP4_Multicast(optraw)
480assert opt.len == SDOPTION_IP4_MCAST_LEN
481assert opt.type == SDOPTION_IP4_MCAST_TYPE
482assert opt.res_hdr == 0x0
483assert opt.addr == "0.0.0.0"
484assert opt.res_tail == 0x0
485assert opt.l4_proto == 0x11
486assert opt.port == 0x0
487
488
489= SDOption_IP4_Multicast: Build and dissect example
490opt = SDOption_IP4_Multicast(addr = "192.168.123.45", l4_proto = "TCP", port = 0x1234)
491optraw = opt.build()
492assert optraw == b"\x00\x09\x14\x00\xc0\xa8\x7b\x2d\x00\x06\x12\x34"
493
494opt = SDOption_IP4_Multicast(optraw)
495assert opt.len == SDOPTION_IP4_MCAST_LEN
496assert opt.type == SDOPTION_IP4_MCAST_TYPE
497assert opt.res_hdr == 0x00
498assert opt.addr == "192.168.123.45"
499assert opt.res_tail == 0x0
500assert opt.l4_proto == 0x06
501assert opt.port == 0x1234
502
503= SDOption_IP4_Multicast: Build and dissect fully populated
504opt = SDOption_IP4_Multicast(len=0x1234, type=0x56, res_hdr=0x78, addr = "11.22.33.44", res_tail = 0x9a, l4_proto = 0xbc, port = 0xdef0)
505optraw = opt.build()
506assert optraw == b"\x12\x34\x56\x78\x0b\x16\x21\x2c\x9a\xbc\xde\xf0"
507
508opt = SDOption_IP4_Multicast(optraw)
509assert opt.len == 0x1234
510assert opt.type == 0x56
511assert opt.res_hdr == 0x78
512assert opt.addr == "11.22.33.44"
513assert opt.res_tail == 0x9a
514assert opt.l4_proto == 0xbc
515assert opt.port == 0xdef0
516
517
518### SDOption_IP4_SD_EndPoint
519= SDOption_IP4_SD_EndPoint: Build and dissect empty
520opt = SDOption_IP4_SD_EndPoint()
521optraw = opt.build()
522assert optraw == b"\x00\x09\x24\x00\x00\x00\x00\x00\x00\x11\x00\x00"
523
524opt = SDOption_IP4_SD_EndPoint(optraw)
525assert opt.len == SDOPTION_IP4_SDENDPOINT_LEN
526assert opt.type == SDOPTION_IP4_SDENDPOINT_TYPE
527assert opt.res_hdr == 0x0
528assert opt.addr == "0.0.0.0"
529assert opt.res_tail == 0x0
530assert opt.l4_proto == 0x11
531assert opt.port == 0x0
532
533
534= SDOption_IP4_SD_EndPoint: Build and dissect example
535opt = SDOption_IP4_SD_EndPoint(addr = "192.168.123.45", l4_proto = "TCP", port = 0x1234)
536optraw = opt.build()
537assert optraw == b"\x00\x09\x24\x00\xc0\xa8\x7b\x2d\x00\x06\x12\x34"
538
539opt = SDOption_IP4_SD_EndPoint(optraw)
540assert opt.len == SDOPTION_IP4_SDENDPOINT_LEN
541assert opt.type == SDOPTION_IP4_SDENDPOINT_TYPE
542assert opt.res_hdr == 0x00
543assert opt.addr == "192.168.123.45"
544assert opt.res_tail == 0x0
545assert opt.l4_proto == 0x06
546assert opt.port == 0x1234
547
548= SDOption_IP4_SD_EndPoint: Build and dissect fully populated
549opt = SDOption_IP4_SD_EndPoint(len=0x1234, type=0x56, res_hdr=0x78, addr = "11.22.33.44", res_tail = 0x9a, l4_proto = 0xbc, port = 0xdef0)
550optraw = opt.build()
551assert optraw == b"\x12\x34\x56\x78\x0b\x16\x21\x2c\x9a\xbc\xde\xf0"
552
553opt = SDOption_IP4_SD_EndPoint(optraw)
554assert opt.len == 0x1234
555assert opt.type == 0x56
556assert opt.res_hdr == 0x78
557assert opt.addr == "11.22.33.44"
558assert opt.res_tail == 0x9a
559assert opt.l4_proto == 0xbc
560assert opt.port == 0xdef0
561
562### SDOption_IP6_EndPoint
563= SDOption_IP6_EndPoint: Build and dissect empty
564opt = SDOption_IP6_EndPoint()
565optraw = opt.build()
566assert optraw == b"\x00\x15\x06\x00" + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + b"\x00\x11\x00\x00"
567
568opt = SDOption_IP6_EndPoint(optraw)
569assert opt.len == SDOPTION_IP6_ENDPOINT_LEN
570assert opt.type == SDOPTION_IP6_ENDPOINT_TYPE
571assert opt.res_hdr == 0x0
572assert opt.addr == "::"
573assert opt.res_tail == 0x0
574assert opt.l4_proto == 0x11
575assert opt.port == 0x0
576
577
578= SDOption_IP6_EndPoint: Build and dissect example
579opt = SDOption_IP6_EndPoint(addr = "2001:cdba::3257:9652", l4_proto = "TCP", port = 0x1234)
580optraw = opt.build()
581assert optraw == b"\x00\x15\x06\x00" + b"\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52" + b"\x00\x06\x12\x34"
582
583opt = SDOption_IP6_EndPoint(optraw)
584assert opt.len == SDOPTION_IP6_ENDPOINT_LEN
585assert opt.type == SDOPTION_IP6_ENDPOINT_TYPE
586assert opt.res_hdr == 0x00
587assert opt.addr == "2001:cdba::3257:9652"
588assert opt.res_tail == 0x0
589assert opt.l4_proto == 0x06
590assert opt.port == 0x1234
591
592= SDOption_IP6_EndPoint: Build and dissect fully populated
593opt = SDOption_IP6_EndPoint(len=0x1234, type=0x56, res_hdr=0x78, addr = "1234:5678:9abc:def0:0fed:cba9:8765:4321", res_tail = 0x9a, l4_proto = 0xbc, port = 0xdef0)
594optraw = opt.build()
595assert optraw == b"\x12\x34\x56\x78" + b"\x12\x34\x56\x78\x9a\xbc\xde\xf0\x0f\xed\xcb\xa9\x87\x65\x43\x21" + b"\x9a\xbc\xde\xf0"
596
597opt = SDOption_IP6_EndPoint(optraw)
598assert opt.len == 0x1234
599assert opt.type == 0x56
600assert opt.res_hdr == 0x78
601assert opt.addr == "1234:5678:9abc:def0:fed:cba9:8765:4321"
602assert opt.res_tail == 0x9a
603assert opt.l4_proto == 0xbc
604assert opt.port == 0xdef0
605
606
607### SDOption_IP6_Multicast
608= SDOption_IP6_Multicast: Build and dissect empty
609opt = SDOption_IP6_Multicast()
610optraw = opt.build()
611assert optraw == b"\x00\x15\x16\x00" + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + b"\x00\x11\x00\x00"
612
613opt = SDOption_IP6_Multicast(optraw)
614assert opt.len == SDOPTION_IP6_MCAST_LEN
615assert opt.type == SDOPTION_IP6_MCAST_TYPE
616assert opt.res_hdr == 0x0
617assert opt.addr == "::"
618assert opt.res_tail == 0x0
619assert opt.l4_proto == 0x11
620assert opt.port == 0x0
621
622
623= SDOption_IP6_Multicast: Build and dissect example
624opt = SDOption_IP6_Multicast(addr = "2001:cdba::3257:9652", l4_proto = "TCP", port = 0x1234)
625optraw = opt.build()
626assert optraw == b"\x00\x15\x16\x00" + b"\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52" + b"\x00\x06\x12\x34"
627
628opt = SDOption_IP6_Multicast(optraw)
629assert opt.len == SDOPTION_IP6_MCAST_LEN
630assert opt.type == SDOPTION_IP6_MCAST_TYPE
631assert opt.res_hdr == 0x00
632assert opt.addr == "2001:cdba::3257:9652"
633assert opt.res_tail == 0x0
634assert opt.l4_proto == 0x06
635assert opt.port == 0x1234
636
637= SDOption_IP6_Multicast: Build and dissect fully populated
638opt = SDOption_IP6_Multicast(len=0x1234, type=0x56, res_hdr=0x78, addr = "1234:5678:9abc:def0:0fed:cba9:8765:4321", res_tail = 0x9a, l4_proto = 0xbc, port = 0xdef0)
639optraw = opt.build()
640assert optraw == b"\x12\x34\x56\x78" + b"\x12\x34\x56\x78\x9a\xbc\xde\xf0\x0f\xed\xcb\xa9\x87\x65\x43\x21" + b"\x9a\xbc\xde\xf0"
641
642opt = SDOption_IP6_Multicast(optraw)
643assert opt.len == 0x1234
644assert opt.type == 0x56
645assert opt.res_hdr == 0x78
646assert opt.addr == "1234:5678:9abc:def0:fed:cba9:8765:4321"
647assert opt.res_tail == 0x9a
648assert opt.l4_proto == 0xbc
649assert opt.port == 0xdef0
650
651
652### SDOption_IP6_SD_EndPoint
653= SDOption_IP6_SD_EndPoint: Build and dissect empty
654opt = SDOption_IP6_SD_EndPoint()
655optraw = opt.build()
656assert optraw == b"\x00\x15\x26\x00" + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + b"\x00\x11\x00\x00"
657
658opt = SDOption_IP6_SD_EndPoint(optraw)
659assert opt.len == SDOPTION_IP6_SDENDPOINT_LEN
660assert opt.type == SDOPTION_IP6_SDENDPOINT_TYPE
661assert opt.res_hdr == 0x0
662assert opt.addr == "::"
663assert opt.res_tail == 0x0
664assert opt.l4_proto == 0x11
665assert opt.port == 0x0
666
667
668= SDOption_IP6_SD_EndPoint: Build and dissect example
669opt = SDOption_IP6_SD_EndPoint(addr = "2001:cdba::3257:9652", l4_proto = "TCP", port = 0x1234)
670optraw = opt.build()
671assert optraw == b"\x00\x15\x26\x00" + b"\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52" + b"\x00\x06\x12\x34"
672
673opt = SDOption_IP6_SD_EndPoint(optraw)
674assert opt.len == SDOPTION_IP6_SDENDPOINT_LEN
675assert opt.type == SDOPTION_IP6_SDENDPOINT_TYPE
676assert opt.res_hdr == 0x00
677assert opt.addr == "2001:cdba::3257:9652"
678assert opt.res_tail == 0x0
679assert opt.l4_proto == 0x06
680assert opt.port == 0x1234
681
682= SDOption_IP6_SD_EndPoint: Build and dissect fully populated
683opt = SDOption_IP6_SD_EndPoint(len=0x1234, type=0x56, res_hdr=0x78, addr = "1234:5678:9abc:def0:0fed:cba9:8765:4321", res_tail = 0x9a, l4_proto = 0xbc, port = 0xdef0)
684optraw = opt.build()
685assert optraw == b"\x12\x34\x56\x78" + b"\x12\x34\x56\x78\x9a\xbc\xde\xf0\x0f\xed\xcb\xa9\x87\x65\x43\x21" + b"\x9a\xbc\xde\xf0"
686
687opt = SDOption_IP6_SD_EndPoint(optraw)
688assert opt.len == 0x1234
689assert opt.type == 0x56
690assert opt.res_hdr == 0x78
691assert opt.addr == "1234:5678:9abc:def0:fed:cba9:8765:4321"
692assert opt.res_tail == 0x9a
693assert opt.l4_proto == 0xbc
694assert opt.port == 0xdef0
695
696= verify building and parsing of multiple SDOptions
697def _opts_check(opts):
698	optslen = sum([len(o) for o in opts])
699	sd = SD()
700	sd.set_optionArray(opts)
701	sd.len_entry_array = 0
702	sd.len_option_array = optslen
703	sd.show()
704	SD(sd.build()).show()
705	assert sd.show(dump=True) == SD(sd.build()).show(dump=True)
706
707# options are built and reparsed, to make sure all is calculated
708opts = [
709	SDOption_Config(SDOption_Config(cfg_str="hello world").build()),
710	SDOption_LoadBalance(SDOption_LoadBalance().build()),
711	SDOption_IP4_EndPoint(SDOption_IP4_EndPoint().build()),
712	SDOption_IP4_Multicast(SDOption_IP4_Multicast().build()),
713	SDOption_IP4_SD_EndPoint(SDOption_IP4_SD_EndPoint().build()),
714	SDOption_IP6_EndPoint(SDOption_IP6_EndPoint().build()),
715	SDOption_IP6_Multicast(SDOption_IP6_Multicast().build()),
716	SDOption_IP6_SD_EndPoint(SDOption_IP6_SD_EndPoint().build()),
717]
718_opts_check(opts[0:0])
719_opts_check(opts[0:2])
720_opts_check(opts)
721_opts_check(opts[::-1])
722_opts_check(opts + opts[::-1])
723
724
725= build test SOMEIP/TP
726
727p = SOMEIP(srv_id=1234, sub_id=4321, msg_type=0xff, retcode=0xff, offset=4294967040, data=[Raw(b"deadbeef")])
728
729assert p.data[0].load == b"deadbeef"
730