def test_0c_SD(self): p = sd.SD() # length of package without entries nor options self.assertTrue(len(binascii.hexlify(str(p))) / 2 == 12) # some Entries to array and size check p.setEntryArray([sd.SDEntry_Service(), sd.SDEntry_EventGroup()]) self.assertTrue(struct.unpack("!L", str(p)[4:8])[0] == 32) p.setEntryArray([]) self.assertTrue(struct.unpack("!L", str(p)[4:8])[0] == 0) # some Options to array and size check p.setOptionArray( [sd.SDOption_IP4_EndPoint(), sd.SDOption_IP4_EndPoint()]) self.assertTrue(struct.unpack("!L", str(p)[8:12])[0] == 24) p.setOptionArray([]) self.assertTrue(struct.unpack("!L", str(p)[8:12])[0] == 0) # some Entries&Options to array and size check p.setEntryArray([sd.SDEntry_Service(), sd.SDEntry_EventGroup()]) p.setOptionArray( [sd.SDOption_IP4_EndPoint(), sd.SDOption_IP4_EndPoint()]) self.assertTrue(struct.unpack("!L", str(p)[4:8])[0] == 32) self.assertTrue(struct.unpack("!L", str(p)[40:44])[0] == 24)
def test_0c_SD(): p = sd.SD() # length of package without entries nor options assert (len(binascii.hexlify(bytes(p))) / 2 == 12) # some Entries to array and size check p.setEntryArray([sd.SDEntry_Service(), sd.SDEntry_EventGroup()]) assert (struct.unpack("!L", bytes(p)[4:8])[0] == 32) # make sure individual entry added as list p.setEntryArray(sd.SDEntry_Service()) assert (isinstance(p.entry_array, list)) assert (len(p.entry_array) == 1) # empty entry array p.setEntryArray([]) assert (struct.unpack("!L", bytes(p)[4:8])[0] == 0) # some Options to array and size check p.setOptionArray([sd.SDOption_IP4_EndPoint(), sd.SDOption_IP4_EndPoint()]) assert (struct.unpack("!L", bytes(p)[8:12])[0] == 24) # make sure individual option added as list p.setOptionArray(sd.SDOption_IP4_EndPoint()) assert (isinstance(p.option_array, list)) assert (len(p.option_array) == 1) # empty option array p.setOptionArray([]) assert (struct.unpack("!L", bytes(p)[8:12])[0] == 0) # some Entries&Options to array and size check p.setEntryArray([sd.SDEntry_Service(), sd.SDEntry_EventGroup()]) p.setOptionArray([sd.SDOption_IP4_EndPoint(), sd.SDOption_IP4_EndPoint()]) assert (struct.unpack("!L", bytes(p)[4:8])[0] == 32) assert (struct.unpack("!L", bytes(p)[40:44])[0] == 24)
def test_00(self): """ SOME/IP-SD Subscribe eventgroup """ # build SOME/IP-SD packet sdp = sd.SD() sdp.flags = 0x00 sdp.entry_array = [ sd.SDEntry_EventGroup(srv_id=0x1111,n_opt_1=1,inst_id=0x2222,major_ver=0x03,eventgroup_id=0x04,cnt=0x0,ttl=0x05)] sdp.option_array = [ sd.SDOption_IP4_EndPoint(addr="192.168.0.1",l4_proto=0x11,port=0xd903)] # build request and reply packages p = Ether()/IP(src=ETH_IFACE_A.ip,dst=ETH_IFACE_B.ip)/UDP(sport=ETH_IFACE_A.port,dport=ETH_IFACE_B.port)/sdp.getSomeip(True) r = Ether()/IP(src=ETH_IFACE_B.ip,dst=ETH_IFACE_A.ip)/UDP(sport=ETH_IFACE_B.port,dport=ETH_IFACE_A.port)/sdp.getSomeip(True) r['SD'].entry_array[0].type = sd.SDEntry_EventGroup.TYPE_EVTGRP_SUBSCRIBE_ACK # 'dummy-ly' use a couple of threads to emulate traffic t_send = threading.Thread(name='sender',target=self._test_00_sender,args=(p,)) t_rcv = threading.Thread(name='receiver',target=self._test_00_rcver,args=(2,r,)) t_send.start() t_rcv.start() t_send.join() t_rcv.join()
def test_01(self): """ SOME/IP-SD : Example for a serialization protocol, 6.7.3.7 Example of SOME/IP-SD PDU.""" # build SOME/IP-SD packet sdp = sd.SD() sdp.flags = 0x80 sdp.entry_array = [ sd.SDEntry_Service(type=sd.SDEntry_Service.TYPE_SRV_FINDSERVICE, srv_id=0x4711, inst_id=0xffff, major_ver=0xff, ttl=3600, minor_ver=0xffffffff), sd.SDEntry_Service(type=sd.SDEntry_Service.TYPE_SRV_OFFERSERVICE, n_opt_1=1, srv_id=0x1234, inst_id=0x0001, major_ver=0x01, ttl=3, minor_ver=0x00000032) ] sdp.option_array = [ sd.SDOption_IP4_EndPoint(addr="192.168.0.1", l4_proto=0x11, port=0xd903) ] # SEND MESSAGE sip = Ether() / IP(src=ETH_IFACE_B.ip, dst=ETH_IFACE_A.ip) / UDP( sport=ETH_IFACE_B.port, dport=ETH_IFACE_A.port) / sdp.getSomeip(stacked=True) sendp(sip, iface=ETH_IFACE_B.name)
def test_04_SDOption_IP4_EndPoint(): p = sd.SDOption_IP4_EndPoint() # pkg type & length assert (p.type == sd._SDOption.IP4_ENDPOINT_TYPE) assert (p.len == sd._SDOption.IP4_ENDPOINT_LEN) # Payload guess p_option = sd._SDOption() assert (p_option.guess_payload_class(bytes(p)) == sd.SDOption_IP4_EndPoint)
def test_04_SDOption_IP4_EndPoint(self): p = sd.SDOption_IP4_EndPoint() # pkg type & length self.assertTrue(p.type == sd._SDOption.IP4_ENDPOINT_TYPE) self.assertTrue(p.len == sd._SDOption.IP4_ENDPOINT_LEN) # Payload guess p_option = sd._SDOption() self.assertTrue( p_option.guess_payload_class(str(p)) == sd.SDOption_IP4_EndPoint)
def test_00_SOMEIPSD(self): p_sd = sd.SD() p_someip = p_sd.getSomeip() # check SOME/IP-SD defaults self.assertTrue(binascii.hexlify(str(p_someip.msg_id)) == "ffff8100") self.assertTrue(p_someip.msg_type == someip.SOMEIP.TYPE_NOTIFICATION) # length of SOME/IP-SD without entries nor options p = p_someip / p_sd self.assertTrue(struct.unpack("!L", str(p)[4:8])[0] == 20) # check SOME/IP-SD lengths (note : lengths calculated on package construction) del (p) p_sd.setEntryArray([sd.SDEntry_Service()]) p_sd.setOptionArray([sd.SDOption_IP4_EndPoint()]) p = p_someip / p_sd self.assertTrue(struct.unpack("!L", str(p)[4:8])[0] == 48)