def test_service_convert_offer(self): host_1 = ipaddress.ip_address("2001:db8::1234:5678:dead:beef") host_2 = ipaddress.ip_address("203.0.113.78") port = 4321 ep_1 = hdr.IPv6EndpointOption(address=host_1, l4proto=hdr.L4Protocols.TCP, port=port) ep_2 = hdr.IPv4EndpointOption(address=host_2, l4proto=hdr.L4Protocols.TCP, port=port) srv = cfg.Service( service_id=0x0000, instance_id=0xFFFF, major_version=0xAA, minor_version=0x123456, options_1=(ep_1, ), options_2=(ep_2, ), ) offer = hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.OfferService, service_id=0, instance_id=0xFFFF, major_version=0xAA, minver_or_counter=0x123456, ttl=3, options_1=(ep_1, ), options_2=(ep_2, ), ) self.assertEqual(srv.create_offer_entry(), offer) self.assertEqual(cfg.Service.from_offer_entry(offer), srv) sd_hdr = hdr.SOMEIPSDHeader(entries=(offer, )) sd_hdr_assigned = sd_hdr.assign_option_indexes() with self.assertRaises(ValueError): cfg.Service.from_offer_entry(sd_hdr_assigned.entries[0]) with self.assertRaises(ValueError): cfg.Service.from_offer_entry( hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.FindService, service_id=0, instance_id=0xFFFF, major_version=0xAA, minver_or_counter=0x123456, ttl=3, ))
def test_service_match_offer(self): host_1 = ipaddress.ip_address("2001:db8::1234:5678:dead:beef") host_2 = ipaddress.ip_address("203.0.113.78") port = 4321 ep_1 = hdr.IPv6EndpointOption(address=host_1, l4proto=hdr.L4Protocols.TCP, port=port) ep_2 = hdr.IPv6EndpointOption(address=host_2, l4proto=hdr.L4Protocols.TCP, port=port) offer = hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.OfferService, service_id=0, instance_id=0xFEEE, major_version=0xAA, minver_or_counter=0x123456, ttl=3, options_1=(ep_1, ), options_2=(ep_2, ), ) srv = cfg.Service( service_id=0x0000, instance_id=0xFEEE, major_version=0xAA, minor_version=0x123456, options_2=(ep_2, ), ) self.assertTrue(srv.matches_offer(offer)) self.assertFalse(replace(srv, service_id=1).matches_offer(offer)) self.assertFalse(replace(srv, instance_id=0xBBBB).matches_offer(offer)) self.assertTrue(replace(srv, instance_id=0xFFFF).matches_offer(offer)) self.assertFalse(replace(srv, major_version=0xBB).matches_offer(offer)) self.assertTrue(replace(srv, major_version=0xFF).matches_offer(offer)) self.assertFalse( replace(srv, minor_version=0xBBBBBB).matches_offer(offer)) self.assertTrue( replace(srv, minor_version=0xFFFFFFFF).matches_offer(offer)) # ttl=0 is StopOffer, should still match stop_offer = replace(offer, ttl=0) self.assertTrue(srv.matches_offer(stop_offer)) with self.assertRaises(ValueError): srv.matches_offer( replace(stop_offer, sd_type=hdr.SOMEIPSDEntryType.FindService))
def test_service_match_subscribe(self): host = ipaddress.ip_address("2001:db8::1234:5678:dead:beef") port = 4321 ep = hdr.IPv6EndpointOption(address=host, l4proto=hdr.L4Protocols.TCP, port=port) srv = cfg.Service( service_id=0x1111, instance_id=0x2222, major_version=0x33, minor_version=0x333333, eventgroups=(23, 24, 25), ) subscribe = hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.Subscribe, service_id=0x1111, instance_id=0x2222, major_version=0x33, minver_or_counter=(12 << 16) | 23, ttl=3, options_1=(ep, ), ) self.assertTrue(srv.matches_subscribe(subscribe)) self.assertFalse( replace(srv, service_id=1).matches_subscribe(subscribe)) self.assertFalse( replace(srv, instance_id=0xBBBB).matches_subscribe(subscribe)) self.assertTrue( replace(srv, instance_id=0xFFFF).matches_subscribe(subscribe)) self.assertFalse( replace(srv, major_version=0xBB).matches_subscribe(subscribe)) self.assertTrue( replace(srv, major_version=0xFF).matches_subscribe(subscribe)) self.assertFalse( replace(srv, eventgroups=(1, 2, 3)).matches_subscribe(subscribe)) # ttl=0 is StopSubscribe, should still match stop_subscribe = replace(subscribe, ttl=0) self.assertTrue(srv.matches_subscribe(stop_subscribe)) with self.assertRaises(ValueError): srv.matches_subscribe( replace(stop_subscribe, sd_type=hdr.SOMEIPSDEntryType.FindService))
def test_sdentry_service_stopoffer(self): payload = b"\x01\xAA\xBB\xCD\x88\x99\x66\x77\xEE\x00\x00\x00\x10\x11\x12\x13" entry = hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.OfferService, option_index_1=0xAA, option_index_2=0xBB, num_options_1=0xC, num_options_2=0xD, service_id=0x8899, instance_id=0x6677, major_version=0xEE, ttl=0, minver_or_counter=0x10111213, ) self._check(payload, entry, lambda x: hdr.SOMEIPSDEntry.parse(x, 512)) self.assertFalse(entry.options_resolved)
def test_service_convert_find(self): srv = cfg.Service( service_id=0x0000, instance_id=0xFFFF, major_version=0xAA, minor_version=0x123456, ) offer = hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.FindService, service_id=0, instance_id=0xFFFF, major_version=0xAA, minver_or_counter=0x123456, ttl=3, ) self.assertEqual(srv.create_find_entry(), offer)
def test_sdentry_service_counter(self): entry = hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.Subscribe, option_index_1=0xAA, option_index_2=0xBB, num_options_1=0xC, num_options_2=0xD, service_id=0x8899, instance_id=0x6677, major_version=0xEE, ttl=0x202122, minver_or_counter=0x091111, ) self.assertEqual(entry.eventgroup_counter, 0x9) self.assertEqual(entry.eventgroup_id, 0x1111) with self.assertRaises(TypeError): entry.service_minor_version
def test_sdentry_eventgroup(self): payload = b"\x06\xAA\xBB\xCD\x88\x99\x66\x77\xEE\x20\x21\x22\x00\x09\x11\x11" result = hdr.SOMEIPSDEntry.parse(payload, 512) entry = hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.Subscribe, option_index_1=0xAA, option_index_2=0xBB, num_options_1=0xC, num_options_2=0xD, service_id=0x8899, instance_id=0x6677, major_version=0xEE, ttl=0x202122, minver_or_counter=0x091111, ) self.assertEqual(result[1], b"") self.assertEqual(result[0], entry) self.assertEqual(entry.build(), payload)
def test_sdentry_service_minver(self): entry = hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.FindService, option_index_1=0xAA, option_index_2=0xBB, num_options_1=0xC, num_options_2=0xD, service_id=0x8899, instance_id=0x6677, major_version=0xEE, ttl=0x202122, minver_or_counter=0x10111213, ) self.assertEqual(entry.service_minor_version, 0x10111213) self.assertFalse(entry.options_resolved) with self.assertRaises(TypeError): entry.eventgroup_counter with self.assertRaises(TypeError): entry.eventgroup_id
def test_service_match_find(self): host = ipaddress.ip_address("2001:db8::1234:5678:dead:beef") port = 4321 ep = hdr.IPv6EndpointOption(address=host, l4proto=hdr.L4Protocols.TCP, port=port) srv = cfg.Service( service_id=0x1111, instance_id=0x2222, major_version=0x33, minor_version=0x333333, options_1=(ep, ), ) find = hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.FindService, service_id=0x1111, instance_id=0x2222, major_version=0x33, minver_or_counter=0x333333, ttl=3, ) self.assertTrue(srv.matches_find(find)) self.assertFalse(srv.matches_find(replace(find, service_id=0x0101))) self.assertFalse(srv.matches_find(replace(find, instance_id=0x0101))) self.assertTrue(srv.matches_find(replace(find, instance_id=0xFFFF))) self.assertFalse(srv.matches_find(replace(find, major_version=0x0B))) self.assertTrue(srv.matches_find(replace(find, major_version=0xFF))) self.assertFalse( srv.matches_find(replace(find, minver_or_counter=0x0B0B0B))) self.assertTrue( srv.matches_find(replace(find, minver_or_counter=0xFFFFFFFF))) with self.assertRaises(ValueError): srv.matches_find( replace(find, sd_type=hdr.SOMEIPSDEntryType.OfferService))
def test_eventgroup_subscribe_ipv6(self): host = ipaddress.ip_address("2001:db8::1234:5678:dead:beef") port = 4321 results = socket.getaddrinfo( str(host), port, type=socket.SOCK_STREAM, proto=socket.IPPROTO_TCP, ) if not results: raise RuntimeError("could not get addr info") sockaddr = results[0][4] evgr = cfg.Eventgroup( service_id=0xDEAD, instance_id=0x42, major_version=23, eventgroup_id=0xF00BAA, sockname=sockaddr, protocol=hdr.L4Protocols.TCP, ) ep = hdr.IPv6EndpointOption(address=host, l4proto=hdr.L4Protocols.TCP, port=port) entry = hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.Subscribe, service_id=0xDEAD, instance_id=0x42, major_version=23, ttl=3, minver_or_counter=0xF00BAA, options_1=(ep, ), ) self.assertEqual(evgr.create_subscribe_entry(), entry)
def test_sd_option_indexes(self): newopt = hdr.SOMEIPSDConfigOption(configs={"foo": "bar"}.items()) entries = [ hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.Subscribe, option_index_1=0x00, option_index_2=0x00, num_options_1=0x2, num_options_2=0x1, service_id=0x8899, instance_id=0x6677, major_version=0xEE, ttl=0x202122, minver_or_counter=0x10, ), hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.OfferService, option_index_1=0x1, option_index_2=0x2, num_options_1=0x2, num_options_2=0x1, service_id=0x5566, instance_id=0x7788, major_version=0x99, ttl=0xAAABAC, minver_or_counter=0xDEADBEEF, ), ] options = [ hdr.IPv4EndpointOption( address=ipaddress.IPv4Address("1.2.3.4"), l4proto=hdr.L4Protocols.UDP, port=2047, ), hdr.IPv4EndpointOption( address=ipaddress.IPv4Address("254.253.252.251"), l4proto=hdr.L4Protocols.UDP, port=65535, ), hdr.SOMEIPSDLoadBalancingOption(priority=0x2222, weight=0x3333), ] sd = hdr.SOMEIPSDHeader( flag_reboot=False, flag_unicast=True, flags_unknown=0, entries=entries, options=options, ) self.assertFalse(any(e.options_resolved for e in sd.entries)) sd_resolved = sd.resolve_options() self.assertTrue(all(e.options_resolved for e in sd_resolved.entries)) with self.assertRaises(ValueError): sd_resolved.resolve_options() with self.assertRaises(ValueError): sd_resolved.build() self.assertEqual(sd_resolved.entries[0].options_1, options[:2]) self.assertEqual(sd_resolved.entries[0].options_2, options[:1]) self.assertEqual(sd_resolved.entries[0].options, options[:2] + options[:1]) self.assertEqual(sd_resolved.entries[1].options_1, options[1:3]) self.assertEqual(sd_resolved.entries[1].options_2, options[2:3]) self.assertEqual(sd_resolved.entries[1].options, options[1:3] + options[2:3]) self.assertIsNone(sd_resolved.entries[0].option_index_1) self.assertIsNone(sd_resolved.entries[0].option_index_2) self.assertIsNone(sd_resolved.entries[1].option_index_1) self.assertIsNone(sd_resolved.entries[1].option_index_2) self.assertIsNone(sd_resolved.entries[0].num_options_1) self.assertIsNone(sd_resolved.entries[0].num_options_2) self.assertIsNone(sd_resolved.entries[1].num_options_1) self.assertIsNone(sd_resolved.entries[1].num_options_2) self.assertNotIn(newopt, sd_resolved.options) newsd = replace( sd_resolved, entries=( replace(sd_resolved.entries[0], options_2=[]), replace(sd_resolved.entries[1], options_1=[options[0], newopt]), ), ).assign_option_indexes() self.assertIn(newopt, newsd.options) self.assertFalse(newsd.entries[0].options_1) self.assertFalse(newsd.entries[0].options_2) self.assertFalse(newsd.entries[0].options) self.assertFalse(newsd.entries[1].options_1) self.assertFalse(newsd.entries[1].options_2) self.assertFalse(newsd.entries[1].options) self.assertEqual(newsd.entries[0].option_index_1, 0) self.assertEqual(newsd.entries[0].option_index_2, 0) self.assertEqual(newsd.entries[0].num_options_1, 2) self.assertEqual(newsd.entries[0].num_options_2, 0) self.assertEqual(newsd.entries[1].option_index_1, 3) self.assertEqual(newsd.entries[1].option_index_2, 2) self.assertEqual(newsd.entries[1].num_options_1, 2) self.assertEqual(newsd.entries[1].num_options_2, 1)
def test_sd(self): payload = b"\xa5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" sd = hdr.SOMEIPSDHeader( flag_reboot=True, flag_unicast=False, flags_unknown=0xA5 & ~0x80, entries=(), options=(), ) self._check(payload, sd, hdr.SOMEIPSDHeader.parse) entries = [ hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.Subscribe, option_index_1=0x00, option_index_2=0x00, num_options_1=0x2, num_options_2=0x1, service_id=0x8899, instance_id=0x6677, major_version=0xEE, ttl=0x202122, minver_or_counter=0x10, ), hdr.SOMEIPSDEntry( sd_type=hdr.SOMEIPSDEntryType.OfferService, option_index_1=0x01, option_index_2=0x01, num_options_1=0x0, num_options_2=0x1, service_id=0x5566, instance_id=0x7788, major_version=0x99, ttl=0xAAABAC, minver_or_counter=0xDEADBEEF, ), ] options = [ hdr.IPv4EndpointOption( address=ipaddress.IPv4Address("1.2.3.4"), l4proto=hdr.L4Protocols.UDP, port=2047, ), hdr.IPv4EndpointOption( address=ipaddress.IPv4Address("254.253.252.251"), l4proto=hdr.L4Protocols.UDP, port=65535, ), hdr.SOMEIPSDLoadBalancingOption(priority=0x2222, weight=0x3333), ] payload = ( b"\x40\x00\x00\x00" b"\x00\x00\x00\x20" b"\x06\x00\x00\x21\x88\x99\x66\x77\xEE\x20\x21\x22\x00\x00\x00\x10" b"\x01\x01\x01\x01\x55\x66\x77\x88\x99\xaa\xab\xac\xde\xad\xbe\xef" b"\x00\x00\x00\x20" b"\x00\x09\x04\x00\x01\x02\x03\x04\x00\x11\x07\xff" b"\x00\x09\x04\x00\xfe\xfd\xfc\xfb\x00\x11\xff\xff" b"\x00\x05\x02\x00\x22\x22\x33\x33") sd = hdr.SOMEIPSDHeader( flag_reboot=False, flag_unicast=True, flags_unknown=0, entries=tuple(entries), options=tuple(options), ) self._check(payload, sd, hdr.SOMEIPSDHeader.parse) with self.assertRaises(hdr.ParseError): hdr.SOMEIPSDHeader.parse( b"\xa5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00") with self.assertRaises(hdr.ParseError): hdr.SOMEIPSDHeader.parse( b"\xa5\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00") with self.assertRaises(hdr.ParseError): hdr.SOMEIPSDHeader.parse( b"\xa5\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00") with self.assertRaises(hdr.ParseError): hdr.SOMEIPSDHeader.parse( b"\xa5\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff") with self.assertRaises(hdr.ParseError): hdr.SOMEIPSDHeader.parse( b"\xa5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04") with self.assertRaises(hdr.ParseError): hdr.SOMEIPSDHeader.parse( b"\xa5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10") with self.assertRaises(hdr.ParseError): hdr.SOMEIPSDHeader.parse( b"\xa5\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff")