aboutsummaryrefslogtreecommitdiff
path: root/unittest
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2020-09-21 16:33:53 +0200
committerSzczepan Zalega <szczepan@nitrokey.com>2020-09-21 16:33:53 +0200
commit61bcd3c68353f36b3754b1d3a66a746f5611ddde (patch)
tree4f59bc104a28d09dc8965118352776fe19c261c6 /unittest
parenta8e6dbfdc0169d3e5286e80450c8fedf7fa1cc24 (diff)
downloadlibnitrokey-61bcd3c68353f36b3754b1d3a66a746f5611ddde.tar.gz
libnitrokey-61bcd3c68353f36b3754b1d3a66a746f5611ddde.tar.bz2
Add test for Storage#98
Diffstat (limited to 'unittest')
-rw-r--r--unittest/test_storage_issue98.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/unittest/test_storage_issue98.py b/unittest/test_storage_issue98.py
new file mode 100644
index 0000000..fd90803
--- /dev/null
+++ b/unittest/test_storage_issue98.py
@@ -0,0 +1,85 @@
+import os
+from time import sleep
+
+import pexpect
+import pytest
+
+from conftest import skip_if_device_version_lower_than
+from constants import DefaultPasswords, DeviceErrorCode
+from helpers import helper_populate_device, \
+ helper_check_device_for_data
+from misc import gs
+
+
+def reinsert():
+ uhub_path = os.getenv('LIBNK_UHUBPATH', 'uhubctl')
+ uhub_loc = os.getenv('LIBNK_UHUBLOC', None) # e.g. -p 1 --loc 5-4
+ assert uhub_path
+ assert uhub_loc
+ uhub_path = f'{uhub_path} {uhub_loc} '
+ param_on = ' -a 1 '
+ param_off = ' -a 0 '
+ print(f"Reinsert device: {uhub_path + param_off}")
+ pexpect.run(uhub_path + param_off)
+ my_sleep(1)
+ print(f"Reinsert device: {uhub_path + param_on}")
+ pexpect.run(uhub_path + param_on)
+ my_sleep(1)
+
+def get_status(C) -> str:
+ status = C.NK_get_status_as_string()
+ s = gs(status)
+ return s
+
+def my_sleep(t:int):
+ print(f"Sleep for {t} seconds")
+ sleep(t)
+
+@pytest.mark.other
+@pytest.mark.firmware
+def test_data_lost(C):
+ """
+ Test case for issue: https://github.com/Nitrokey/nitrokey-storage-firmware/issues/98
+ """
+
+ skip_if_device_version_lower_than({'S': 54})
+
+ # print(f"Status: {get_status(C)}")
+
+ # helper_populate_device(C)
+ helper_check_device_for_data(C)
+
+ commands = [
+ lambda: C.NK_unlock_encrypted_volume(DefaultPasswords.USER),
+ lambda: C.NK_set_unencrypted_read_only_admin(DefaultPasswords.ADMIN),
+ lambda: C.NK_set_unencrypted_read_write_admin(DefaultPasswords.ADMIN),
+ lambda: C.NK_set_encrypted_read_only(DefaultPasswords.ADMIN),
+ lambda: C.NK_set_encrypted_read_write(DefaultPasswords.ADMIN),
+ lambda: C.NK_clear_new_sd_card_warning(DefaultPasswords.ADMIN),
+ lambda: C.NK_send_startup(4242),
+ lambda: C.NK_lock_encrypted_volume(),
+ ]
+
+ iterations = 0
+ failed = []
+ while True:
+ iterations += 1
+ print(f"*** Starting iteration {iterations} (failed [{len(failed)}]{failed})")
+
+ for cmd_i , c in enumerate(commands):
+ assert c() == DeviceErrorCode.STATUS_OK
+ my_sleep(1)
+ reinsert()
+
+ try:
+ helper_check_device_for_data(C)
+ except Exception as e:
+ print(f"Check failed: {str(e)}")
+ print("".center(80, "*"))
+ print("".center(80, "*"))
+ print("".center(80, "*"))
+ my_sleep(3)
+ failed += cmd_i
+ # print(f"Status: {get_status(C)}")
+ assert C.NK_lock_device() == DeviceErrorCode.STATUS_OK
+ my_sleep(1)