Skip to content

Commit 11f18e5

Browse files
committed
wip
1 parent 66d53d7 commit 11f18e5

File tree

2 files changed

+36
-57
lines changed

2 files changed

+36
-57
lines changed

src/nrfcredstore/command_interface.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,14 @@ def set_shell_mode(self, shell):
9898

9999
def detect_shell_mode(self):
100100
"""Detect if the device is in shell mode or not."""
101-
for _ in range(3):
102-
self.write_raw("at AT+CGSN")
103-
result, output = self.comms.expect_response("OK", "ERROR", "", suppress_errors=True)
104-
if result and len(re.findall("[0-9]{15}", output)) > 0:
105-
self.set_shell_mode(True)
106-
return
107-
self.set_shell_mode(False)
101+
for cmd, shell_mode in [("at AT+CGSN", True), ("AT+CGSN", False)]:
102+
for _ in range(3):
103+
self.write_raw(cmd)
104+
result, output = self.comms.expect_response("OK", "ERROR", "", suppress_errors=True, timeout=1)
105+
if result and len(re.findall("[0-9]{15}", output)) > 0:
106+
self.set_shell_mode(shell_mode)
107+
return
108+
raise TimeoutError("Failed to detect shell mode. Device does not respond to AT commands.")
108109

109110
def enable_error_codes(self):
110111
"""Enable error codes in the AT client"""

tests/test_cli.py

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from unittest.mock import Mock, ANY, patch
44
from serial import SerialException
5-
from nrfcredstore.cli import main
5+
from nrfcredstore.cli import main, parse_args
66

77
from nrfcredstore.credstore import CredType
88
from nrfcredstore.exceptions import NoATClientException, ATCommandError
@@ -11,138 +11,116 @@
1111
class TestCli():
1212

1313
@pytest.fixture
14-
def at_client(self):
15-
at_client = Mock()
16-
return at_client
14+
def command_interface(self):
15+
command_interface = Mock()
16+
return command_interface
1717

1818
@pytest.fixture
19-
def credstore(self, at_client):
19+
def credstore(self, command_interface):
2020
credstore = Mock()
21-
credstore.at_client = at_client
21+
credstore.command_interface = command_interface
2222
return credstore
2323

24-
@pytest.fixture
25-
def offline(self, credstore):
26-
credstore.funk_mode.return_value = True
27-
2824
@pytest.fixture
2925
def empty_cred_list(self, credstore):
3026
credstore.list.return_value = []
3127

32-
def test_device_passed_to_connect(self, credstore, at_client, offline, empty_cred_list):
33-
main(['/dev/tty.usb', 'list'], credstore)
34-
at_client.connect.assert_called_with('/dev/tty.usb', ANY, ANY)
35-
36-
def test_baudrate_passed_to_connect(self, credstore, at_client, offline, empty_cred_list):
37-
main(['fakedev', '--baudrate', '9600', 'list'], credstore)
38-
at_client.connect.assert_called_with(ANY, 9600, ANY)
39-
40-
def test_timeout_passed_to_connect(self, credstore, at_client, offline, empty_cred_list):
41-
main(['fakedev', '--timeout', '3', 'list'], credstore)
42-
at_client.connect.assert_called_with(ANY, ANY, 3)
43-
44-
def test_at_client_verify(self, credstore, at_client, offline, empty_cred_list):
45-
main(['fakedev', 'list'], credstore)
46-
at_client.verify.assert_called()
47-
48-
@patch('builtins.print')
49-
def test_at_client_verify_fail(self, mock_print, credstore, at_client):
50-
at_client.verify.side_effect = NoATClientException()
28+
# non-responsive device
29+
def test_non_responsive_device(self, credstore, command_interface):
30+
command_interface.detect_shell_mode.side_effect = TimeoutError()
5131
with pytest.raises(SystemExit) as e:
52-
main(['fakedev', 'list'], credstore)
53-
assert 'does not respond to AT commands' in mock_print.call_args[0][0]
32+
main(parse_args(['fakedev', 'list']), credstore)
33+
assert e.type == SystemExit
34+
assert e.value.code == 12
5435

55-
def test_at_client_enable_error_codes(self, credstore, at_client, offline, empty_cred_list):
56-
main(['fakedev', 'list'], credstore)
57-
at_client.enable_error_codes.assert_called()
5836

5937
def test_list_default(self, credstore, offline, empty_cred_list):
60-
main(['fakedev', 'list'], credstore)
38+
main(parse_args(['fakedev', 'list']), credstore)
6139
credstore.list.assert_called_with(None, CredType.ANY)
6240

6341
def test_list_with_tag(self, credstore, offline, empty_cred_list):
64-
main(['fakedev', 'list', '--tag', '123'], credstore)
42+
main(parse_args(['fakedev', 'list', '--tag', '123']), credstore)
6543
credstore.list.assert_called_with(123, ANY)
6644

6745
def test_list_with_type(self, credstore, offline, empty_cred_list):
68-
main(['fakedev', 'list', '--tag', '123', '--type', 'CLIENT_KEY'], credstore)
46+
main(parse_args(['fakedev', 'list', '--tag', '123', '--type', 'CLIENT_KEY']), credstore)
6947
credstore.list.assert_called_with(ANY, CredType.CLIENT_KEY)
7048

7149
def test_write_tag_and_type(self, credstore, offline):
7250
credstore.write.return_value = True
73-
main(['fakedev', 'write', '123', 'ROOT_CA_CERT', 'tests/fixtures/root-ca.pem'], credstore)
51+
main(parse_args(['fakedev', 'write', '123', 'ROOT_CA_CERT', 'tests/fixtures/root-ca.pem']), credstore)
7452
credstore.write.assert_called_with(123, CredType.ROOT_CA_CERT, ANY)
7553

7654
@patch('builtins.open')
7755
def test_write_file(self, mock_file, credstore, offline):
7856
credstore.write.return_value = True
79-
main(['fakedev', 'write', '123', 'ROOT_CA_CERT', 'foo.pem'], credstore)
57+
main(parse_args(['fakedev', 'write', '123', 'ROOT_CA_CERT', 'foo.pem']), credstore)
8058
mock_file.assert_called_with('foo.pem', 'r', ANY, ANY, ANY)
8159

8260
@patch('builtins.open')
8361
def test_write_psk_file(self, mock_file, credstore, offline):
8462
credstore.write.return_value = True
85-
main(['fakedev', 'write', '123', 'PSK', 'foo.psk'], credstore)
63+
main(parse_args(['fakedev', 'write', '123', 'PSK', 'foo.psk']), credstore)
8664
mock_file.assert_called_with('foo.psk', 'r', ANY, ANY, ANY)
8765

8866
def test_delete(self, credstore, offline):
8967
credstore.delete.return_value = True
90-
main(['fakedev', 'delete', '123', 'CLIENT_KEY'], credstore)
68+
main(parse_args(['fakedev', 'delete', '123', 'CLIENT_KEY']), credstore)
9169
credstore.delete.assert_called_with(123, CredType.CLIENT_KEY)
9270

9371
def test_delete_any_should_fail(self, credstore, offline):
9472
with pytest.raises(SystemExit):
95-
main(['fakedev', 'delete', '123', 'ANY'], credstore)
73+
main(parse_args(['fakedev', 'delete', '123', 'ANY']), credstore)
9674

9775
@patch('builtins.open')
9876
def test_generate_tag(self, mock_file, credstore, offline):
9977
credstore.keygen.return_value = True
100-
main(['fakedev', 'generate', '123', 'foo.der'], credstore)
78+
main(parse_args(['fakedev', 'generate', '123', 'foo.der']), credstore)
10179
credstore.keygen.assert_called_with(123, ANY, ANY)
10280

10381
@patch('builtins.open')
10482
def test_generate_file(self, mock_file, credstore, offline):
10583
credstore.keygen.return_value = True
106-
main(['fakedev', 'generate', '123', 'foo.der'], credstore)
84+
main(parse_args(['fakedev', 'generate', '123', 'foo.der']), credstore)
10785
mock_file.assert_called_with('foo.der', 'wb', ANY, ANY, ANY)
10886

10987
@patch('builtins.open')
11088
def test_generate_with_attributes(self, credstore, offline):
11189
credstore.keygen.return_value = True
112-
main(['fakedev', 'generate', '123', 'foo.der', '--attributes', 'CN=foo'], credstore)
90+
main(parse_args(['fakedev', 'generate', '123', 'foo.der', '--attributes', 'CN=foo']), credstore)
11391
credstore.keygen.assert_called_with(123, ANY, 'CN=foo')
11492

11593
def test_no_at_client_exit_code(self, credstore, at_client):
11694
at_client.verify.side_effect = NoATClientException()
11795
with pytest.raises(SystemExit) as e:
118-
main(['fakedev', 'list'], credstore)
96+
main(parse_args(['fakedev', 'list']), credstore)
11997
assert e.type == SystemExit
12098
assert e.value.code == 10
12199

122100
def test_at_command_error_exit_code(self, credstore, at_client):
123101
at_client.verify.side_effect = ATCommandError()
124102
with pytest.raises(SystemExit) as e:
125-
main(['fakedev', 'list'], credstore)
103+
main(parse_args(['fakedev', 'list']), credstore)
126104
assert e.type == SystemExit
127105
assert e.value.code == 11
128106

129107
def test_timeout_error_exit_code(self, credstore, at_client):
130108
at_client.verify.side_effect = TimeoutError()
131109
with pytest.raises(SystemExit) as e:
132-
main(['fakedev', 'list'], credstore)
110+
main(parse_args(['fakedev', 'list']), credstore)
133111
assert e.type == SystemExit
134112
assert e.value.code == 12
135113

136114
def test_serial_exception_exit_code(self, credstore, at_client):
137115
at_client.connect.side_effect = SerialException()
138116
with pytest.raises(SystemExit) as e:
139-
main(['fakedev', 'list'], credstore)
117+
main(parse_args(['fakedev', 'list']), credstore)
140118
assert e.type == SystemExit
141119
assert e.value.code == 13
142120

143121
def test_unhandled_exception_exit_code(self, credstore, at_client):
144122
at_client.verify.side_effect = Exception()
145123
with pytest.raises(SystemExit) as e:
146-
main(['fakedev', 'list'], credstore)
124+
main(parse_args(['fakedev', 'list']), credstore)
147125
assert e.type == SystemExit
148126
assert e.value.code == 1

0 commit comments

Comments
 (0)