aboutsummaryrefslogtreecommitdiff
path: root/unittest/test_storage_issue98.py
blob: 3448e948ed9ec817e257551e0bada18ac1569af2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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 = []
    errcnt = 0
    while True:
        iterations += 1
        print(f"*** Starting iteration {iterations} (failed {errcnt} [{len(failed)}]{failed})")

        for cmd_i , c in enumerate(commands):
            errcnt += 0 if c() == DeviceErrorCode.STATUS_OK else 1
            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.append(cmd_i)
        # print(f"Status: {get_status(C)}")
        assert C.NK_lock_device() == DeviceErrorCode.STATUS_OK
        my_sleep(1)