def test_invalid_computed_field_handler(): """Test invalid case for computed filed handler.""" cmpa = CMPA(device="lpc55s6x") fields = {"test_field": "invalid_handler"} with pytest.raises(SPSDKError): cmpa.reg_computed_fields_handler(b"\x00", fields)
def test_set_config_invalid(data_dir): """Test invalid cases for set_config.""" cmpa = CMPA(device="lpc55s6x") cfg = PfrConfiguration(data_dir + "/bad_dev.yml") with pytest.raises(SPSDKPfrConfigError): cmpa.set_config(cfg) cfg = PfrConfiguration(data_dir + "/bad_rev.yml") with pytest.raises(SPSDKPfrConfigError): cmpa.set_config(cfg) cfg = PfrConfiguration() cfg.device = cmpa.device cfg.revision = cmpa.revision cfg.type = "INV" with pytest.raises(SPSDKPfrConfigError): cmpa.set_config(cfg) cfg = PfrConfiguration() cfg.device = cmpa.device cfg.revision = cmpa.revision cfg.type = "CMPA" cfg.settings = None with pytest.raises(SPSDKPfrConfigError): cmpa.set_config(cfg)
def test_invalid_key_size(data_dir): """Test Invalid Key size for ROTKH computation""" cfpa = CMPA("lpc55s6x") keys_path = [ data_dir + "/ec_secp384r1_cert0.pem", data_dir + "/ec_secp384r1_cert1.pem", data_dir + "/ec_secp384r1_cert2.pem", data_dir + "/ec_secp384r1_cert3.pem", ] with pytest.raises(SPSDKPfrError): cfpa.export(keys=extract_public_keys(keys_path, password=None))
def test_generate_cmpa(data_dir): config = json.loads(load_file(data_dir, 'cmpa_96mhz.json')) binary = load_file(data_dir, 'CMPA_96MHz.bin', mode='rb') key = load_pem_private_key(load_file(data_dir, 'selfsign_privatekey_rsa2048.pem', mode='rb'), password=None, backend=default_backend()) cmpa = CMPA('lpc55s6x', keys=[key.public_key()], user_config=config['settings']) assert binary == cmpa.export(add_seal=False, compute_inverses=True)
def test_config_no_device_no_revision(): cfg = PfrConfiguration(device="lpc55s6x") cfg.device = "lpc55s6x" cmpa = CMPA(user_config=cfg) cmpa.device = None with pytest.raises(SPSDKError, match="No device provided"): cmpa.set_config(cfg) cmpa.device = "lpc55s6x" cmpa.revision = None with pytest.raises(SPSDKError, match="No revision provided"): cmpa.set_config(cfg)
def test_lpc55s3x_binary_ec384(data_dir): """Test silicon LPC55S3x ECC384. Binary generation/ROTKH computation""" cfpa = CMPA("lpc55s3x") keys_path = [ data_dir + "/ec_secp384r1_cert0.pem", data_dir + "/ec_secp384r1_cert1.pem", data_dir + "/ec_secp384r1_cert2.pem", data_dir + "/ec_secp384r1_cert3.pem", ] data = cfpa.export(keys=extract_public_keys(keys_path, password=None)) assert len(data) == 512 with open(data_dir + "/lpc55s3x_CMPA_384.bin", "rb") as binary: assert data == binary.read()
def test_config_cmpa(): """Test PFR tool - Test CMPA configuration.""" cmpa = CMPA("lpc55s6x") config = cmpa.generate_config() config2 = cmpa.generate_config(exclude_computed=False) assert config != config2 cmpa2 = CMPA("lpc55s6x", user_config=PfrConfiguration(config2)) cmpa2.parse(bytes(512)) out = cmpa2.get_yaml_config(exclude_computed=False) assert out == config2
def test_json_yml_configs(data_dir): """Test of JSON and YML configuration, it must be equal.""" cmpa_json = CMPA( "lpc55s6x", user_config=PfrConfiguration(f"{data_dir}/cmpa_96mhz.json")) cmpa_yml = CMPA("lpc55s6x", user_config=PfrConfiguration(f"{data_dir}/cmpa_96mhz.yml")) assert cmpa_yml.get_yaml_config(False) == cmpa_json.get_yaml_config(False) assert cmpa_yml.get_yaml_config(True) == cmpa_json.get_yaml_config(True)
def test_config_cmpa(): cmpa = CMPA('lpc55s6x') config = cmpa.generate_config() config2 = cmpa.generate_config(exclude_computed=False) assert config != config2 cmpa2 = CMPA('lpc55s6x', user_config=config2) out = cmpa2.parse(bytes(512), exclude_computed=False) assert out == config2
def test_config_various_revisions(): """Simple test to check is_invalid functionality.""" cfg = PfrConfiguration() cfg.device = "lpc55s6x" cfg.revision = None cfg.type = "CMPA" cfg.settings = {"BOOT_CFG": {"value": 0}} cmpa = CMPA(user_config=cfg) assert cmpa assert cmpa.revision cmpa.set_config(cfg) assert cmpa.revision cfg.revision = "latest" cmpa.set_config(cfg) assert cmpa.revision
def test_generate_cmpa(data_dir): """Test PFR tool - Generating CMPA binary.""" binary = load_file(data_dir, "CMPA_96MHz.bin", mode="rb") key = load_pem_private_key( load_file(data_dir, "selfsign_privatekey_rsa2048.pem", mode="rb"), password=None, backend=default_backend(), ) pfr_cfg_json = PfrConfiguration(os.path.join(data_dir, "cmpa_96mhz.json")) cmpa_json = CMPA("lpc55s6x", user_config=pfr_cfg_json) assert binary == cmpa_json.export(add_seal=False, keys=[key.public_key()]) pfr_cfg_yml = PfrConfiguration(os.path.join(data_dir, "cmpa_96mhz.yml")) cmpa_yml = CMPA("lpc55s6x", user_config=pfr_cfg_yml) assert binary == cmpa_yml.export(add_seal=False, keys=[key.public_key()])
def test_config_invalid_yaml_config(): cfg = PfrConfiguration(device="lpc55s6x") cmpa = CMPA("lpc55s6x", user_config=cfg) cmpa.parse(bytes(512)) cmpa.user_config.device = None with pytest.raises(SPSDKError, match="Device not found"): cmpa.get_yaml_config(exclude_computed=False) cfg = PfrConfiguration(device="lpc55s6x") cmpa = CMPA("lpc55s6x", user_config=cfg) cmpa.parse(bytes(512)) cmpa.user_config.type = None with pytest.raises(SPSDKError, match="Type not found"): cmpa.get_yaml_config(exclude_computed=False)
def test_basic_cmpa(): cmpa = CMPA('lpc55s6x') with pytest.raises(AssertionError): cmpa.export()
def test_supported_devices(): cfpa_devices = CFPA.devices() cmpa_devices = CMPA.devices() assert sorted(cmpa_devices) == sorted(cfpa_devices)
def test_config_cmpa_yml(tmpdir): """Test PFR tool - Test CMPA configuration from YAML.""" yaml = YAML() yaml.indent(sequence=4, offset=2) cmpa = CMPA("lpc55s6x") config = cmpa.get_yaml_config(exclude_computed=True) with open(tmpdir + "/config.yml", "w") as yml_file: yaml.dump(config, yml_file) config2 = cmpa.get_yaml_config(exclude_computed=False) with open(tmpdir + "/config2.yml", "w") as yml_file: yaml.dump(config2, yml_file) assert not filecmp.cmp(tmpdir + "/config.yml", tmpdir + "/config2.yml") cmpa2 = CMPA("lpc55s6x") cmpa2_pfr_cfg = PfrConfiguration(tmpdir + "/config.yml") cmpa2.set_config(cmpa2_pfr_cfg) out_config = cmpa2.get_yaml_config(exclude_computed=True) with open(tmpdir + "/out_config.yml", "w") as yml_file: yaml.dump(out_config, yml_file) assert filecmp.cmp(tmpdir + "/config.yml", tmpdir + "/out_config.yml") cmpa2_pfr_cfg = PfrConfiguration(tmpdir + "/config2.yml") cmpa2.set_config(cmpa2_pfr_cfg, raw=True) out_config2 = cmpa2.get_yaml_config(exclude_computed=False) with open(tmpdir + "/out_config2.yml", "w") as yml_file: yaml.dump(out_config2, yml_file) assert filecmp.cmp(tmpdir + "/config2.yml", tmpdir + "/out_config2.yml")
def test_address(): cmpa = CMPA('lpc55s6x') assert '0x9_E400' == cmpa.get_address(remove_underscore=False) assert '0x9E400' == cmpa.get_address(remove_underscore=True)
def test_cli_devices_global(): """Test PFR CLI - devices from global space.""" runner = CliRunner() result = runner.invoke(spsdk_apps.main, ["pfr", "devices"]) for device in CMPA.devices(): assert device in result.stdout
def test_cli_devices(): """Test PFR CLI - devices.""" runner = CliRunner() result = runner.invoke(cli.main, ["devices"]) for device in CMPA.devices(): assert device in result.stdout
def test_basic_cmpa(): """Test PFR tool - Test CMPA basis.""" CMPA("lpc55s6x")
def test_supported_devices(): """Test PFR tool - Getting supported devices.""" cfpa_devices = CFPA.devices() cmpa_devices = CMPA.devices() assert sorted(cmpa_devices) == sorted(cfpa_devices)
def test_cli_devices_global(): runner = CliRunner() result = runner.invoke(spsdk_apps.main, ['pfr', 'devices']) for device in CMPA.devices(): assert device in result.stdout
def test_get_data_for_html(data_dir): data = _get_data_for_html(CMPA('lpc55s6x')) schema = json.loads(read_file(data_dir, 'html_data.schema')) # in case of a failure, an exception is thrown jsonschema.validate(data, schema) assert True
def test_set_config_rev_latest(data_dir): """Test invalid cases for set_config.""" pfr_cfg = PfrConfiguration(data_dir + "/latest_rev.yml") cmpa = CMPA(user_config=pfr_cfg) cmpa.set_config(pfr_cfg) assert cmpa
def test_get_bitfields_ignore(): """Test invalid case for computed filed handler.""" cmpa = CMPA(device="lpc55s6x") cmpa.config.config.pop("ignored_fields", None) assert cmpa.generate_config()
def test_generate_html(): data = _get_data_for_html(CMPA('lpc55s6x')) html = _generate_html('CMPA', data) assert "<h1>CMPA</h1>" in html
def test_cli_devices(): runner = CliRunner() result = runner.invoke(cli.main, ['devices']) for device in CMPA.devices(): assert device in result.stdout