How to use side_effect method in uiautomator

Best Python code snippet using uiautomator

test_clf_udp.py

Source:test_clf_udp.py Github

copy

Full Screen

1# -*- coding: latin-1 -*-2from __future__ import absolute_import, division3import nfc4import nfc.clf5import nfc.clf.udp6import pytest7from pytest_mock import mocker # noqa: F4018from mock import call9import logging10logging.basicConfig(level=logging.DEBUG-1)11logging_level = logging.getLogger().getEffectiveLevel()12logging.getLogger("nfc.clf").setLevel(logging_level)13logging.getLogger("nfc.clf.udp").setLevel(logging_level)14def HEX(s):15 return bytearray.fromhex(s)16def FRAME(brty, hexstr):17 hexstr = hexstr.replace(' ', '')18 return ('{:s} {:s}'.format(brty, hexstr), ('127.0.0.1', 54321))19def CMD106A(hexstr):20 return FRAME('106A', hexstr)21def RSP106A(hexstr):22 return FRAME('106A', hexstr)23def CMD106B(hexstr):24 return FRAME('106B', hexstr)25def RSP106B(hexstr):26 return FRAME('106B', hexstr)27def CMD212F(hexstr):28 return FRAME('212F', hexstr)29def RSP212F(hexstr):30 return FRAME('212F', hexstr)31def CMD424F(hexstr):32 return FRAME('424F', hexstr)33def RSP424F(hexstr):34 return FRAME('424F', hexstr)35def CMD_CALLS(exchange):36 return [call(*cmd) for cmd, rsp in exchange]37def RSP_CALLS(exchange):38 return [call(*rsp) for cmd, rsp in exchange]39def CMD_SIZES(exchange):40 return [len(cmd[0]) for cmd, rsp in exchange]41def RSP_SIZES(exchange):42 return [len(rsp[0]) for cmd, rsp in exchange]43@pytest.fixture() # noqa: F81144def device(mocker):45 nameinfo = ('127.0.0.1', '54321')46 mocker.patch('nfc.clf.udp.select.select').return_value = ([1], [], [])47 mocker.patch('nfc.clf.udp.socket.getnameinfo').return_value = nameinfo48 mocker.patch('nfc.clf.udp.socket.socket')49 device = nfc.clf.udp.Device('localhost', 54321)50 assert device.addr == ('127.0.0.1', 54321)51 device._device_name = "IP-Stack"52 device._chipset_name = "UDP"53 yield device54 device.close()55def test_init(mocker): # noqa: F81156 nameinfo = ('127.0.0.1', '54321')57 mocker.patch('nfc.clf.udp.select.select').return_value = ([1], [], [])58 mocker.patch('nfc.clf.udp.socket.getnameinfo').return_value = nameinfo59 mocker.patch('nfc.clf.udp.socket.socket')60 device = nfc.clf.udp.init('localhost', 54321)61 assert isinstance(device, nfc.clf.udp.Device)62class TestDevice(object):63 def test_init(self, device):64 pass65 def test_close(self, device):66 assert device.close() is None67 #68 # SENSE69 #70 def test_sense_tta_with_no_target_found(self, device):71 device.socket.sendto.side_effect = [len(CMD106A('26')[0])]72 device.socket.recvfrom.side_effect = [nfc.clf.TimeoutError]73 assert device.sense_tta(nfc.clf.RemoteTarget('106A')) is None74 device.socket.sendto.assert_called_once_with(*CMD106A('26'))75 def test_sense_tta_with_tt1_target_found(self, device):76 exchange = [77 (CMD106A('26'), RSP106A('000C')),78 (CMD106A('78000000000000'), RSP106A('110001020304')),79 ]80 device.socket.sendto.side_effect = CMD_SIZES(exchange)81 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]82 target = device.sense_tta(nfc.clf.RemoteTarget('106A'))83 assert isinstance(target, nfc.clf.RemoteTarget)84 assert target.brty == '106A'85 assert target.rid_res == HEX('110001020304')86 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)87 return target88 def test_sense_tta_with_proprietary_target(self, device):89 exchange = [90 (CMD106A('26'), RSP106A('0000')),91 ]92 device.socket.sendto.side_effect = CMD_SIZES(exchange)93 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]94 target = device.sense_tta(nfc.clf.RemoteTarget('106A'))95 assert isinstance(target, nfc.clf.RemoteTarget)96 assert target.brty == "106A"97 assert target.sens_res == HEX('0000')98 assert target.rid_res is None99 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)100 def test_sense_tta_find_tt1_but_receive_error(self, device):101 exchange = [102 (CMD106A('26'), RSP106A('000C')),103 (CMD106A('78000000000000'), RSP106A('')),104 ]105 device.socket.sendto.side_effect = CMD_SIZES(exchange)106 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]107 assert device.sense_tta(nfc.clf.RemoteTarget('106A')) is None108 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)109 def test_sense_tta_find_tt2_target_uid_4(self, device):110 exchange = [111 (CMD106A('26'), RSP106A('4400')),112 (CMD106A('9320'), RSP106A('01020304')),113 (CMD106A('937001020304'), RSP106A('00')),114 ]115 device.socket.sendto.side_effect = CMD_SIZES(exchange)116 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]117 target = device.sense_tta(nfc.clf.RemoteTarget('106A'))118 assert isinstance(target, nfc.clf.RemoteTarget)119 assert target.brty == '106A'120 assert target.sens_res == HEX('4400')121 assert target.sdd_res == HEX('01020304')122 assert target.sel_res == HEX('00')123 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)124 def test_sense_tta_find_tt2_target_uid_7(self, device):125 exchange = [126 (CMD106A('26'), RSP106A('4400')),127 (CMD106A('9320'), RSP106A('88010203')),128 (CMD106A('937088010203'), RSP106A('04')),129 (CMD106A('9520'), RSP106A('04050607')),130 (CMD106A('957004050607'), RSP106A('00')),131 ]132 device.socket.sendto.side_effect = CMD_SIZES(exchange)133 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]134 target = device.sense_tta(nfc.clf.RemoteTarget('106A'))135 assert isinstance(target, nfc.clf.RemoteTarget)136 assert target.brty == "106A"137 assert target.sens_res == HEX('4400')138 assert target.sdd_res == HEX('01020304050607')139 assert target.sel_res == HEX('00')140 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)141 def test_sense_tta_find_tt2_target_uid_10(self, device):142 exchange = [143 (CMD106A('26'), RSP106A('4400')),144 (CMD106A('9320'), RSP106A('88010203')),145 (CMD106A('937088010203'), RSP106A('04')),146 (CMD106A('9520'), RSP106A('88040506')),147 (CMD106A('957088040506'), RSP106A('04')),148 (CMD106A('9720'), RSP106A('07080910')),149 (CMD106A('977007080910'), RSP106A('00')),150 ]151 device.socket.sendto.side_effect = CMD_SIZES(exchange)152 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]153 target = device.sense_tta(nfc.clf.RemoteTarget('106A'))154 assert isinstance(target, nfc.clf.RemoteTarget)155 assert target.brty == "106A"156 assert target.sens_res == HEX('4400')157 assert target.sdd_res == HEX('01020304050607080910')158 assert target.sel_res == HEX('00')159 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)160 def test_sense_tta_find_tt2_excessive_uid(self, device):161 exchange = [162 (CMD106A('26'), RSP106A('4400')),163 (CMD106A('9320'), RSP106A('88010203')),164 (CMD106A('937088010203'), RSP106A('04')),165 (CMD106A('9520'), RSP106A('88040506')),166 (CMD106A('957088040506'), RSP106A('04')),167 (CMD106A('9720'), RSP106A('07080910')),168 (CMD106A('977007080910'), RSP106A('04')),169 ]170 device.socket.sendto.side_effect = CMD_SIZES(exchange)171 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]172 assert device.sense_tta(nfc.clf.RemoteTarget('106A')) is None173 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)174 def test_sense_tta_tt2_request_uid_4(self, device):175 exchange = [176 (CMD106A('26'), RSP106A('4400')),177 (CMD106A('93700102030404'), RSP106A('00')),178 ]179 uid = '01020304'180 device.socket.sendto.side_effect = CMD_SIZES(exchange)181 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]182 target = nfc.clf.RemoteTarget('106A', sel_req=HEX(uid))183 target = device.sense_tta(target)184 assert isinstance(target, nfc.clf.RemoteTarget)185 assert target.brty == "106A"186 assert target.sens_res == HEX('4400')187 assert target.sdd_res == HEX(uid)188 assert target.sel_res == HEX('00')189 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)190 def test_sense_tta_tt2_request_uid_7(self, device):191 exchange = [192 (CMD106A('26'), RSP106A('4400')),193 (CMD106A('93708801020388'), RSP106A('04')),194 (CMD106A('95700405060700'), RSP106A('00')),195 ]196 uid = '01020304050607'197 device.socket.sendto.side_effect = CMD_SIZES(exchange)198 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]199 target = nfc.clf.RemoteTarget('106A', sel_req=HEX(uid))200 target = device.sense_tta(target)201 assert isinstance(target, nfc.clf.RemoteTarget)202 assert target.brty == "106A"203 assert target.sens_res == HEX('4400')204 assert target.sdd_res == HEX(uid)205 assert target.sel_res == HEX('00')206 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)207 def test_sense_tta_tt2_request_uid_10(self, device):208 exchange = [209 (CMD106A('26'), RSP106A('4400')),210 (CMD106A('93708801020388'), RSP106A('04')),211 (CMD106A('9570880405068f'), RSP106A('04')),212 (CMD106A('97700708091016'), RSP106A('00')),213 ]214 uid = '01020304050607080910'215 device.socket.sendto.side_effect = CMD_SIZES(exchange)216 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]217 target = nfc.clf.RemoteTarget('106A', sel_req=HEX(uid))218 target = device.sense_tta(target)219 assert isinstance(target, nfc.clf.RemoteTarget)220 assert target.brty == "106A"221 assert target.sens_res == HEX('4400')222 assert target.sdd_res == HEX(uid)223 assert target.sel_res == HEX('00')224 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)225 def test_sense_tta_find_tt2_but_receive_error(self, device):226 exchange = [227 (CMD106A('26'), RSP106A('4400')),228 (CMD106A('9320'), RSP106A('')),229 ]230 device.socket.sendto.side_effect = CMD_SIZES(exchange)231 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]232 assert device.sense_tta(nfc.clf.RemoteTarget('106A')) is None233 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)234 def test_sense_tta_with_invalid_target(self, device):235 with pytest.raises(nfc.clf.UnsupportedTargetError) as excinfo:236 device.sense_tta(nfc.clf.RemoteTarget('106B'))237 assert str(excinfo.value) == "unsupported bitrate 106B"238 def test_sense_ttb_with_no_target_found(self, device):239 device.socket.sendto.side_effect = [len(CMD106B('050010')[0])]240 device.socket.recvfrom.side_effect = [nfc.clf.TimeoutError]241 assert device.sense_ttb(nfc.clf.RemoteTarget('106B')) is None242 device.socket.sendto.assert_called_once_with(*CMD106B('050010'))243 def test_sense_ttb_with_tt4_target_found(self, device):244 sensb_res = '50E8253EEC00000011008185'245 exchange = [(CMD106B('050010'), RSP106B(sensb_res))]246 device.socket.sendto.side_effect = CMD_SIZES(exchange)247 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]248 target = device.sense_ttb(nfc.clf.RemoteTarget('106B'))249 assert isinstance(target, nfc.clf.RemoteTarget)250 assert target.sensb_res == HEX(sensb_res)251 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)252 def test_sense_ttb_with_receive_errors(self, device):253 exchange = [(CMD106B('050010'), RSP106B(''))]254 device.socket.sendto.side_effect = CMD_SIZES(exchange)255 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]256 assert device.sense_ttb(nfc.clf.RemoteTarget('106B')) is None257 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)258 @pytest.mark.parametrize('sensb_res', [259 '51E8253EEC00000011008185', '50E8253EEC000000110081',260 ])261 def test_sense_ttb_with_response_errors(self, device, sensb_res):262 exchange = [(CMD106B('050010'), RSP106B(sensb_res))]263 device.socket.sendto.side_effect = CMD_SIZES(exchange)264 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]265 assert device.sense_ttb(nfc.clf.RemoteTarget('106B')) is None266 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)267 def test_sense_ttb_with_invalid_target(self, device):268 with pytest.raises(nfc.clf.UnsupportedTargetError) as excinfo:269 device.sense_ttb(nfc.clf.RemoteTarget('106A'))270 assert str(excinfo.value) == "unsupported bitrate 106A"271 def test_sense_ttf_with_no_target_found(self, device):272 device.socket.sendto.side_effect = [len(CMD212F('0600ffff0100')[0])]273 device.socket.recvfrom.side_effect = [nfc.clf.TimeoutError]274 assert device.sense_ttf(nfc.clf.RemoteTarget('212F')) is None275 device.socket.sendto.assert_called_once_with(*CMD212F('0600ffff0100'))276 def test_sense_ttf_with_tt3_target_found(self, device):277 sensf_res = '14 01 01010701260cca02 0f0d23042f7783ff 12fc'278 exchange = [(CMD212F('0600ffff0100'), RSP212F(sensf_res))]279 device.socket.sendto.side_effect = CMD_SIZES(exchange)280 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]281 target = device.sense_ttf(nfc.clf.RemoteTarget('212F'))282 assert isinstance(target, nfc.clf.RemoteTarget)283 assert target.brty == '212F'284 assert target.sensf_res == HEX(sensf_res)[1:]285 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)286 @pytest.mark.parametrize("tg, sensf_req, sensf_res", [287 (nfc.clf.RemoteTarget('212F', sensf_req=None),288 '0600ffff0100', '140101010701260cca020f0d23042f7783ff12fc'),289 (nfc.clf.RemoteTarget('212F', sensf_req=HEX('00ffff0100')),290 '0600ffff0100', '140101010701260cca020f0d23042f7783ff12fc'),291 (nfc.clf.RemoteTarget('212F', sensf_req=HEX('00ffff0000')),292 '0600ffff0000', '120101010701260cca020f0d23042f7783ff'),293 ])294 def test_sense_ttf_with_sensf_req(self, device, tg, sensf_req, sensf_res):295 exchange = [(CMD212F(sensf_req), RSP212F(sensf_res))]296 device.socket.sendto.side_effect = CMD_SIZES(exchange)297 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]298 target = device.sense_ttf(tg)299 assert isinstance(target, nfc.clf.RemoteTarget)300 assert target.brty == tg.brty301 assert target.sensf_res == HEX(sensf_res)[1:]302 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)303 @pytest.mark.parametrize("brty, rf_settings", [304 ('212F', '01010f01'),305 ('424F', '01020f02'),306 ])307 def test_sense_ttf_with_bitrate_type(self, device, brty, rf_settings):308 sensf_res = '14 01 01010701260cca020f0d23042f7783ff12fc'309 exchange = [(FRAME(brty, '0600ffff0100'), FRAME(brty, sensf_res))]310 device.socket.sendto.side_effect = CMD_SIZES(exchange)311 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]312 target = device.sense_ttf(nfc.clf.RemoteTarget(brty))313 assert isinstance(target, nfc.clf.RemoteTarget)314 assert target.brty == brty315 assert target.sensf_res == HEX(sensf_res)[1:]316 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)317 def test_sense_ttf_with_receive_errors(self, device):318 exchange = [(CMD212F('0600ffff0100'), RSP212F(''))]319 device.socket.sendto.side_effect = CMD_SIZES(exchange)320 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]321 assert device.sense_ttf(nfc.clf.RemoteTarget('212F')) is None322 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)323 @pytest.mark.parametrize("sensf_res", [324 '110101010701260cca020f0d23042f7783',325 '130101010701260cca020f0d23042f7783ff12fc',326 '140201010701260cca020f0d23042f7783ff12fc',327 ])328 def test_sense_ttf_with_response_errors(self, device, sensf_res):329 exchange = [(CMD212F('0600ffff0100'), RSP212F(sensf_res))]330 device.socket.sendto.side_effect = CMD_SIZES(exchange)331 device.socket.recvfrom.side_effect = [rsp for cmd, rsp in exchange]332 assert device.sense_ttf(nfc.clf.RemoteTarget('212F')) is None333 assert device.socket.sendto.mock_calls == CMD_CALLS(exchange)334 def test_sense_ttf_with_invalid_target(self, device):335 with pytest.raises(nfc.clf.UnsupportedTargetError) as excinfo:336 device.sense_ttf(nfc.clf.RemoteTarget('106A'))337 assert str(excinfo.value) == "unsupported bitrate 106A"338 def test_sense_dep_is_not_supported(self, device):339 with pytest.raises(nfc.clf.UnsupportedTargetError) as excinfo:340 device.sense_dep(nfc.clf.RemoteTarget('106A'))341 assert str(excinfo.value) == (342 "IP-Stack UDP at 127.0.0.1:54321 does not "343 "support sense for active DEP Target")344 #345 # LISTEN346 #347 def test_listen_tta_tt2_uid4_activated(self, device):348 exchange = [349 (CMD106A('26'), RSP106A('0400')),350 (CMD106A('9320'), RSP106A('3132333404')),351 (CMD106A('93703132333404'), RSP106A('00')),352 (CMD106A('3000'), RSP106A('')),353 ]354 device.socket.sendto.side_effect = RSP_SIZES(exchange[:-1])355 device.socket.recvfrom.side_effect = [cmd for cmd, rsp in exchange]356 target = nfc.clf.LocalTarget('106A')357 target.sens_res = HEX("0400")358 target.sel_res = HEX("00")359 target.sdd_res = HEX("31323334")360 target = device.listen_tta(target, 1.0)361 assert isinstance(target, nfc.clf.LocalTarget)362 assert target.brty == '106A'363 assert target.tt2_cmd == HEX('3000')364 assert device.socket.sendto.mock_calls == RSP_CALLS(exchange[:-1])365 return target366 def test_listen_tta_tt2_uid7_activated(self, device):367 exchange = [368 (CMD106A('26'), RSP106A('4400')),369 (CMD106A('9320'), RSP106A('88313233b8')),370 (CMD106A('937088313233b8'), RSP106A('04')),371 (CMD106A('9520'), RSP106A('3435363700')),372 (CMD106A('95703435363700'), RSP106A('00')),373 (CMD106A('3000'), RSP106A('')),374 ]375 device.socket.sendto.side_effect = RSP_SIZES(exchange[:-1])376 device.socket.recvfrom.side_effect = [cmd for cmd, rsp in exchange]377 target = nfc.clf.LocalTarget('106A')378 target.sens_res = HEX("4400")379 target.sel_res = HEX("00")380 target.sdd_res = HEX("31323334353637")381 target = device.listen_tta(target, 1.0)382 assert isinstance(target, nfc.clf.LocalTarget)383 assert target.brty == '106A'384 assert target.tt2_cmd == HEX('3000')385 assert device.socket.sendto.mock_calls == RSP_CALLS(exchange[:-1])386 def test_listen_tta_tt2_uid10_activated(self, device):387 exchange = [388 (CMD106A('26'), RSP106A('4400')),389 (CMD106A('9320'), RSP106A('88313233b8')),390 (CMD106A('937088313233b8'), RSP106A('04')),391 (CMD106A('9520'), RSP106A('88343536bf')),392 (CMD106A('957088343536bf'), RSP106A('04')),393 (CMD106A('9720'), RSP106A('3738393006')),394 (CMD106A('95703738393006'), RSP106A('00')),395 (CMD106A('3000'), RSP106A('')),396 ]397 device.socket.sendto.side_effect = RSP_SIZES(exchange[:-1])398 device.socket.recvfrom.side_effect = [cmd for cmd, rsp in exchange]399 target = nfc.clf.LocalTarget('106A')400 target.sens_res = HEX("4400")401 target.sel_res = HEX("00")402 target.sdd_res = HEX("31323334353637383930")403 target = device.listen_tta(target, 1.0)404 assert isinstance(target, nfc.clf.LocalTarget)405 assert target.brty == '106A'406 assert target.tt2_cmd == HEX('3000')407 assert device.socket.sendto.mock_calls == RSP_CALLS(exchange[:-1])408 def test_listen_tta_tt2_excessive_uid(self, device):409 exchange = [410 (CMD106A('26'), RSP106A('4400')),411 (CMD106A('9320'), RSP106A('88313233b8')),412 (CMD106A('937088313233b8'), RSP106A('04')),413 (CMD106A('9520'), RSP106A('88343536bf')),414 (CMD106A('957088343536bf'), RSP106A('04')),415 (CMD106A('9720'), RSP106A('3738393006')),416 (CMD106A('95703738393006'), RSP106A('04')),417 (CMD106A('3000'), RSP106A('')),418 ]419 device.socket.sendto.side_effect = RSP_SIZES(exchange)420 device.socket.recvfrom.side_effect = [421 cmd for cmd, rsp in exchange] + [nfc.clf.TimeoutError]422 target = nfc.clf.LocalTarget('106A')423 target.sens_res = HEX("4400")424 target.sel_res = HEX("00")425 target.sdd_res = HEX("3132333435363738393031")426 assert device.listen_tta(target, 1.0) is None427 assert device.socket.sendto.mock_calls == RSP_CALLS(exchange[:-1])428 def test_listen_tta_tt4_activated(self, device):429 exchange = [430 (CMD106A('26'), RSP106A('0400')),431 (CMD106A('9320'), RSP106A('3132333404')),432 (CMD106A('93703132333404'), RSP106A('00')),433 (CMD106A('E000'), RSP106A('')),434 ]435 device.socket.sendto.side_effect = RSP_SIZES(exchange[:-1])436 device.socket.recvfrom.side_effect = [cmd for cmd, rsp in exchange]437 target = nfc.clf.LocalTarget('106A')438 target.sens_res = HEX("0400")439 target.sel_res = HEX("00")440 target.sdd_res = HEX("31323334")441 target = device.listen_tta(target, 1.0)442 assert isinstance(target, nfc.clf.LocalTarget)443 assert target.brty == '106A'444 assert target.tt4_cmd == HEX('E000')445 assert device.socket.sendto.mock_calls == RSP_CALLS(exchange[:-1])446 def test_listen_tta_dep_activated(self, device):447 atr_req_frame = 'F0 13 D400 30313233343536373839 00000002 aabb'448 exchange = [449 (CMD106A('26'), RSP106A('4400')),450 (CMD106A('9320'), RSP106A('3132333404')),451 (CMD106A('93703132333404'), RSP106A('00')),452 (CMD106A(atr_req_frame), RSP106A('')),453 ]454 device.socket.sendto.side_effect = RSP_SIZES(exchange[:-1])455 device.socket.recvfrom.side_effect = [cmd for cmd, rsp in exchange]456 target = nfc.clf.LocalTarget('106A')457 target.sens_res = HEX("4400")458 target.sel_res = HEX("00")459 target.sdd_res = HEX("31323334")460 target = device.listen_tta(target, 1.0)461 assert isinstance(target, nfc.clf.LocalTarget)462 assert target.brty == '106A'463 assert target.atr_req == HEX(atr_req_frame)[2:]464 assert device.socket.sendto.mock_calls == RSP_CALLS(exchange[:-1])465 def test_listen_tta_timeout_error(self, device):466 device.socket.recvfrom.side_effect = nfc.clf.TimeoutError467 target = nfc.clf.LocalTarget('106A')468 target.sens_res = HEX("4400")469 target.sel_res = HEX("00")470 target.sdd_res = HEX("31323334")471 assert device.listen_tta(target, 1.0) is None472 def test_listen_tta_communication_error(self, device):473 device.socket.recvfrom.side_effect = nfc.clf.CommunicationError474 target = nfc.clf.LocalTarget('106A')475 target.sens_res = HEX("4400")476 target.sel_res = HEX("00")477 target.sdd_res = HEX("31323334")478 assert device.listen_tta(target, 0.01) is None479 def test_listen_tta_socket_bind_error(self, device):480 device.socket.bind.side_effect \481 = nfc.clf.udp.socket.error(nfc.clf.udp.errno.EADDRINUSE, "test")482 assert device.listen_tta(nfc.clf.LocalTarget('106A'), 1.0) is None483 def test_listen_ttb_tt4_activated(self, device):484 device.socket.sendto.side_effect = [485 len('106B 50e8253eec00000011008185')486 ]487 device.socket.recvfrom.side_effect = [488 CMD106B('000000'), CMD106B('050000'), CMD106B('E03132'),489 ]490 target = nfc.clf.LocalTarget('106B')491 target.sensb_res = HEX('50e8253eec00000011008185')492 target = device.listen_ttb(target, 1.0)493 assert isinstance(target, nfc.clf.LocalTarget)494 assert target.brty == '106B'495 assert target.tt4_cmd == HEX('E03132')496 assert device.socket.sendto.mock_calls == [497 call(*RSP106B('50e8253eec00000011008185'))498 ]499 def test_listen_ttb_timeout_error(self, device):500 device.socket.recvfrom.side_effect = nfc.clf.TimeoutError501 target = nfc.clf.LocalTarget('106B')502 target.sensb_res = HEX('50e8253eec00000011008185')503 assert device.listen_ttb(target, 1.0) is None504 def test_listen_ttb_communication_error(self, device):505 device.socket.recvfrom.side_effect = nfc.clf.CommunicationError506 target = nfc.clf.LocalTarget('106B')507 target.sensb_res = HEX('50e8253eec00000011008185')508 assert device.listen_ttb(target, 0.01) is None509 def test_listen_ttb_socket_bind_error(self, device):510 device.socket.bind.side_effect \511 = nfc.clf.udp.socket.error(nfc.clf.udp.errno.EADDRINUSE, "test")512 assert device.listen_ttb(nfc.clf.LocalTarget('106B'), 1.0) is None513 @pytest.mark.parametrize("sensf_req, sensf_res", [514 ('0600ffff0000', '120101010701260cca020f0d23042f7783ff'),515 ('0600ffff0100', '140101010701260cca020f0d23042f7783ff12fc'),516 ('0600ffff0200', '140101010701260cca020f0d23042f7783ff0001'),517 ])518 def test_listen_ttf_tt3_activated(self, device, sensf_req, sensf_res):519 device.socket.sendto.side_effect = [520 len('106B ' + sensf_res)521 ]522 device.socket.recvfrom.side_effect = [523 CMD212F('000000'), CMD212F('030000'), CMD212F(sensf_req),524 CMD212F('0a 02 01010701260cca02'),525 ]526 target = nfc.clf.LocalTarget('212F')527 target.sensf_res = HEX('01 01010701260cca02 0f0d23042f7783ff 12fc')528 target = device.listen_ttf(target, 1.0)529 assert isinstance(target, nfc.clf.LocalTarget)530 assert target.brty == '212F'531 assert target.tt3_cmd == HEX('02 01010701260cca02')532 assert device.socket.sendto.mock_calls == [533 call(*RSP212F(sensf_res))534 ]535 def test_listen_ttf_dep_activated(self, device):536 atr_req_frame = '13 D400 30313233343536373839 00000002 aabb'537 device.socket.sendto.side_effect = [538 len('212F 120101010701260cca020f0d23042f7783ff')539 ]540 device.socket.recvfrom.side_effect = [541 CMD212F('060000000000'), CMD212F('0600ffff0000'),542 CMD212F('030000'), CMD212F(atr_req_frame),543 ]544 target = nfc.clf.LocalTarget('212F')545 target.sensf_res = HEX('01 3031323334353637 0f0d23042f7783ff 12fc')546 target = device.listen_ttf(target, 1.0)547 assert isinstance(target, nfc.clf.LocalTarget)548 assert target.brty == '212F'549 assert target.atr_req == HEX(atr_req_frame)[1:]550 assert device.socket.sendto.mock_calls == [551 call(*RSP212F('12 01 3031323334353637 0f0d23042f7783ff'))552 ]553 def test_listen_ttf_timeout_error(self, device):554 device.socket.recvfrom.side_effect = nfc.clf.TimeoutError555 target = nfc.clf.LocalTarget('212F')556 target.sensf_res = HEX('01 3031323334353637 0f0d23042f7783ff 12fc')557 assert device.listen_ttf(target, 1.0) is None558 def test_listen_ttf_communication_error(self, device):559 device.socket.recvfrom.side_effect = nfc.clf.CommunicationError560 target = nfc.clf.LocalTarget('212F')561 target.sensf_res = HEX('01 3031323334353637 0f0d23042f7783ff 12fc')562 assert device.listen_ttf(target, 0.01) is None563 def test_listen_ttf_socket_bind_error(self, device):564 device.socket.bind.side_effect \565 = nfc.clf.udp.socket.error(nfc.clf.udp.errno.EADDRINUSE, "test")566 assert device.listen_ttf(nfc.clf.LocalTarget('212F'), 1.0) is None567 def test_listen_dep_activated_tta_passive(self, device):568 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'569 dep_req_frame = '06 D40600 0000'570 device.socket.sendto.side_effect = [571 len('106A 0000'),572 len('106A 0102030404'),573 len('106A 60'),574 len('106A f012d501d0d1d2d3d4d5d6d7d8d90000000800'),575 ]576 device.socket.recvfrom.side_effect = [577 CMD106A('000000'), # garbage578 CMD106A('26'),579 CMD106A('9320'),580 CMD106A('93700102030404'),581 CMD106A('f0' + atr_req_frame),582 CMD106A('f0' + dep_req_frame),583 ]584 target = nfc.clf.LocalTarget()585 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')586 target.sens_res = HEX('0000')587 target.sdd_res = HEX('01020304')588 target.sel_res = HEX('60')589 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')590 target = device.listen_dep(target, 1.0)591 assert isinstance(target, nfc.clf.LocalTarget)592 assert target.brty == '106A'593 assert target.atr_req == HEX(atr_req_frame)[1:]594 assert target.dep_req == HEX(dep_req_frame)[1:]595 assert device.socket.sendto.mock_calls == [call(*_) for _ in [596 RSP106A('0000'),597 RSP106A('0102030404'),598 RSP106A('60'),599 RSP106A('f012d501d0d1d2d3d4d5d6d7d8d90000000800'),600 ]]601 def test_listen_dep_activated_tta_active(self, device):602 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'603 dep_req_frame = '06 D40600 0000'604 device.socket.sendto.side_effect = [605 len('106A f012d501d0d1d2d3d4d5d6d7d8d90000000800'),606 ]607 device.socket.recvfrom.side_effect = [608 CMD106A('f0' + atr_req_frame),609 CMD106A('f0' + dep_req_frame),610 ]611 target = nfc.clf.LocalTarget()612 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')613 target.sens_res = HEX('0000')614 target.sdd_res = HEX('01020304')615 target.sel_res = HEX('60')616 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')617 target = device.listen_dep(target, 1.0)618 assert isinstance(target, nfc.clf.LocalTarget)619 assert target.brty == '106A'620 assert target.atr_req == HEX(atr_req_frame)[1:]621 assert target.dep_req == HEX(dep_req_frame)[1:]622 assert device.socket.sendto.mock_calls == [call(*_) for _ in [623 RSP106A('f012d501d0d1d2d3d4d5d6d7d8d90000000800'),624 ]]625 def test_listen_dep_activated_ttf_passive(self, device):626 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'627 dep_req_frame = '06 D40600 0000'628 device.socket.sendto.side_effect = [629 len('424F 120101010701260cca020f0d23042f7783ff'),630 len('424F 12d501d0d1d2d3d4d5d6d7d8d90000000800'),631 ]632 device.socket.recvfrom.side_effect = [633 CMD424F('000000'), # garbage634 CMD424F('030000'), # garbage635 CMD424F('0600ffff0000'),636 CMD424F(atr_req_frame),637 CMD424F(dep_req_frame),638 ]639 target = nfc.clf.LocalTarget()640 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')641 target.sens_res = HEX('0000')642 target.sdd_res = HEX('01020304')643 target.sel_res = HEX('60')644 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')645 target = device.listen_dep(target, 1.0)646 assert isinstance(target, nfc.clf.LocalTarget)647 assert target.brty == '424F'648 assert target.atr_req == HEX(atr_req_frame)[1:]649 assert target.dep_req == HEX(dep_req_frame)[1:]650 assert device.socket.sendto.mock_calls == [call(*_) for _ in [651 RSP424F('12 01 01fe303132333435 0f0d23042f7783ff'),652 RSP424F('12 d501 d0d1d2d3d4d5d6d7d8d9 0000000800'),653 ]]654 def test_listen_dep_activated_ttf_active(self, device):655 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'656 dep_req_frame = '06 D40600 0000'657 device.socket.sendto.side_effect = [658 len('424F 12d501d0d1d2d3d4d5d6d7d8d90000000800'),659 ]660 device.socket.recvfrom.side_effect = [661 CMD424F(atr_req_frame),662 CMD424F(dep_req_frame),663 ]664 target = nfc.clf.LocalTarget()665 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')666 target.sens_res = HEX('0000')667 target.sdd_res = HEX('01020304')668 target.sel_res = HEX('60')669 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')670 target = device.listen_dep(target, 1.0)671 assert isinstance(target, nfc.clf.LocalTarget)672 assert target.brty == '424F'673 assert target.atr_req == HEX(atr_req_frame)[1:]674 assert target.dep_req == HEX(dep_req_frame)[1:]675 assert device.socket.sendto.mock_calls == [call(*_) for _ in [676 RSP424F('12 d501 d0d1d2d3d4d5d6d7d8d9 0000000800'),677 ]]678 def test_listen_dep_activated_106_psl_106(self, device):679 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'680 dep_req_frame = '06 D40600 0000'681 device.socket.sendto.side_effect = [682 len('106A f012d501d0d1d2d3d4d5d6d7d8d90000000800'),683 len('106A f004d50500'),684 ]685 device.socket.recvfrom.side_effect = [686 CMD106A('f0' + atr_req_frame),687 CMD106A('f0' + '06D404000003'),688 CMD106A('f0' + dep_req_frame),689 ]690 target = nfc.clf.LocalTarget()691 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')692 target.sens_res = HEX('0000')693 target.sdd_res = HEX('01020304')694 target.sel_res = HEX('60')695 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')696 target = device.listen_dep(target, 1.0)697 assert isinstance(target, nfc.clf.LocalTarget)698 assert target.brty == '106A'699 assert target.atr_req == HEX(atr_req_frame)[1:]700 assert target.dep_req == HEX(dep_req_frame)[1:]701 assert device.socket.sendto.mock_calls == [call(*_) for _ in [702 RSP106A('f012d501d0d1d2d3d4d5d6d7d8d90000000800'),703 RSP106A('f004d50500'),704 ]]705 def test_listen_dep_activated_106_psl_424(self, device):706 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'707 dep_req_frame = '06 D40600 0000'708 device.socket.sendto.side_effect = [709 len('106A f012d501d0d1d2d3d4d5d6d7d8d90000000800'),710 len('106A f004d50500'),711 ]712 device.socket.recvfrom.side_effect = [713 CMD106A('f0' + atr_req_frame),714 CMD106A('f0' + '06D404001203'),715 CMD424F(dep_req_frame),716 ]717 target = nfc.clf.LocalTarget()718 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')719 target.sens_res = HEX('0000')720 target.sdd_res = HEX('01020304')721 target.sel_res = HEX('60')722 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')723 target = device.listen_dep(target, 1.0)724 assert isinstance(target, nfc.clf.LocalTarget)725 assert target.brty == '424F'726 assert target.atr_req == HEX(atr_req_frame)[1:]727 assert target.dep_req == HEX(dep_req_frame)[1:]728 assert device.socket.sendto.mock_calls == [call(*_) for _ in [729 RSP106A('f012d501d0d1d2d3d4d5d6d7d8d90000000800'),730 RSP106A('f004d50500'),731 ]]732 def test_listen_dep_activated_212_psl_424(self, device):733 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'734 dep_req_frame = '06 D40600 0000'735 device.socket.sendto.side_effect = [736 len('212F 12d501d0d1d2d3d4d5d6d7d8d90000000800'),737 len('212F 04d50500'),738 ]739 device.socket.recvfrom.side_effect = [740 CMD212F(atr_req_frame),741 CMD212F('06D404001203'),742 CMD424F(dep_req_frame),743 ]744 target = nfc.clf.LocalTarget()745 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')746 target.sens_res = HEX('0000')747 target.sdd_res = HEX('01020304')748 target.sel_res = HEX('60')749 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')750 target = device.listen_dep(target, 1.0)751 assert isinstance(target, nfc.clf.LocalTarget)752 assert target.brty == '424F'753 assert target.atr_req == HEX(atr_req_frame)[1:]754 assert target.dep_req == HEX(dep_req_frame)[1:]755 assert device.socket.sendto.mock_calls == [call(*_) for _ in [756 RSP212F('12d501d0d1d2d3d4d5d6d7d8d90000000800'),757 RSP212F('04d50500'),758 ]]759 def test_listen_dep_activated_tta_deselect(self, device):760 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'761 device.socket.sendto.side_effect = [762 len('106A f012d501d0d1d2d3d4d5d6d7d8d90000000800'),763 len('106A f003d509'),764 ]765 device.socket.recvfrom.side_effect = [766 CMD106A('f0' + atr_req_frame),767 CMD106A('f0' + '03d408'),768 ]769 target = nfc.clf.LocalTarget()770 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')771 target.sens_res = HEX('0000')772 target.sdd_res = HEX('01020304')773 target.sel_res = HEX('60')774 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')775 assert device.listen_dep(target, 1.0) is None776 assert device.socket.sendto.mock_calls == [call(*_) for _ in [777 RSP106A('f0 12 d501d0d1d2d3d4d5d6d7d8d90000000800'),778 RSP106A('f0 03 d509'),779 ]]780 def test_listen_dep_activated_tta_release(self, device):781 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'782 device.socket.sendto.side_effect = [783 len('106A f012d501d0d1d2d3d4d5d6d7d8d90000000800'),784 len('106A f003d509'),785 ]786 device.socket.recvfrom.side_effect = [787 CMD106A('f0' + atr_req_frame),788 CMD106A('f0' + '03d40a'),789 ]790 target = nfc.clf.LocalTarget()791 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')792 target.sens_res = HEX('0000')793 target.sdd_res = HEX('01020304')794 target.sel_res = HEX('60')795 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')796 assert device.listen_dep(target, 1.0) is None797 assert device.socket.sendto.mock_calls == [call(*_) for _ in [798 RSP106A('f0 12 d501d0d1d2d3d4d5d6d7d8d90000000800'),799 RSP106A('f0 03 d50b'),800 ]]801 def test_listen_dep_activated_ttf_deselect(self, device):802 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'803 device.socket.sendto.side_effect = [804 len('424F 12d501d0d1d2d3d4d5d6d7d8d90000000800'),805 len('424F 03d509'),806 ]807 device.socket.recvfrom.side_effect = [808 CMD424F(atr_req_frame),809 CMD424F('03d408'),810 ]811 target = nfc.clf.LocalTarget()812 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')813 target.sens_res = HEX('0000')814 target.sdd_res = HEX('01020304')815 target.sel_res = HEX('60')816 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')817 assert device.listen_dep(target, 1.0) is None818 assert device.socket.sendto.mock_calls == [call(*_) for _ in [819 RSP424F('12 d501 d0d1d2d3d4d5d6d7d8d9 0000000800'),820 RSP424F('03 d509'),821 ]]822 def test_listen_dep_activated_ttf_release(self, device):823 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'824 device.socket.sendto.side_effect = [825 len('424F 12d501d0d1d2d3d4d5d6d7d8d90000000800'),826 len('424F 03d50b'),827 ]828 device.socket.recvfrom.side_effect = [829 CMD424F(atr_req_frame),830 CMD424F('03d40a'),831 ]832 target = nfc.clf.LocalTarget()833 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')834 target.sens_res = HEX('0000')835 target.sdd_res = HEX('01020304')836 target.sel_res = HEX('60')837 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')838 assert device.listen_dep(target, 1.0) is None839 assert device.socket.sendto.mock_calls == [call(*_) for _ in [840 RSP424F('12 d501 d0d1d2d3d4d5d6d7d8d9 0000000800'),841 RSP424F('03 d50b'),842 ]]843 @pytest.mark.parametrize('garbage', [844 '030000', '000000'845 ])846 def test_listen_dep_activated_ttf_garbage(self, device, garbage):847 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'848 device.socket.sendto.side_effect = [849 len('424F 12d501d0d1d2d3d4d5d6d7d8d90000000800'),850 ]851 device.socket.recvfrom.side_effect = [852 CMD424F(atr_req_frame),853 CMD424F(garbage),854 ]855 target = nfc.clf.LocalTarget()856 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')857 target.sens_res = HEX('0000')858 target.sdd_res = HEX('01020304')859 target.sel_res = HEX('60')860 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')861 assert device.listen_dep(target, 1.0) is None862 assert device.socket.sendto.mock_calls == [call(*_) for _ in [863 RSP424F('12 d501 d0d1d2d3d4d5d6d7d8d9 0000000800'),864 ]]865 @pytest.mark.parametrize('garbage', [866 'f0030000', 'f0000000', 'ff000000'867 ])868 def test_listen_dep_activated_tta_garbage(self, device, garbage):869 atr_req_frame = 'F0 13 D400 01fe303132333435ffff 00000002 aabb'870 device.socket.sendto.side_effect = [871 len('106A f012d501d0d1d2d3d4d5d6d7d8d90000000800'),872 ]873 device.socket.recvfrom.side_effect = [874 CMD106A(atr_req_frame),875 CMD106A(garbage),876 ]877 target = nfc.clf.LocalTarget()878 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')879 target.sens_res = HEX('0000')880 target.sdd_res = HEX('01020304')881 target.sel_res = HEX('60')882 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')883 assert device.listen_dep(target, 1.0) is None884 assert device.socket.sendto.mock_calls == [call(*_) for _ in [885 RSP106A('f0 12 d501 d0d1d2d3d4d5d6d7d8d9 0000000800'),886 ]]887 @pytest.mark.parametrize('garbage', [888 '010000', '000000'889 ])890 def test_listen_dep_activated_106_psl_garbage(self, device, garbage):891 atr_req_frame = '13 D400 01fe303132333435ffff 00000002 aabb'892 device.socket.sendto.side_effect = [893 len('106A f012d501d0d1d2d3d4d5d6d7d8d90000000800'),894 len('106A f004d50500'),895 ]896 device.socket.recvfrom.side_effect = [897 CMD106A('f0' + atr_req_frame),898 CMD106A('f0' + '06D404001203'),899 CMD424F(garbage),900 ]901 target = nfc.clf.LocalTarget()902 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')903 target.sens_res = HEX('0000')904 target.sdd_res = HEX('01020304')905 target.sel_res = HEX('60')906 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')907 assert device.listen_dep(target, 1.0) is None908 assert device.socket.sendto.mock_calls == [call(*_) for _ in [909 RSP106A('f012d501d0d1d2d3d4d5d6d7d8d90000000800'),910 RSP106A('f004d50500'),911 ]]912 def test_listen_dep_communication_error(self, device):913 device.socket.recvfrom.side_effect = [914 FRAME('RFOFF', ''),915 ]916 target = nfc.clf.LocalTarget()917 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')918 target.sens_res = HEX('0000')919 target.sdd_res = HEX('01020304')920 target.sel_res = HEX('60')921 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')922 assert device.listen_dep(target, 1.0) is None923 assert device.socket.sendto.mock_calls == []924 def test_listen_dep_timeout_error(self, device):925 device.socket.recvfrom.return_value = CMD106A('00')926 target = nfc.clf.LocalTarget()927 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')928 target.sens_res = HEX('0000')929 target.sdd_res = HEX('01020304')930 target.sel_res = HEX('60')931 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')932 assert device.listen_dep(target, 0.001) is None933 assert device.socket.sendto.mock_calls == []934 def test_listen_dep_socket_bind_error(self, device):935 device.socket.bind.side_effect \936 = nfc.clf.udp.socket.error(nfc.clf.udp.errno.EADDRINUSE, "test")937 target = nfc.clf.LocalTarget()938 target.sensf_res = HEX('01 01fe303132333435 0f0d23042f7783ff ffff')939 target.sens_res = HEX('0000')940 target.sdd_res = HEX('01020304')941 target.sel_res = HEX('60')942 target.atr_res = HEX('D501 d0d1d2d3d4d5d6d7d8d9 0000000800')943 assert device.listen_dep(target, 1.0) is None944 assert device.socket.sendto.mock_calls == []945 #946 # SEND/RECV DATA947 #948 def test_send_cmd_recv_rsp(self, device):949 target = self.test_sense_tta_with_tt1_target_found(device)950 cdata, rdata = ('01020304', '05060708')951 device.socket.sendto.side_effect = [len(CMD106A(cdata)[0])]952 device.socket.recvfrom.side_effect = [RSP106A(rdata)]953 assert device.send_cmd_recv_rsp(target, HEX(cdata), 1) == HEX(rdata)954 device.socket.sendto.assert_called_with(*CMD106A(cdata))955 device.socket.sendto.side_effect = []956 device.socket.recvfrom.side_effect = [RSP106A(rdata)]957 assert device.send_cmd_recv_rsp(target, None, 1) == HEX(rdata)958 device.socket.sendto.side_effect = [len(CMD106A(cdata)[0])]959 device.socket.recvfrom.side_effect = []960 assert device.send_cmd_recv_rsp(target, HEX(cdata), 0) is None961 device.socket.sendto.assert_called_with(*CMD106A(cdata))962 device.socket.sendto.side_effect = [len(CMD106A(cdata)[0])]963 device.socket.recvfrom.side_effect = [RSP106A('')]964 with pytest.raises(nfc.clf.TransmissionError):965 device.send_cmd_recv_rsp(target, HEX(cdata), 1)966 device.socket.sendto.assert_called_with(*CMD106A(cdata))967 device.socket.sendto.side_effect = [len(CMD106A(cdata)[0])+1]968 device.socket.recvfrom.side_effect = []969 with pytest.raises(nfc.clf.TransmissionError):970 device.send_cmd_recv_rsp(target, HEX(cdata), 1)971 device.socket.sendto.assert_called_with(*CMD106A(cdata))972 device.socket.sendto.side_effect = [len(CMD106A(cdata)[0])]973 device.socket.recvfrom.side_effect = [RSP424F(rdata), RSP106A(rdata)]974 assert device.send_cmd_recv_rsp(target, HEX(cdata), 1) == HEX(rdata)975 device.socket.sendto.assert_called_with(*CMD106A(cdata))976 def test_send_rsp_recv_cmd(self, device):977 target = self.test_listen_tta_tt2_uid4_activated(device)978 cdata, rdata = ('01020304', '05060708')979 device.socket.sendto.side_effect = [len(RSP106A(rdata)[0])]980 device.socket.recvfrom.side_effect = [CMD106A(cdata)]981 assert device.send_rsp_recv_cmd(target, HEX(rdata), 1) == HEX(cdata)982 device.socket.sendto.assert_called_with(*RSP106A(rdata))983 device.socket.sendto.side_effect = [len(RSP106A(rdata)[0])]984 device.socket.recvfrom.side_effect = [CMD106A(cdata)]985 assert device.send_rsp_recv_cmd(target, HEX(rdata), None) == HEX(cdata)986 device.socket.sendto.assert_called_with(*RSP106A(rdata))987 device.socket.sendto.side_effect = []988 device.socket.recvfrom.side_effect = [CMD106A(cdata)]989 assert device.send_rsp_recv_cmd(target, None, 1) == HEX(cdata)990 device.socket.sendto.side_effect = [len(RSP106A(rdata)[0])]991 device.socket.recvfrom.side_effect = []992 assert device.send_rsp_recv_cmd(target, HEX(rdata), 0) is None993 device.socket.sendto.assert_called_with(*RSP106A(rdata))994 device.socket.sendto.side_effect = [len(RSP106A(rdata)[0])]995 device.socket.recvfrom.side_effect = [CMD106A('')]996 with pytest.raises(nfc.clf.TransmissionError):997 device.send_rsp_recv_cmd(target, HEX(rdata), 1)998 device.socket.sendto.assert_called_with(*RSP106A(rdata))999 def test_recv_data_timeout_error(self, mocker, device): # noqa: F8111000 target = self.test_sense_tta_with_tt1_target_found(device)1001 mocker.patch('nfc.clf.udp.select.select').return_value = ([], [], [])1002 with pytest.raises(nfc.clf.TimeoutError):1003 device.send_cmd_recv_rsp(target, None, 0.001)1004 def test_get_max_send_data_size(self, device):1005 assert device.get_max_send_data_size(None) == 2901006 def test_get_max_recv_data_size(self, device):1007 assert device.get_max_recv_data_size(None) == 2901008 #1009 # INTERNAL METHODS1010 #1011 def test_bind_socket(self, device):1012 assert device._bind_socket(nfc.clf.udp.time.time() - 1) is None1013 device.socket.bind.side_effect \1014 = nfc.clf.udp.socket.error(nfc.clf.udp.errno.EADDRINUSE, "test")1015 assert device._bind_socket(nfc.clf.udp.time.time() + 1) is False1016 device.socket.bind.side_effect \1017 = nfc.clf.udp.socket.error(nfc.clf.udp.errno.ENODEV, "test")1018 with pytest.raises(nfc.clf.udp.socket.error) as excinfo:1019 device._bind_socket(nfc.clf.udp.time.time() + 1)...

Full Screen

Full Screen

test_java_keystore.py

Source:test_java_keystore.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2# Copyright (c) 2018, Ansible Project3# Copyright (c) 2018, Abhijeet Kasurde <akasurde@redhat.com>4#5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)6from __future__ import (absolute_import, division, print_function)7__metaclass__ = type8import os9from ansible_collections.community.general.tests.unit.plugins.modules.utils import ModuleTestCase, set_module_args10from ansible_collections.community.general.tests.unit.compat.mock import patch11from ansible_collections.community.general.tests.unit.compat.mock import Mock12from ansible.module_utils.basic import AnsibleModule13from ansible_collections.community.general.plugins.modules.system.java_keystore import JavaKeystore14module_argument_spec = dict(15 name=dict(type='str', required=True),16 dest=dict(type='path', required=True),17 certificate=dict(type='str', no_log=True),18 certificate_path=dict(type='path'),19 private_key=dict(type='str', no_log=True),20 private_key_path=dict(type='path', no_log=False),21 private_key_passphrase=dict(type='str', no_log=True),22 password=dict(type='str', required=True, no_log=True),23 ssl_backend=dict(type='str', default='openssl', choices=['openssl', 'cryptography']),24 keystore_type=dict(type='str', choices=['jks', 'pkcs12']),25 force=dict(type='bool', default=False),26)27module_supports_check_mode = True28module_choose_between = (['certificate', 'certificate_path'],29 ['private_key', 'private_key_path'])30class TestCreateJavaKeystore(ModuleTestCase):31 """Test the creation of a Java keystore."""32 def setUp(self):33 """Setup."""34 super(TestCreateJavaKeystore, self).setUp()35 orig_exists = os.path.exists36 self.mock_create_file = patch('ansible_collections.community.general.plugins.modules.system.java_keystore.create_file')37 self.mock_create_path = patch('ansible_collections.community.general.plugins.modules.system.java_keystore.create_path')38 self.mock_current_type = patch('ansible_collections.community.general.plugins.modules.system.java_keystore.JavaKeystore.current_type')39 self.mock_run_command = patch('ansible.module_utils.basic.AnsibleModule.run_command')40 self.mock_get_bin_path = patch('ansible.module_utils.basic.AnsibleModule.get_bin_path')41 self.mock_preserved_copy = patch('ansible.module_utils.basic.AnsibleModule.preserved_copy')42 self.mock_atomic_move = patch('ansible.module_utils.basic.AnsibleModule.atomic_move')43 self.mock_os_path_exists = patch('os.path.exists',44 side_effect=lambda path: True if path == '/path/to/keystore.jks' else orig_exists(path))45 self.mock_selinux_context = patch('ansible.module_utils.basic.AnsibleModule.selinux_context',46 side_effect=lambda path: ['unconfined_u', 'object_r', 'user_home_t', 's0'])47 self.mock_is_special_selinux_path = patch('ansible.module_utils.basic.AnsibleModule.is_special_selinux_path',48 side_effect=lambda path: (False, None))49 self.run_command = self.mock_run_command.start()50 self.get_bin_path = self.mock_get_bin_path.start()51 self.preserved_copy = self.mock_preserved_copy.start()52 self.atomic_move = self.mock_atomic_move.start()53 self.create_file = self.mock_create_file.start()54 self.create_path = self.mock_create_path.start()55 self.current_type = self.mock_current_type.start()56 self.selinux_context = self.mock_selinux_context.start()57 self.is_special_selinux_path = self.mock_is_special_selinux_path.start()58 self.os_path_exists = self.mock_os_path_exists.start()59 def tearDown(self):60 """Teardown."""61 super(TestCreateJavaKeystore, self).tearDown()62 self.mock_create_file.stop()63 self.mock_create_path.stop()64 self.mock_current_type.stop()65 self.mock_run_command.stop()66 self.mock_get_bin_path.stop()67 self.mock_preserved_copy.stop()68 self.mock_atomic_move.stop()69 self.mock_selinux_context.stop()70 self.mock_is_special_selinux_path.stop()71 self.mock_os_path_exists.stop()72 def test_create_jks_success(self):73 set_module_args(dict(74 certificate='cert-foo',75 private_key='private-foo',76 dest='/path/to/keystore.jks',77 name='test',78 password='changeit'79 ))80 module = AnsibleModule(81 argument_spec=module_argument_spec,82 supports_check_mode=module_supports_check_mode,83 mutually_exclusive=module_choose_between,84 required_one_of=module_choose_between85 )86 with patch('os.remove', return_value=True):87 self.create_path.side_effect = ['/tmp/tmpgrzm2ah7']88 self.create_file.side_effect = ['/tmp/etacifitrec', '/tmp/yek_etavirp', '']89 self.run_command.side_effect = [(0, '', ''), (0, '', '')]90 self.get_bin_path.side_effect = ['keytool', 'openssl', '']91 jks = JavaKeystore(module)92 assert jks.create() == {93 'changed': True,94 'cmd': ["keytool", "-importkeystore",95 "-destkeystore", "/path/to/keystore.jks",96 "-srckeystore", "/tmp/tmpgrzm2ah7", "-srcstoretype", "pkcs12", "-alias", "test",97 "-noprompt"],98 'msg': '',99 'rc': 0100 }101 def test_create_jks_keypass_fail_export_pkcs12(self):102 set_module_args(dict(103 certificate='cert-foo',104 private_key='private-foo',105 private_key_passphrase='passphrase-foo',106 dest='/path/to/keystore.jks',107 name='test',108 password='changeit'109 ))110 module = AnsibleModule(111 argument_spec=module_argument_spec,112 supports_check_mode=module_supports_check_mode,113 mutually_exclusive=module_choose_between,114 required_one_of=module_choose_between115 )116 module.exit_json = Mock()117 module.fail_json = Mock()118 with patch('os.remove', return_value=True):119 self.create_path.side_effect = ['/tmp/tmp1cyp12xa']120 self.create_file.side_effect = ['/tmp/tmpvalcrt32', '/tmp/tmpwh4key0c', '']121 self.run_command.side_effect = [(1, '', 'Oops'), (0, '', '')]122 self.get_bin_path.side_effect = ['keytool', 'openssl', '']123 jks = JavaKeystore(module)124 jks.create()125 module.fail_json.assert_called_once_with(126 cmd=["openssl", "pkcs12", "-export", "-name", "test",127 "-in", "/tmp/tmpvalcrt32",128 "-inkey", "/tmp/tmpwh4key0c",129 "-out", "/tmp/tmp1cyp12xa",130 "-passout", "stdin",131 "-passin", "stdin"],132 msg='',133 err='Oops',134 rc=1135 )136 def test_create_jks_fail_export_pkcs12(self):137 set_module_args(dict(138 certificate='cert-foo',139 private_key='private-foo',140 dest='/path/to/keystore.jks',141 name='test',142 password='changeit'143 ))144 module = AnsibleModule(145 argument_spec=module_argument_spec,146 supports_check_mode=module_supports_check_mode,147 mutually_exclusive=module_choose_between,148 required_one_of=module_choose_between149 )150 module.exit_json = Mock()151 module.fail_json = Mock()152 with patch('os.remove', return_value=True):153 self.create_path.side_effect = ['/tmp/tmp1cyp12xa']154 self.create_file.side_effect = ['/tmp/tmpvalcrt32', '/tmp/tmpwh4key0c', '']155 self.run_command.side_effect = [(1, '', 'Oops'), (0, '', '')]156 self.get_bin_path.side_effect = ['keytool', 'openssl', '']157 jks = JavaKeystore(module)158 jks.create()159 module.fail_json.assert_called_once_with(160 cmd=["openssl", "pkcs12", "-export", "-name", "test",161 "-in", "/tmp/tmpvalcrt32",162 "-inkey", "/tmp/tmpwh4key0c",163 "-out", "/tmp/tmp1cyp12xa",164 "-passout", "stdin"],165 msg='',166 err='Oops',167 rc=1168 )169 def test_create_jks_fail_import_key(self):170 set_module_args(dict(171 certificate='cert-foo',172 private_key='private-foo',173 dest='/path/to/keystore.jks',174 name='test',175 password='changeit'176 ))177 module = AnsibleModule(178 argument_spec=module_argument_spec,179 supports_check_mode=module_supports_check_mode,180 mutually_exclusive=module_choose_between,181 required_one_of=module_choose_between182 )183 module.exit_json = Mock()184 module.fail_json = Mock()185 with patch('os.remove', return_value=True):186 self.create_path.side_effect = ['/tmp/tmpgrzm2ah7']187 self.create_file.side_effect = ['/tmp/etacifitrec', '/tmp/yek_etavirp', '']188 self.run_command.side_effect = [(0, '', ''), (1, '', 'Oops')]189 self.get_bin_path.side_effect = ['keytool', 'openssl', '']190 jks = JavaKeystore(module)191 jks.create()192 module.fail_json.assert_called_once_with(193 cmd=["keytool", "-importkeystore",194 "-destkeystore", "/path/to/keystore.jks",195 "-srckeystore", "/tmp/tmpgrzm2ah7", "-srcstoretype", "pkcs12", "-alias", "test",196 "-noprompt"],197 msg='',198 err='Oops',199 rc=1200 )201class TestCertChanged(ModuleTestCase):202 """Test if the cert has changed."""203 def setUp(self):204 """Setup."""205 super(TestCertChanged, self).setUp()206 self.mock_create_file = patch('ansible_collections.community.general.plugins.modules.system.java_keystore.create_file')207 self.mock_current_type = patch('ansible_collections.community.general.plugins.modules.system.java_keystore.JavaKeystore.current_type')208 self.mock_run_command = patch('ansible.module_utils.basic.AnsibleModule.run_command')209 self.mock_get_bin_path = patch('ansible.module_utils.basic.AnsibleModule.get_bin_path')210 self.mock_preserved_copy = patch('ansible.module_utils.basic.AnsibleModule.preserved_copy')211 self.mock_atomic_move = patch('ansible.module_utils.basic.AnsibleModule.atomic_move')212 self.run_command = self.mock_run_command.start()213 self.create_file = self.mock_create_file.start()214 self.get_bin_path = self.mock_get_bin_path.start()215 self.current_type = self.mock_current_type.start()216 self.preserved_copy = self.mock_preserved_copy.start()217 self.atomic_move = self.mock_atomic_move.start()218 def tearDown(self):219 """Teardown."""220 super(TestCertChanged, self).tearDown()221 self.mock_create_file.stop()222 self.mock_current_type.stop()223 self.mock_run_command.stop()224 self.mock_get_bin_path.stop()225 self.mock_preserved_copy.stop()226 self.mock_atomic_move.stop()227 def test_cert_unchanged_same_fingerprint(self):228 set_module_args(dict(229 certificate='cert-foo',230 private_key='private-foo',231 dest='/path/to/keystore.jks',232 name='foo',233 password='changeit'234 ))235 module = AnsibleModule(236 argument_spec=module_argument_spec,237 supports_check_mode=module_supports_check_mode,238 mutually_exclusive=module_choose_between,239 required_one_of=module_choose_between240 )241 with patch('os.remove', return_value=True):242 self.create_file.side_effect = ['/tmp/placeholder', '']243 self.run_command.side_effect = [(0, 'foo=abcd:1234:efgh', ''), (0, 'SHA256: abcd:1234:efgh', '')]244 self.get_bin_path.side_effect = ['keytool', 'openssl', '']245 self.current_type.side_effect = ['jks']246 jks = JavaKeystore(module)247 result = jks.cert_changed()248 self.assertFalse(result, 'Fingerprint is identical')249 def test_cert_changed_fingerprint_mismatch(self):250 set_module_args(dict(251 certificate='cert-foo',252 private_key='private-foo',253 dest='/path/to/keystore.jks',254 name='foo',255 password='changeit'256 ))257 module = AnsibleModule(258 argument_spec=module_argument_spec,259 supports_check_mode=module_supports_check_mode,260 mutually_exclusive=module_choose_between,261 required_one_of=module_choose_between262 )263 with patch('os.remove', return_value=True):264 self.create_file.side_effect = ['/tmp/placeholder', '']265 self.run_command.side_effect = [(0, 'foo=abcd:1234:efgh', ''), (0, 'SHA256: wxyz:9876:stuv', '')]266 self.get_bin_path.side_effect = ['keytool', 'openssl', '']267 self.current_type.side_effect = ['jks']268 jks = JavaKeystore(module)269 result = jks.cert_changed()270 self.assertTrue(result, 'Fingerprint mismatch')271 def test_cert_changed_alias_does_not_exist(self):272 set_module_args(dict(273 certificate='cert-foo',274 private_key='private-foo',275 dest='/path/to/keystore.jks',276 name='foo',277 password='changeit'278 ))279 module = AnsibleModule(280 argument_spec=module_argument_spec,281 supports_check_mode=module_supports_check_mode,282 mutually_exclusive=module_choose_between,283 required_one_of=module_choose_between284 )285 with patch('os.remove', return_value=True):286 self.create_file.side_effect = ['/tmp/placeholder', '']287 self.run_command.side_effect = [(0, 'foo=abcd:1234:efgh', ''),288 (1, 'keytool error: java.lang.Exception: Alias <foo> does not exist', '')]289 self.get_bin_path.side_effect = ['keytool', 'openssl', '']290 jks = JavaKeystore(module)291 result = jks.cert_changed()292 self.assertTrue(result, 'Alias mismatch detected')293 def test_cert_changed_password_mismatch(self):294 set_module_args(dict(295 certificate='cert-foo',296 private_key='private-foo',297 dest='/path/to/keystore.jks',298 name='foo',299 password='changeit'300 ))301 module = AnsibleModule(302 argument_spec=module_argument_spec,303 supports_check_mode=module_supports_check_mode,304 mutually_exclusive=module_choose_between,305 required_one_of=module_choose_between306 )307 with patch('os.remove', return_value=True):308 self.create_file.side_effect = ['/tmp/placeholder', '']309 self.run_command.side_effect = [(0, 'foo=abcd:1234:efgh', ''),310 (1, 'keytool error: java.io.IOException: Keystore password was incorrect', '')]311 self.get_bin_path.side_effect = ['keytool', 'openssl', '']312 jks = JavaKeystore(module)313 result = jks.cert_changed()314 self.assertTrue(result, 'Password mismatch detected')315 def test_cert_changed_fail_read_cert(self):316 set_module_args(dict(317 certificate='cert-foo',318 private_key='private-foo',319 dest='/path/to/keystore.jks',320 name='foo',321 password='changeit'322 ))323 module = AnsibleModule(324 argument_spec=module_argument_spec,325 supports_check_mode=module_supports_check_mode,326 mutually_exclusive=module_choose_between,327 required_one_of=module_choose_between328 )329 module.exit_json = Mock()330 module.fail_json = Mock()331 with patch('os.remove', return_value=True):332 self.create_file.side_effect = ['/tmp/tmpdj6bvvme', '']333 self.run_command.side_effect = [(1, '', 'Oops'), (0, 'SHA256: wxyz:9876:stuv', '')]334 self.get_bin_path.side_effect = ['keytool', 'openssl', '']335 self.current_type.side_effect = ['jks']336 jks = JavaKeystore(module)337 jks.cert_changed()338 module.fail_json.assert_called_once_with(339 cmd=["openssl", "x509", "-noout", "-in", "/tmp/tmpdj6bvvme", "-fingerprint", "-sha256"],340 msg='',341 err='Oops',342 rc=1343 )344 def test_cert_changed_fail_read_keystore(self):345 set_module_args(dict(346 certificate='cert-foo',347 private_key='private-foo',348 dest='/path/to/keystore.jks',349 name='foo',350 password='changeit'351 ))352 module = AnsibleModule(353 argument_spec=module_argument_spec,354 supports_check_mode=module_supports_check_mode,355 mutually_exclusive=module_choose_between,356 required_one_of=module_choose_between357 )358 module.exit_json = Mock()359 module.fail_json = Mock(return_value=True)360 with patch('os.remove', return_value=True):361 self.create_file.side_effect = ['/tmp/placeholder', '']362 self.run_command.side_effect = [(0, 'foo: wxyz:9876:stuv', ''), (1, '', 'Oops')]363 self.get_bin_path.side_effect = ['keytool', 'openssl', '']364 jks = JavaKeystore(module)365 jks.cert_changed()366 module.fail_json.assert_called_with(367 cmd=["keytool", "-list", "-alias", "foo", "-keystore", "/path/to/keystore.jks", "-v"],368 msg='',369 err='Oops',370 rc=1...

Full Screen

Full Screen

test_oc_group.py

Source:test_oc_group.py Github

copy

Full Screen

1'''2 Unit tests for oc group3'''4import copy5import os6import six7import sys8import unittest9import mock10# Removing invalid variable names for tests so that I can11# keep them brief12# pylint: disable=invalid-name,no-name-in-module13# Disable import-error b/c our libraries aren't loaded in jenkins14# pylint: disable=import-error,wrong-import-position15# place class in our python path16module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E50117sys.path.insert(0, module_path)18from oc_group import OCGroup, locate_oc_binary # noqa: E40219class OCGroupTest(unittest.TestCase):20 '''21 Test class for OCGroup22 '''23 params = {'kubeconfig': '/etc/origin/master/admin.kubeconfig',24 'state': 'present',25 'debug': False,26 'name': 'acme',27 'namespace': 'test'}28 @mock.patch('oc_group.Utils.create_tmpfile_copy')29 @mock.patch('oc_group.OCGroup._run')30 def test_create_group(self, mock_run, mock_tmpfile_copy):31 ''' Testing a group create '''32 params = copy.deepcopy(OCGroupTest.params)33 group = '''{34 "kind": "Group",35 "apiVersion": "v1",36 "metadata": {37 "name": "acme"38 },39 "users": []40 }'''41 mock_run.side_effect = [42 (1, '', 'Error from server: groups "acme" not found'),43 (1, '', 'Error from server: groups "acme" not found'),44 (0, '', ''),45 (0, group, ''),46 ]47 mock_tmpfile_copy.side_effect = [48 '/tmp/mocked_kubeconfig',49 ]50 results = OCGroup.run_ansible(params, False)51 self.assertTrue(results['changed'])52 self.assertEqual(results['results']['results'][0]['metadata']['name'], 'acme')53 @mock.patch('oc_group.Utils.create_tmpfile_copy')54 @mock.patch('oc_group.OCGroup._run')55 def test_failed_get_group(self, mock_run, mock_tmpfile_copy):56 ''' Testing a group create '''57 params = copy.deepcopy(OCGroupTest.params)58 params['state'] = 'list'59 params['name'] = 'noexist'60 mock_run.side_effect = [61 (1, '', 'Error from server: groups "acme" not found'),62 ]63 mock_tmpfile_copy.side_effect = [64 '/tmp/mocked_kubeconfig',65 ]66 results = OCGroup.run_ansible(params, False)67 self.assertTrue(results['failed'])68 @mock.patch('oc_group.Utils.create_tmpfile_copy')69 @mock.patch('oc_group.OCGroup._run')70 def test_delete_group(self, mock_run, mock_tmpfile_copy):71 ''' Testing a group create '''72 params = copy.deepcopy(OCGroupTest.params)73 params['state'] = 'absent'74 group = '''{75 "kind": "Group",76 "apiVersion": "v1",77 "metadata": {78 "name": "acme"79 },80 "users": [81 "user1"82 ]83 }'''84 mock_run.side_effect = [85 (0, group, ''),86 (0, '', ''),87 ]88 mock_tmpfile_copy.side_effect = [89 '/tmp/mocked_kubeconfig',90 ]91 results = OCGroup.run_ansible(params, False)92 self.assertTrue(results['changed'])93 @mock.patch('oc_group.Utils.create_tmpfile_copy')94 @mock.patch('oc_group.OCGroup._run')95 def test_get_group(self, mock_run, mock_tmpfile_copy):96 ''' Testing a group create '''97 params = copy.deepcopy(OCGroupTest.params)98 params['state'] = 'list'99 group = '''{100 "kind": "Group",101 "apiVersion": "v1",102 "metadata": {103 "name": "acme"104 },105 "users": [106 "user1"107 ]108 }'''109 mock_run.side_effect = [110 (0, group, ''),111 ]112 mock_tmpfile_copy.side_effect = [113 '/tmp/mocked_kubeconfig',114 ]115 results = OCGroup.run_ansible(params, False)116 self.assertFalse(results['changed'])117 self.assertEqual(results['results'][0]['metadata']['name'], 'acme')118 self.assertEqual(results['results'][0]['users'][0], 'user1')119 @unittest.skipIf(six.PY3, 'py2 test only')120 @mock.patch('os.path.exists')121 @mock.patch('os.environ.get')122 def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):123 ''' Testing binary lookup fallback '''124 mock_env_get.side_effect = lambda _v, _d: ''125 mock_path_exists.side_effect = lambda _: False126 self.assertEqual(locate_oc_binary(), 'oc')127 @unittest.skipIf(six.PY3, 'py2 test only')128 @mock.patch('os.path.exists')129 @mock.patch('os.environ.get')130 def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):131 ''' Testing binary lookup in path '''132 oc_bin = '/usr/bin/oc'133 mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'134 mock_path_exists.side_effect = lambda f: f == oc_bin135 self.assertEqual(locate_oc_binary(), oc_bin)136 @unittest.skipIf(six.PY3, 'py2 test only')137 @mock.patch('os.path.exists')138 @mock.patch('os.environ.get')139 def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):140 ''' Testing binary lookup in /usr/local/bin '''141 oc_bin = '/usr/local/bin/oc'142 mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'143 mock_path_exists.side_effect = lambda f: f == oc_bin144 self.assertEqual(locate_oc_binary(), oc_bin)145 @unittest.skipIf(six.PY3, 'py2 test only')146 @mock.patch('os.path.exists')147 @mock.patch('os.environ.get')148 def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):149 ''' Testing binary lookup in ~/bin '''150 oc_bin = os.path.expanduser('~/bin/oc')151 mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'152 mock_path_exists.side_effect = lambda f: f == oc_bin153 self.assertEqual(locate_oc_binary(), oc_bin)154 @unittest.skipIf(six.PY2, 'py3 test only')155 @mock.patch('shutil.which')156 @mock.patch('os.environ.get')157 def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):158 ''' Testing binary lookup fallback '''159 mock_env_get.side_effect = lambda _v, _d: ''160 mock_shutil_which.side_effect = lambda _f, path=None: None161 self.assertEqual(locate_oc_binary(), 'oc')162 @unittest.skipIf(six.PY2, 'py3 test only')163 @mock.patch('shutil.which')164 @mock.patch('os.environ.get')165 def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):166 ''' Testing binary lookup in path '''167 oc_bin = '/usr/bin/oc'168 mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'169 mock_shutil_which.side_effect = lambda _f, path=None: oc_bin170 self.assertEqual(locate_oc_binary(), oc_bin)171 @unittest.skipIf(six.PY2, 'py3 test only')172 @mock.patch('shutil.which')173 @mock.patch('os.environ.get')174 def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):175 ''' Testing binary lookup in /usr/local/bin '''176 oc_bin = '/usr/local/bin/oc'177 mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'178 mock_shutil_which.side_effect = lambda _f, path=None: oc_bin179 self.assertEqual(locate_oc_binary(), oc_bin)180 @unittest.skipIf(six.PY2, 'py3 test only')181 @mock.patch('shutil.which')182 @mock.patch('os.environ.get')183 def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):184 ''' Testing binary lookup in ~/bin '''185 oc_bin = os.path.expanduser('~/bin/oc')186 mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'187 mock_shutil_which.side_effect = lambda _f, path=None: oc_bin...

Full Screen

Full Screen

bktptcmd.py

Source:bktptcmd.py Github

copy

Full Screen

1from __future__ import print_function2import side_effect3def useless_function(first, second):4 print("I have the wrong number of arguments.")5def function(frame, bp_loc, dict):6 side_effect.bktptcmd = "function was here"7def another_function(frame, bp_loc, extra_args, dict):8 se_value = extra_args.GetValueForKey("side_effect")9 se_string = se_value.GetStringValue(100)10 side_effect.fancy = se_string11def a_third_function(frame, bp_loc, extra_args, dict):12 se_value = extra_args.GetValueForKey("side_effect")13 se_string = se_value.GetStringValue(100)14 side_effect.fancier = se_string15def a_list_function(frame, bp_loc, extra_args, dict):16 se_value = extra_args.GetValueForKey("side_effect")17 se_string = se_value.GetStringValue(100)18 side_effect.from_list = se_string19 20def empty_extra_args(frame, bp_loc, extra_args, dict):21 if extra_args.IsValid():22 side_effect.not_so_fancy = "Extra args should not be valid"...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run uiautomator automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful