Skip to content

Commit 7290ba3

Browse files
committed
wip credstore tests
1 parent 3861820 commit 7290ba3

File tree

2 files changed

+62
-54
lines changed

2 files changed

+62
-54
lines changed

src/nrfcredstore/credstore.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def write(self, tag: int, type: CredType, file: io.TextIOBase):
8181
if type == CredType.ANY:
8282
raise ValueError
8383
cert = file.read().rstrip()
84-
return self.command_interface.at_command(f'AT%CMNG=0,{tag},{type.value},"{cert}"', wait_for_result=True)
84+
if not self.command_interface.at_command(f'AT%CMNG=0,{tag},{type.value},"{cert}"', wait_for_result=True):
85+
raise RuntimeError("Failed to write credential")
8586

8687
def delete(self, tag: int, type: CredType):
8788
"""Delete a credential from the modem
@@ -91,7 +92,8 @@ def delete(self, tag: int, type: CredType):
9192

9293
if type == CredType.ANY:
9394
raise ValueError
94-
return self.command_interface.at_command(f'AT%CMNG=3,{tag},{type.value}', wait_for_result=True)
95+
if not self.command_interface.at_command(f'AT%CMNG=3,{tag},{type.value}', wait_for_result=True):
96+
raise RuntimeError("Failed to delete credential")
9597

9698
def keygen(self, tag: int, file: io.BufferedIOBase, attributes: str = ''):
9799
"""Generate a new private key and return a certificate signing request in DER format"""

tests/test_credstore.py

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,71 @@ class TestCredStore:
1515

1616
@pytest.fixture
1717
def cred_store(self):
18-
self.at_client = Mock()
19-
return CredStore(self.at_client)
18+
self.command_interface = Mock()
19+
return CredStore(self.command_interface)
2020

2121
@pytest.fixture
2222
def list_all_resp(self, cred_store):
23-
cred_store.at_client.at_command.return_value = [
24-
'%CMNG: 12345678, 0, "978C...02C4"',
25-
'%CMNG: 567890, 1, "C485...CF09"'
26-
]
23+
cred_store.command_interface.comms.expect_response.return_value = (
24+
True,
25+
"\r\f".join([
26+
'%CMNG: 12345678, 0, "978C...02C4"',
27+
'%CMNG: 567890, 1, "C485...CF09"'
28+
])
29+
)
2730

2831
@pytest.fixture
2932
def list_all_resp_blank_lines(self, cred_store):
30-
cred_store.at_client.at_command.return_value = [
31-
'',
32-
'%CMNG: 12345678, 0, "978C...02C4"',
33-
'%CMNG: 567890, 1, "C485...CF09"',
34-
''
35-
]
33+
cred_store.command_interface.comms.expect_response.return_value = (
34+
True,
35+
"\r\f".join([
36+
'',
37+
'%CMNG: 12345678, 0, "978C...02C4"',
38+
'%CMNG: 567890, 1, "C485...CF09"',
39+
''
40+
])
41+
)
3642

3743
@pytest.fixture
3844
def ok_resp(self, cred_store):
39-
cred_store.at_client.at_command.return_value = []
45+
cred_store.command_interface.at_command.return_value = True
4046

41-
@pytest.fixture
42-
def csr_resp(self, cred_store):
43-
# KEYGEN value is base64-encoded 'foo' and base64-encoded 'bar' joined by '.'
44-
cred_store.at_client.at_command.return_value = ['', '%KEYGEN: "Zm9v.YmFy"']
47+
# @pytest.fixture
48+
# def csr_resp(self, cred_store):
49+
# # KEYGEN value is base64-encoded 'foo' and base64-encoded 'bar' joined by '.'
50+
# cred_store.command_interface.at_command.return_value = ['', '%KEYGEN: "Zm9v.YmFy"']
4551

4652
@pytest.fixture
4753
def at_error(self, cred_store):
48-
cred_store.at_client.at_command.side_effect = ATCommandError('')
54+
cred_store.command_interface.at_command.return_value = False
55+
56+
@pytest.fixture
57+
def at_error_in_expect_response(self, cred_store):
58+
cred_store.command_interface.comms.expect_response.return_value = (False, "")
4959

50-
def test_exposes_at_client(self, cred_store):
51-
assert cred_store.at_client is self.at_client
60+
def test_exposes_command_interface(self, cred_store):
61+
assert cred_store.command_interface is self.command_interface
5262

5363
def test_func_mode_offline(self, cred_store):
5464
cred_store.func_mode(4)
55-
self.at_client.at_command.assert_called_with('AT+CFUN=4')
65+
self.command_interface.at_command.assert_called_with('AT+CFUN=4', wait_for_result=True)
5666

5767
def test_func_mode_min(self, cred_store):
5868
cred_store.func_mode(0)
59-
self.at_client.at_command.assert_called_with('AT+CFUN=0')
60-
61-
def test_func_mode_fail(self, cred_store, at_error):
62-
with pytest.raises(ATCommandError):
63-
cred_store.func_mode(4)
69+
self.command_interface.at_command.assert_called_with('AT+CFUN=0', wait_for_result=True)
6470

6571
def test_list_sends_cmng_command(self, cred_store, list_all_resp):
6672
cred_store.list()
67-
self.at_client.at_command.assert_called_with('AT%CMNG=1')
73+
self.command_interface.at_command.assert_called_with('AT%CMNG=1', wait_for_result=False)
74+
self.command_interface.comms.expect_response.assert_called_with("OK", "ERROR", "%CMNG: ")
6875

6976
def test_list_with_tag_part_of_cmng(self, cred_store, list_all_resp):
7077
cred_store.list(12345678)
71-
self.at_client.at_command.assert_called_with('AT%CMNG=1,12345678')
78+
self.command_interface.at_command.assert_called_with('AT%CMNG=1,12345678', wait_for_result=False)
7279

7380
def test_list_with_tag_and_type_part_of_cmng(self, cred_store, list_all_resp):
7481
cred_store.list(12345678, CredType(0))
75-
self.at_client.at_command.assert_called_with('AT%CMNG=1,12345678,0')
82+
self.command_interface.at_command.assert_called_with('AT%CMNG=1,12345678,0', wait_for_result=False)
7683

7784
def test_list_credentials_contains_tag(self, cred_store, list_all_resp):
7885
first = cred_store.list()[0]
@@ -102,17 +109,17 @@ def test_list_type_without_tag(self, cred_store):
102109
with pytest.raises(RuntimeError):
103110
cred_store.list(None, CredType(0))
104111

105-
def test_list_fail(self, cred_store, at_error):
106-
with pytest.raises(ATCommandError):
112+
def test_list_fail(self, cred_store, at_error_in_expect_response):
113+
with pytest.raises(RuntimeError):
107114
cred_store.list()
108115

109116
def test_delete_success(self, cred_store, ok_resp):
110117
response = cred_store.delete(567890, CredType(1))
111-
assert len(response) == 0
112-
self.at_client.at_command.assert_called_with('AT%CMNG=3,567890,1')
118+
assert response == True
119+
self.command_interface.at_command.assert_called_with('AT%CMNG=3,567890,1', wait_for_result=True)
113120

114121
def test_delete_fail(self, cred_store, at_error):
115-
with pytest.raises(ATCommandError):
122+
with pytest.raises(RuntimeError):
116123
cred_store.delete(123, CredType(1))
117124

118125
def test_delete_any_type_fail(self, cred_store):
@@ -124,33 +131,32 @@ def test_write_success(self, cred_store, ok_resp):
124131
dGVzdA==
125132
-----END CERTIFICATE-----'''
126133
fake_file = io.StringIO(cert_text)
127-
response = cred_store.write(567890, CredType.CLIENT_KEY, fake_file)
128-
assert len(response) == 0
129-
self.at_client.at_command.assert_called_with(f'AT%CMNG=0,567890,2,"{cert_text}"')
134+
cred_store.write(567890, CredType.CLIENT_KEY, fake_file)
135+
self.command_interface.at_command.assert_called_with(f'AT%CMNG=0,567890,2,"{cert_text}"', wait_for_result=True)
130136

131137
def test_write_fail(self, cred_store, at_error):
132-
with pytest.raises(ATCommandError):
138+
with pytest.raises(RuntimeError):
133139
cred_store.write(567890, CredType.CLIENT_KEY, io.StringIO())
134140

135141
def test_write_any_type_fail(self, cred_store):
136142
with pytest.raises(ValueError):
137143
cred_store.write(567890, CredType.ANY, io.StringIO())
138144

139-
def test_generate_sends_keygen_cmd(self, cred_store, csr_resp):
140-
fake_binary_file = Mock()
141-
cred_store.keygen(12345678, fake_binary_file)
142-
self.at_client.at_command.assert_called_with(f'AT%KEYGEN=12345678,2,0')
145+
# def test_generate_sends_keygen_cmd(self, cred_store, csr_resp):
146+
# fake_binary_file = Mock()
147+
# cred_store.keygen(12345678, fake_binary_file)
148+
# self.command_interface.at_command.assert_called_with(f'AT%KEYGEN=12345678,2,0', wait_for_result=True)
143149

144-
def test_generate_with_attributes(self, cred_store, csr_resp):
145-
cred_store.keygen(12345678, Mock(), 'O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice')
146-
self.at_client.at_command.assert_called_with(
147-
f'AT%KEYGEN=12345678,2,0,"O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice"')
150+
# def test_generate_with_attributes(self, cred_store, csr_resp):
151+
# cred_store.keygen(12345678, Mock(), 'O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice')
152+
# self.command_interface.at_command.assert_called_with(
153+
# f'AT%KEYGEN=12345678,2,0,"O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice"')
148154

149-
def test_generate_writes_csr_to_stream(self, cred_store, csr_resp):
150-
fake_binary_file = Mock()
151-
cred_store.keygen(12345678, fake_binary_file)
152-
fake_binary_file.write.assert_called_with(b'foo')
155+
# def test_generate_writes_csr_to_stream(self, cred_store, csr_resp):
156+
# fake_binary_file = Mock()
157+
# cred_store.keygen(12345678, fake_binary_file)
158+
# fake_binary_file.write.assert_called_with(b'foo')
153159

154-
def test_generate_fail(self, cred_store, at_error):
155-
with pytest.raises(ATCommandError):
160+
def test_generate_fail(self, cred_store, at_error_in_expect_response):
161+
with pytest.raises(RuntimeError):
156162
cred_store.keygen(12345678, Mock())

0 commit comments

Comments
 (0)