def test_make_payload_custom_data_negative(self): response = Response(DummyServiceNormal(), code=0x10) self.assertTrue(response.valid) self.assertFalse(response.positive) response.data = b"\x12\x34\x56\x78" payload = response.get_payload() self.assertEqual(b"\x7F\x13\x10\x12\x34\x56\x78", payload)
def test_make_payload_basic_negative(self): response = Response(DummyServiceNormal(), code=0x10) # General Reject self.assertFalse(response.positive) self.assertTrue(response.valid) payload = response.get_payload() self.assertEqual(b"\x7F\x13\x10", payload) # Original ID + 0x40. 7F indicate negative
def test_p2_star_timeout(self): self.conn.touserqueue.get(timeout=0.2) response = Response( service=services.TesterPresent, code=Response.Code.RequestCorrectlyReceived_ResponsePending) self.conn.fromuserqueue.put(response.get_payload()) time.sleep(0.1) self.conn.fromuserqueue.put(response.get_payload())
def test_timeout_pending_response(self): if not hasattr(self, 'completed'): self.completed = False self.conn.touserqueue.get(timeout=0.2) response = Response(service=services.TesterPresent, code=Response.Code.RequestCorrectlyReceived_ResponsePending) t1 = time.time() while time.time() - t1 < 1 and not self.completed: self.conn.fromuserqueue.put(response.get_payload()) time.sleep(0.1)
def test_p2_star_timeout_overrided_by_diagnostic_session_control(self): self.conn.touserqueue.get(timeout=0.2) self.conn.fromuserqueue.put( b"\x50\x01\x03\xE8\x00\xC8" ) # Respond to diagnostic session control with timeout of 2 sec response = Response( service=services.TesterPresent, code=Response.Code.RequestCorrectlyReceived_ResponsePending) self.conn.fromuserqueue.put(response.get_payload()) time.sleep(0.1) self.conn.fromuserqueue.put(response.get_payload())
def testerPresent(self, *args): try: with Client(self.conn, request_timeout=1, config={'exception_on_unexpected_response': False}) as client: # noqa: F841 self.conn.send(b'\x10\x03') self.termPrint('Request', b'\x10\x03') payload = self.conn.wait_frame(timeout=1) response = Response.\ from_payload(payload) # noqa: F841, E501 response = str(response).split('<') response = (response[1].split('-'))[0] self.termPrint(response) self.conn.send(b'\x27\x63') self.termPrint('Request', b'\x27\x63') payload = self.conn.wait_frame(timeout=1) response = Response.from_payload(payload) seed = (response.data.hex()[2:]) sA = sec.securityAlgo(seed, 'series') sleep(.1) key = b'\x27\x64' + sA.calculatedKey response = str(response).split('<') response = (response[1].split('-'))[0] self.conn.send(key) payload = self.conn.wait_frame(timeout=1) response = Response.from_payload(payload) response = str(response).split('<') response = (response[1].split('-'))[0] self.termPrint(response) self.tpTime += 4 service = (self.begServicesList[self.begService.get()])[0] while self.commStatus and service == 'Write Data': if time() - self.tpTime >= 4: self.tpTime = time() with Client( self.conn, request_timeout=1, config={'exception_on_unexpected_response': False}) as client: # noqa: F841 msg = b'\x3E\x00' self.conn.send(msg) self.termPrint('Request', msg) '''payload = self.conn.wait_frame(timeout=1) response = Response.\ from_payload(payload) # noqa: F841, E501 self.termPrint('Response', response.data)''' service = (self.begServicesList[self.begService.get()])[0] self.begService.set(0) except Exception as e: messagebox.showinfo('Function unavailable', e) self.begService.set(0)
def test_from_payload_basic_positive(self): payload = b'\x7E\x00' # 0x7e = TesterPresent response = Response.from_payload(payload) self.assertTrue(response.valid) self.assertTrue(response.positive) self.assertEqual(response.service.response_id(), 0x7E) self.assertEqual(response.code, 0)
def test_from_payload_custom_data_positive(self): payload = b'\x7E\x01\x12\x34\x56\x78' # 0x3E = TesterPresent response = Response.from_payload(payload) self.assertTrue(response.valid) self.assertTrue(response.positive) self.assertEqual(response.service.response_id(), 0x7E) self.assertEqual(response.data, b'\x01\x12\x34\x56\x78')
def test_from_payload_custom_data_negative(self): payload = b'\x7F\x3E\x10\x12\x34\x56\x78' # 0x3E = TesterPresent, 0x10 = General Reject response = Response.from_payload(payload) self.assertTrue(response.valid) self.assertEqual(response.service.response_id(), 0x7E) self.assertEqual(response.code, 0x10) self.assertEqual(response.data, b'\x12\x34\x56\x78')
def test_from_payload_basic_negative(self): payload = b'\x7F\x3E\x10' # 0x3e = TesterPresent, 0x10 = General Reject response = Response.from_payload(payload) self.assertTrue(response.valid) self.assertFalse(response.positive) self.assertEqual(response.service.response_id(), 0x7E) self.assertEqual(response.code, 0x10)
def __getSeed(self): req = Request(SecurityAccess, subfunction=self.ACCESS_LEVEL) self.client.send(req.get_payload()) payload = self.client.wait_frame(timeout=self.timeout) response = Response.from_payload(payload) if response.service == SecurityAccess and response.code == Response.Code.PositiveResponse: return response.data[1:] else: return None
def __sendKeyAndMesureTime(self, keyLevel, data): req = Request(SecurityAccess, subfunction=keyLevel, data=data) self.client.send(req.get_payload()) startTime = time.time() payload = self.client.wait_frame(timeout=self.timeout) endTime = time.time() progressTime = endTime - startTime response = Response.from_payload(payload) return response, progressTime
def RCRRP_responses(self): self.conn.touserqueue.get(timeout=0.2) response = Response( service=services.TesterPresent, code=Response.Code.RequestCorrectlyReceived_ResponsePending) t1 = time.time() while time.time() - t1 <= 4.0: self.conn.fromuserqueue.put(response.get_payload()) time.sleep(0.1) response = Response(service=services.TesterPresent, code=Response.Code.PositiveResponse, data=bytes(2)) self.conn.fromuserqueue.put(response.get_payload())
def send_request(self, request, timeout=-1): if timeout is not None and timeout < 0: timeout = self.request_timeout self.conn.empty_rxqueue() self.logger.debug("Sending request to server") self.conn.send(request) if not request.suppress_positive_response: self.logger.debug("Waiting for server response") try: payload = self.conn.wait_frame(timeout=timeout, exception=True) except Exception as e: raise e response = Response.from_payload(payload) self.last_response = response self.logger.debug("Received response from server") if not response.valid: raise InvalidResponseException( response, 'Received response is invalid.') if response.service.response_id() != request.service.response_id(): msg = "Response gotten from server has a service ID different than the one of the request. Received=0x%02x, Expected=0x%02x" % ( response.service.response_id(), request.service.response_id()) raise UnexpectedResponseException(response, msg) if not response.positive: if not request.service.is_supported_negative_response( response.code): self.logger.warning( "Given response (%s) is not a supported negative response code according to UDS standard." % response.code_name) raise NegativeResponseException(response) self.logger.info('Received positive response from server.') return response
def test_from_input_param(self): with self.assertRaises(ValueError): response = Response(service="a string") with self.assertRaises(ValueError): response = Response(service=RandomClass()) with self.assertRaises(ValueError): response = Response(service=DummyServiceNormal(), code="string") with self.assertRaises(ValueError): response = Response(service=DummyServiceNormal(), code=-1) with self.assertRaises(ValueError): response = Response(service=DummyServiceNormal(), code=0x100) with self.assertRaises(ValueError): response = Response(service=DummyServiceNormal(), code=0x10, data=11)
import securityAlgo as sec from time import sleep tp_addr = isotp.Address(isotp.AddressingMode.Normal_29bits, txid=0x18DA2AF1, rxid=0x18DAFA2A) bus = Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000) stack = isotp.CanStack(bus=bus, address=tp_addr) conn = PythonIsoTpConnection(stack) with Client(conn, request_timeout=1, config={'exception_on_unexpected_response': False}) as client: try: conn.send(b'\x10\x03') payload = conn.wait_frame(timeout=1) response = Response.from_payload(payload) print(response) conn.send(b'\x27\x63') payload = conn.wait_frame(timeout=1) response = Response.from_payload(payload) print('key: ' + response.data.hex()[2:]) seed = (response.data.hex()[2:]) sA = sec.securityAlgo(seed, 'series') sleep(.1) print('calculated key: ' + (sA.calculatedKey).hex()) conn.send(b'\x27\x64' + sA.calculatedKey) payload = conn.wait_frame(timeout=1) response = Response.from_payload(payload) response = str(response).split('<') response = (response[1].split('-'))[0] print(response)
def test_str_repr(self): response = Response(DummyServiceNormal, code=0x22) str(response) response.__repr__()
def test_from_empty_payload(self): payload = b'' response = Response.from_payload(payload) self.assertFalse(response.valid) self.assertIsNone(response.service) self.assertEqual(b'', response.data)
def test_from_bad_payload(self): payload = '\xFF\xFF' response = Response.from_payload(payload) self.assertFalse(response.valid) self.assertIsNone(response.service) self.assertEqual('', response.data)
def test_make_payload_basic_positive(self): response = Response(DummyServiceNormal(), code=0, data=b"\x01") self.assertTrue(response.positive) self.assertTrue(response.valid) payload = response.get_payload() self.assertEqual(b"\x53\x01", payload) # Original ID + 0x40
def test_create_from_class_ok(self): response = Response(DummyServiceNormal, code=0x22) self.assertTrue(response.valid) self.assertEqual(response.service.request_id(), 0x13) self.assertEqual(response.code, 0x22)