Skip to content

Commit

Permalink
Add remaining tests for clf.
Browse files Browse the repository at this point in the history
  • Loading branch information
nehpetsde committed Apr 2, 2017
1 parent a90c6aa commit 98f61b0
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/nfc/clf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class ContactlessFrontend(object):
"""
def __init__(self, path=None):
self.device = None
self.target = None
self.lock = threading.Lock()
if path and not self.open(path):
raise IOError(errno.ENODEV, os.strerror(errno.ENODEV))
Expand Down
4 changes: 4 additions & 0 deletions tests/test_clf_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def test_init(self):
with pytest.raises(NotImplementedError):
nfc.clf.device.Device()

def test_close(self, device):
with pytest.raises(NotImplementedError):
device.close()

def test_str(self, device):
device._chipset_name = "IC"
device._path = 'usb:001:001'
Expand Down
115 changes: 103 additions & 12 deletions tests/test_clf_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
logging_level = logging.getLogger().getEffectiveLevel()
logging.getLogger("nfc.clf").setLevel(logging_level)
logging.getLogger("nfc.tag").setLevel(logging_level)
logging.getLogger("nfc.dep").setLevel(logging_level)


def HEX(s):
Expand All @@ -32,7 +33,11 @@ def device_connect(self, mocker):

@pytest.fixture() # noqa: F811
def device(self, mocker):
return mocker.Mock(spec=nfc.clf.device.Device)
device = mocker.Mock(spec=nfc.clf.device.Device)
device.path = "usb:001:001"
device.vendor_name = "Vendor"
device.product_name = "Product"
return device

@pytest.fixture() # noqa: F811
def clf(self, device_connect, device):
Expand All @@ -49,6 +54,7 @@ def clf(self, device_connect, device):
clf.device.listen_ttb.return_value = None
clf.device.listen_ttf.return_value = None
clf.device.listen_dep.return_value = None
assert str(clf) == "Vendor Product on usb:001:001"
return clf

@pytest.fixture() # noqa: F811
Expand Down Expand Up @@ -114,15 +120,6 @@ def test_connect_with_terminate_true(self, clf, terminate):
assert clf.connect(rdwr={}, terminate=terminate) is None
assert clf.connect(card={}, terminate=terminate) is None

def test_connect_llcp_initiator(self, clf, terminate):
terminate.side_effect = [False, True]
assert clf.connect(llcp={}, terminate=terminate) is None

def test_connect_rdwr_defaults(self, clf, terminate):
terminate.side_effect = [False, True]
rdwr_options = {'iterations': 1}
assert clf.connect(rdwr=rdwr_options, terminate=terminate) is None

@pytest.mark.parametrize("error", [
IOError, nfc.clf.UnsupportedTargetError, KeyboardInterrupt,
])
Expand All @@ -131,8 +128,12 @@ def test_connect_false_on_error(self, clf, error):
clf.device.sense_ttb.side_effect = error
clf.device.sense_ttf.side_effect = error
clf.device.sense_dep.side_effect = error
llcp_options = {'role': 'initiator'}
assert clf.connect(llcp=llcp_options) is False
assert clf.connect(rwdr={}, llcp={}) is False

def test_connect_rdwr_defaults(self, clf, terminate):
terminate.side_effect = [False, True]
rdwr_options = {'iterations': 1}
assert clf.connect(rdwr=rdwr_options, terminate=terminate) is None

def test_connect_rdwr_remote_is_tta_tt1(self, clf, terminate):
terminate.side_effect = [False, True]
Expand Down Expand Up @@ -307,6 +308,57 @@ def test_connect_card_with_on_discover_false(self, clf, terminate):
'on-discover': lambda tag: False}
assert clf.connect(card=card_options, terminate=terminate) is None

def test_connect_llcp_role_initiator(self, clf, terminate):
terminate.side_effect = [False, True]
target = nfc.clf.RemoteTarget('212F')
target.sensf_res = HEX('01 01fe000000000000 ffffffffffffffff')
clf.device.sense_ttf.return_value = target
clf.device.send_cmd_recv_rsp.side_effect = [
HEX('18 d501 01fe0000000000005354 0000000032 46666d010113'),
HEX('06 d507 000000'),
HEX('03 d509'),
]
llcp_options = {'role': 'initiator', 'brs': 1}
assert clf.connect(llcp=llcp_options, terminate=terminate) is True

def test_connect_llcp_role_target(self, clf, terminate):
terminate.side_effect = [False, True]
atr_req = 'd400 01fe0000000000005354 00000032 46666d010113'
atr_res = 'd501 01fe1111111111115354 0000000032 46666d010113'
target = nfc.clf.LocalTarget('212F')
target.atr_req = HEX(atr_req)
target.atr_res = HEX(atr_res)
target.dep_req = HEX('d406 000000')
clf.device.listen_dep.return_value = target
clf.device.send_rsp_recv_cmd.side_effect = [
HEX('03 d408'),
nfc.clf.TimeoutError,
]
llcp_options = {'role': 'target'}
assert clf.connect(llcp=llcp_options, terminate=terminate) is True

def test_connect_llcp_on_connect_false(self, clf, terminate):
terminate.side_effect = [False, True]
atr_req = 'd400 01fe0000000000005354 00000032 46666d010113'
atr_res = 'd501 01fe1111111111115354 0000000032 46666d010113'
target = nfc.clf.LocalTarget('212F')
target.atr_req = HEX(atr_req)
target.atr_res = HEX(atr_res)
target.dep_req = HEX('d406 000000')
clf.device.listen_dep.return_value = target
clf.device.send_rsp_recv_cmd.side_effect = [
HEX('03 d408'),
nfc.clf.TimeoutError,
]
llcp_options = {'role': 'target', 'on-connect': lambda clf: False}
llc = clf.connect(llcp=llcp_options, terminate=terminate)
assert isinstance(llc, nfc.llcp.llc.LogicalLinkController)

def test_connect_llcp_role_invalid(self, clf, terminate):
terminate.side_effect = [False, True]
llcp_options = {'role': 'invalid'}
assert clf.connect(llcp=llcp_options, terminate=terminate) is None

#
# SENSE
#
Expand Down Expand Up @@ -445,6 +497,45 @@ def test_listen_for_xxx_target(self, clf):
clf.listen(target, 1.0)
assert str(excinfo.value) == "unsupported bitrate technology type xxx"

#
# EXCHANGE
#

def test_exchange_without_device(self, clf):
clf.device = None
with pytest.raises(IOError) as excinfo:
clf.exchange(HEX(''), 1.0)
assert excinfo.value.errno == errno.ENODEV

def test_exchange_without_target(self, clf):
assert clf.exchange(HEX(''), 1.0) is None

#
# MISCELLEANEOUS
#

def test_max_send_data_size_without_device(self, clf):
clf.device = None
with pytest.raises(IOError) as excinfo:
clf.max_send_data_size()
assert excinfo.value.errno == errno.ENODEV

def test_max_recv_data_size_without_device(self, clf):
clf.device = None
with pytest.raises(IOError) as excinfo:
clf.max_recv_data_size()
assert excinfo.value.errno == errno.ENODEV

def test_format_string_without_device(self, clf):
clf.device = None
assert str(clf).startswith("<nfc.clf.ContactlessFrontend object")

def test_with_statement_enter_exit(self, clf):
device = clf.device
with clf as contactless_frontend:
assert contactless_frontend is clf
assert device.close.call_count == 1


class TestRemoteTarget(object):
@pytest.mark.parametrize("brty, send, recv, kwargs", [
Expand Down

0 comments on commit 98f61b0

Please sign in to comment.