def test_format(self, tag): tag.clf.exchange.side_effect = 13 * [ # Read block 0x7fff, 0x3fff, 0x1fff, 0x0fff, 0x07ff, 0x03ff, # 0x01ff, 0x00ff, 0x007f, 0x003f, 0x001f, 0x000f, 0x0007 fails. HEX('0c 07 0102030405060708 FFFF'), ] + 3 * [ # Read block 0x0003, 0x0005, 0x0006 succeeds. HEX('1d 07 0102030405060708 0000 01') + bytearray(16), ] + [ # number of blocks that can be read in one command HEX('1d 07 0102030405060708 0000 01') + bytearray(16), # 0 HEX('2d 07 0102030405060708 0000 02') + bytearray(32), # 0, 1 HEX('3d 07 0102030405060708 0000 03') + bytearray(48), # 0, 1, 2 HEX('0c 07 0102030405060708 FFFF'), # read 4 blocks fails ] + [ # number of blocks that can be written in one command HEX('1d 07 0102030405060708 0000 01') + bytearray(16), # 0 HEX('0c 09 0102030405060708 0000'), # write 1 block ok HEX('0c 09 0102030405060708 0000'), # write 2 blocks ok HEX('0c 09 0102030405060708 FFFF'), # write 3 blocks fail ] + [ # response to write attribute information block HEX('0c 09 0102030405060708 0000'), ] + 6 * [ # Wipe NmaxB (6) data blocks HEX('0c 09 0102030405060708 0000'), ] assert tag.format(version=0x1F, wipe=0x5A) is True tag.clf.exchange.assert_any_call( HEX('20 08 0102030405060708 010900 018000' # Ver Nbr Nbw NmaxB reserved WF RW Length Check ' 1f 03 02 0006 00000000 00 01 000000 002b'), 0.3093504) tag.clf.exchange.assert_called_with( HEX('20 08 0102030405060708 010900 018001' '5a5a5a5a 5a5a5a5a 5a5a5a5a 5a5a5a5a'), 0.3093504) # Test no data block can be read. tag.clf.exchange.reset_mock() tag.clf.exchange.side_effect = 16 * [ # Read block 0x7fff, 0x3fff, 0x1fff, 0x0fff, 0x07ff, 0x03ff, # 0x01ff, 0x00ff, 0x007f, 0x003f, 0x001f, 0x000f, 0x0007, # 0x0003, 0x0001, 0x0000 fails. HEX('0c 07 0102030405060708 FFFF'), ] assert tag.format() is False # Test invalid version number. assert tag.format(version=0xF0) is False # Test wrong system code. tag.sys = 0x0000 assert tag.format() is False
def test_format_with_one_data_block(self, tag): tag.clf.exchange.side_effect = [ # read block 0 succeeds HEX('1d 07 0102030405060708 0000 01') + bytearray(16), ] + 16 * [ # fail read all other blocks HEX('0c 07 0102030405060708 FFFF'), ] + [ # number of blocks that can be read in one command HEX('%xD 07 0102030405060708 0000 %02x' % (i, i)) + bytearray(i*16) for i in range(1, 16) ] + [ # number of blocks that can be written in one command HEX('1d 07 0102030405060708 0000 01') + bytearray(16), # 0 ] + 13 * [ HEX('0c 09 0102030405060708 0000'), # write N blocks ok ] + [ # response to write attribute information block HEX('0c 09 0102030405060708 0000'), ] assert tag.format(version=0x1F) is True tag.clf.exchange.assert_called_with(HEX( '20 08 0102030405060708 010900 018000' # Ver Nbr Nbw NmaxB reserved WF RW Length Check ' 1f 0F 0D 0000 00000000 00 01 000000 003C'), 0.3093504)
def test_format_ndef_wipe_all_data(self, tag): commands = [ (HEX('0200a4040007d276000085010100'), 0.08095339233038348), (HEX('0300a4000c02e103'), 0.08095339233038348), (HEX('0200b0000002'), 0.08095339233038348), (HEX('0300b000020d'), 0.08095339233038348), (HEX('0200a4000c02e104'), 0.08095339233038348), (HEX('0300b0000002'), 0.08095339233038348), (HEX('0200b000020e'), 0.08095339233038348), (HEX('0300d60000020000'), 0.08095339233038348), (HEX('0200d6000234a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9' 'a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9'), 0.08095339233038348), (HEX('0300d6003608a9a9a9a9a9a9a9a9'), 0.08095339233038348), ] responses = [ HEX('02 9000'), HEX('03 9000'), HEX('02 000f9000'), HEX('03 20 003b 0034 04 06 e104 0040 00 00 9000'), HEX('02 9000'), HEX('03 000e9000'), HEX('02 d1010a55036e666370792e6f7267 9000'), HEX('03 9000'), HEX('02 9000'), HEX('03 9000'), ] tag.clf.exchange.side_effect = responses assert tag.format(wipe=0xA9) is True assert tag.clf.exchange.mock_calls == [mock.call(*_) for _ in commands]
def test_format_nbr_15_and_nbw_13_not_wipe(self, tag): tag.clf.exchange.side_effect = [ # Read block 0 succeeds. HEX('1d 07 0102030405060708 0000 01') + bytearray(16), ] + 13 * [ # Read block 0x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, # 0x0200, 0x0100, 0x0080, 0x0040, 0x0020, 0x0010, 0x0008 fails. HEX('0c 07 0102030405060708 FFFF'), ] + 3 * [ # Read block 0x0004, 0x0006, 0x0007 succeeds. HEX('1d 07 0102030405060708 0000 01') + bytearray(16), ] + [ # number of blocks that can be read in one command HEX('%xD 07 0102030405060708 0000 %02x' % (i, i)) + bytearray(i*16) for i in range(1, 16) ] + [ # number of blocks that can be written in one command HEX('1d 07 0102030405060708 0000 01') + bytearray(16), # 0 ] + 13 * [ HEX('0c 09 0102030405060708 0000'), # write N blocks ok ] + [ # response to write attribute information block HEX('0c 09 0102030405060708 0000'), ] assert tag.format(version=0x1F) is True tag.clf.exchange.assert_called_with(HEX( '20 08 0102030405060708 010900 018000' # Ver Nbr Nbw NmaxB reserved WF RW Length Check ' 1f 0F 0D 0007 00000000 00 01 000000 0043'), 0.3093504)
def test_format_with_one_data_block(self, tag): tag.clf.exchange.side_effect = [ # read block 0 succeeds HEX('1d 07 0102030405060708 0000 01') + bytearray(16), ] + 16 * [ # fail read all other blocks HEX('0c 07 0102030405060708 FFFF'), ] + [ # number of blocks that can be read in one command HEX('%xD 07 0102030405060708 0000 %02x' % (i, i)) + bytearray(i * 16) for i in range(1, 16) ] + [ # number of blocks that can be written in one command HEX('1d 07 0102030405060708 0000 01') + bytearray(16), # 0 ] + 13 * [ HEX('0c 09 0102030405060708 0000'), # write N blocks ok ] + [ # response to write attribute information block HEX('0c 09 0102030405060708 0000'), ] assert tag.format(version=0x1F) is True tag.clf.exchange.assert_called_with( HEX('20 08 0102030405060708 010900 018000' # Ver Nbr Nbw NmaxB reserved WF RW Length Check ' 1f 0F 0D 0000 00000000 00 01 000000 003C'), 0.3093504)
def test_format_with_wipe_all_zero(self, tag): tag.clf.exchange.side_effect = [ # Responses tag.target.rid_res[:2] + self.mmap[:120], # RALL HEX("0F") + self.mmap[120:128], # READ8(15) HEX("10") + self.mmap[128:256], # RSEG(1) HEX("20") + self.mmap[256:384], # RSEG(2) HEX("30") + self.mmap[384:512], # RSEG(3) HEX("54 330203f002030300"), # WRITE-E8(2) ] + [ HEX("54 0000000000000000") for _ in range(3, 38) ] assert tag.format(wipe=0) is True assert tag.clf.exchange.mock_calls == [ mock.call(HEX("00 00 00 01020304"), 0.1), mock.call(HEX("02 0f 0000000000000000 01020304"), 0.1), mock.call(HEX("10 10 0000000000000000 01020304"), 0.1), mock.call(HEX("10 20 0000000000000000 01020304"), 0.1), mock.call(HEX("10 30 0000000000000000 01020304"), 0.1), mock.call(HEX("54 02 330203f002030300 01020304"), 0.1), ] + [ mock.call(HEX("54 %02x 0000000000000000 01020304" % b), 0.1) for b in range(3, 13) ] + [ mock.call(HEX("54 %02x 0000000000000000 01020304" % b), 0.1) for b in range(16, 38) ]
def test_format_nbr_15_and_nbw_13_not_wipe(self, tag): tag.clf.exchange.side_effect = [ # Read block 0 succeeds. HEX('1d 07 0102030405060708 0000 01') + bytearray(16), ] + 13 * [ # Read block 0x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, # 0x0200, 0x0100, 0x0080, 0x0040, 0x0020, 0x0010, 0x0008 fails. HEX('0c 07 0102030405060708 FFFF'), ] + 3 * [ # Read block 0x0004, 0x0006, 0x0007 succeeds. HEX('1d 07 0102030405060708 0000 01') + bytearray(16), ] + [ # number of blocks that can be read in one command HEX('%xD 07 0102030405060708 0000 %02x' % (i, i)) + bytearray(i * 16) for i in range(1, 16) ] + [ # number of blocks that can be written in one command HEX('1d 07 0102030405060708 0000 01') + bytearray(16), # 0 ] + 13 * [ HEX('0c 09 0102030405060708 0000'), # write N blocks ok ] + [ # response to write attribute information block HEX('0c 09 0102030405060708 0000'), ] assert tag.format(version=0x1F) is True tag.clf.exchange.assert_called_with( HEX('20 08 0102030405060708 010900 018000' # Ver Nbr Nbw NmaxB reserved WF RW Length Check ' 1f 0F 0D 0007 00000000 00 01 000000 0043'), 0.3093504)
def test_format_with_wipe_all_zero(self, tag): tag.clf.exchange.side_effect = [ # Responses tag.target.rid_res[:2] + self.mmap[:120], # RALL HEX("0F") + self.mmap[120:128], # READ8(15) HEX("10") + self.mmap[128:256], # RSEG(1) HEX("20") + self.mmap[256:384], # RSEG(2) HEX("30") + self.mmap[384:512], # RSEG(3) HEX("54 330203f002030300"), # WRITE-E8(2) ] + [ HEX("54 0000000000000000") for _ in range(3, 38) ] assert tag.format(wipe=0) is True assert tag.clf.exchange.mock_calls == [ call(HEX("00 00 00 01020304"), 0.1), call(HEX("02 0f 0000000000000000 01020304"), 0.1), call(HEX("10 10 0000000000000000 01020304"), 0.1), call(HEX("10 20 0000000000000000 01020304"), 0.1), call(HEX("10 30 0000000000000000 01020304"), 0.1), call(HEX("54 02 330203f002030300 01020304"), 0.1), ] + [ call(HEX("54 %02x 0000000000000000 01020304" % b), 0.1) for b in range(3, 13) ] + [ call(HEX("54 %02x 0000000000000000 01020304" % b), 0.1) for b in range(16, 38) ]
def test_format_with_wipe_all_zero(self, tag): tag.clf.exchange.side_effect = [ tag.target.rid_res[:2] + self.mmap[:120], # RALL ] + [bytearray([addr, 0]) for addr in range(13, 57)] # WRITE-E assert tag.format(wipe=0) is True commands = [mock.call(HEX("00 00 00 01020304"), 0.1)] for addr in range(13, 57): cmd = bytearray([0x53, addr, 0, 1, 2, 3, 4]) commands.append(mock.call(cmd, 0.1)) assert tag.clf.exchange.mock_calls == commands
def test_format_blank_tag(self, tag): commands = [ (HEX('30 00'), 0.005), ] responses = [ HEX("01020304 05060708 00000000 00000000"), ] tag.clf.exchange.side_effect = responses assert tag.format() is False assert tag.clf.exchange.mock_calls == [call(*_) for _ in commands]
def test_format_blank_tag(self, tag): commands = [ (HEX('30 00'), 0.005), ] responses = [ HEX("01020304 05060708 00000000 00000000"), ] tag.clf.exchange.side_effect = responses assert tag.format() is False assert tag.clf.exchange.mock_calls == [mock.call(*_) for _ in commands]
def test_format_with_wipe_all_zero(self, tag): tag.clf.exchange.side_effect = [ tag.target.rid_res[:2] + self.mmap[:120], # RALL ] + [bytearray([addr, 0]) for addr in range(13, 57)] # WRITE-E assert tag.format(wipe=0) is True commands = [call(HEX("00 00 00 01020304"), 0.1)] for addr in range(13, 57): cmd = bytearray([0x53, addr, 0, 1, 2, 3, 4]) commands.append(call(cmd, 0.1)) assert tag.clf.exchange.mock_calls == commands
def test_format_with_version_one_dot_two(self, tag): tag.clf.exchange.side_effect = [ tag.target.rid_res[:2] + self.mmap[:120], # RALL HEX("09 12"), # WRITE-E HEX("0d 00"), # WRITE-E ] assert tag.format(version=0x12) is True assert tag.clf.exchange.mock_calls == [ mock.call(HEX("00 00 00 01020304"), 0.1), mock.call(HEX("53 09 12 01020304"), 0.1), mock.call(HEX("53 0d 00 01020304"), 0.1), ]
def test_format_with_version_one_dot_two(self, tag): tag.clf.exchange.side_effect = [ tag.target.rid_res[:2] + self.mmap[:120], # RALL HEX("09 12"), # WRITE-E HEX("0d 00"), # WRITE-E ] assert tag.format(version=0x12) is True assert tag.clf.exchange.mock_calls == [ call(HEX("00 00 00 01020304"), 0.1), call(HEX("53 09 12 01020304"), 0.1), call(HEX("53 0d 00 01020304"), 0.1), ]
def test_format_with_version_one_dot_two(self, tag): tag.clf.exchange.side_effect = [ tag.target.rid_res[:2] + self.mmap[:120], HEX("54 e1123f000103f230"), HEX("54 330203f002030300"), nfc.clf.TimeoutError, nfc.clf.TimeoutError, nfc.clf.TimeoutError ] assert tag.format(version=0x12) is True print(tag.clf.exchange.mock_calls) assert tag.clf.exchange.mock_calls == [ call(HEX("00 00 00 01020304"), 0.1), call(HEX("54 01 e1123f000103f230 01020304"), 0.1), call(HEX("54 02 330203f002030300 01020304"), 0.1), ]
def test_format_default(self, tag): commands = [ (HEX('30 00'), 0.005), (HEX('30 04'), 0.005), (HEX('a2 04 0300fe00'), 0.1), ] responses = [ HEX("01020304 05060708 00000000 E1100100"), HEX("0305d500 023132fe 00000000 00000000"), HEX('0a'), ] tag.clf.exchange.side_effect = responses assert tag.format() is True assert tag.clf.exchange.mock_calls == [mock.call(*_) for _ in commands]
def test_format_with_version_one_dot_two(self, tag): tag.clf.exchange.side_effect = [ tag.target.rid_res[:2] + self.mmap[:120], HEX("54 e1123f000103f230"), HEX("54 330203f002030300"), nfc.clf.TimeoutError, nfc.clf.TimeoutError, nfc.clf.TimeoutError ] assert tag.format(version=0x12) is True print(tag.clf.exchange.mock_calls) assert tag.clf.exchange.mock_calls == [ mock.call(HEX("00 00 00 01020304"), 0.1), mock.call(HEX("54 01 e1123f000103f230 01020304"), 0.1), mock.call(HEX("54 02 330203f002030300 01020304"), 0.1), ]
def test_format_default(self, tag): commands = [ (HEX('30 00'), 0.005), (HEX('30 04'), 0.005), (HEX('a2 04 0300fe00'), 0.1), ] responses = [ HEX("01020304 05060708 00000000 E1100100"), HEX("0305d500 023132fe 00000000 00000000"), HEX('0a'), ] tag.clf.exchange.side_effect = responses assert tag.format() is True assert tag.clf.exchange.mock_calls == [call(*_) for _ in commands]
def test_format_with_wipe(self, tag): commands = [ (HEX('30 00'), 0.005), (HEX('30 04'), 0.005), (HEX('a2 05 020300fe'), 0.1), (HEX('a2 06 000000ff'), 0.1), (HEX('a2 07 ff000000'), 0.1), ] responses = [ HEX("01020304 05060708 00000000 E1100200"), HEX("02036302 020305d5 000231ff ff320000"), HEX('0a'), HEX('0a'), HEX('0a'), ] tag.clf.exchange.side_effect = responses assert tag.ndef is not None assert tag.ndef.octets == HEX('d500023132') assert tag.format(wipe=0) is True assert tag.clf.exchange.mock_calls == [mock.call(*_) for _ in commands]
def test_format_nbr_3_and_nbw_2_and_wipe(self, tag): tag.clf.exchange.side_effect = [ # Read block 0 succeeds. HEX('1d 07 0102030405060708 0000 01') + bytearray(16), ] + 13 * [ # Read block 0x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, # 0x0200, 0x0100, 0x0080, 0x0040, 0x0020, 0x0010, 0x0008 fails. HEX('0c 07 0102030405060708 FFFF'), ] + 3 * [ # Read block 0x0004, 0x0006, 0x0007 succeeds. HEX('1d 07 0102030405060708 0000 01') + bytearray(16), ] + [ # number of blocks that can be read in one command HEX('1d 07 0102030405060708 0000 01') + bytearray(16), # 0 HEX('2d 07 0102030405060708 0000 02') + bytearray(32), # 0, 1 HEX('3d 07 0102030405060708 0000 03') + bytearray(48), # 0, 1, 2 HEX('0c 07 0102030405060708 FFFF'), # read 4 blocks fails ] + [ # number of blocks that can be written in one command HEX('1d 07 0102030405060708 0000 01') + bytearray(16), # 0 HEX('0c 09 0102030405060708 0000'), # write 1 block ok HEX('0c 09 0102030405060708 0000'), # write 2 blocks ok HEX('0c 09 0102030405060708 FFFF'), # write 3 blocks fail ] + [ # response to write attribute information block HEX('0c 09 0102030405060708 0000'), ] + 7 * [ # Wipe NmaxB (7) data blocks HEX('0c 09 0102030405060708 0000'), ] assert tag.format(version=0x1F, wipe=0x5A) is True tag.clf.exchange.assert_any_call( HEX('20 08 0102030405060708 010900 018000' # Ver Nbr Nbw NmaxB reserved WF RW Length Check ' 1f 03 02 0007 00000000 00 01 000000 002c'), 0.3093504) tag.clf.exchange.assert_called_with( HEX('20 08 0102030405060708 010900 018001' '5a5a5a5a 5a5a5a5a 5a5a5a5a 5a5a5a5a'), 0.3093504)
def test_format_nbr_3_and_nbw_2_and_wipe(self, tag): tag.clf.exchange.side_effect = [ # Read block 0 succeeds. HEX('1d 07 0102030405060708 0000 01') + bytearray(16), ] + 13 * [ # Read block 0x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, # 0x0200, 0x0100, 0x0080, 0x0040, 0x0020, 0x0010, 0x0008 fails. HEX('0c 07 0102030405060708 FFFF'), ] + 3 * [ # Read block 0x0004, 0x0006, 0x0007 succeeds. HEX('1d 07 0102030405060708 0000 01') + bytearray(16), ] + [ # number of blocks that can be read in one command HEX('1d 07 0102030405060708 0000 01') + bytearray(16), # 0 HEX('2d 07 0102030405060708 0000 02') + bytearray(32), # 0, 1 HEX('3d 07 0102030405060708 0000 03') + bytearray(48), # 0, 1, 2 HEX('0c 07 0102030405060708 FFFF'), # read 4 blocks fails ] + [ # number of blocks that can be written in one command HEX('1d 07 0102030405060708 0000 01') + bytearray(16), # 0 HEX('0c 09 0102030405060708 0000'), # write 1 block ok HEX('0c 09 0102030405060708 0000'), # write 2 blocks ok HEX('0c 09 0102030405060708 FFFF'), # write 3 blocks fail ] + [ # response to write attribute information block HEX('0c 09 0102030405060708 0000'), ] + 7 * [ # Wipe NmaxB (7) data blocks HEX('0c 09 0102030405060708 0000'), ] assert tag.format(version=0x1F, wipe=0x5A) is True tag.clf.exchange.assert_any_call(HEX( '20 08 0102030405060708 010900 018000' # Ver Nbr Nbw NmaxB reserved WF RW Length Check ' 1f 03 02 0007 00000000 00 01 000000 002c'), 0.3093504) tag.clf.exchange.assert_called_with(HEX( '20 08 0102030405060708 010900 018001' '5a5a5a5a 5a5a5a5a 5a5a5a5a 5a5a5a5a'), 0.3093504)
def test_format_invalid_version_number(self, tag): tag.clf.exchange.side_effect = [ tag.target.rid_res[:2] + self.mmap[:120], # RALL ] assert tag.format(version=0xFF) is False
def test_format_with_zero_data_blocks(self, tag): tag.clf.exchange.side_effect = [ # read block 0 fails HEX('0c 07 0102030405060708 FFFF'), ] assert tag.format() is False
def test_format_wrong_system_code(self, tag): tag.sys = 0x0000 assert tag.format() is False
def test_format_invalid_version_number(self, tag): assert tag.format(version=0xF0) is False
def test_format_tag(self, tag): assert tag.format() is None