blob: ab7c1826aefb07a7d1e0c9667d3a9d3ea96e3c09 [file] [log] [blame]
Mate Toth-Palb2508d52022-04-30 14:10:06 +02001#-------------------------------------------------------------------------------
2# Copyright (c) 2022, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8"""This module contains utility functions for tests."""
9
10import os
11import tempfile
12from io import BytesIO
13from cbor2 import CBOREncoder
14from iatverifier.util import read_token_map
15
16def bytes_equal_to_file(data, filepath):
17 """
18 return compare a bytestring and the content of a file and return whether they are the same
19 """
20 with open(filepath, 'rb') as file:
21 file_data = file.read()
22 if len(file_data) != len(data):
23 return False
24 for bytes1, bytes2 in zip(data, file_data):
25 if bytes1 != bytes2:
26 return False
27 return True
28
Mate Toth-Pale305e552022-10-07 14:04:53 +020029def convert_map_to_token_bytes(token_map, verifier):
Mate Toth-Palb2508d52022-04-30 14:10:06 +020030 """Converts a map to cbor token"""
31 with BytesIO() as bytes_io:
32 encoder = CBOREncoder(bytes_io)
33 verifier.convert_map_to_token(
34 encoder,
35 token_map,
Mate Toth-Palb2508d52022-04-30 14:10:06 +020036 name_as_key=True,
37 parse_raw_value=True,
38 root=True)
39 return bytes_io.getvalue()
40
Mate Toth-Pale305e552022-10-07 14:04:53 +020041def create_token(data_dir, source_name, verifier):
Thomas Fossatif4e1ca32024-08-16 16:01:31 +000042 """Create a cbor token from a yaml file."""
Mate Toth-Palb2508d52022-04-30 14:10:06 +020043 source_path = os.path.join(data_dir, source_name)
44 token_map = read_token_map(source_path)
Mate Toth-Pale305e552022-10-07 14:04:53 +020045 return convert_map_to_token_bytes(token_map, verifier)
Mate Toth-Palb2508d52022-04-30 14:10:06 +020046
Mate Toth-Pale305e552022-10-07 14:04:53 +020047def create_token_file(data_dir, source_name, verifier, dest_path):
Mate Toth-Palf03ea692022-07-28 21:49:25 +020048 """Create a cbor token from a yaml file and write it to a file
49 """
50 token = create_token(
51 data_dir=data_dir,
52 source_name=source_name,
Mate Toth-Pale305e552022-10-07 14:04:53 +020053 verifier=verifier)
Mate Toth-Palf03ea692022-07-28 21:49:25 +020054
55 with open(dest_path, 'wb') as wfh:
56 wfh.write(token)
57
Mate Toth-Palb2508d52022-04-30 14:10:06 +020058def create_token_tmp_file(data_dir, source_name, verifier):
59 """Create a cbor token from a yaml file and write it to a temp file. Return the name of the temp
60 file
61 """
62 temp_file, dest_path = tempfile.mkstemp()
63 os.close(temp_file)
Mate Toth-Palf03ea692022-07-28 21:49:25 +020064 create_token_file(data_dir, source_name, verifier, dest_path)
Mate Toth-Palb2508d52022-04-30 14:10:06 +020065 return dest_path
66
67
Mate Toth-Pale305e552022-10-07 14:04:53 +020068def read_iat(data_dir, filename, verifier):
Mate Toth-Palb2508d52022-04-30 14:10:06 +020069 """Read a cbor file and returns the parsed dictionary"""
70 filepath = os.path.join(data_dir, filename)
71 with open(filepath, 'rb') as file:
Mate Toth-Palc7404e92022-07-15 11:11:13 +020072 token_item = verifier.parse_token(
Mate Toth-Palb2508d52022-04-30 14:10:06 +020073 token=file.read(),
Mate Toth-Palb2508d52022-04-30 14:10:06 +020074 lower_case_key=False)
Mate Toth-Palc7404e92022-07-15 11:11:13 +020075 token_item.verify()
Mate Toth-Palc9417662022-10-09 13:34:08 +020076 token_item.get_token_map()
Mate Toth-Palc7404e92022-07-15 11:11:13 +020077 return token_item
Mate Toth-Palb2508d52022-04-30 14:10:06 +020078
79def create_and_read_iat(data_dir, source_name, verifier):
80 """Read a yaml file, compile it into a cbor token, and read it back"""
81 token_file = create_token_tmp_file(data_dir, source_name, verifier)
82 return read_iat(data_dir, token_file, verifier)