Harrison Mutai | 19dc4f9 | 2024-05-10 16:54:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # type: ignore[attr-defined] |
| 3 | |
| 4 | # |
| 5 | # Copyright (c) 2024, Arm Limited. All rights reserved. |
| 6 | # |
| 7 | # SPDX-License-Identifier: BSD-3-Clause |
| 8 | # |
| 9 | |
| 10 | """ Common configurations and fixtures for test environment.""" |
| 11 | |
Harrison Mutai | 9f851e7 | 2024-12-02 15:06:55 +0000 | [diff] [blame] | 12 | from random import randint |
| 13 | |
Harrison Mutai | 19dc4f9 | 2024-05-10 16:54:29 +0000 | [diff] [blame] | 14 | import pytest |
Charlie Bareham | d7a9efc | 2024-06-17 11:58:03 +0100 | [diff] [blame] | 15 | import yaml |
Harrison Mutai | 19dc4f9 | 2024-05-10 16:54:29 +0000 | [diff] [blame] | 16 | from click.testing import CliRunner |
| 17 | |
| 18 | from tlc.cli import cli |
| 19 | |
| 20 | |
Harrison Mutai | 9f851e7 | 2024-12-02 15:06:55 +0000 | [diff] [blame] | 21 | def generate_random_bytes(n): |
| 22 | return bytes([randint(0, 255) for _ in range(n)]) |
| 23 | |
| 24 | |
Harrison Mutai | 19dc4f9 | 2024-05-10 16:54:29 +0000 | [diff] [blame] | 25 | @pytest.fixture |
| 26 | def tmptlstr(tmpdir): |
| 27 | return tmpdir.join("tl.bin").strpath |
| 28 | |
| 29 | |
| 30 | @pytest.fixture |
Charlie Bareham | d7a9efc | 2024-06-17 11:58:03 +0100 | [diff] [blame] | 31 | def tmpyamlconfig(tmpdir): |
| 32 | return tmpdir.join("config.yaml").strpath |
| 33 | |
| 34 | |
| 35 | @pytest.fixture |
Harrison Mutai | 19dc4f9 | 2024-05-10 16:54:29 +0000 | [diff] [blame] | 36 | def tmpfdt(tmpdir): |
| 37 | fdt = tmpdir.join("fdt.dtb") |
| 38 | fdt.write_binary(b"\x00" * 100) |
| 39 | return fdt |
| 40 | |
| 41 | |
Charlie Bareham | d7a9efc | 2024-06-17 11:58:03 +0100 | [diff] [blame] | 42 | @pytest.fixture(params=[1, 2, 3, 4, 5, 0x100, 0x101, 0x102, 0x104]) |
| 43 | def non_empty_tag_id(request): |
| 44 | return request.param |
| 45 | |
| 46 | |
| 47 | @pytest.fixture |
| 48 | def tmpyamlconfig_blob_file(tmpdir, tmpfdt, non_empty_tag_id): |
| 49 | config_path = tmpdir.join("config.yaml") |
| 50 | |
| 51 | config = { |
| 52 | "has_checksum": True, |
| 53 | "max_size": 0x1000, |
| 54 | "entries": [ |
| 55 | { |
| 56 | "tag_id": non_empty_tag_id, |
| 57 | "blob_file_path": tmpfdt.strpath, |
| 58 | }, |
| 59 | ], |
| 60 | } |
| 61 | |
| 62 | with open(config_path, "w") as f: |
| 63 | yaml.safe_dump(config, f) |
| 64 | |
| 65 | return config_path |
| 66 | |
| 67 | |
Harrison Mutai | 19dc4f9 | 2024-05-10 16:54:29 +0000 | [diff] [blame] | 68 | @pytest.fixture |
| 69 | def tlcrunner(tmptlstr): |
| 70 | runner = CliRunner() |
| 71 | with runner.isolated_filesystem(): |
J-Alves | 81f0fb2 | 2024-11-27 15:57:08 +0000 | [diff] [blame] | 72 | runner.invoke(cli, ["create", "--size", 0x1F000, tmptlstr]) |
Harrison Mutai | 19dc4f9 | 2024-05-10 16:54:29 +0000 | [diff] [blame] | 73 | return runner |
| 74 | |
| 75 | |
| 76 | @pytest.fixture |
| 77 | def tlc_entries(tmpfdt): |
| 78 | return [(0, "/dev/null"), (1, tmpfdt.strpath), (0x102, tmpfdt.strpath)] |
Harrison Mutai | 9f851e7 | 2024-12-02 15:06:55 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | @pytest.fixture |
| 82 | def random_entry(): |
| 83 | def _random_entry(max_size): |
| 84 | return randint(0, 0xFFFFFF), generate_random_bytes(randint(0, max_size)) |
| 85 | |
| 86 | return _random_entry |
| 87 | |
| 88 | |
| 89 | @pytest.fixture |
| 90 | def random_entries(random_entry): |
| 91 | def _random_entries(n=5, max_size=0x100): |
| 92 | for _ in range(n): |
| 93 | yield random_entry(max_size) |
| 94 | |
| 95 | return _random_entries |