class TestFwFingerprint(unittest.TestCase): def assertFingerprints(self, candidates, expected): candidates = list(candidates) self.assertEqual(len(candidates), 1, f"got more than one candidate: {candidates}") self.assertEqual(candidates[0], expected) @parameterized.expand([(k, v) for k, v in FW_VERSIONS.items()]) def test_fw_fingerprint(self, car_model, ecus): CP = car.CarParams.new_message() for _ in range(200): fw = [] for ecu, fw_versions in ecus.items(): ecu_name, addr, sub_addr = ecu fw.append({ "ecu": ecu_name, "fwVersion": random.choice(fw_versions), "address": addr, "subAddress": 0 if sub_addr is None else sub_addr }) CP.carFw = fw _, matches = match_fw_to_car(CP.carFw) self.assertFingerprints(matches, car_model) def test_no_duplicate_fw_versions(self): passed = True for car_model, ecus in FW_VERSIONS.items(): for ecu, ecu_fw in ecus.items(): duplicates = {fw for fw in ecu_fw if ecu_fw.count(fw) > 1} if len(duplicates): print(car_model, ECU_NAME[ecu[0]], duplicates) passed = False self.assertTrue(passed, "Duplicate FW versions found")
def test_no_duplicate_fw_versions(self): passed = True for car_model, ecus in FW_VERSIONS.items(): for ecu, ecu_fw in ecus.items(): duplicates = set([fw for fw in ecu_fw if ecu_fw.count(fw) > 1]) if len(duplicates): print(car_model, ECU_NAME[ecu[0]], duplicates) passed = False self.assertTrue(passed, "Duplicate FW versions found")
def test_blacklisted_ecus(self): passed = True blacklisted_addrs = (0x7c4, 0x7d0) # includes A/C ecu and an unknown ecu for car_model, ecus in FW_VERSIONS.items(): CP = interfaces[car_model][0].get_params(car_model) if CP.carName == 'subaru': for ecu in ecus.keys(): if ecu[1] in blacklisted_addrs: print(f'{car_model}: Blacklisted ecu: (Ecu.{ECU_NAME[ecu[0]]}, {hex(ecu[1])})') passed = False self.assertTrue(passed, "Blacklisted FW versions found")
def match_fw_to_car_fuzzy(fw_versions_dict, log=True, exclude=None): """Do a fuzzy FW match. This function will return a match, and the number of firmware version that were matched uniquely to that specific car. If multiple ECUs uniquely match to different cars the match is rejected.""" # These ECUs are known to be shared between models (EPS only between hybrid/ICE version) # Getting this exactly right isn't crucial, but excluding camera and radar makes it almost # impossible to get 3 matching versions, even if two models with shared parts are released at the same # time and only one is in our database. exclude_types = [Ecu.fwdCamera, Ecu.fwdRadar, Ecu.eps, Ecu.debug] # Build lookup table from (addr, sub_addr, fw) to list of candidate cars all_fw_versions = defaultdict(list) for candidate, fw_by_addr in FW_VERSIONS.items(): if candidate == exclude: continue for addr, fws in fw_by_addr.items(): if addr[0] in exclude_types: continue for f in fws: all_fw_versions[(addr[1], addr[2], f)].append(candidate) match_count = 0 candidate = None for addr, versions in fw_versions_dict.items(): for version in versions: # All cars that have this FW response on the specified address candidates = all_fw_versions[(addr[0], addr[1], version)] if len(candidates) == 1: match_count += 1 if candidate is None: candidate = candidates[0] # We uniquely matched two different cars. No fuzzy match possible elif candidate != candidates[0]: return set() if match_count >= 2: if log: cloudlog.error( f"Fingerprinted {candidate} using fuzzy match. {match_count} matching ECUs" ) return {candidate} else: return set()
class TestFwFingerprint(unittest.TestCase): def assertFingerprints(self, candidates, expected): candidates = list(candidates) self.assertEqual(len(candidates), 1) self.assertEqual(candidates[0], expected) def test_rav4_tss2(self): CP = car.CarParams.new_message() CP.carFw = [ {"ecu": Ecu.esp, "fwVersion": b"\x01F15260R210\x00\x00\x00\x00\x00\x00", "address": 1968, "subAddress": 0}, {"ecu": Ecu.engine, "fwVersion": b"\x028966342Y8000\x00\x00\x00\x00897CF1201001\x00\x00\x00\x00", "address": 1792, "subAddress": 0}, {"ecu": Ecu.eps, "fwVersion": b"\x028965B0R01200\x00\x00\x00\x008965B0R02200\x00\x00\x00\x00", "address": 1953, "subAddress": 0}, {"ecu": Ecu.fwdRadar, "fwVersion": b"\x018821F3301200\x00\x00\x00\x00", "address": 1872, "subAddress": 15}, {"ecu": Ecu.fwdCamera, "fwVersion": b"\x028646F4203300\x00\x00\x00\x008646G26011A0\x00\x00\x00\x00", "address": 1872, "subAddress": 109} ] self.assertFingerprints(match_fw_to_car(CP.carFw), TOYOTA.RAV4_TSS2) @parameterized.expand([(k, v) for k, v in FW_VERSIONS.items()]) def test_fw_fingerprint_all(self, car_model, ecus): # TODO: this is too slow, so don't run for now return ecu_fw_lists = [] # pylint: disable=W0101 for ecu, fw_versions in ecus.items(): ecu_name, addr, sub_addr = ecu ecu_fw_lists.append([]) for fw in fw_versions: ecu_fw_lists[-1].append({"ecu": ecu_name, "fwVersion": fw, "address": addr, "subAddress": 0 if sub_addr is None else sub_addr}) CP = car.CarParams.new_message() for car_fw in product(*ecu_fw_lists): CP.carFw = car_fw self.assertFingerprints(match_fw_to_car(CP.carFw), car_model) @parameterized.expand([(k, v) for k, v in FW_VERSIONS.items()]) def test_fw_fingerprint(self, car_model, ecus): CP = car.CarParams.new_message() for _ in range(20): fw = [] for ecu, fw_versions in ecus.items(): ecu_name, addr, sub_addr = ecu fw.append({"ecu": ecu_name, "fwVersion": random.choice(fw_versions), "address": addr, "subAddress": 0 if sub_addr is None else sub_addr}) CP.carFw = fw self.assertFingerprints(match_fw_to_car(CP.carFw), car_model) def test_no_duplicate_fw_versions(self): passed = True for car_model, ecus in FW_VERSIONS.items(): for ecu, ecu_fw in ecus.items(): duplicates = set([fw for fw in ecu_fw if ecu_fw.count(fw) > 1]) if len(duplicates): print(car_model, ECU_NAME[ecu[0]], duplicates) passed = False self.assertTrue(passed, "Duplicate FW versions found")
class TestFwFingerprint(unittest.TestCase): def assertFingerprints(self, candidates, expected): candidates = list(candidates) self.assertEqual(len(candidates), 1, f"got more than one candidate: {candidates}") self.assertEqual(candidates[0], expected) @parameterized.expand([(k, v) for k, v in FW_VERSIONS.items()]) def test_fw_fingerprint(self, car_model, ecus): CP = car.CarParams.new_message() for _ in range(200): fw = [] for ecu, fw_versions in ecus.items(): ecu_name, addr, sub_addr = ecu fw.append({ "ecu": ecu_name, "fwVersion": random.choice(fw_versions), "address": addr, "subAddress": 0 if sub_addr is None else sub_addr }) CP.carFw = fw _, matches = match_fw_to_car(CP.carFw) self.assertFingerprints(matches, car_model) def test_no_duplicate_fw_versions(self): passed = True for car_model, ecus in FW_VERSIONS.items(): for ecu, ecu_fw in ecus.items(): duplicates = {fw for fw in ecu_fw if ecu_fw.count(fw) > 1} if len(duplicates): print(car_model, ECU_NAME[ecu[0]], duplicates) passed = False self.assertTrue(passed, "Duplicate FW versions found") def test_blacklisted_ecus(self): passed = True blacklisted_addrs = (0x7c4, 0x7d0 ) # includes A/C ecu and an unknown ecu for car_model, ecus in FW_VERSIONS.items(): CP = interfaces[car_model][0].get_params(car_model) if CP.carName == 'subaru': for ecu in ecus.keys(): if ecu[1] in blacklisted_addrs: print( f'{car_model}: Blacklisted ecu: (Ecu.{ECU_NAME[ecu[0]]}, {hex(ecu[1])})' ) passed = False self.assertTrue(passed, "Blacklisted FW versions found") def test_fw_request_ecu_whitelist(self): passed = True brands = set(r.brand for r in REQUESTS) versions = get_interface_attr('FW_VERSIONS') for brand in brands: whitelisted_ecus = [ ecu for r in REQUESTS for ecu in r.whitelist_ecus if r.brand == brand ] brand_ecus = set([ fw[0] for car_fw in versions[brand].values() for fw in car_fw ]) # each ecu in brand's fw versions needs to be whitelisted at least once ecus_not_whitelisted = set(brand_ecus) - set(whitelisted_ecus) if len(whitelisted_ecus) and len(ecus_not_whitelisted): ecu_strings = ", ".join( [f'Ecu.{ECU_NAME[ecu]}' for ecu in ecus_not_whitelisted]) print( f'{brand.title()}: FW query whitelist missing ecus: {ecu_strings}' ) passed = False self.assertTrue( passed, "Not all ecus in FW versions found in query whitelists")