1 /*
2  * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 #include <cstring>
7 #include <CppUTest/TestHarness.h>
8 #include <psa/internal_trusted_storage.h>
9 #include "its_api_tests.h"
10 
storeNewItem()11 void its_api_tests::storeNewItem()
12 {
13     psa_status_t status;
14     psa_storage_uid_t uid = 10;
15     struct psa_storage_info_t storage_info;
16     static const size_t ITEM_SIZE = 68;
17     uint8_t item[ITEM_SIZE];
18     uint8_t read_item[ITEM_SIZE];
19 
20     memset(item, 0x55, sizeof(item));
21 
22     /* Probe to check item does not exist */
23     status = psa_its_get_info(uid, &storage_info);
24     CHECK_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);
25 
26     /* Store the item */
27     status = psa_its_set(uid, sizeof(item), item, PSA_STORAGE_FLAG_NONE);
28     CHECK_EQUAL(PSA_SUCCESS, status);
29 
30     /* Probe to check item now exists */
31     status = psa_its_get_info(uid, &storage_info);
32     CHECK_EQUAL(PSA_SUCCESS, status);
33     CHECK_EQUAL(sizeof(item), storage_info.size);
34 
35     /* Get the item */
36     size_t read_len = 0;
37     status = psa_its_get(uid, 0, sizeof(read_item), read_item, &read_len);
38     CHECK_EQUAL(PSA_SUCCESS, status);
39     CHECK_EQUAL(sizeof(item), read_len);
40     CHECK(memcmp(item, read_item, sizeof(item)) == 0);
41 
42     /* Remove the item */
43     status = psa_its_remove(uid);
44     CHECK_EQUAL(PSA_SUCCESS, status);
45 
46     /* Expect it to have gone */
47     status = psa_its_get_info(uid, &storage_info);
48     CHECK_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);
49 }
50 
storageLimitTest(size_t size_limit)51 void its_api_tests::storageLimitTest(size_t size_limit)
52 {
53     psa_status_t status;
54     psa_storage_uid_t uid = 10;
55     struct psa_storage_info_t storage_info;
56     static const size_t MAX_ITEM_SIZE = 10000;
57     uint8_t item[MAX_ITEM_SIZE];
58 
59     memset(item, 0x55, sizeof(item));
60 
61     /* Probe to check item does not exist */
62     status = psa_its_get_info(uid, &storage_info);
63     CHECK_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);
64 
65     /* Attempt to store an item that just exceeds the store's limit*/
66     status = psa_its_set(uid, size_limit + 1, item, PSA_STORAGE_FLAG_NONE);
67     CHECK(PSA_SUCCESS != status);
68 
69     /* Attempt to store a stupidly big item */
70     status = psa_its_set(uid, static_cast<size_t>(-1), item, PSA_STORAGE_FLAG_NONE);
71     CHECK(PSA_SUCCESS != status);
72 
73     /* Attempt to store a zero length item */
74     status = psa_its_set(uid, 0, item, PSA_STORAGE_FLAG_NONE);
75     CHECK_EQUAL(PSA_SUCCESS, status);
76 
77     /* Remove the item */
78     status = psa_its_remove(uid);
79     CHECK_EQUAL(PSA_SUCCESS, status);
80 }
81