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_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_00_SDEntry_Service(self): p = sd.SDEntry_Service() # packet length self.assertTrue( len(binascii.hexlify(str(p))) / 2 == sd._SDEntry.OVERALL_LEN) # fields' setting p.type = sd._SDEntry.TYPE_SRV_OFFERSERVICE p.index_1 = 0x11 p.index_2 = 0x22 p.srv_id = 0x3333 p.inst_id = 0x4444 p.major_ver = 0x55 p.ttl = 0x666666 p.minor_ver = 0xdeadbeef p_str = binascii.hexlify(str(p)) bin_str = "011122003333444455666666deadbeef" self.assertTrue(p_str == bin_str) # fields' setting : N_OPT # value above 4 bits, serialized packet should feature 0x1 and 0x2 del (p) p = sd.SDEntry_Service() p.n_opt_1 = 0xf1 p.n_opt_2 = 0xf2 p_str = binascii.hexlify(str(p)) bin_str = "00" * 3 + "12" + "00" * 12 self.assertTrue(p_str == bin_str) self.assertTrue(len(p_str) / 2 == sd._SDEntry.OVERALL_LEN) # Payload guess p_entry = sd._SDEntry() p_entry_srv = sd.SDEntry_Service() self.assertTrue( p_entry.guess_payload_class(str(p_entry_srv)) == sd.SDEntry_Service)
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)