blob: e8745d861137b110efc5fc6393d37226266e4355 [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>
13#include "rpc/common/logging/logging_caller.h"
Julian Halld4071382021-07-07 16:45:53 +010014#include "service_under_test.h"
Gabor Tothe53a2a22023-04-12 09:51:35 +020015#include "service_locator.h"
16
17#define TEST_ID_OFFSET (5)
18#define TEST_POSTFIX_OFFSET (8)
19#define TEST_ENTRY_LENGTH (9)
20
Julian Halld4071382021-07-07 16:45:53 +010021
22int32_t val_entry(void);
Gabor Tothe53a2a22023-04-12 09:51:35 +020023extern size_t val_get_test_list(uint32_t *test_id_list, size_t size);
24extern void pal_set_custom_test_list(char *custom_test_list);
Julian Halld4071382021-07-07 16:45:53 +010025
Gabor Tothe53a2a22023-04-12 09:51:35 +020026/* Returns whether option_switch is in the argv list and provide its index in the array */
27static bool option_selected(const char *option_switch, int argc, char *argv[], int *index)
Julian Hall04460402021-07-08 17:40:57 +010028{
29 bool selected = false;
Gabor Tothe53a2a22023-04-12 09:51:35 +020030 *index = 0;
Julian Hall04460402021-07-08 17:40:57 +010031
32 for (int i = 1; (i < argc) && !selected; ++i) {
33
34 selected = (strcmp(argv[i], option_switch) == 0);
Gabor Tothe53a2a22023-04-12 09:51:35 +020035 *index = i;
Julian Hall04460402021-07-08 17:40:57 +010036 }
37
38 return selected;
39}
40
Gabor Tothe53a2a22023-04-12 09:51:35 +020041/* Print the supported command line arguments */
42static void print_help(void)
43{
44 printf("Supported command line arguments:\n\n");
45 printf("\t -l: Print list of tests.\n");
46 printf("\t -t <test_list>: Run only the listed tests (e.g: test_201;test_202;). test_list = ^(test_[0-9]{3};)+ \n");
47 printf("\t -v: Verbose mode.\n");
48 printf("\t -h: Print this help message.\n");
49 printf("\n");
50}
51
52/* Prints the list of selectable psa-api tests */
53static void print_psa_api_tests(void)
54{
55 /* Request the number of tests to find out the size of the area needed to store the test ID-s. */
56 size_t n_test = val_get_test_list(NULL, 0);
57
58 uint32_t *test_id_list = (uint32_t *)calloc(n_test, sizeof(uint32_t));
59
60 if (test_id_list) {
61 n_test = val_get_test_list(test_id_list, n_test);
62
63 printf("Available psa-api tests:\n");
64 for (int i = 0; i < n_test; i++) {
65 printf("\t test_%d;\n", test_id_list[i]);
66 }
67
68 free(test_id_list);
69 }
70 else {
71 printf("Could not allocate enough memory to store the list of tests\n");
72 }
73}
74
75/* Check if the received test list string is formatted as expected */
76static bool is_test_list_wrong(char* test_list)
77{
78 size_t len = strlen(test_list);
79
80 for (unsigned i = 0; i < len; i += TEST_ENTRY_LENGTH) {
81
82 /* Report error when the test entry is not properly finished */
83 if (i + TEST_ENTRY_LENGTH > len) {
84 printf("Expecting \"test_xxx;\" test entry at the %dth character, got \"%s\" instead.\n", i, &test_list[i]);
85 return true;
86 }
87
88 /* Report error at incorrect test entry prefix */
89 if (memcmp(&test_list[i], "test_", TEST_ID_OFFSET)) {
90 printf("Expecting \"test_\" at the %dth character, got \"%.5s\" instead.\n", i, &test_list[i]);
91 return true;
92 }
93
94 /* Report error if the test ID is incorrect */
95 if (!(isdigit(test_list[i + TEST_ID_OFFSET]) &&
96 isdigit(test_list[i + TEST_ID_OFFSET + 1]) &&
97 isdigit(test_list[i + TEST_ID_OFFSET + 2]))) {
98 printf("Expecting three digits at the %dth character, got \"%.3s\" instead.\n",
99 i + TEST_ID_OFFSET,
100 &test_list[i + TEST_ID_OFFSET]);
101 return true;
102 }
103
104 /* Report error at incorrect test entry postfix */
105 if (test_list[i + TEST_POSTFIX_OFFSET] != ';') {
106 printf("Expecting ; at the %dth character, got \"%.1s\" instead.\n",
107 i + TEST_POSTFIX_OFFSET,
108 &test_list[i + TEST_POSTFIX_OFFSET]);
109 return true;
110 }
111 }
112
113 return false;
114}
115
116/* Entry point */
Julian Halld4071382021-07-07 16:45:53 +0100117int main(int argc, char *argv[])
118{
119 int rval = -1;
Gabor Tothe53a2a22023-04-12 09:51:35 +0200120 int option_index = 0;
Julian Hall04460402021-07-08 17:40:57 +0100121 struct logging_caller *selected_call_logger = NULL;
122 struct logging_caller call_logger;
Julian Halld4071382021-07-07 16:45:53 +0100123
Julian Hall04460402021-07-08 17:40:57 +0100124 logging_caller_init(&call_logger, stdout);
Julian Halld4071382021-07-07 16:45:53 +0100125 service_locator_init();
126
Gabor Tothe53a2a22023-04-12 09:51:35 +0200127 /* Print available tests */
128 if (option_selected("-l", argc, argv, &option_index)) {
129 print_psa_api_tests();
130 return 0;
131 }
132
133 /* Create custom test list */
134 if (option_selected("-t", argc, argv, &option_index)) {
135 /* Avoid overindexing of argv and detect if the option is followed by another option */
136 char *test_list_values = argv[option_index + 1];
137 if ((option_index >= argc) || (test_list_values[0] == '-')) {
138 printf("Testlist string is expected after -t argument!\n");
139 return -1;
140 }
141
142 if (is_test_list_wrong(test_list_values)) {
143 printf("Testlist string is not valid!\n");
144 print_psa_api_tests();
145 return -1;
146 }
147
148 /* Filter tests */
149 pal_set_custom_test_list(test_list_values);
150 }
151
152 /* Setup verbose mode */
153 if (option_selected("-v", argc, argv, &option_index))
154 selected_call_logger = &call_logger;
155
156 /* Print help */
157 if (option_selected("-h", argc, argv, &option_index)) {
158 print_help();
159 return 0;
160 }
Julian Halld4071382021-07-07 16:45:53 +0100161
Julian Hall04460402021-07-08 17:40:57 +0100162 /* Locate service under test */
163 rval = locate_service_under_test(selected_call_logger);
164
165 /* Run tests */
Julian Halld4071382021-07-07 16:45:53 +0100166 if (!rval) {
167
168 rval = val_entry();
169
170 relinquish_service_under_test();
171 }
172 else {
173
174 printf("Failed to locate service under test. Error code: %d\n", rval);
175 }
176
Julian Hall04460402021-07-08 17:40:57 +0100177 logging_caller_deinit(&call_logger);
178
Julian Halld4071382021-07-07 16:45:53 +0100179 return rval;
180}