33import pytest
44
55from nrfcredstore .command_interface import CredentialCommandInterface , ATCommandInterface , TLSCredShellInterface
6+ from nrfcredstore .credstore import CredType
67
78@pytest .fixture
89def comms ():
@@ -13,11 +14,141 @@ def comms():
1314@pytest .fixture
1415def at_command_interface (comms ):
1516 """Mock command interface"""
16- command_interface = ATCommandInterface (comms )
17- return command_interface
17+ interface = ATCommandInterface (comms )
18+ return interface
1819
19- def test_write_raw (at_command_interface ):
20+ @pytest .fixture
21+ def tls_cred_shell_interface (comms ):
22+ """Mock TLSCredShellInterface"""
23+ interface = TLSCredShellInterface (comms )
24+ return interface
25+
26+ def test_write_raw_at (at_command_interface ):
2027 """Test writing raw AT commands"""
2128 at_command_interface .comms .write_line = Mock ()
2229 at_command_interface .write_raw ('AT+TEST=1' )
2330 at_command_interface .comms .write_line .assert_called_once_with ('AT+TEST=1' )
31+
32+ def test_write_raw_tls (tls_cred_shell_interface ):
33+ """Test writing raw TLS commands"""
34+ tls_cred_shell_interface .comms .write_line = Mock ()
35+ tls_cred_shell_interface .write_raw ('AT+TEST=1' )
36+ tls_cred_shell_interface .comms .write_line .assert_called_once_with ('AT+TEST=1' )
37+
38+ def test_write_credential_at (at_command_interface ):
39+ """Test writing a credential using ATCommandInterface"""
40+ at_command_interface .comms .write_line = Mock ()
41+ at_command_interface .comms .expect_response .return_value = (True , "" )
42+ at_command_interface .write_credential (sectag = 42 , cred_type = CredType .CLIENT_CERT .value , cred_text = 'test_value' )
43+ at_command_interface .comms .write_line .assert_called_once_with ('AT%CMNG=0,42,1,"test_value"' )
44+
45+ def test_delete_credential_at (at_command_interface ):
46+ """Test deleting a credential using ATCommandInterface"""
47+ at_command_interface .comms .write_line = Mock ()
48+ at_command_interface .comms .expect_response .return_value = (True , "" )
49+ at_command_interface .delete_credential (sectag = 42 , cred_type = CredType .CLIENT_CERT .value )
50+ at_command_interface .comms .write_line .assert_called_once_with ('AT%CMNG=3,42,1' )
51+
52+ def test_check_credential_exists_at (at_command_interface ):
53+ """Test checking if a credential exists using ATCommandInterface"""
54+ at_command_interface .comms .write_line = Mock ()
55+ at_command_interface .comms .expect_response .return_value = (True , '%CMNG: 42,1,"8CEA57609B0F95C0D0F80383A7A21ECD1C6E102FDCC3CDCEB1948B0EA828601D"' )
56+ exists , sha = at_command_interface .check_credential_exists (sectag = 42 , cred_type = CredType .CLIENT_CERT .value )
57+ assert exists is True
58+ assert sha == '8CEA57609B0F95C0D0F80383A7A21ECD1C6E102FDCC3CDCEB1948B0EA828601D'
59+
60+ def test_get_csr_at (at_command_interface ):
61+ """Test getting a CSR using ATCommandInterface"""
62+ at_command_interface .comms .write_line = Mock ()
63+ at_command_interface .comms .expect_response .return_value = (True , '%KEYGEN: "foo.bar"' )
64+ csr = at_command_interface .get_csr (sectag = 42 , attributes = 'O=Test,CN=Device' )
65+ assert csr == 'foo.bar'
66+ at_command_interface .comms .write_line .assert_called_once_with ('AT%KEYGEN=42,2,0,"O=Test,CN=Device"' )
67+
68+ def test_get_csr_no_attributes (at_command_interface ):
69+ """Test getting a CSR without attributes using ATCommandInterface"""
70+ at_command_interface .comms .write_line = Mock ()
71+ at_command_interface .comms .expect_response .return_value = (True , '%KEYGEN: "foo.bar"' )
72+ csr = at_command_interface .get_csr (sectag = 42 )
73+ assert csr == 'foo.bar'
74+ at_command_interface .comms .write_line .assert_called_once_with ('AT%KEYGEN=42,2,0' )
75+
76+ def test_get_imei (at_command_interface ):
77+ """Test getting IMEI using ATCommandInterface"""
78+ at_command_interface .comms .write_line = Mock ()
79+ at_command_interface .comms .expect_response .return_value = (True , '123456789012345' )
80+ imei = at_command_interface .get_imei ()
81+ assert imei == '123456789012345'
82+ at_command_interface .comms .write_line .assert_called_once_with ('AT+CGSN' )
83+
84+ def test_go_offline (at_command_interface ):
85+ """Test going offline using ATCommandInterface"""
86+ at_command_interface .comms .write_line = Mock ()
87+ at_command_interface .comms .expect_response .return_value = (True , '' )
88+ at_command_interface .go_offline ()
89+ at_command_interface .comms .write_line .assert_called_once_with ('AT+CFUN=4' )
90+
91+ def test_get_model_id (at_command_interface ):
92+ """Test getting model ID using ATCommandInterface"""
93+ at_command_interface .comms .write_line = Mock ()
94+ at_command_interface .comms .expect_response .return_value = (True , 'nRF9151-LACA' )
95+ model_id = at_command_interface .get_model_id ()
96+ assert model_id == 'nRF9151-LACA'
97+ at_command_interface .comms .write_line .assert_called_once_with ('AT+CGMM' )
98+
99+ def test_get_mfw_version (at_command_interface ):
100+ """Test getting MFW version using ATCommandInterface"""
101+ at_command_interface .comms .write_line = Mock ()
102+ at_command_interface .comms .expect_response .return_value = (True , 'mfw_nrf91x1_2.0.2' )
103+ mfw_version = at_command_interface .get_mfw_version ()
104+ assert mfw_version == 'mfw_nrf91x1_2.0.2'
105+ at_command_interface .comms .write_line .assert_called_once_with ('AT+CGMR' )
106+
107+ def test_get_attestation_token (at_command_interface ):
108+ """Test getting attestation token using ATCommandInterface"""
109+ at_command_interface .comms .write_line = Mock ()
110+ at_command_interface .comms .expect_response .return_value = (True , '%ATTESTTOKEN: "foo.bar"' )
111+ attestation_token = at_command_interface .get_attestation_token ()
112+ assert attestation_token == 'foo.bar'
113+ at_command_interface .comms .write_line .assert_called_once_with ('AT%ATTESTTOKEN' )
114+
115+ def test_enable_error_codes (at_command_interface ):
116+ """Test enabling error codes using ATCommandInterface"""
117+ at_command_interface .comms .write_line = Mock ()
118+ at_command_interface .comms .expect_response .return_value = (True , '' )
119+ at_command_interface .enable_error_codes ()
120+ at_command_interface .comms .write_line .assert_called_once_with ('AT+CMEE=1' )
121+
122+ class MockCommsAT :
123+ """Mock comms for ATCommandInterface"""
124+ def write_line (self , line ):
125+ self .last_written = line
126+
127+ def expect_response (self , ok_str = None , error_str = None , store_str = None , timeout = 15 , suppress_errors = False ):
128+ if self .last_written == 'AT+CGSN' :
129+ return True , '123456789012345'
130+ else :
131+ return False , ''
132+
133+ def test_detect_shell_mode_at (at_command_interface ):
134+ """Test detecting shell mode using ATCommandInterface (AT Host)"""
135+ at_command_interface .comms = MockCommsAT ()
136+ at_command_interface .detect_shell_mode ()
137+ assert at_command_interface .shell == False
138+
139+ class MockCommsATShell :
140+ """Mock comms for TLSCredShellInterface"""
141+ def write_line (self , line ):
142+ self .last_written = line
143+
144+ def expect_response (self , ok_str = None , error_str = None , store_str = None , timeout = 15 , suppress_errors = False ):
145+ if self .last_written == 'at AT+CGSN' :
146+ return True , '123456789012345'
147+ else :
148+ return False , ''
149+
150+ def test_detect_shell_mode_shell (at_command_interface ):
151+ """Test detecting shell mode using ATCommandInterface (AT Shell)"""
152+ at_command_interface .comms = MockCommsATShell ()
153+ at_command_interface .detect_shell_mode ()
154+ assert at_command_interface .shell == True
0 commit comments