|
| 1 | +/* |
| 2 | +* Copyright (c) 2019 ARM Limited. All rights reserved. |
| 3 | +* |
| 4 | +* SPDX-License-Identifier: Apache-2.0 |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | +* not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | +* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +#ifndef TARGET_PSA |
| 20 | +#error [NOT_SUPPORTED] ITS/PS tests can run only on PSA-enabled targets. |
| 21 | +#endif // TARGET_PSA |
| 22 | + |
| 23 | +#include "greentea-client/test_env.h" |
| 24 | +#include "unity/unity.h" |
| 25 | +#include "utest/utest.h" |
| 26 | +#include "psa/error.h" |
| 27 | +#include "psa/storage_common.h" |
| 28 | +#include "psa/internal_trusted_storage.h" |
| 29 | +#include "psa/protected_storage.h" |
| 30 | +#include "psa/lifecycle.h" |
| 31 | + |
| 32 | +using namespace utest::v1; |
| 33 | + |
| 34 | +#define TEST_BUFF_SIZE 16 |
| 35 | + |
| 36 | +typedef enum { |
| 37 | + its, |
| 38 | + ps |
| 39 | +} storage_type_t; |
| 40 | + |
| 41 | +extern "C" psa_status_t psa_ps_reset(); |
| 42 | + |
| 43 | +static psa_status_t set_func(storage_type_t stype, psa_storage_uid_t uid, uint32_t data_length, |
| 44 | + const void *p_data, psa_storage_create_flags_t create_flags) |
| 45 | +{ |
| 46 | + return (stype == its) ? |
| 47 | + psa_its_set(uid, data_length, p_data, create_flags) : |
| 48 | + psa_ps_set(uid, data_length, p_data, create_flags); |
| 49 | +} |
| 50 | + |
| 51 | +static psa_status_t get_func(storage_type_t stype, psa_storage_uid_t uid, uint32_t data_offset, |
| 52 | + uint32_t data_length, void *p_data) |
| 53 | +{ |
| 54 | + return (stype == its) ? |
| 55 | + psa_its_get(uid, data_offset, data_length, p_data) : |
| 56 | + psa_ps_get(uid, data_offset, data_length, p_data); |
| 57 | +} |
| 58 | + |
| 59 | +static psa_status_t get_info_func(storage_type_t stype, psa_storage_uid_t uid, |
| 60 | + struct psa_storage_info_t *p_info) |
| 61 | +{ |
| 62 | + return (stype == its) ? |
| 63 | + psa_its_get_info(uid, p_info) : |
| 64 | + psa_ps_get_info(uid, p_info); |
| 65 | +} |
| 66 | + |
| 67 | +static psa_status_t remove_func(storage_type_t stype, psa_storage_uid_t uid) |
| 68 | +{ |
| 69 | + return (stype == its) ? |
| 70 | + psa_its_remove(uid) : |
| 71 | + psa_ps_remove(uid); |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +template <storage_type_t stype> |
| 76 | +void pits_ps_test() |
| 77 | +{ |
| 78 | + psa_status_t status = PSA_SUCCESS; |
| 79 | + uint8_t write_buff[TEST_BUFF_SIZE] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; |
| 80 | + uint8_t read_buff[TEST_BUFF_SIZE] = {0}; |
| 81 | + struct psa_storage_info_t info = {0, PSA_STORAGE_FLAG_WRITE_ONCE}; |
| 82 | + memset(read_buff, 0, TEST_BUFF_SIZE); |
| 83 | + |
| 84 | + status = get_info_func(stype, 5, &info); |
| 85 | + TEST_ASSERT_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status); |
| 86 | + |
| 87 | + status = set_func(stype, 5, TEST_BUFF_SIZE, write_buff, 0); |
| 88 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 89 | + |
| 90 | + status = get_info_func(stype, 5, &info); |
| 91 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 92 | + TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size); |
| 93 | + TEST_ASSERT_EQUAL(0, info.flags); |
| 94 | + |
| 95 | + status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff); |
| 96 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 97 | + TEST_ASSERT_EQUAL_MEMORY(write_buff, read_buff, TEST_BUFF_SIZE); |
| 98 | + |
| 99 | + memset(read_buff, 0, TEST_BUFF_SIZE); |
| 100 | + status = get_func(stype, 5, 1, TEST_BUFF_SIZE, read_buff); |
| 101 | + TEST_ASSERT_NOT_EQUAL(PSA_SUCCESS, status); |
| 102 | + |
| 103 | + status = get_func(stype, 5, 1, TEST_BUFF_SIZE - 1, read_buff); |
| 104 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 105 | + TEST_ASSERT_EQUAL_MEMORY(write_buff + 1, read_buff, TEST_BUFF_SIZE - 1); |
| 106 | + |
| 107 | + status = remove_func(stype, 5); |
| 108 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 109 | + |
| 110 | + status = get_info_func(stype, 5, &info); |
| 111 | + TEST_ASSERT_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status); |
| 112 | +} |
| 113 | + |
| 114 | +template <storage_type_t stype> |
| 115 | +void pits_ps_write_once_test() |
| 116 | +{ |
| 117 | + psa_status_t status = PSA_SUCCESS; |
| 118 | + uint8_t write_buff[TEST_BUFF_SIZE] = {0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00}; |
| 119 | + uint8_t read_buff[TEST_BUFF_SIZE] = {0}; |
| 120 | + struct psa_storage_info_t info = {0, 0}; |
| 121 | + |
| 122 | + status = get_info_func(stype, 5, &info); |
| 123 | + TEST_ASSERT_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status); |
| 124 | + |
| 125 | + status = set_func(stype, 5, TEST_BUFF_SIZE, write_buff, PSA_STORAGE_FLAG_WRITE_ONCE); |
| 126 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 127 | + |
| 128 | + info.size = 0; |
| 129 | + info.flags = PSA_STORAGE_FLAG_WRITE_ONCE; |
| 130 | + status = get_info_func(stype, 5, &info); |
| 131 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 132 | + TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size); |
| 133 | + TEST_ASSERT_EQUAL(PSA_STORAGE_FLAG_WRITE_ONCE, info.flags); |
| 134 | + |
| 135 | + status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff); |
| 136 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 137 | + TEST_ASSERT_EQUAL_MEMORY(write_buff, read_buff, TEST_BUFF_SIZE); |
| 138 | + |
| 139 | + status = set_func(stype, 5, TEST_BUFF_SIZE, write_buff, PSA_STORAGE_FLAG_WRITE_ONCE); |
| 140 | + TEST_ASSERT_EQUAL(PSA_ERROR_NOT_PERMITTED, status); |
| 141 | + |
| 142 | + status = set_func(stype, 5, TEST_BUFF_SIZE, write_buff, 0); |
| 143 | + TEST_ASSERT_EQUAL(PSA_ERROR_NOT_PERMITTED, status); |
| 144 | + |
| 145 | + status = remove_func(stype, 5); |
| 146 | + TEST_ASSERT_EQUAL(PSA_ERROR_NOT_PERMITTED, status); |
| 147 | + |
| 148 | + info.size = 0; |
| 149 | + info.flags = PSA_STORAGE_FLAG_WRITE_ONCE; |
| 150 | + status = get_info_func(stype, 5, &info); |
| 151 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 152 | + TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size); |
| 153 | + TEST_ASSERT_EQUAL(PSA_STORAGE_FLAG_WRITE_ONCE, info.flags); |
| 154 | +} |
| 155 | + |
| 156 | +utest::v1::status_t case_its_teardown_handler(const Case *const source, const size_t passed, const size_t failed, const failure_t reason) |
| 157 | +{ |
| 158 | + psa_status_t status; |
| 159 | + status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST); |
| 160 | + TEST_ASSERT_EQUAL(PSA_LIFECYCLE_SUCCESS, status); |
| 161 | + return greentea_case_teardown_handler(source, passed, failed, reason); |
| 162 | +} |
| 163 | + |
| 164 | +template <storage_type_t stype> |
| 165 | +utest::v1::status_t case_its_setup_handler(const Case *const source, const size_t index_of_case) |
| 166 | +{ |
| 167 | + psa_status_t status; |
| 168 | + if (stype == its) { |
| 169 | + status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST); |
| 170 | + TEST_ASSERT_EQUAL(PSA_LIFECYCLE_SUCCESS, status); |
| 171 | + } else { |
| 172 | + status = psa_ps_reset(); |
| 173 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 174 | + } |
| 175 | + return greentea_case_setup_handler(source, index_of_case); |
| 176 | +} |
| 177 | + |
| 178 | +Case cases[] = { |
| 179 | + Case("PSA prot internal storage - Basic", case_its_setup_handler<its>, pits_ps_test<its>, case_its_teardown_handler), |
| 180 | + Case("PSA prot internal storage - Write-once", case_its_setup_handler<its>, pits_ps_write_once_test<its>, case_its_teardown_handler), |
| 181 | + Case("PSA protected storage - Basic", case_its_setup_handler<ps>, pits_ps_test<ps>), |
| 182 | + Case("PSA protected storage - Write-once", case_its_setup_handler<ps>, pits_ps_write_once_test<ps>) |
| 183 | +}; |
| 184 | + |
| 185 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
| 186 | +{ |
| 187 | +#ifndef NO_GREENTEA |
| 188 | + GREENTEA_SETUP(60, "default_auto"); |
| 189 | +#endif |
| 190 | + return greentea_test_setup_handler(number_of_cases); |
| 191 | +} |
| 192 | + |
| 193 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 194 | + |
| 195 | +int main() |
| 196 | +{ |
| 197 | + return !Harness::run(specification); |
| 198 | +} |
0 commit comments