File: test_deviceinfo.py

package info (click to toggle)
pmbootstrap 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,864 kB
  • sloc: python: 17,395; sh: 425; makefile: 17
file content (211 lines) | stat: -rw-r--r-- 5,602 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Copyright 2024 Caleb Connolly
# SPDX-License-Identifier: GPL-3.0-or-later

import random
import tempfile
from pathlib import Path

import pytest

from pmb.config import deviceinfo_chassis_types
from pmb.parse.deviceinfo import Deviceinfo

# Exported from the wiki using https://www.convertcsv.com/html-table-to-csv.htm
# on 2024/10/23
deviceinfo_keys = [
    "format_version",
    "name",
    "manufacturer",
    "codename",
    "uboot_boardname",
    "year",
    "chassis",
    "dtb",
    "append_dtb",
    "external_storage",
    "flash_method",
    "arch",
    "dev_internal_storage",
    "dev_internal_storage_repartition",
    "dev_touchscreen",
    "dev_touchscreen_calibration",
    "keymaps",
    "swap_size_recommended",
    "zram_swap_pct",
    "tmp_as_tmpfs_size",
    "disable_dhcpd",
    "no_framebuffer",
    "initfs_compression",
    "create_initfs_extra",
    "getty",
    "gpu_accelerated",
    "super_partitions",
    "Variable",
    "flash_offset_base",
    "flash_offset_dtb",
    "flash_offset_kernel",
    "flash_offset_ramdisk",
    "flash_offset_second",
    "flash_offset_tags",
    "flash_pagesize",
    "flash_sparse",
    "flash_sparse_samsung_format",
    "flash_kernel_on_update",
    "kernel_cmdline",
    "kernel_cmdline_append",
    "bootimg_amazon_omap_header_size",
    "bootimg_blobpack",
    "bootimg_qcdt",
    "bootimg_qcdt_type",
    "bootimg_mtk_label_kernel",
    "bootimg_mtk_label_ramdisk",
    "bootimg_override_payload",
    "bootimg_override_initramfs",
    "bootimg_override_payload_compression",
    "deviceinfo_bootimg_prepend_dhtb",
    "bootimg_override_payload_append_dtb",
    "bootimg_dtb_second",
    "bootimg_append_seandroidenforce",
    "bootimg_pxa",
    "bootimg_custom_args",
    "header_version",
    "generate_bootimg",
    "generate_extlinux_config",
    "generate_grub_config",
    "generate_systemd_boot",
    "generate_uboot_fit_images",
    "generate_legacy_uboot_initfs",
    "legacy_uboot_load_address",
    "legacy_uboot_image_name",
    "flash_fastboot_partition_kernel",
    "flash_fastboot_partition_system",
    "flash_fastboot_partition_vbmeta",
    "flash_fastboot_partition_dtbo",
    "flash_fastboot_max_size",
    "flash_heimdall_partition_kernel",
    "flash_heimdall_partition_initfs",
    "flash_heimdall_partition_system",
    "flash_heimdall_partition_vbmeta",
    "flash_rk_partition_kernel",
    "flash_rk_partition_system",
    "flash_mtkclient_partition_kernel",
    "flash_mtkclient_partition_rootfs",
    "flash_mtkclient_partition_vbmeta",
    "flash_mtkclient_partition_dtbo",
    "boot_filesystem",
    "root_filesystem",
    "rootfs_image_sector_size",
    "sd_embed_firmware",
    "sd_embed_firmware_step_size",
    "partition_blacklist",
    "boot_part_start",
    "partition_type",
    "mkinitfs_postprocess",
    "Variable",
    "bootimg_vendor_dependent",
    "bootimg_vendor_android_boot_image",
    "bootimg_vendor_device_tree_identifiers",
    "Variable",
    "screen_width",
    "screen_height",
    "Variable",
    "usb_idVendor",
    "usb_idProduct",
    "usb_serialnumber",
    "usb_network_function",
    "usb_network_udc",
    "Variable",
    "cgpt_kpart",
    "cgpt_kpart_start",
    "cgpt_kpart_size",
    "depthcharge_board",
    "generate_depthcharge_image",
]

deprecated_keys = [
    "flash_methods",
    "external_disk",
    "external_disk_install",
    "msm_refresher",
    "flash_fastboot_vendor_id",
    "nonfree",
    "dev_keyboard",
    "date",
]

required_keys = [
    "codename",
    "chassis",
    "arch",
]

# Guaranteed by fair dice roll
random_values = [
    "",
    "test",
    "True",
    "false",
    "2020",
    "0xf100f",
    "843772384",
    "0",
    "1",
    "///@@",
    "/34u3294£$*R",
]


def random_deviceinfo_props(nprops: int = 10) -> dict:
    props = {}
    for _ in range(nprops):
        key = random.choice(deviceinfo_keys)
        value = random.choice(random_values)
        props[key] = value

    return props


def random_valid_deviceinfo(tmp_path: Path) -> Path:
    _, name = tempfile.mkstemp(dir=tmp_path)
    path = Path(name)

    info = random_deviceinfo_props(random.randint(1, len(deviceinfo_keys)))

    # Set the required properties
    # This would be the device package dir...
    info["codename"] = tmp_path.name[7:]
    info["chassis"] = random.choice(deviceinfo_chassis_types)
    info["arch"] = random.choice(["armhf", "aarch64", "x86_64"])
    info["header_version"] = random.randint(0, 4)
    info["initfs_compression"] = random.choice(
        ["zstd", "zstd:best", "lz4", "lzma", "gzip", "gzip:fast", "none"]
    )

    # Now write it all out to a file
    with open(path, "w") as f:
        f.writelines(f'deviceinfo_{key}="{value}"\n' for key, value in info.items())

    return path


# Test deviceinfo files that are technically valid but have bogus data
# These are expected to get more strict as the parser is improved
def test_random_valid_deviceinfos(tmp_path: Path) -> None:
    for _ in range(1000):
        info_path = random_valid_deviceinfo(tmp_path)
        print(f"Testing randomly generate deviceinfo file {info_path}")
        info = Deviceinfo(info_path)
        print(info.codename)


# Check that lines starting with deviceinfo_ but don't have an
# "=" raise a syntax error
def test_syntax_error(tmp_file: Path) -> None:
    with open(tmp_file, "w") as f:
        f.write('deviceinfo_codename="test"\n')
        f.write('deviceinfo_chassis="test"\n')
        f.write('deviceinfo_arch="test"\n')
        f.write("deviceinfo_nothing??\n\n\n")

    with pytest.raises(SyntaxError):
        Deviceinfo(tmp_file)