Skip to content

Commit 10065f7

Browse files
committed
all lights green!
1 parent 7290ba3 commit 10065f7

File tree

4 files changed

+32
-82
lines changed

4 files changed

+32
-82
lines changed

src/nrfcredstore/command_interface.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,12 @@ def get_attestation_token(self):
186186
attest_tok = output.split('"')[1]
187187
return attest_tok
188188

189-
def get_csr(self, sectag=0, cn=""):
190-
191-
# provide attributes parameter if a custom CN is specified
192-
attr = f',"CN={cn}"' if len(cn) else ''
193-
194-
self.at_command(f'AT%KEYGEN={sectag},2,0{attr}')
189+
def get_csr(self, sectag=0, attributes=""):
190+
if attributes:
191+
self.at_command(f'AT%KEYGEN={sectag},2,0,"{attributes}"')
192+
else:
193+
self.at_command(f'AT%KEYGEN={sectag},2,0')
195194

196-
# include the CR in OK because 'OK' could be found in the CSR string
197195
retval, output = self.comms.expect_response("OK", "ERROR", "%KEYGEN:")
198196

199197
if not retval:

src/nrfcredstore/credstore.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,14 @@ def delete(self, tag: int, type: CredType):
9797

9898
def keygen(self, tag: int, file: io.BufferedIOBase, attributes: str = ''):
9999
"""Generate a new private key and return a certificate signing request in DER format"""
100-
cmd = f'AT%KEYGEN={tag},2,0'
101100

102-
if attributes != '':
103-
cmd = f'{cmd},"{attributes}"'
101+
keygen_output = self.command_interface.get_csr(sectag=tag, attributes=attributes)
104102

105-
self.command_interface.at_command(cmd, wait_for_result=False)
106-
result, response = self.command_interface.comms.expect_response("OK", "ERROR", "%KEYGEN: ")
107-
108-
if not result:
103+
if not keygen_output:
109104
raise RuntimeError("Failed to generate key")
110105

111-
keygen_output = response.replace('%KEYGEN: "', '')
112106
csr_der_b64 = keygen_output.split('.')[0]
113107
csr_der_bytes = base64.urlsafe_b64decode(csr_der_b64 + '===')
114-
file.write(csr_der_bytes)
115108

109+
file.write(csr_der_bytes)
116110
file.close()

tests/test_comms.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,3 @@
1-
"""
2-
What to test for comms module?
3-
4-
get_connected_nordic_boards
5-
* both device and hwid need to be emulated
6-
* macos doesn't provide unique hwid per port
7-
* empty list
8-
* some non-nordic comports
9-
* some nordic comports on mac, linux and windows ()
10-
* test both Thingy91x and JLink based kits
11-
12-
select_jlink
13-
* mock cases for no, one and multiple jlinks
14-
* figure out how to test with inquirer (can we emulate prompt answers?)
15-
16-
select_device_by_serial
17-
similar to the above
18-
19-
select_device
20-
* rtt enabled with and without snr
21-
* port given
22-
* serial number given
23-
* list all
24-
* no list-all, select from nordic devices: zero, one, two devices
25-
26-
test with emulated serial device
27-
* _init_serial
28-
* close
29-
* expect_response
30-
* reset_device: expect error
31-
* write_line
32-
* _readline_serial: returns stripped string, not bytes
33-
* _write_serial: blank forward
34-
35-
test with emulated rtt device
36-
* _init_rtt
37-
* close
38-
* expect_response
39-
* reset_device
40-
* _readline_rtt: internal line buffering
41-
* _write_rtt: chopping up of data
42-
43-
"""
44-
451
from unittest.mock import patch, Mock
462
from collections import namedtuple
473
import pytest

tests/test_credstore.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import io
22
import pytest
33

4-
from unittest.mock import Mock
4+
from unittest.mock import Mock, patch
55
from nrfcredstore.credstore import *
66
from nrfcredstore.exceptions import ATCommandError
77

@@ -44,10 +44,10 @@ def list_all_resp_blank_lines(self, cred_store):
4444
def ok_resp(self, cred_store):
4545
cred_store.command_interface.at_command.return_value = True
4646

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"']
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.get_csr.return_value = "Zm9v.YmFy"
5151

5252
@pytest.fixture
5353
def at_error(self, cred_store):
@@ -114,8 +114,7 @@ def test_list_fail(self, cred_store, at_error_in_expect_response):
114114
cred_store.list()
115115

116116
def test_delete_success(self, cred_store, ok_resp):
117-
response = cred_store.delete(567890, CredType(1))
118-
assert response == True
117+
cred_store.delete(567890, CredType(1))
119118
self.command_interface.at_command.assert_called_with('AT%CMNG=3,567890,1', wait_for_result=True)
120119

121120
def test_delete_fail(self, cred_store, at_error):
@@ -142,21 +141,24 @@ def test_write_any_type_fail(self, cred_store):
142141
with pytest.raises(ValueError):
143142
cred_store.write(567890, CredType.ANY, io.StringIO())
144143

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)
144+
def test_generate_sends_keygen_cmd(self, cred_store, csr_resp):
145+
fake_binary_file = Mock()
146+
cred_store.keygen(12345678, fake_binary_file)
147+
self.command_interface.get_csr.assert_called_with(sectag=12345678, attributes='')
149148

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"')
149+
def test_generate_with_attributes(self, cred_store, csr_resp):
150+
cred_store.keygen(12345678, Mock(), 'O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice')
151+
self.command_interface.get_csr.assert_called_with(
152+
sectag=12345678,
153+
attributes="O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice"
154+
)
154155

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')
156+
def test_generate_writes_csr_to_stream(self, cred_store, csr_resp):
157+
fake_binary_file = Mock()
158+
cred_store.keygen(12345678, fake_binary_file)
159+
fake_binary_file.write.assert_called_with(b'foo')
159160

160-
def test_generate_fail(self, cred_store, at_error_in_expect_response):
161-
with pytest.raises(RuntimeError):
162-
cred_store.keygen(12345678, Mock())
161+
def test_generate_fail(self, cred_store):
162+
with patch.object(cred_store.command_interface, 'get_csr', return_value=None):
163+
with pytest.raises(RuntimeError):
164+
cred_store.keygen(12345678, Mock())

0 commit comments

Comments
 (0)