Skip to content

Commit

Permalink
More tests for pn53x
Browse files Browse the repository at this point in the history
  • Loading branch information
nehpetsde committed Mar 20, 2017
1 parent 795134e commit eb11d8f
Show file tree
Hide file tree
Showing 3 changed files with 412 additions and 25 deletions.
35 changes: 29 additions & 6 deletions tests/base_clf_pn53x.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,28 @@ def HEX(s):
return bytearray.fromhex(s)


def SFRAME(hexstr):
data = HEX(hexstr)
return (bytearray([0, 0, 255, len(data), 256-len(data)]) + data +
bytearray([256 - sum(data) & 255, 0]))
def STD_FRAME(data):
LEN = bytearray([len(data)])
LCS = bytearray([256 - sum(LEN) & 255])
DCS = bytearray([256 - sum(data) & 255])
return HEX('0000ff') + LEN + LCS + data + DCS + HEX('00')


def EXT_FRAME(data):
LEN = bytearray([len(data) // 256, len(data) % 256])
LCS = bytearray([256 - sum(LEN) & 255])
DCS = bytearray([256 - sum(data) & 255])
return HEX('0000ffffff') + LEN + LCS + data + DCS + HEX('00')


def CMD(hexstr):
return SFRAME('D4' + hexstr)
data = HEX('D4' + hexstr)
return STD_FRAME(data) if len(data) < 256 else EXT_FRAME(data)


def RSP(hexstr):
return SFRAME('D5' + hexstr)
data = HEX('D5' + hexstr)
return STD_FRAME(data) if len(data) < 256 else EXT_FRAME(data)


def ACK():
Expand Down Expand Up @@ -460,3 +470,16 @@ def test_tg_get_target_status(self, chipset):
assert chipset.tg_get_target_status() == (0, 0, 0)
assert chipset.transport.read.mock_calls == 4 * [call(100), call(100)]
assert chipset.transport.write.mock_calls == 4 * [call(CMD('8A'))]


class TestDevice:
def test_sense_tta_no_target_found(self, device):
device.chipset.transport.read.side_effect = [
ACK(), RSP('4B 00'), # InListPassiveTarget
ACK(), RSP('07 26'), # ReadRegister
]
assert device.sense_tta(nfc.clf.RemoteTarget('106A')) is None
assert device.chipset.transport.write.mock_calls == [call(_) for _ in [
CMD('4A 0100'), # InListPassiveTarget
CMD('06 6339'), # ReadRegister
]]
23 changes: 11 additions & 12 deletions tests/test_clf_pn531.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ def device(self, transport):
assert isinstance(device.chipset, nfc.clf.pn531.Chipset)
assert transport.write.mock_calls == [call(_) for _ in [
CMD('00 00' + hexlify(bytearray(range(251)))), # Diagnose
CMD('02'), # GetFirmwareVersion
CMD('14 0100'), # SAMConfiguration
CMD('12 00'), # SetTAMAParameters
CMD('32 02000b0a'), # RFConfiguration
CMD('32 0400'), # RFConfiguration
CMD('32 05010001'), # RFConfiguration
CMD('32 0102'), # RFConfiguration
CMD('02'), # GetFirmwareVersion
CMD('14 0100'), # SAMConfiguration
CMD('12 00'), # SetTAMAParameters
CMD('32 02000b0a'), # RFConfiguration
CMD('32 0400'), # RFConfiguration
CMD('32 05010001'), # RFConfiguration
CMD('32 0102'), # RFConfiguration
]]
transport.write.reset_mock()
transport.read.reset_mock()
Expand All @@ -126,8 +126,7 @@ def test_sense_tta_no_target_found(self, device):
ACK(), RSP('07 26'), # ReadRegister
]
assert device.sense_tta(nfc.clf.RemoteTarget('106A')) is None
assert device.chipset.transport.write.mock_calls == [
call(CMD('4A 0100')), # InListPassiveTarget
call(CMD('06 6339')), # ReadRegister
]
pass
assert device.chipset.transport.write.mock_calls == [call(_) for _ in [
CMD('4A 0100'), # InListPassiveTarget
CMD('06 6339'), # ReadRegister
]]

0 comments on commit eb11d8f

Please sign in to comment.