You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					75 lines
				
				2.5 KiB
			
		
		
			
		
	
	
					75 lines
				
				2.5 KiB
			| 
											5 years ago
										 | import json
 | ||
|  | from Crypto.PublicKey import RSA
 | ||
|  | from pathlib import Path
 | ||
|  | 
 | ||
| 
											2 years ago
										 | from openpilot.common.params import Params
 | ||
| 
											1 year ago
										 | from openpilot.system.athena.registration import register, UNREGISTERED_DONGLE_ID
 | ||
|  | from openpilot.system.athena.tests.helpers import MockResponse
 | ||
| 
											2 years ago
										 | from openpilot.system.hardware.hw import Paths
 | ||
| 
											5 years ago
										 | 
 | ||
|  | 
 | ||
| 
											1 year ago
										 | class TestRegistration:
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											1 year ago
										 |   def setup_method(self):
 | ||
| 
											5 years ago
										 |     # clear params and setup key paths
 | ||
|  |     self.params = Params()
 | ||
|  |     self.params.clear_all()
 | ||
|  | 
 | ||
| 
											2 years ago
										 |     persist_dir = Path(Paths.persist_root()) / "comma"
 | ||
|  |     persist_dir.mkdir(parents=True, exist_ok=True)
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											2 years ago
										 |     self.priv_key = persist_dir / "id_rsa"
 | ||
|  |     self.pub_key = persist_dir / "id_rsa.pub"
 | ||
| 
											5 years ago
										 | 
 | ||
|  |   def _generate_keys(self):
 | ||
|  |     self.pub_key.touch()
 | ||
|  |     k = RSA.generate(2048)
 | ||
|  |     with open(self.priv_key, "wb") as f:
 | ||
|  |       f.write(k.export_key())
 | ||
|  |     with open(self.pub_key, "wb") as f:
 | ||
|  |       f.write(k.publickey().export_key())
 | ||
|  | 
 | ||
| 
											1 year ago
										 |   def test_valid_cache(self, mocker):
 | ||
| 
											5 years ago
										 |     # if all params are written, return the cached dongle id
 | ||
|  |     self.params.put("IMEI", "imei")
 | ||
|  |     self.params.put("HardwareSerial", "serial")
 | ||
|  |     self._generate_keys()
 | ||
|  | 
 | ||
| 
											1 year ago
										 |     m = mocker.patch("openpilot.system.athena.registration.api_get", autospec=True)
 | ||
| 
											1 year ago
										 |     dongle = "DONGLE_ID_123"
 | ||
|  |     self.params.put("DongleId", dongle)
 | ||
|  |     assert register() == dongle
 | ||
|  |     assert not m.called
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											1 year ago
										 |   def test_no_keys(self, mocker):
 | ||
| 
											4 years ago
										 |     # missing pubkey
 | ||
| 
											1 year ago
										 |     m = mocker.patch("openpilot.system.athena.registration.api_get", autospec=True)
 | ||
| 
											1 year ago
										 |     dongle = register()
 | ||
|  |     assert m.call_count == 0
 | ||
|  |     assert dongle == UNREGISTERED_DONGLE_ID
 | ||
|  |     assert self.params.get("DongleId", encoding='utf-8') == dongle
 | ||
| 
											4 years ago
										 | 
 | ||
| 
											1 year ago
										 |   def test_missing_cache(self, mocker):
 | ||
| 
											5 years ago
										 |     # keys exist but no dongle id
 | ||
|  |     self._generate_keys()
 | ||
| 
											1 year ago
										 |     m = mocker.patch("openpilot.system.athena.registration.api_get", autospec=True)
 | ||
| 
											1 year ago
										 |     dongle = "DONGLE_ID_123"
 | ||
|  |     m.return_value = MockResponse(json.dumps({'dongle_id': dongle}), 200)
 | ||
|  |     assert register() == dongle
 | ||
|  |     assert m.call_count == 1
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											1 year ago
										 |     # call again, shouldn't hit the API this time
 | ||
|  |     assert register() == dongle
 | ||
|  |     assert m.call_count == 1
 | ||
|  |     assert self.params.get("DongleId", encoding='utf-8') == dongle
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											1 year ago
										 |   def test_unregistered(self, mocker):
 | ||
| 
											4 years ago
										 |     # keys exist, but unregistered
 | ||
|  |     self._generate_keys()
 | ||
| 
											1 year ago
										 |     m = mocker.patch("openpilot.system.athena.registration.api_get", autospec=True)
 | ||
| 
											1 year ago
										 |     m.return_value = MockResponse(None, 402)
 | ||
|  |     dongle = register()
 | ||
|  |     assert m.call_count == 1
 | ||
|  |     assert dongle == UNREGISTERED_DONGLE_ID
 | ||
|  |     assert self.params.get("DongleId", encoding='utf-8') == dongle
 |