|
| 1 | +/* Copyright (c) 2017 ARM Limited |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#if !ENABLE_SPM |
| 17 | + #error [NOT_SUPPORTED] SPM is not supported on this platform |
| 18 | +#endif |
| 19 | + |
| 20 | +#include "cmsis_os2.h" |
| 21 | +#include "mbed.h" |
| 22 | +#include "greentea-client/test_env.h" |
| 23 | +#include "unity.h" |
| 24 | +#include "utest.h" |
| 25 | +#include "rtos.h" |
| 26 | +#include "spm_client.h" |
| 27 | +#include "psa_neg_client_part1_ifs.h" |
| 28 | +#include "neg_tests.h" |
| 29 | + |
| 30 | + |
| 31 | +#define MINOR_VER 5 |
| 32 | +#define CLIENT_RSP_BUF_SIZE 128 |
| 33 | +#define OFFSET_POS 1 |
| 34 | +#define INVALID_SFID (NEG_CLIENT_PART1_SF1 + 30) |
| 35 | +#define INVALID_MINOR (MINOR_VER + 10) |
| 36 | +#define INVALID_TX_LEN (PSA_MAX_INVEC_LEN + 1) |
| 37 | + |
| 38 | + |
| 39 | +using namespace utest::v1; |
| 40 | + |
| 41 | +Semaphore test_sem(0); |
| 42 | +bool error_thrown = false; |
| 43 | +uint8_t response_buf[CLIENT_RSP_BUF_SIZE]; |
| 44 | +extern "C" void spm_reboot(void); |
| 45 | + |
| 46 | +void error(const char* format, ...) |
| 47 | +{ |
| 48 | + error_thrown = true; |
| 49 | + osStatus status = test_sem.release(); |
| 50 | + MBED_ASSERT(status == osOK); |
| 51 | + while(1); |
| 52 | + PSA_UNUSED(status); |
| 53 | +} |
| 54 | + |
| 55 | +/* ------------------------------------- Functions ----------------------------------- */ |
| 56 | + |
| 57 | +static psa_handle_t negative_client_ipc_tests_connect(uint32_t sfid, uint32_t minor_version) |
| 58 | +{ |
| 59 | + psa_handle_t handle = psa_connect(sfid, minor_version); |
| 60 | + TEST_ASSERT_TRUE(handle > 0); |
| 61 | + return handle; |
| 62 | +} |
| 63 | + |
| 64 | +static void negative_client_ipc_tests_call( psa_handle_t handle, |
| 65 | + psa_invec_t *iovec_temp, |
| 66 | + size_t tx_len, |
| 67 | + size_t rx_len |
| 68 | + ) |
| 69 | +{ |
| 70 | + error_t status = PSA_SUCCESS; |
| 71 | + memset(response_buf, 0, sizeof(response_buf)); |
| 72 | + psa_outvec_t resp = { response_buf, rx_len }; |
| 73 | + |
| 74 | + status = psa_call(handle, iovec_temp, tx_len, &resp, 1); |
| 75 | + |
| 76 | + TEST_ASSERT_EQUAL_INT(PSA_SUCCESS, status); |
| 77 | +} |
| 78 | + |
| 79 | +static void negative_client_ipc_tests_close(psa_handle_t handle) |
| 80 | +{ |
| 81 | + error_t status = PSA_SUCCESS; |
| 82 | + status = psa_close(handle); |
| 83 | + |
| 84 | + TEST_ASSERT_EQUAL_INT(PSA_SUCCESS, status); |
| 85 | +} |
| 86 | + |
| 87 | +//Testing client call with an invalid SFID |
| 88 | +void client_connect_invalid_sfid() |
| 89 | +{ |
| 90 | + psa_connect( INVALID_SFID, |
| 91 | + MINOR_VER |
| 92 | + ); |
| 93 | + |
| 94 | + TEST_FAIL_MESSAGE("client_connect_invalid_sfid negative test failed"); |
| 95 | +} |
| 96 | + |
| 97 | +//Testing client connect version policy is RELAXED and minor version is bigger than the minimum version |
| 98 | +void client_connect_invalid_pol_ver_relaxed() |
| 99 | +{ |
| 100 | + psa_connect( NEG_CLIENT_PART1_SF1, //NEG_CLIENT_PART1_SF1 should have relaxed policy |
| 101 | + INVALID_MINOR |
| 102 | + ); |
| 103 | + |
| 104 | + TEST_FAIL_MESSAGE("client_connect_invalid_pol_ver_relaxed negative test failed"); |
| 105 | +} |
| 106 | + |
| 107 | +//Testing client connect version policy is STRICT and minor version is different than the minimum version |
| 108 | +void client_connect_invalid_pol_ver_strict() |
| 109 | +{ |
| 110 | + psa_connect( NEG_CLIENT_PART1_SF2, //NEG_CLIENT_PART1_SF2 should have strict policy |
| 111 | + INVALID_MINOR |
| 112 | + ); |
| 113 | + |
| 114 | + TEST_FAIL_MESSAGE("client_connect_invalid_pol_ver_strict negative test failed"); |
| 115 | +} |
| 116 | + |
| 117 | +//Testing client call num of iovec (tx_len) is bigger than max value allowed |
| 118 | +void client_call_invalid_tx_len() |
| 119 | +{ |
| 120 | + psa_handle_t handle = 0; |
| 121 | + |
| 122 | + handle = negative_client_ipc_tests_connect(NEG_CLIENT_PART1_SF1, MINOR_VER); |
| 123 | + |
| 124 | + uint8_t data[2] = {1, 0}; |
| 125 | + |
| 126 | + psa_invec_t iovec_temp[PSA_MAX_INVEC_LEN] = { |
| 127 | + {data, sizeof(data)}, |
| 128 | + {data, sizeof(data)}, |
| 129 | + {data, sizeof(data)} |
| 130 | + }; |
| 131 | + |
| 132 | + negative_client_ipc_tests_call(handle, iovec_temp, INVALID_TX_LEN, CLIENT_RSP_BUF_SIZE); |
| 133 | + |
| 134 | + TEST_FAIL_MESSAGE("client_call_invalid_tx_len negative test failed"); |
| 135 | +} |
| 136 | + |
| 137 | +//Testing client call return buffer (rx_buff) is NULL and return buffer len is not 0 |
| 138 | +void client_call_rx_buff_null_rx_len_not_zero() |
| 139 | +{ |
| 140 | + psa_handle_t handle = 0; |
| 141 | + uint8_t data[2] = {1, 0}; |
| 142 | + psa_invec_t iovec_temp[PSA_MAX_INVEC_LEN] = { |
| 143 | + {data, sizeof(data)}, |
| 144 | + {data, sizeof(data)}, |
| 145 | + {data, sizeof(data)} |
| 146 | + }; |
| 147 | + |
| 148 | + handle = negative_client_ipc_tests_connect(NEG_CLIENT_PART1_SF1, MINOR_VER); |
| 149 | + |
| 150 | + psa_call(handle, iovec_temp, PSA_MAX_INVEC_LEN, NULL, 1); |
| 151 | + |
| 152 | + TEST_FAIL_MESSAGE("client_call_rx_buff_null_rx_len_not_zero negative test failed"); |
| 153 | +} |
| 154 | + |
| 155 | +//Testing client call iovecs pointer is NULL and num of iovecs is not 0 |
| 156 | +void client_call_iovecs_null_tx_len_not_zero() |
| 157 | +{ |
| 158 | + psa_handle_t handle = 0; |
| 159 | + |
| 160 | + handle = negative_client_ipc_tests_connect(NEG_CLIENT_PART1_SF1, MINOR_VER); |
| 161 | + memset(response_buf, 0, CLIENT_RSP_BUF_SIZE); |
| 162 | + psa_outvec_t resp = { response_buf, CLIENT_RSP_BUF_SIZE }; |
| 163 | + |
| 164 | + psa_call(handle, NULL, PSA_MAX_INVEC_LEN, &resp, 1); |
| 165 | + |
| 166 | + TEST_FAIL_MESSAGE("client_call_iovecs_null_tx_len_not_zero negative test failed"); |
| 167 | +} |
| 168 | + |
| 169 | +//Testing client call iovec base |
| 170 | +void client_call_iovec_base_null_len_not_zero() |
| 171 | +{ |
| 172 | + negative_client_ipc_tests_connect(NEG_CLIENT_PART1_SF1, MINOR_VER); |
| 173 | + |
| 174 | + uint8_t data[2] = {1, 0}; |
| 175 | + |
| 176 | + psa_invec_t iovec_temp[PSA_MAX_INVEC_LEN] = { |
| 177 | + {NULL, sizeof(data)}, |
| 178 | + {data, sizeof(data)}, |
| 179 | + {data, sizeof(data)} |
| 180 | + }; |
| 181 | + |
| 182 | + negative_client_ipc_tests_call(PSA_NULL_HANDLE, iovec_temp, PSA_MAX_INVEC_LEN, 0); |
| 183 | + |
| 184 | + TEST_FAIL_MESSAGE("client_call_iovec_base_null_len_not_zero negative test failed"); |
| 185 | +} |
| 186 | + |
| 187 | +//Testing client call handle does not exist on the platform |
| 188 | +void client_call_invalid_handle() |
| 189 | +{ |
| 190 | + psa_handle_t handle = 0, invalid_handle = 0; |
| 191 | + |
| 192 | + handle = negative_client_ipc_tests_connect(NEG_CLIENT_PART1_SF1, MINOR_VER); |
| 193 | + invalid_handle = handle + 10; |
| 194 | + |
| 195 | + uint8_t data[2] = {1, 0}; |
| 196 | + |
| 197 | + psa_invec_t iovec_temp[PSA_MAX_INVEC_LEN] = { |
| 198 | + {data, sizeof(data)}, |
| 199 | + {data, sizeof(data)}, |
| 200 | + {data, sizeof(data)} |
| 201 | + }; |
| 202 | + |
| 203 | + negative_client_ipc_tests_call(invalid_handle, iovec_temp, PSA_MAX_INVEC_LEN, 0); |
| 204 | + |
| 205 | + TEST_FAIL_MESSAGE("client_call_invalid_handle negative test failed"); |
| 206 | +} |
| 207 | + |
| 208 | +//Testing client call handle is null (PSA_NULL_HANDLE) |
| 209 | +void client_call_handle_is_null() |
| 210 | +{ |
| 211 | + negative_client_ipc_tests_connect(NEG_CLIENT_PART1_SF1, MINOR_VER); |
| 212 | + |
| 213 | + uint8_t data[2] = {1, 0}; |
| 214 | + |
| 215 | + psa_invec_t iovec_temp[PSA_MAX_INVEC_LEN] = { |
| 216 | + {data, sizeof(data)}, |
| 217 | + {data, sizeof(data)}, |
| 218 | + {data, sizeof(data)} |
| 219 | + }; |
| 220 | + |
| 221 | + negative_client_ipc_tests_call(PSA_NULL_HANDLE, iovec_temp, PSA_MAX_INVEC_LEN, 0); |
| 222 | + |
| 223 | + TEST_FAIL_MESSAGE("client_call_handle_is_null negative test failed"); |
| 224 | +} |
| 225 | + |
| 226 | +//Testing client close handle does not exist on the platform |
| 227 | +void client_close_invalid_handle() |
| 228 | +{ |
| 229 | + psa_handle_t handle = 0, invalid_handle = 0; |
| 230 | + |
| 231 | + handle = negative_client_ipc_tests_connect(NEG_CLIENT_PART1_SF1, MINOR_VER); |
| 232 | + invalid_handle = handle + 10; |
| 233 | + |
| 234 | + uint8_t data[2] = {1, 0}; |
| 235 | + |
| 236 | + psa_invec_t iovec_temp[PSA_MAX_INVEC_LEN] = { |
| 237 | + {data, sizeof(data)}, |
| 238 | + {data, sizeof(data)}, |
| 239 | + {data, sizeof(data)} |
| 240 | + }; |
| 241 | + |
| 242 | + negative_client_ipc_tests_call(handle, iovec_temp, PSA_MAX_INVEC_LEN, 0); |
| 243 | + |
| 244 | + negative_client_ipc_tests_close(invalid_handle); |
| 245 | + |
| 246 | + TEST_FAIL_MESSAGE("client_close_invalid_handle negative test failed"); |
| 247 | +} |
| 248 | + |
| 249 | +void client_call_buffer_wrap_around() |
| 250 | +{ |
| 251 | + psa_handle_t handle = 0; |
| 252 | + psa_invec_t iovec_temp = { (void *)0x80000000, UINT32_MAX }; |
| 253 | + |
| 254 | + handle = negative_client_ipc_tests_connect(NEG_CLIENT_PART1_SF1, MINOR_VER); |
| 255 | + psa_call(handle, &iovec_temp, 1, NULL, 0); |
| 256 | + |
| 257 | + TEST_FAIL_MESSAGE("client_call_buffer_wrap_around negative test failed"); |
| 258 | +} |
| 259 | + |
| 260 | +void client_connect_not_allowed_from_nspe() |
| 261 | +{ |
| 262 | + psa_connect(NEG_CLIENT_PART1_SF2, 5); |
| 263 | + |
| 264 | + TEST_FAIL_MESSAGE("client_connect_not_allowed_from_nspe negative test failed"); |
| 265 | +} |
| 266 | + |
| 267 | +void client_call_excese_outvec() |
| 268 | +{ |
| 269 | + psa_handle_t handle = 0; |
| 270 | + uint8_t data[2] = {1, 0}; |
| 271 | + psa_outvec_t iovec_temp[PSA_MAX_OUTVEC_LEN + 1] = { |
| 272 | + {data, sizeof(data)}, |
| 273 | + {data, sizeof(data)}, |
| 274 | + {data, sizeof(data)}, |
| 275 | + {data, sizeof(data)} |
| 276 | + }; |
| 277 | + |
| 278 | + handle = negative_client_ipc_tests_connect(NEG_CLIENT_PART1_SF1, MINOR_VER); |
| 279 | + psa_call(handle, NULL, 0, iovec_temp, PSA_MAX_OUTVEC_LEN + 1); |
| 280 | + |
| 281 | + TEST_FAIL_MESSAGE("client_call_excese_outvec negative test failed"); |
| 282 | +} |
| 283 | + |
| 284 | +PSA_NEG_TEST(client_connect_invalid_sfid) |
| 285 | +PSA_NEG_TEST(client_connect_invalid_pol_ver_relaxed) |
| 286 | +PSA_NEG_TEST(client_connect_invalid_pol_ver_strict) |
| 287 | +PSA_NEG_TEST(client_call_invalid_tx_len) |
| 288 | +PSA_NEG_TEST(client_call_rx_buff_null_rx_len_not_zero) |
| 289 | +PSA_NEG_TEST(client_call_iovecs_null_tx_len_not_zero) |
| 290 | +PSA_NEG_TEST(client_call_iovec_base_null_len_not_zero) |
| 291 | +PSA_NEG_TEST(client_call_invalid_handle) |
| 292 | +PSA_NEG_TEST(client_call_handle_is_null) |
| 293 | +PSA_NEG_TEST(client_close_invalid_handle) |
| 294 | +PSA_NEG_TEST(client_call_buffer_wrap_around) |
| 295 | +PSA_NEG_TEST(client_connect_not_allowed_from_nspe) |
| 296 | +PSA_NEG_TEST(client_call_excese_outvec) |
| 297 | + |
| 298 | +utest::v1::status_t spm_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t reason) |
| 299 | +{ |
| 300 | + spm_reboot(); |
| 301 | + error_thrown = false; |
| 302 | + return greentea_case_teardown_handler(source, passed, failed, reason); |
| 303 | +} |
| 304 | + |
| 305 | +#define SPM_UTEST_CASE(desc, test) Case(desc, PSA_NEG_TEST_NAME(test), spm_case_teardown) |
| 306 | + |
| 307 | +// Test cases |
| 308 | +Case cases[] = { |
| 309 | + SPM_UTEST_CASE("Testing client connect invalid sfid", client_connect_invalid_sfid), |
| 310 | + SPM_UTEST_CASE("Testing client connect version policy relaxed invalid minor", client_connect_invalid_pol_ver_relaxed), |
| 311 | + SPM_UTEST_CASE("Testing client connect version policy strict invalid minor", client_connect_invalid_pol_ver_strict), |
| 312 | + SPM_UTEST_CASE("Testing client call invalid tx_len", client_call_invalid_tx_len), |
| 313 | + SPM_UTEST_CASE("Testing client call rx_buff is NULL rx_len is not 0", client_call_rx_buff_null_rx_len_not_zero), |
| 314 | + SPM_UTEST_CASE("Testing client call iovecs is NULL tx_len is not 0", client_call_iovecs_null_tx_len_not_zero), |
| 315 | + SPM_UTEST_CASE("Testing client call iovec base NULL while iovec len not 0", client_call_iovec_base_null_len_not_zero), |
| 316 | + SPM_UTEST_CASE("Testing client call handle does not exist", client_call_invalid_handle), |
| 317 | + SPM_UTEST_CASE("Testing client call handle is PSA_NULL_HANDLE", client_call_handle_is_null), |
| 318 | + SPM_UTEST_CASE("Testing client close handle does not exist", client_close_invalid_handle), |
| 319 | + SPM_UTEST_CASE("Testing client call with buffer wrap-around", client_call_buffer_wrap_around), |
| 320 | + SPM_UTEST_CASE("Testing client connect to non-NSPE SF", client_connect_not_allowed_from_nspe), |
| 321 | + SPM_UTEST_CASE("Testing client call with too much outvec's", client_call_excese_outvec) |
| 322 | +}; |
| 323 | + |
| 324 | +utest::v1::status_t spm_setup(const size_t number_of_cases) |
| 325 | +{ |
| 326 | +#ifndef NO_GREENTEA |
| 327 | + GREENTEA_SETUP(60, "default_auto"); |
| 328 | +#endif |
| 329 | + return greentea_test_setup_handler(number_of_cases); |
| 330 | +} |
| 331 | + |
| 332 | +Specification specification(spm_setup, cases); |
| 333 | + |
| 334 | +int main() |
| 335 | +{ |
| 336 | + !Harness::run(specification); |
| 337 | + return 0; |
| 338 | +} |
0 commit comments