blob: e08d3a2308827950ea4a8f85af4223c9a2793da2 [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
29def convert_map_to_token_bytes(token_map, verifier, add_p_header):
30 """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,
36 add_p_header=add_p_header,
37 name_as_key=True,
38 parse_raw_value=True,
39 root=True)
40 return bytes_io.getvalue()
41
42def create_token(data_dir, source_name, verifier, add_p_header):
43 """Creats a cbor token from a yaml file."""
44 source_path = os.path.join(data_dir, source_name)
45 token_map = read_token_map(source_path)
46 return convert_map_to_token_bytes(token_map, verifier, add_p_header)
47
Mate Toth-Palb21ae522022-09-01 12:02:21 +020048def create_token_file(data_dir, source_name, verifier, dest_path, *, add_p_header=True):
Mate Toth-Palf03ea692022-07-28 21:49:25 +020049 """Create a cbor token from a yaml file and write it to a file
50 """
51 token = create_token(
52 data_dir=data_dir,
53 source_name=source_name,
54 verifier=verifier,
55 add_p_header=add_p_header)
56
57 with open(dest_path, 'wb') as wfh:
58 wfh.write(token)
59
Mate Toth-Palb2508d52022-04-30 14:10:06 +020060def create_token_tmp_file(data_dir, source_name, verifier):
61 """Create a cbor token from a yaml file and write it to a temp file. Return the name of the temp
62 file
63 """
64 temp_file, dest_path = tempfile.mkstemp()
65 os.close(temp_file)
Mate Toth-Palf03ea692022-07-28 21:49:25 +020066 create_token_file(data_dir, source_name, verifier, dest_path)
Mate Toth-Palb2508d52022-04-30 14:10:06 +020067 return dest_path
68
69
Mate Toth-Pal138637a2022-07-28 10:57:06 +020070def read_iat(data_dir, filename, verifier, *, check_p_header=False):
Mate Toth-Palb2508d52022-04-30 14:10:06 +020071 """Read a cbor file and returns the parsed dictionary"""
72 filepath = os.path.join(data_dir, filename)
73 with open(filepath, 'rb') as file:
Mate Toth-Palc7404e92022-07-15 11:11:13 +020074 token_item = verifier.parse_token(
Mate Toth-Palb2508d52022-04-30 14:10:06 +020075 token=file.read(),
Mate Toth-Pal138637a2022-07-28 10:57:06 +020076 check_p_header=check_p_header,
Mate Toth-Palb2508d52022-04-30 14:10:06 +020077 lower_case_key=False)
Mate Toth-Palc7404e92022-07-15 11:11:13 +020078 token_item.verify()
Mate Toth-Palc9417662022-10-09 13:34:08 +020079 token_item.get_token_map()
Mate Toth-Palc7404e92022-07-15 11:11:13 +020080 return token_item
Mate Toth-Palb2508d52022-04-30 14:10:06 +020081
82def create_and_read_iat(data_dir, source_name, verifier):
83 """Read a yaml file, compile it into a cbor token, and read it back"""
84 token_file = create_token_tmp_file(data_dir, source_name, verifier)
85 return read_iat(data_dir, token_file, verifier)