def testGetId2(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master("eth", config={"HOST": "localhost", "LOGLEVEL": "DEBUG"}) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with(self.DefaultConnectCmd) ms.push_frame([0x08, 0x00, 0x01, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00]) gid = xm.getId(0x01) mock_socket.return_value.send.assert_called_with(bytes([0x02, 0x00, 0x01, 0x00, 0xFA, 0x01])) assert gid.mode == 0 assert gid.length == 6 ms.push_frame([0x07, 0x00, 0x02, 0x00, 0xFF, 0x58, 0x43, 0x50, 0x73, 0x69, 0x6D]) res = xm.upload(gid.length) mock_socket.return_value.send.assert_called_with(bytes([0x02, 0x00, 0x02, 0x00, 0xF5, 0x06])) assert res == b"XCPsim" ms.push_frame( [ 0x0E, 0x00, 0x03, 0x00, 0xFF, 0x01, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x58, 0x43, 0x50, 0x73, 0x69, 0x6D, ] ) gid = xm.getId(0x01) mock_socket.return_value.send.assert_called_with(bytes([0x02, 0x00, 0x03, 0x00, 0xFA, 0x01])) assert gid.mode == 1 assert gid.length == 6 assert gid.identification == list(b"XCPsim")
def testUserCmd(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master("eth", config={"HOST": 'localhost', "LOGLEVEL": "DEBUG"}) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with( self.DefaultConnectCmd) ms.push_frame([ 0x03, 0x00, 0x00, 0x00, 0xff, 0xaa, 0xbb]) data = [0xbe, 0xef] res = xm.userCmd(0x55, data) mock_socket.return_value.send.assert_called_with(bytes([ 0x04, 0x00, 0x01, 0x00, 0xf1, 0x55, 0xbe, 0xef])) assert res == b'\xaa\xbb'
def testUpload(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master("eth", config={"HOST": 'localhost', "LOGLEVEL": "DEBUG"}) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with( self.DefaultConnectCmd) ms.push_frame([ 0x09, 0x00, 0x00, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]) res = xm.upload(8) mock_socket.return_value.send.assert_called_with(bytes([ 0x02, 0x00, 0x01, 0x00, 0xf5, 0x08])) assert res == b'\x01\x02\x03\x04\x05\x06\x07\x08'
def testGetVersion(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master("eth", config={"HOST": 'localhost', "LOGLEVEL": "DEBUG"}) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with( self.DefaultConnectCmd) ms.push_packet("FF 00 01 05 01 04") res = xm.getVersion() mock_socket.return_value.send.assert_called_with(bytes( [0x02, 0x00, 0x01, 0x00, 0xc0, 0x00])) assert res.protocolMajor == 1 assert res.protocolMinor == 5 assert res.transportMajor == 1 assert res.transportMinor == 4
def testBuildChecksum(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master("eth", config={"HOST": 'localhost', "LOGLEVEL": "DEBUG"}) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with( self.DefaultConnectCmd) ms.push_frame("08 00 01 00 FF 09 00 00 04 05 06 07") res = xm.buildChecksum(1024) mock_socket.return_value.send.assert_called_with(bytes([ 0x08, 0x00, 0x01, 0x00, 0xf3, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00])) assert res.checksumType == \ types.BuildChecksumResponse.checksumType.XCP_CRC_32 assert res.checksum == 0x07060504
def testDownloadNext(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master("eth", config={"HOST": 'localhost', "LOGLEVEL": "DEBUG"}) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with( self.DefaultConnectCmd) ms.push_frame([0x01, 0x00, 0x00, 0x00, 0xff]) data = [0xCA, 0xFE, 0xBA, 0xBE] remaining_block_length = 42 res = xm.downloadNext(data, remainingBlockLength=remaining_block_length) mock_socket.return_value.send.assert_called_with(bytes( [0x06, 0x00, 0x01, 0x00, 0xef, remaining_block_length, 0xca, 0xfe, 0xba, 0xbe])) # no response shall be expected if it is not the last DOWNLOAD_NEXT packet of a block assert res is None
def testUnlock(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master("eth", config={"HOST": 'localhost', "LOGLEVEL": "DEBUG"}) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with( self.DefaultConnectCmd) ms.push_packet("FF 10") res = xm.unlock(0x04, [0x12, 0x34, 0x56, 0x78]) mock_socket.return_value.send.assert_called_with(bytes( [0x06, 0x00, 0x01, 0x00, 0xf7, 0x04, 0x12, 0x34, 0x56, 0x78])) assert res.calpag is False assert res.daq is False assert res.stim is False assert res.pgm is True
def testTimeCorrelationProperties(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with(bytes( [0x02, 0x00, 0x00, 0x00, 0xff, 0x00])) ms.push_packet("FF 15 25 01 1F 00 78 56") res = xm.timeCorrelationProperties(0x15, 0x01, 0x1234) mock_socket.return_value.send.assert_called_with(bytes([ 0x06, 0x00, 0x01, 0x00, 0xC6, 0x15, 0x01, 0x00, 0x34, 0x12])) assert res.slaveConfig == 0x15 assert res.observableClocks == 0x25 assert res.syncState == 0x01 assert res.clockInfo == 0x1F assert res.clusterId == 0x5678
def testDownloadNextBlock(self): conf = { 'CAN_ID_MASTER': 1, 'CAN_ID_SLAVE': 2, 'CAN_DRIVER': 'MockCanInterface', 'CAN_USE_DEFAULT_LISTENER': False } with Master("can", config=conf) as xm: mock_caninterface = xm.transport.canInterface mock_caninterface.push_packet(self.DefaultConnectResponse) xm.connect() data = bytes([i for i in range(14)]) # Downloading 14 bytes in block mode: # command code n payload... # DOWNLOAD: 0xF0, 14, 0, 1, 2, 3, 4, 5 # testing -> DOWNLOAD_NEXT: 0xEF, 8, 6, 7, 8, 9, 10,11 # testing -> DOWNLOAD_NEXT: 0xEF, 2, 12,13 # This is the first DOWNLOAD_NEXT packet of a block, no response is expected from the slave device. res = xm.downloadNext(data=data, remainingBlockLength=8, last=False) assert res is None # This is the last DOWNLOAD_NEXT packet of a block, positive response is expected from the slave device. mock_caninterface.push_packet('FF') res = xm.downloadNext(data=data, remainingBlockLength=2, last=True) assert res == b''
def testShortDownload(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with( bytes([0x02, 0x00, 0x00, 0x00, 0xff, 0x00])) ms.push_frame("01 00 01 00 FF") data = [0xCA, 0xFE, 0xBA, 0xBE] res = xm.shortDownload(0x12345678, 0x55, *data) mock_socket.return_value.send.assert_called_with( bytes([ 0x0c, 0x00, 0x01, 0x00, 0xed, 0x04, 0x00, 0x55, 0x78, 0x56, 0x34, 0x12, 0xca, 0xfe, 0xba, 0xbe ])) assert res == b''
def testGetStatus2(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() ms.push_packet("FF 09 1D 00 34 12") res = xm.getStatus() mock_socket.return_value.send.assert_called_with(bytes( [0x01, 0x00, 0x01, 0x00, 0xfd])) assert res.sessionStatus.storeCalRequest is True assert res.sessionStatus.storeDaqRequest is False assert res.sessionStatus.clearDaqRequest is True assert res.sessionStatus.daqRunning is False assert res.sessionStatus.resume is False assert res.resourceProtectionStatus.pgm is True assert res.resourceProtectionStatus.stim is True assert res.resourceProtectionStatus.daq is True assert res.resourceProtectionStatus.calpag is True assert res.sessionConfiguration == 0x1234
def testBuildChecksum(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with(bytes( [0x02, 0x00, 0x00, 0x00, 0xff, 0x00])) ms.push_frame("08 00 01 00 FF 09 00 00 04 05 06 07") res = xm.buildChecksum(1024) mock_socket.return_value.send.assert_called_with(bytes([ 0x08, 0x00, 0x01, 0x00, 0xf3, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00])) assert res.checksumType == \ types.BuildChecksumResponse.checksumType.XCP_CRC_32 assert res.checksum == 0x07060504
def testShortUpload(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with( bytes([0x02, 0x00, 0x00, 0x00, 0xff, 0x00])) ms.push_frame("09 00 01 00 FF 01 02 03 04 05 06 07 08") res = xm.shortUpload(8, 0xcafebabe, 1) mock_socket.return_value.send.assert_called_with( bytes([ 0x08, 0x00, 0x01, 0x00, 0xf4, 0x08, 0x00, 0x01, 0xbe, 0xba, 0xfe, 0xca ])) assert res == b'\x01\x02\x03\x04\x05\x06\x07\x08'
def testSetMta(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with( bytes([0x02, 0x00, 0x00, 0x00, 0xff, 0x00])) ms.push_frame("01 00 01 00 FF") res = xm.setMta(0x12345678, 0x55) mock_socket.return_value.send.assert_called_with( bytes([ 0x08, 0x00, 0x01, 0x00, 0xf6, 0x00, 0x00, 0x55, 0x78, 0x56, 0x34, 0x12 ])) assert res == b''
def testDownloadBlock(self): conf = { 'CAN_ID_MASTER': 1, 'CAN_ID_SLAVE': 2, 'CAN_DRIVER': 'MockCanInterface', 'CAN_USE_DEFAULT_LISTENER': False } with Master("can", config=conf) as xm: mock_caninterface = xm.transport.canInterface mock_caninterface.push_packet(self.DefaultConnectResponse) xm.connect() data = bytes([i for i in range(14)]) # Downloading 14 bytes in block mode: # command code n payload... # testing -> DOWNLOAD: 0xF0, 14, 0, 1, 2, 3, 4, 5 # DOWNLOAD_NEXT: 0xEF, 8, 6, 7, 8, 9, 10,11 # DOWNLOAD_NEXT: 0xEF, 2, 12,13 # DOWNLOAD service with block mode, this is the first DOWNLOAD packet of a block, no response # is expected from the slave device: res = xm.download(data=data, blockModeLength=len(data)) assert res is None # DOWNLOAD service with normal mode, normal response expected mock_caninterface.push_packet('FF') res = xm.download(data=data, blockModeLength=None) assert res == b''
def testGetCommModeInfo2(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master("eth", config={"HOST": 'localhost', "LOGLEVEL": "DEBUG"}) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with( self.DefaultConnectCmd) ms.push_packet("FF 00 01 FF 02 00 00 19") res = xm.getCommModeInfo() mock_socket.return_value.send.assert_called_with(bytes( [0x01, 0x00, 0x01, 0x00, 0xfb])) assert res.commModeOptional.interleavedMode is False assert res.commModeOptional.masterBlockMode is True assert res.maxBs == 2 assert res.minSt == 0 assert res.queueSize == 0 assert res.xcpDriverVersionNumber == 25
def testGetCommModeInfo(self, Eth): tr = Eth() tr.request.return_value = bytes( [0x00, 0x01, 0xff, 0x02, 0x00, 0x00, 0x19]) with Master(tr) as xm: res = xm.getCommModeInfo() self.assertEqual(res.commModeOptional.interleavedMode, False) self.assertEqual(res.commModeOptional.masterBlockMode, True) self.assertEqual(res.maxbs, 2) self.assertEqual(res.minSt, 0) self.assertEqual(res.queueSize, 0) self.assertEqual(res.xcpDriverVersionNumber, 25)
def run(self): """ """ self._args = self.parser.parse_args() args = self.args config = readConfiguration(args.conf) config["LOGLEVEL"] = args.loglevel if not "TRANSPORT" in config: raise AttributeError("TRANSPORT must be specified in config!") transport = config['TRANSPORT'].lower() return Master(transport, config=config)
def testGetId2(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: ms.push_packet(self.DefaultConnectResponse) res = xm.connect() mock_socket.return_value.send.assert_called_with( bytes([0x02, 0x00, 0x00, 0x00, 0xff, 0x00])) ms.push_frame([ 0x08, 0x00, 0x01, 0x00, 0xff, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00 ]) gid = xm.getId(0x01) mock_socket.return_value.send.assert_called_with( bytes([0x02, 0x00, 0x01, 0x00, 0xfa, 0x01])) assert gid.mode == 0 assert gid.length == 6 ms.push_frame([ 0x07, 0x00, 0x02, 0x00, 0xff, 0x58, 0x43, 0x50, 0x73, 0x69, 0x6d ]) res = xm.upload(gid.length) mock_socket.return_value.send.assert_called_with( bytes([0x02, 0x00, 0x02, 0x00, 0xf5, 0x06])) assert res == b'XCPsim' ms.push_frame([ 0x0e, 0x00, 0x03, 0x00, 0xff, 0x01, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x58, 0x43, 0x50, 0x73, 0x69, 0x6d ]) gid = xm.getId(0x01) mock_socket.return_value.send.assert_called_with( bytes([0x02, 0x00, 0x03, 0x00, 0xfa, 0x01])) assert gid.mode == 1 assert gid.length == 6 assert gid.identification == list(b'XCPsim')
def testGetID(self, Eth): tr = Eth() tr.request.return_value = bytes( [0x00, 0x01, 0xff, 0x06, 0x00, 0x00, 0x00]) with Master(tr) as xm: gid = xm.getID(0x01) tr.request.return_value = bytes( [0x58, 0x43, 0x50, 0x73, 0x69, 0x6d]) res = xm.upload(gid.length) self.assertEqual(gid.mode, 0) self.assertEqual(gid.reserved, 65281) self.assertEqual(gid.length, 6) self.assertEqual(res, b'XCPsim')
def run(self): """""" self._args = self.parser.parse_args() args = self.args config = readConfiguration(args.conf) config["LOGLEVEL"] = args.loglevel if "TRANSPORT" not in config: raise AttributeError("TRANSPORT must be specified in config!") transport = config["TRANSPORT"].lower() master = Master(transport, config=config) if self.callout: self.callout(master, args) return master
def testGetStatus(self, Eth): tr = Eth() tr.request.return_value = bytes([0x00, 0x1d, 0xff, 0x00, 0x00]) with Master(tr) as xm: res = xm.getStatus() self.assertEqual(res.sessionConfiguration, 0) self.assertEqual(res.sessionStatus.resume, False) self.assertEqual(res.sessionStatus.daqRunning, False) self.assertEqual(res.sessionStatus.clearDaqRequest, False) self.assertEqual(res.sessionStatus.storeDaqRequest, False) self.assertEqual(res.sessionStatus.storeCalRequest, False) self.assertEqual(res.resourceProtectionStatus.pgm, True) self.assertEqual(res.resourceProtectionStatus.stim, True) self.assertEqual(res.resourceProtectionStatus.daq, True) self.assertEqual(res.resourceProtectionStatus.calpag, True)
def testSetRequest(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select ms.push_frame([0x01, 0x00, 0x00, 0x00, 0xff]) with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: res = xm.setRequest(0x15, 0x1234) mock_socket.return_value.send.assert_called_with(bytes( [0x04, 0x00, 0x00, 0x00, 0xf9, 0x15, 0x12, 0x34])) assert res == b''
def testSynch(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select ms.push_frame([0x02, 0x00, 0x00, 0x00, 0xfe, 0x00]) with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: res = xm.synch() mock_socket.return_value.send.assert_called_with(bytes( [0x01, 0x00, 0x00, 0x00, 0xfc])) assert res == b'\x00'
def testDisconnect2(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: ms.push_frame("01 00 00 00 FF") res = xm.disconnect() mock_socket.return_value.send.assert_called_with(bytes( [0x01, 0x00, 0x00, 0x00, 0xfe])) assert res == b''
def testDownloadMax(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select ms.push_frame([0x01, 0x00, 0x00, 0x00, 0xff]) with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: data = [0xCA, 0xFE, 0xBA, 0xBE] res = xm.downloadMax(*data) mock_socket.return_value.send.assert_called_with(bytes( [0x05, 0x00, 0x00, 0x00, 0xee, 0xca, 0xfe, 0xba, 0xbe])) assert res == b''
def testUserCmd(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select ms.push_frame([0x03, 0x00, 0x00, 0x00, 0xff, 0xaa, 0xbb]) with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: data = [0xbe, 0xef] res = xm.userCmd(0x55, *data) mock_socket.return_value.send.assert_called_with( bytes([0x04, 0x00, 0x00, 0x00, 0xf1, 0x55, 0xbe, 0xef])) assert res == b'\xaa\xbb'
def testGetCommModeInfo(self, eth): with Master("eth") as xm: xm.transport = eth() xm.transport.request.return_value = bytes([0x1D, 0xC0, 0xFF, 0xDC, 0x05, 0x01, 0x01]) res = xm.connect() xm.transport.request.return_value = bytes([0x00, 0x01, 0xFF, 0x02, 0x00, 0x00, 0x19]) res = xm.getCommModeInfo() assert res.commModeOptional.interleavedMode is False assert res.commModeOptional.masterBlockMode is True assert res.maxBs == 2 assert res.minSt == 0 assert res.queueSize == 0 assert res.xcpDriverVersionNumber == 25
def testGetId(self, eth): with Master("eth") as xm: xm.transport = eth() xm.transport.MAX_DATAGRAM_SIZE = 512 xm.transport.request.return_value = bytes([0x1D, 0xC0, 0xFF, 0xDC, 0x05, 0x01, 0x01]) res = xm.connect() xm.transport.request.return_value = bytes([0x00, 0x01, 0xFF, 0x06, 0x00, 0x00, 0x00]) gid = xm.getId(0x01) xm.transport.DATAGRAM_SIZE = 512 xm.transport.request.return_value = bytes([0x58, 0x43, 0x50, 0x73, 0x69, 0x6D]) res = xm.upload(gid.length) assert gid.mode == 0 assert gid.length == 6 assert res == b"XCPsim"
def testUpload(self, mock_selector, mock_socket): ms = MockSocket() mock_socket.return_value.recv.side_effect = ms.recv mock_selector.return_value.select.side_effect = ms.select ms.push_frame([ 0x09, 0x00, 0x00, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]) with Master(transport.Eth('localhost', loglevel="DEBUG")) as xm: res = xm.upload(8) mock_socket.return_value.send.assert_called_with(bytes([ 0x02, 0x00, 0x00, 0x00, 0xf5, 0x08])) assert res == b'\x01\x02\x03\x04\x05\x06\x07\x08'