Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 1 | /* |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 2 | * Copyright (c) 2021-2023, Arm Limited and Contributors. All rights reserved. |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 7 | #include <ctype.h> |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 8 | #include <stdint.h> |
Julian Hall | 0446040 | 2021-07-08 17:40:57 +0100 | [diff] [blame] | 9 | #include <stdbool.h> |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 10 | #include <stdio.h> |
Julian Hall | 0446040 | 2021-07-08 17:40:57 +0100 | [diff] [blame] | 11 | #include <string.h> |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 12 | #include <stdlib.h> |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 13 | #include "service_under_test.h" |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 14 | |
| 15 | #define TEST_ID_OFFSET (5) |
| 16 | #define TEST_POSTFIX_OFFSET (8) |
| 17 | #define TEST_ENTRY_LENGTH (9) |
| 18 | |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 19 | |
| 20 | int32_t val_entry(void); |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 21 | extern size_t val_get_test_list(uint32_t *test_id_list, size_t size); |
| 22 | extern void pal_set_custom_test_list(char *custom_test_list); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 23 | |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 24 | /* Returns whether option_switch is in the argv list and provide its index in the array */ |
| 25 | static bool option_selected(const char *option_switch, int argc, char *argv[], int *index) |
Julian Hall | 0446040 | 2021-07-08 17:40:57 +0100 | [diff] [blame] | 26 | { |
| 27 | bool selected = false; |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 28 | *index = 0; |
Julian Hall | 0446040 | 2021-07-08 17:40:57 +0100 | [diff] [blame] | 29 | |
| 30 | for (int i = 1; (i < argc) && !selected; ++i) { |
| 31 | |
| 32 | selected = (strcmp(argv[i], option_switch) == 0); |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 33 | *index = i; |
Julian Hall | 0446040 | 2021-07-08 17:40:57 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | return selected; |
| 37 | } |
| 38 | |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 39 | /* Print the supported command line arguments */ |
| 40 | static void print_help(void) |
| 41 | { |
| 42 | printf("Supported command line arguments:\n\n"); |
| 43 | printf("\t -l: Print list of tests.\n"); |
| 44 | printf("\t -t <test_list>: Run only the listed tests (e.g: test_201;test_202;). test_list = ^(test_[0-9]{3};)+ \n"); |
| 45 | printf("\t -v: Verbose mode.\n"); |
| 46 | printf("\t -h: Print this help message.\n"); |
| 47 | printf("\n"); |
| 48 | } |
| 49 | |
| 50 | /* Prints the list of selectable psa-api tests */ |
| 51 | static void print_psa_api_tests(void) |
| 52 | { |
| 53 | /* Request the number of tests to find out the size of the area needed to store the test ID-s. */ |
| 54 | size_t n_test = val_get_test_list(NULL, 0); |
| 55 | |
| 56 | uint32_t *test_id_list = (uint32_t *)calloc(n_test, sizeof(uint32_t)); |
| 57 | |
| 58 | if (test_id_list) { |
| 59 | n_test = val_get_test_list(test_id_list, n_test); |
| 60 | |
| 61 | printf("Available psa-api tests:\n"); |
| 62 | for (int i = 0; i < n_test; i++) { |
| 63 | printf("\t test_%d;\n", test_id_list[i]); |
| 64 | } |
| 65 | |
| 66 | free(test_id_list); |
| 67 | } |
| 68 | else { |
| 69 | printf("Could not allocate enough memory to store the list of tests\n"); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* Check if the received test list string is formatted as expected */ |
| 74 | static bool is_test_list_wrong(char* test_list) |
| 75 | { |
| 76 | size_t len = strlen(test_list); |
| 77 | |
| 78 | for (unsigned i = 0; i < len; i += TEST_ENTRY_LENGTH) { |
| 79 | |
| 80 | /* Report error when the test entry is not properly finished */ |
| 81 | if (i + TEST_ENTRY_LENGTH > len) { |
| 82 | printf("Expecting \"test_xxx;\" test entry at the %dth character, got \"%s\" instead.\n", i, &test_list[i]); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | /* Report error at incorrect test entry prefix */ |
| 87 | if (memcmp(&test_list[i], "test_", TEST_ID_OFFSET)) { |
| 88 | printf("Expecting \"test_\" at the %dth character, got \"%.5s\" instead.\n", i, &test_list[i]); |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | /* Report error if the test ID is incorrect */ |
| 93 | if (!(isdigit(test_list[i + TEST_ID_OFFSET]) && |
| 94 | isdigit(test_list[i + TEST_ID_OFFSET + 1]) && |
| 95 | isdigit(test_list[i + TEST_ID_OFFSET + 2]))) { |
| 96 | printf("Expecting three digits at the %dth character, got \"%.3s\" instead.\n", |
| 97 | i + TEST_ID_OFFSET, |
| 98 | &test_list[i + TEST_ID_OFFSET]); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | /* Report error at incorrect test entry postfix */ |
| 103 | if (test_list[i + TEST_POSTFIX_OFFSET] != ';') { |
| 104 | printf("Expecting ; at the %dth character, got \"%.1s\" instead.\n", |
| 105 | i + TEST_POSTFIX_OFFSET, |
| 106 | &test_list[i + TEST_POSTFIX_OFFSET]); |
| 107 | return true; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | /* Entry point */ |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 115 | int main(int argc, char *argv[]) |
| 116 | { |
| 117 | int rval = -1; |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 118 | int option_index = 0; |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 119 | |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 120 | /* Print available tests */ |
| 121 | if (option_selected("-l", argc, argv, &option_index)) { |
| 122 | print_psa_api_tests(); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | /* Create custom test list */ |
| 127 | if (option_selected("-t", argc, argv, &option_index)) { |
| 128 | /* Avoid overindexing of argv and detect if the option is followed by another option */ |
| 129 | char *test_list_values = argv[option_index + 1]; |
| 130 | if ((option_index >= argc) || (test_list_values[0] == '-')) { |
| 131 | printf("Testlist string is expected after -t argument!\n"); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | if (is_test_list_wrong(test_list_values)) { |
| 136 | printf("Testlist string is not valid!\n"); |
| 137 | print_psa_api_tests(); |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | /* Filter tests */ |
| 142 | pal_set_custom_test_list(test_list_values); |
| 143 | } |
| 144 | |
Gabor Toth | e53a2a2 | 2023-04-12 09:51:35 +0200 | [diff] [blame] | 145 | /* Print help */ |
| 146 | if (option_selected("-h", argc, argv, &option_index)) { |
| 147 | print_help(); |
| 148 | return 0; |
| 149 | } |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 150 | |
Julian Hall | 0446040 | 2021-07-08 17:40:57 +0100 | [diff] [blame] | 151 | /* Locate service under test */ |
Balint Dobszay | a7a1a5b | 2023-07-05 15:48:21 +0200 | [diff] [blame] | 152 | rval = locate_service_under_test(); |
Julian Hall | 0446040 | 2021-07-08 17:40:57 +0100 | [diff] [blame] | 153 | |
| 154 | /* Run tests */ |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 155 | if (!rval) { |
| 156 | |
| 157 | rval = val_entry(); |
| 158 | |
| 159 | relinquish_service_under_test(); |
| 160 | } |
| 161 | else { |
| 162 | |
| 163 | printf("Failed to locate service under test. Error code: %d\n", rval); |
| 164 | } |
| 165 | |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 166 | return rval; |
| 167 | } |