From cd20b42396ba5c7f4183e092b326469e0a74c584 Mon Sep 17 00:00:00 2001 From: Hector Cao <122458375+hector-cao@users.noreply.github.com> Date: Mon, 2 Dec 2024 18:15:43 +0100 Subject: [PATCH] tests : fix intel trust authority quote generation tests (#290) The version 1.6.1 for 24.10 change the output format for the generated quote, we have to modify the tests to take into account this API change --- tests/tests/test_guest_ita.py | 41 ++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/tests/tests/test_guest_ita.py b/tests/tests/test_guest_ita.py index 385b36d..1b8a95f 100644 --- a/tests/tests/test_guest_ita.py +++ b/tests/tests/test_guest_ita.py @@ -23,15 +23,15 @@ import Qemu import util +ubuntu_codename = None + def test_guest_measurement_trust_authority_success(): """ Trust Authority CLI quote generation success """ change_qgsd_state('start') quote_str = run_trust_authority() - quote = json.loads(quote_str.replace(' ', ',')) - assert len(quote) > 0, "Quote not valid: %s" % (quote_str) - + check_ita_output(quote_str, for_success = True) def test_guest_measurement_trust_authority_failure(): """ @@ -40,9 +40,7 @@ def test_guest_measurement_trust_authority_failure(): change_qgsd_state('stop') quote_str = run_trust_authority() change_qgsd_state('start') - quote = json.loads(quote_str.replace(' ', ',')) - assert len(quote) == 0, "Quote not valid: %s" % (quote_str) - + check_ita_output(quote_str, for_success = False) def change_qgsd_state(state): cmd = ['systemctl', state, 'qgsd'] @@ -50,8 +48,36 @@ def change_qgsd_state(state): rc = subprocess.run(cmd, stderr=subprocess.STDOUT, timeout=30) assert 0 == rc.returncode, 'Failed change state of qgsd' +def check_ita_output(quote_str : str, for_success : bool = True): + """ + Check the validity of ITA quote output + Depending on the version of the ITA client, the output + may vary: + - Ubuntu 24.04 (ITA 1.5.0) + On success: [4 0 2 0 129 0 0 ... 0 0 0 0 0 ] + On failure: [] + - Ubuntu 24.10 (ITA 1.6.1) + On success: + Quote: + runtime_data: base64_encoded_runtime_data <- Optional + user_data: base64_encoded_user_data <- Optional + On failure: + Quote: + """ + # regex to check the output of ITA quote command, the regex depends on ITA version + # for the moment, we extract the ITA version from the ubuntu release + # {10,0}: check for at least 10 characters to declare the quote valid + ita_output_regexp = { + 'noble' : '\\[[0-9 ]{10,}\\]', + 'oracular' : 'Quote: [-A-Za-z0-9+/]{10,}' + } + import re + pattern = re.compile(ita_output_regexp[ubuntu_codename]) + assert (bool(pattern.match(quote_str)) == for_success), f'Error for code name : {codename}' def run_trust_authority(): + global ubuntu_codename + quote_str = "" with Qemu.QemuMachine() as qm: machine = qm.qcmd.plugins['machine'] @@ -61,6 +87,9 @@ def run_trust_authority(): ssh = Qemu.QemuSSH(qm) + stdout, _ = ssh.check_exec('lsb_release -cs') + ubuntu_codename = stdout.read().decode().strip() + stdout, stderr = ssh.check_exec('trustauthority-cli quote') quote_str = stdout.read().decode() return quote_str