test_cli.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #!/usr/bin/env python3
  2. # type: ignore[attr-defined]
  3. #
  4. # Copyright (c) 2024, Arm Limited. All rights reserved.
  5. #
  6. # SPDX-License-Identifier: BSD-3-Clause
  7. #
  8. """Contains unit tests for the CLI functionality."""
  9. from math import ceil, log2
  10. from pathlib import Path
  11. from re import findall, search
  12. from unittest import mock
  13. import pytest
  14. import yaml
  15. from click.testing import CliRunner
  16. from tlc.cli import cli
  17. from tlc.te import TransferEntry
  18. from tlc.tl import TransferList
  19. def test_create_empty_tl(tmpdir):
  20. runner = CliRunner()
  21. test_file = tmpdir.join("tl.bin")
  22. result = runner.invoke(cli, ["create", test_file.strpath])
  23. assert result.exit_code == 0
  24. assert TransferList.fromfile(test_file) is not None
  25. def test_create_with_fdt(tmpdir):
  26. runner = CliRunner()
  27. fdt = tmpdir.join("fdt.dtb")
  28. fdt.write_binary(b"\x00" * 100)
  29. result = runner.invoke(
  30. cli,
  31. [
  32. "create",
  33. "--fdt",
  34. fdt.strpath,
  35. "--size",
  36. "1000",
  37. tmpdir.join("tl.bin").strpath,
  38. ],
  39. )
  40. assert result.exit_code == 0
  41. def test_add_single_entry(tlcrunner, tmptlstr):
  42. tlcrunner.invoke(cli, ["add", "--entry", "0", "/dev/null", tmptlstr])
  43. tl = TransferList.fromfile(tmptlstr)
  44. assert tl is not None
  45. assert len(tl.entries) == 1
  46. assert tl.entries[0].id == 0
  47. def test_add_multiple_entries(tlcrunner, tlc_entries, tmptlstr):
  48. for id, path in tlc_entries:
  49. tlcrunner.invoke(cli, ["add", "--entry", id, path, tmptlstr])
  50. tl = TransferList.fromfile(tmptlstr)
  51. assert tl is not None
  52. assert len(tl.entries) == len(tlc_entries)
  53. def test_info(tlcrunner, tmptlstr, tmpfdt):
  54. tlcrunner.invoke(cli, ["add", "--entry", "0", "/dev/null", tmptlstr])
  55. tlcrunner.invoke(cli, ["add", "--fdt", tmpfdt.strpath, tmptlstr])
  56. result = tlcrunner.invoke(cli, ["info", tmptlstr])
  57. assert result.exit_code == 0
  58. assert "signature" in result.stdout
  59. assert "id" in result.stdout
  60. result = tlcrunner.invoke(cli, ["info", "--header", tmptlstr])
  61. assert result.exit_code == 0
  62. assert "signature" in result.stdout
  63. assert "id" not in result.stdout
  64. result = tlcrunner.invoke(cli, ["info", "--entries", tmptlstr])
  65. assert result.exit_code == 0
  66. assert "signature" not in result.stdout
  67. assert "id" in result.stdout
  68. def test_raises_max_size_error(tmptlstr, tmpfdt):
  69. tmpfdt.write_binary(bytes(6000))
  70. runner = CliRunner()
  71. result = runner.invoke(cli, ["create", "--fdt", tmpfdt, tmptlstr])
  72. assert result.exception
  73. assert isinstance(result.exception, MemoryError)
  74. assert "TL max size exceeded, consider increasing with the option -s" in str(
  75. result.exception
  76. )
  77. assert "TL size has exceeded the maximum allocation" in str(
  78. result.exception.__cause__
  79. )
  80. def test_info_get_fdt_offset(tmptlstr, tmpfdt):
  81. runner = CliRunner()
  82. with runner.isolated_filesystem():
  83. runner.invoke(cli, ["create", "--size", "1000", tmptlstr])
  84. runner.invoke(cli, ["add", "--entry", "1", tmpfdt.strpath, tmptlstr])
  85. result = runner.invoke(cli, ["info", "--fdt-offset", tmptlstr])
  86. assert result.exit_code == 0
  87. assert result.output.strip("\n").isdigit()
  88. def test_remove_tag(tlcrunner, tmptlstr):
  89. tlcrunner.invoke(cli, ["add", "--entry", "0", "/dev/null", tmptlstr])
  90. result = tlcrunner.invoke(cli, ["info", tmptlstr])
  91. assert result.exit_code == 0
  92. assert "signature" in result.stdout
  93. tlcrunner.invoke(cli, ["remove", "--tags", "0", tmptlstr])
  94. tl = TransferList.fromfile(tmptlstr)
  95. assert result.exit_code == 0
  96. assert len(tl.entries) == 0
  97. def test_unpack_tl(tlcrunner, tmptlstr, tmpfdt, tmpdir):
  98. with tlcrunner.isolated_filesystem(temp_dir=tmpdir):
  99. tlcrunner.invoke(cli, ["add", "--entry", 1, tmpfdt.strpath, tmptlstr])
  100. tlcrunner.invoke(cli, ["unpack", tmptlstr])
  101. assert Path("te_0_1.bin").exists()
  102. def test_unpack_multiple_tes(tlcrunner, tlc_entries, tmptlstr, tmpdir):
  103. with tlcrunner.isolated_filesystem(temp_dir=tmpdir):
  104. for id, path in tlc_entries:
  105. tlcrunner.invoke(cli, ["add", "--entry", id, path, tmptlstr])
  106. assert all(
  107. filter(
  108. lambda te: (Path(tmpdir.strpath) / f"te_{te[0]}.bin").exists(), tlc_entries
  109. )
  110. )
  111. def test_unpack_into_dir(tlcrunner, tmpdir, tmptlstr, tmpfdt):
  112. tlcrunner.invoke(cli, ["add", "--entry", 1, tmpfdt.strpath, tmptlstr])
  113. tlcrunner.invoke(cli, ["unpack", "-C", tmpdir.strpath, tmptlstr])
  114. assert (Path(tmpdir.strpath) / "te_0_1.bin").exists()
  115. def test_unpack_into_dir_with_conflicting_tags(tlcrunner, tmpdir, tmptlstr, tmpfdt):
  116. tlcrunner.invoke(cli, ["add", "--entry", 1, tmpfdt.strpath, tmptlstr])
  117. tlcrunner.invoke(cli, ["add", "--entry", 1, tmpfdt.strpath, tmptlstr])
  118. tlcrunner.invoke(cli, ["unpack", "-C", tmpdir.strpath, tmptlstr])
  119. assert (Path(tmpdir.strpath) / "te_0_1.bin").exists()
  120. assert (Path(tmpdir.strpath) / "te_1_1.bin").exists()
  121. def test_validate_invalid_signature(tmptlstr, tlcrunner, monkeypatch):
  122. tl = TransferList()
  123. tl.signature = 0xDEADBEEF
  124. mock_open = lambda tmptlstr, mode: mock.mock_open(read_data=tl.header_to_bytes())()
  125. monkeypatch.setattr("builtins.open", mock_open)
  126. result = tlcrunner.invoke(cli, ["validate", tmptlstr])
  127. assert result.exit_code != 0
  128. def test_validate_misaligned_entries(tmptlstr, tlcrunner, monkeypatch):
  129. """Base address of a TE must be 8-byte aligned."""
  130. mock_open = lambda tmptlstr, mode: mock.mock_open(
  131. read_data=TransferList().header_to_bytes()
  132. + bytes(5)
  133. + TransferEntry(0, 0, bytes(0)).header_to_bytes
  134. )()
  135. monkeypatch.setattr("builtins.open", mock_open)
  136. result = tlcrunner.invoke(cli, ["validate", tmptlstr])
  137. assert result.exit_code == 1
  138. @pytest.mark.parametrize(
  139. "version", [0, TransferList.version, TransferList.version + 1, 1 << 8]
  140. )
  141. def test_validate_unsupported_version(version, tmptlstr, tlcrunner, monkeypatch):
  142. tl = TransferList()
  143. tl.version = version
  144. mock_open = lambda tmptlstr, mode: mock.mock_open(read_data=tl.header_to_bytes())()
  145. monkeypatch.setattr("builtins.open", mock_open)
  146. result = tlcrunner.invoke(cli, ["validate", tmptlstr])
  147. if version >= TransferList.version and version <= 0xFF:
  148. assert result.exit_code == 0
  149. else:
  150. assert result.exit_code == 1
  151. def test_create_entry_from_yaml_and_blob_file(
  152. tlcrunner, tmpyamlconfig_blob_file, tmptlstr, non_empty_tag_id
  153. ):
  154. tlcrunner.invoke(
  155. cli,
  156. [
  157. "create",
  158. "--from-yaml",
  159. tmpyamlconfig_blob_file.strpath,
  160. tmptlstr,
  161. ],
  162. )
  163. tl = TransferList.fromfile(tmptlstr)
  164. assert tl is not None
  165. assert len(tl.entries) == 1
  166. assert tl.entries[0].id == non_empty_tag_id
  167. @pytest.mark.parametrize(
  168. "entry",
  169. [
  170. {"tag_id": 0},
  171. {
  172. "tag_id": 0x104,
  173. "addr": 0x0400100000000010,
  174. "size": 0x0003300000000000,
  175. },
  176. {
  177. "tag_id": 0x100,
  178. "pp_addr": 100,
  179. },
  180. {
  181. "tag_id": "optee_pageable_part",
  182. "pp_addr": 100,
  183. },
  184. ],
  185. )
  186. def test_create_from_yaml_check_sum_bytes(tlcrunner, tmpyamlconfig, tmptlstr, entry):
  187. """Test creating a TL from a yaml file, but only check that the sum of the
  188. data in the yaml file matches the sum of the data in the TL. This means
  189. you don't have to type the exact sequence of expected bytes. All the data
  190. in the yaml file must be integers (except for the tag IDs, which can be
  191. strings).
  192. """
  193. # create yaml config file
  194. config = {
  195. "has_checksum": True,
  196. "max_size": 0x1000,
  197. "entries": [entry],
  198. }
  199. with open(tmpyamlconfig, "w") as f:
  200. yaml.safe_dump(config, f)
  201. # invoke TLC
  202. tlcrunner.invoke(
  203. cli,
  204. [
  205. "create",
  206. "--from-yaml",
  207. tmpyamlconfig,
  208. tmptlstr,
  209. ],
  210. )
  211. # open created TL, and check
  212. tl = TransferList.fromfile(tmptlstr)
  213. assert tl is not None
  214. assert len(tl.entries) == 1
  215. # Check that the sum of all the data in the transfer entry in the yaml file
  216. # is the same as the sum of all the data in the transfer list. Don't count
  217. # the tag id or the TE headers.
  218. # every item in the entry dict must be an integer
  219. yaml_total = 0
  220. for key, data in iter_nested_dict(entry):
  221. if key != "tag_id":
  222. num_bytes = ceil(log2(data + 1) / 8)
  223. yaml_total += sum(data.to_bytes(num_bytes, "little"))
  224. tl_total = sum(tl.entries[0].data)
  225. assert tl_total == yaml_total
  226. @pytest.mark.parametrize(
  227. "entry,expected",
  228. [
  229. (
  230. {
  231. "tag_id": 0x102,
  232. "ep_info": {
  233. "h": {
  234. "type": 0x01,
  235. "version": 0x02,
  236. "attr": 8,
  237. },
  238. "pc": 67239936,
  239. "spsr": 965,
  240. "args": [67112976, 67112960, 0, 0, 0, 0, 0, 0],
  241. },
  242. },
  243. (
  244. "0x00580201 0x00000008 0x04020000 0x00000000 "
  245. "0x000003C5 0x00000000 0x04001010 0x00000000 "
  246. "0x04001000 0x00000000 0x00000000 0x00000000 "
  247. "0x00000000 0x00000000 0x00000000 0x00000000 "
  248. "0x00000000 0x00000000 0x00000000 0x00000000 "
  249. "0x00000000 0x00000000"
  250. ),
  251. ),
  252. (
  253. {
  254. "tag_id": 0x102,
  255. "ep_info": {
  256. "h": {
  257. "type": 0x01,
  258. "version": 0x02,
  259. "attr": "EP_NON_SECURE | EP_ST_ENABLE",
  260. },
  261. "pc": 67239936,
  262. "spsr": 965,
  263. "args": [67112976, 67112960, 0, 0, 0, 0, 0, 0],
  264. },
  265. },
  266. (
  267. "0x00580201 0x00000005 0x04020000 0x00000000 "
  268. "0x000003C5 0x00000000 0x04001010 0x00000000 "
  269. "0x04001000 0x00000000 0x00000000 0x00000000 "
  270. "0x00000000 0x00000000 0x00000000 0x00000000 "
  271. "0x00000000 0x00000000 0x00000000 0x00000000 "
  272. "0x00000000 0x00000000"
  273. ),
  274. ),
  275. ],
  276. )
  277. def test_create_from_yaml_check_exact_data(
  278. tlcrunner, tmpyamlconfig, tmptlstr, entry, expected
  279. ):
  280. """Test creating a TL from a yaml file, checking the exact sequence of
  281. bytes. This is useful for checking that the alignment is correct. You can
  282. get the expected sequence of bytes by copying it from the ArmDS debugger.
  283. """
  284. # create yaml config file
  285. config = {
  286. "has_checksum": True,
  287. "max_size": 0x1000,
  288. "entries": [entry],
  289. }
  290. with open(tmpyamlconfig, "w") as f:
  291. yaml.safe_dump(config, f)
  292. # invoke TLC
  293. tlcrunner.invoke(
  294. cli,
  295. [
  296. "create",
  297. "--from-yaml",
  298. tmpyamlconfig,
  299. tmptlstr,
  300. ],
  301. )
  302. # open TL and check
  303. tl = TransferList.fromfile(tmptlstr)
  304. assert tl is not None
  305. assert len(tl.entries) == 1
  306. # check expected and actual data
  307. actual = tl.entries[0].data
  308. actual = bytes_to_hex(actual)
  309. assert actual == expected
  310. @pytest.mark.parametrize("option", ["-O", "--output"])
  311. def test_gen_tl_header_with_output_name(tlcrunner, tmptlstr, option, filename="test.h"):
  312. with tlcrunner.isolated_filesystem():
  313. result = tlcrunner.invoke(
  314. cli,
  315. [
  316. "gen-header",
  317. option,
  318. filename,
  319. tmptlstr,
  320. ],
  321. )
  322. assert result.exit_code == 0
  323. assert Path(filename).exists()
  324. def test_gen_tl_with_fdt_header(tmptlstr, tmpfdt):
  325. tlcrunner = CliRunner()
  326. with tlcrunner.isolated_filesystem():
  327. tlcrunner.invoke(cli, ["create", "--size", 1000, "--fdt", tmpfdt, tmptlstr])
  328. result = tlcrunner.invoke(
  329. cli,
  330. [
  331. "gen-header",
  332. tmptlstr,
  333. ],
  334. )
  335. assert result.exit_code == 0
  336. assert Path("header.h").exists()
  337. with open("header.h", "r") as f:
  338. dtb_match = search(r"DTB_OFFSET\s+(\d+)", "".join(f.readlines()))
  339. assert dtb_match and dtb_match[1].isnumeric()
  340. def test_gen_empty_tl_c_header(tlcrunner, tmptlstr):
  341. with tlcrunner.isolated_filesystem():
  342. result = tlcrunner.invoke(
  343. cli,
  344. [
  345. "gen-header",
  346. tmptlstr,
  347. ],
  348. )
  349. assert result.exit_code == 0
  350. assert Path("header.h").exists()
  351. with open("header.h", "r") as f:
  352. lines = "".join(f.readlines())
  353. assert TransferList.hdr_size == int(
  354. findall(r"SIZE\s+(0x[0-9a-fA-F]+|\d+)", lines)[0], 16
  355. )
  356. assert TransferList.version == int(
  357. findall(r"VERSION.+(0x[0-9a-fA-F]+|\d+)", lines)[0]
  358. )
  359. def bytes_to_hex(data: bytes) -> str:
  360. """Convert bytes to a hex string in the same format as the debugger in
  361. ArmDS
  362. You can copy data from the debugger in Arm Development Studio and put it
  363. into a unit test. You can then run this function on the output from tlc,
  364. and compare it to the data you copied.
  365. The format is groups of 4 bytes with 0x prefixes separated by spaces.
  366. Little endian is used.
  367. """
  368. words_hex = []
  369. for i in range(0, len(data), 4):
  370. word = data[i : i + 4]
  371. word_int = int.from_bytes(word, "little")
  372. word_hex = "0x" + f"{word_int:0>8x}".upper()
  373. words_hex.append(word_hex)
  374. return " ".join(words_hex)
  375. def iter_nested_dict(dictionary: dict):
  376. for key, value in dictionary.items():
  377. if isinstance(value, dict):
  378. yield from iter_nested_dict(value)
  379. else:
  380. yield key, value