1 /*
2 * Copyright (c) 2021, 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/protected_storage.h>
9 #include "ps_api_tests.h"
10
11 /**
12 * Creates a new slot and sets its contents in one go. Uses
13 * mandatory PS API operations only.
14 */
set()15 void ps_api_tests::set()
16 {
17 psa_status_t status;
18 psa_storage_uid_t uid = 10;
19 struct psa_storage_info_t storage_info;
20 static const size_t ITEM_SIZE = 68;
21 uint8_t item[ITEM_SIZE];
22 uint8_t read_item[ITEM_SIZE];
23
24 memset(item, 0x55, sizeof(item));
25
26 /* Probe to check item does not exist */
27 status = psa_ps_get_info(uid, &storage_info);
28 CHECK_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);
29
30 /* Store the item */
31 status = psa_ps_set(uid, sizeof(item), item, PSA_STORAGE_FLAG_NONE);
32 CHECK_EQUAL(PSA_SUCCESS, status);
33
34 /* Probe to check item now exists */
35 status = psa_ps_get_info(uid, &storage_info);
36 CHECK_EQUAL(PSA_SUCCESS, status);
37 CHECK_EQUAL(sizeof(item), storage_info.size);
38
39 /* Get the item */
40 size_t read_len = 0;
41 status = psa_ps_get(uid, 0, sizeof(read_item), read_item, &read_len);
42 CHECK_EQUAL(PSA_SUCCESS, status);
43 CHECK_EQUAL(sizeof(item), read_len);
44 CHECK(memcmp(item, read_item, sizeof(item)) == 0);
45
46 /* Remove the item */
47 status = psa_ps_remove(uid);
48 CHECK_EQUAL(PSA_SUCCESS, status);
49
50 /* Expect it to have gone */
51 status = psa_ps_get_info(uid, &storage_info);
52 CHECK_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);
53 }
54
55 /**
56 * Creates a new slot and if supported, uses the psa_ps_set_extended()
57 * method to set the object value in chunks.
58 */
createAndSetExtended()59 void ps_api_tests::createAndSetExtended()
60 {
61 psa_status_t status;
62 psa_storage_uid_t uid = 0xffff1;
63 struct psa_storage_info_t storage_info;
64 static const size_t ITEM_SIZE = 100;
65 uint8_t item[ITEM_SIZE];
66 uint8_t read_item[ITEM_SIZE];
67
68 if ((psa_ps_get_support() & PSA_STORAGE_SUPPORT_SET_EXTENDED) == 0)
69 return;
70
71 memset(item, 0xaa, sizeof(item));
72
73 /* Probe to check item does not exist */
74 status = psa_ps_get_info(uid, &storage_info);
75 CHECK_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);
76
77 /* Create empty store record with capcity for the item */
78 status = psa_ps_create(uid, sizeof(item), PSA_STORAGE_FLAG_NONE);
79 CHECK_EQUAL(PSA_SUCCESS, status);
80
81 /* The psa_ps_set_extended() method is optional so respect
82 * the feature capabilities advertised.
83 */
84 uint32_t supported_features = psa_ps_get_support();
85
86 if (supported_features & PSA_STORAGE_SUPPORT_SET_EXTENDED) {
87
88 size_t offset = 0;
89 size_t chunk_len = 10;
90
91 while (offset + chunk_len <= ITEM_SIZE) {
92
93 status = psa_ps_set_extended(uid, offset, chunk_len, &item[offset]);
94 CHECK_EQUAL(PSA_SUCCESS, status);
95
96 offset += chunk_len;
97 }
98
99 /* Expect the item to have been saved in its entirety */
100 size_t read_len = 0;
101 status = psa_ps_get(uid, 0, sizeof(read_item), read_item, &read_len);
102 CHECK_EQUAL(PSA_SUCCESS, status);
103 CHECK_EQUAL(sizeof(item), read_len);
104 CHECK(memcmp(item, read_item, sizeof(item)) == 0);
105 }
106
107 /* Remove the item */
108 status = psa_ps_remove(uid);
109 CHECK_EQUAL(PSA_SUCCESS, status);
110
111 /* Expect it to have gone */
112 status = psa_ps_get_info(uid, &storage_info);
113 CHECK_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);
114 }