blob: 8fd3dac49d3a5acfcfc20335e45d3309edb59626 [file] [log] [blame]
Julian Halld4071382021-07-07 16:45:53 +01001/*
Gabor Tothe53a2a22023-04-12 09:51:35 +02002 * Copyright (c) 2021-2023, Arm Limited and Contributors. All rights reserved.
Julian Halld4071382021-07-07 16:45:53 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Gabor Tothe53a2a22023-04-12 09:51:35 +02007#include <ctype.h>
Julian Halld4071382021-07-07 16:45:53 +01008#include <stdint.h>
Julian Hall04460402021-07-08 17:40:57 +01009#include <stdbool.h>
Julian Halld4071382021-07-07 16:45:53 +010010#include <stdio.h>
Julian Hall04460402021-07-08 17:40:57 +010011#include <string.h>
Gabor Tothe53a2a22023-04-12 09:51:35 +020012#include <stdlib.h>
Julian Halld4071382021-07-07 16:45:53 +010013#include "service_under_test.h"
Gabor Tothe53a2a22023-04-12 09:51:35 +020014
15#define TEST_ID_OFFSET (5)
16#define TEST_POSTFIX_OFFSET (8)
17#define TEST_ENTRY_LENGTH (9)
18
Julian Halld4071382021-07-07 16:45:53 +010019
20int32_t val_entry(void);
Gabor Tothe53a2a22023-04-12 09:51:35 +020021extern size_t val_get_test_list(uint32_t *test_id_list, size_t size);
22extern void pal_set_custom_test_list(char *custom_test_list);
Julian Halld4071382021-07-07 16:45:53 +010023
Gabor Tothe53a2a22023-04-12 09:51:35 +020024/* Returns whether option_switch is in the argv list and provide its index in the array */
25static bool option_selected(const char *option_switch, int argc, char *argv[], int *index)
Julian Hall04460402021-07-08 17:40:57 +010026{
27 bool selected = false;
Gabor Tothe53a2a22023-04-12 09:51:35 +020028 *index = 0;
Julian Hall04460402021-07-08 17:40:57 +010029
30 for (int i = 1; (i < argc) && !selected; ++i) {
31
32 selected = (strcmp(argv[i], option_switch) == 0);
Gabor Tothe53a2a22023-04-12 09:51:35 +020033 *index = i;
Julian Hall04460402021-07-08 17:40:57 +010034 }
35
36 return selected;
37}
38
Gabor Tothe53a2a22023-04-12 09:51:35 +020039/* Print the supported command line arguments */
40static 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 */
51static 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 */
74static 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 Halld4071382021-07-07 16:45:53 +0100115int main(int argc, char *argv[])
116{
117 int rval = -1;
Gabor Tothe53a2a22023-04-12 09:51:35 +0200118 int option_index = 0;
Julian Halld4071382021-07-07 16:45:53 +0100119
Gabor Tothe53a2a22023-04-12 09:51:35 +0200120 /* 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 Tothe53a2a22023-04-12 09:51:35 +0200145 /* Print help */
146 if (option_selected("-h", argc, argv, &option_index)) {
147 print_help();
148 return 0;
149 }
Julian Halld4071382021-07-07 16:45:53 +0100150
Julian Hall04460402021-07-08 17:40:57 +0100151 /* Locate service under test */
Balint Dobszaya7a1a5b2023-07-05 15:48:21 +0200152 rval = locate_service_under_test();
Julian Hall04460402021-07-08 17:40:57 +0100153
154 /* Run tests */
Julian Halld4071382021-07-07 16:45:53 +0100155 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 Halld4071382021-07-07 16:45:53 +0100166 return rval;
167}