Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _test_request_transfer_exit_success(self):
response = self.udsclient.request_transfer_exit(data=b'\x12\x34\x56')
self.assertEqual(response.service, services.RequestTransferExit)
self.assertEqual(response.service_data.parameter_records, b'\x89\xab\xcd\xef')
from udsoncan import Request, services
from test.UdsTest import UdsTest
class DummyServiceNormal(services.BaseService):
_sid = 0x13
class DummyServiceNoSubunction(services.BaseService):
_sid = 0x13
_use_subfunction = False
class TestRequest(UdsTest):
def test_create_from_instance_ok(self):
req = Request(DummyServiceNormal())
self.assertEqual(req.service.request_id(), 0x13)
def test_create_from_class_ok(self):
req = Request(DummyServiceNormal, subfunction=0x44)
self.assertEqual(req.service.request_id(), 0x13)
def test_make_payload_basic(self):
req = Request(DummyServiceNormal, subfunction=0x44)
def _test_dsc_success(self):
response = self.udsclient.change_session(services.DiagnosticSessionControl.Session.defaultSession)
self.assertEqual(response.service_data.session_echo, 1)
self.assertEqual(response.service_data.session_param_records, b"\x99\x88")
def _test_bad_param(self):
with self.assertRaises(ValueError):
response = self.udsclient.access_timing_parameter(access_type=-1)
with self.assertRaises(ValueError):
response = self.udsclient.access_timing_parameter(access_type=0x100)
with self.assertRaises(ValueError):
response = self.udsclient.access_timing_parameter(access_type=services.AccessTimingParameter.AccessType.setTimingParametersToGivenValues, timing_param_record=None)
with self.assertRaises(ValueError):
response = self.udsclient.access_timing_parameter(access_type=services.AccessTimingParameter.AccessType.readExtendedTimingParameterSet, timing_param_record=b"\xaa\xbb")
with self.assertRaises(ValueError):
response = self.udsclient.access_timing_parameter(access_type=services.AccessTimingParameter.AccessType.setTimingParametersToDefaultValues, timing_param_record=123)
def _test_tester_present_denied_no_exception(self):
self.udsclient.config['exception_on_negative_response'] = False
response = self.udsclient.tester_present()
self.assertTrue(response.valid)
self.assertFalse(response.positive)
self.assertTrue(issubclass(response.service, services.TesterPresent))
self.assertEqual(response.code, 0x13)
from udsoncan.server import Server
import struct
import time
conn = Connection('vcan0', rxid=0x456, txid=0x123)
with conn.open():
while True:
payload = conn.wait_frame(timeout=None)
if payload is not None:
print("Received: " + ''.join(['%02X' % b for b in payload]))
req = Request.from_payload(payload)
response = Response(req.service, Response.Code.GeneralReject)
## DiagnosticSessionControl
if req.service == services.DiagnosticSessionControl:
if sessions.from_id(req.subfunction) == sessions.ExtendedDiagnosticSession:
response = Response(req.service, Response.Code.PositiveResponse)
else:
response = Response(req.service, Response.Code.SubFunctionNotSupported)
## SecurityAccess
elif req.service == services.SecurityAccess:
if req.subfunction == 3:
response = Response(req.service, Response.Code.PositiveResponse, data=b"\x12\x34\x56\x78")
elif req.subfunction == 4:
if req.data == b"\xed\xcb\xa9\x87":
response = Response(req.service, Response.Code.PositiveResponse)
else:
response = Response(req.service, Response.Code.InvalidKey)
else:
response = Response(req.service, Response.Code.SubFunctionNotSupported)
def _test_timeout_override(self):
req = Request(service = services.TesterPresent, subfunction=0)
timeout = 0.5
try:
t1 = time.time()
response = self.udsclient.send_request(req, timeout=timeout)
raise Exception('Request did not raise a TimeoutException')
except TimeoutException as e:
diff = time.time() - t1
self.assertGreater(diff, timeout, 'Timeout raised after %.3f seconds when it should be %.3f sec' % (diff, timeout))
self.assertLess(diff, timeout+0.5, 'Timeout raised after %.3f seconds when it should be %.3f sec' % (diff, timeout))
def _test_routine_control_denied_exception(self):
with self.assertRaises(NegativeResponseException) as handle:
self.udsclient.routine_control(routine_id=0x1234, control_type=0x11, data=b'\x99\x88')
response = handle.exception.response
self.assertTrue(response.valid)
self.assertFalse(response.positive)
self.assertTrue(issubclass(response.service, services.RoutineControl))
self.assertEqual(response.code, 0x72)
def _test_dsc_success_spr(self):
with self.udsclient.suppress_positive_response:
response = self.udsclient.change_session(services.DiagnosticSessionControl.Session.defaultSession)
self.assertEqual(response, None)
self.conn.fromuserqueue.get(timeout=0.2) #Avoid closing connection prematurely
def get_payload(self, suppress_positive_response=None):
"""
Generates a payload to be given to the underlying protocol.
This method is meant to be used by a UDS client
:return: A payload to be sent through the underlying protocol
:rtype: bytes
"""
if not issubclass(self.service, services.BaseService):
raise ValueError("Cannot generate a payload. Given service is not a subclass of BaseService")
if self.service.use_subfunction() and not isinstance(self.subfunction, int):
raise ValueError("Cannot generate a payload. Given subfunction is not a valid integer")
requestid = self.service.request_id() # Returns the service ID used to make a client request
payload = struct.pack("B", requestid)
if self.service.use_subfunction():
subfunction = self.subfunction
if suppress_positive_response is None:
if self.suppress_positive_response:
subfunction |= 0x80
else:
if suppress_positive_response == True:
subfunction |= 0x80