Skip to content

Commit

Permalink
Add tests for dep initiator deactivate method.
Browse files Browse the repository at this point in the history
  • Loading branch information
nehpetsde committed May 18, 2017
1 parent 856fa3b commit 3011cb7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/nfc/dep.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,14 @@ def activate(self, target=None, **options):

def deactivate(self, release=True):
log.debug("deactivate {0}".format(self))
req = RLS_REQ(self.did) if release else DSL_REQ(self.did)
try:
req = RLS_REQ(self.did) if release else DSL_REQ(self.did)
res = self.send_req_recv_res(req, 0.1)
except nfc.clf.CommunicationError:
return
else:
if type(res) != (RLS_RES if release else DSL_RES):
log.error("received unexpected response for " + req.NAME)
if res.did != req.did:
log.error("target returned wrong DID in " + res.NAME)
log.error("target returned wrong DID in " + res.PDU_NAME)
finally:
log.debug("packets {0}".format(self.pcnt))

Expand Down
18 changes: 17 additions & 1 deletion tests/test_dep.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest
from pytest_mock import mocker # noqa: F401
# from mock import call
from mock import call

import logging
logging.basicConfig(level=logging.DEBUG-1)
Expand Down Expand Up @@ -133,3 +133,19 @@ def test_activate_active_target_communication_error(self, dep):
assert dep.clf.sense.call_count == 3
assert dep.activate() is None
assert dep.clf.sense.call_count == 6

@pytest.mark.parametrize("release, command, response", [
(True, 'F004D40A00', 'F004D50B00'),
(True, 'F004D40A00', 'F004D50B01'),
(False, 'F004D40800', 'F004D50900'),
(False, 'F004D40800', 'F004D50901'),
(True, 'F004D40A00', 'F004D50900'),
(False, 'F004D40800', 'F004D50B00'),
])
def test_deactivate_with_rls_or_dsl(self, dep, release, command, response):
target = nfc.clf.RemoteTarget("106A", atr_res=HEX(self.atr_res))
dep.clf.sense.return_value = target
dep.clf.exchange.side_effect = [HEX(response)]
assert dep.activate(None, did=0, brs=0) == HEX('46666D010113')
assert dep.deactivate(release) is None
assert dep.clf.exchange.mock_calls == [call(HEX(command), 0.1)]

0 comments on commit 3011cb7

Please sign in to comment.