From 5b45be4e83c7e0d62fb949dba32bb9f832c1df5b Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 15 Jan 2019 13:47:26 +0100 Subject: Add support for multiline C definitions in NK_C_API + test Signed-off-by: Szczepan Zalega --- unittest/conftest.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'unittest/conftest.py') diff --git a/unittest/conftest.py b/unittest/conftest.py index 253e1d8..669dc23 100644 --- a/unittest/conftest.py +++ b/unittest/conftest.py @@ -44,13 +44,13 @@ def C(request=None): cnt = 0 a = iter(declarations) for declaration in a: - if declaration.strip().startswith('NK_C_API'): + if declaration.strip().startswith('NK_C_API') \ + or declaration.strip().startswith('struct'): declaration = declaration.replace('NK_C_API', '').strip() - while ';' not in declaration: - declaration += (next(a)).strip() - # print(declaration) + while ');' not in declaration and '};' not in declaration: + declaration += (next(a)).strip()+'\n' ffi.cdef(declaration, override=True) - cnt +=1 + cnt += 1 print('Imported {} declarations'.format(cnt)) C = None -- cgit v1.2.1 From aa2778c5a4602137ee86db6c5fa9810b5c566b5d Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 15 Jan 2019 13:48:15 +0100 Subject: Add proxy object for logging libnitrokey calls Signed-off-by: Szczepan Zalega --- unittest/conftest.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'unittest/conftest.py') diff --git a/unittest/conftest.py b/unittest/conftest.py index 669dc23..bf18fc8 100644 --- a/unittest/conftest.py +++ b/unittest/conftest.py @@ -33,6 +33,32 @@ def skip_if_device_version_lower_than(allowed_devices): pytest.skip('This device model is not applicable to run this test') +class AtrrCallProx(object): + def __init__(self, C, name): + self.C = C + self.name = name + + def __call__(self, *args, **kwargs): + print('Calling {}{}'.format(self.name, args)) + res = self.C(*args, **kwargs) + res_s = res + try: + res_s = '{} => '.format(res) + '{}'.format(gs(res)) + except Exception as e: + pass + print('Result of {}: {}'.format(self.name, res_s)) + return res + + +class AttrProxy(object): + def __init__(self, C, name): + self.C = C + self.name = name + + def __getattr__(self, attr): + return AtrrCallProx(getattr(self.C, attr), attr) + + @pytest.fixture(scope="module") def C(request=None): fp = '../NK_C_API.h' @@ -104,4 +130,5 @@ def C(request=None): # C.NK_set_debug(True) C.NK_set_debug_level(int(os.environ.get('LIBNK_DEBUG', 3))) - return C + return AttrProxy(C, "libnitrokey C") + -- cgit v1.2.1 From c284da21d2eb9e9cbdc30cd82e9dd46ea4528683 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 15 Jan 2019 13:48:49 +0100 Subject: Use logger instead of bare printing Signed-off-by: Szczepan Zalega --- unittest/conftest.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'unittest/conftest.py') diff --git a/unittest/conftest.py b/unittest/conftest.py index bf18fc8..ba21a38 100644 --- a/unittest/conftest.py +++ b/unittest/conftest.py @@ -21,10 +21,17 @@ SPDX-License-Identifier: LGPL-3.0 import pytest -from misc import ffi +from misc import ffi, gs device_type = None +from logging import getLogger, basicConfig, DEBUG + +basicConfig(format='* %(relativeCreated)6d %(filename)s:%(lineno)d %(message)s',level=DEBUG) +log = getLogger('conftest') +print = log.debug + + def skip_if_device_version_lower_than(allowed_devices): global device_type model, version = device_type -- cgit v1.2.1 From 4078bf60135e9ad161eefa775e90dca3cced2730 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 15 Jan 2019 14:48:02 +0100 Subject: Add Python offline tests Signed-off-by: Szczepan Zalega --- unittest/conftest.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'unittest/conftest.py') diff --git a/unittest/conftest.py b/unittest/conftest.py index ba21a38..a9f46c6 100644 --- a/unittest/conftest.py +++ b/unittest/conftest.py @@ -66,8 +66,19 @@ class AttrProxy(object): return AtrrCallProx(getattr(self.C, attr), attr) +@pytest.fixture(scope="module") +def C_offline(request=None): + print("Getting library without initializing connection") + return get_library(request, allow_offline=True) + + @pytest.fixture(scope="module") def C(request=None): + print("Getting library with connection initialized") + return get_library(request) + + +def get_library(request, allow_offline=False): fp = '../NK_C_API.h' declarations = [] @@ -115,12 +126,13 @@ def C(request=None): nk_login = C.NK_login_auto() if nk_login != 1: print('No devices detected!') - assert nk_login != 0 # returns 0 if not connected or wrong model or 1 when connected - global device_type - firmware_version = C.NK_get_minor_firmware_version() - model = 'P' if firmware_version < 20 else 'S' - device_type = (model, firmware_version) - print('Connected device: {} {}'.format(model, firmware_version)) + if not allow_offline: + assert nk_login != 0 # returns 0 if not connected or wrong model or 1 when connected + global device_type + firmware_version = C.NK_get_minor_firmware_version() + model = 'P' if firmware_version < 20 else 'S' + device_type = (model, firmware_version) + print('Connected device: {} {}'.format(model, firmware_version)) # assert C.NK_first_authenticate(DefaultPasswords.ADMIN, DefaultPasswords.ADMIN_TEMP) == DeviceErrorCode.STATUS_OK # assert C.NK_user_authenticate(DefaultPasswords.USER, DefaultPasswords.USER_TEMP) == DeviceErrorCode.STATUS_OK -- cgit v1.2.1