Mate Toth-Pal | b2508d5 | 2022-04-30 14:10:06 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 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 | |
| 10 | import os |
| 11 | import tempfile |
| 12 | from io import BytesIO |
| 13 | from cbor2 import CBOREncoder |
| 14 | from iatverifier.util import read_token_map |
| 15 | |
| 16 | def 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 | |
| 29 | def 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 | |
| 42 | def 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-Pal | b21ae52 | 2022-09-01 12:02:21 +0200 | [diff] [blame] | 48 | def create_token_file(data_dir, source_name, verifier, dest_path, *, add_p_header=True): |
Mate Toth-Pal | f03ea69 | 2022-07-28 21:49:25 +0200 | [diff] [blame] | 49 | """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-Pal | b2508d5 | 2022-04-30 14:10:06 +0200 | [diff] [blame] | 60 | def 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-Pal | f03ea69 | 2022-07-28 21:49:25 +0200 | [diff] [blame] | 66 | create_token_file(data_dir, source_name, verifier, dest_path) |
Mate Toth-Pal | b2508d5 | 2022-04-30 14:10:06 +0200 | [diff] [blame] | 67 | return dest_path |
| 68 | |
| 69 | |
Mate Toth-Pal | 138637a | 2022-07-28 10:57:06 +0200 | [diff] [blame] | 70 | def read_iat(data_dir, filename, verifier, *, check_p_header=False): |
Mate Toth-Pal | b2508d5 | 2022-04-30 14:10:06 +0200 | [diff] [blame] | 71 | """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-Pal | c7404e9 | 2022-07-15 11:11:13 +0200 | [diff] [blame] | 74 | token_item = verifier.parse_token( |
Mate Toth-Pal | b2508d5 | 2022-04-30 14:10:06 +0200 | [diff] [blame] | 75 | token=file.read(), |
Mate Toth-Pal | 138637a | 2022-07-28 10:57:06 +0200 | [diff] [blame] | 76 | check_p_header=check_p_header, |
Mate Toth-Pal | b2508d5 | 2022-04-30 14:10:06 +0200 | [diff] [blame] | 77 | lower_case_key=False) |
Mate Toth-Pal | c7404e9 | 2022-07-15 11:11:13 +0200 | [diff] [blame] | 78 | token_item.verify() |
Mate Toth-Pal | c941766 | 2022-10-09 13:34:08 +0200 | [diff] [blame] | 79 | token_item.get_token_map() |
Mate Toth-Pal | c7404e9 | 2022-07-15 11:11:13 +0200 | [diff] [blame] | 80 | return token_item |
Mate Toth-Pal | b2508d5 | 2022-04-30 14:10:06 +0200 | [diff] [blame] | 81 | |
| 82 | def 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) |