Etienne Carriere | 9b7b70d | 2020-05-16 10:27:23 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014, STMicroelectronics International N.V. |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include "xtest_test.h" |
| 13 | #include "xtest_helpers.h" |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 14 | #include "tee_api_defines.h" |
| 15 | #include "tee_client_api.h" |
| 16 | |
| 17 | #define OFFSET0 0 |
| 18 | |
| 19 | #define PARAM_0 0 |
| 20 | #define PARAM_1 1 |
| 21 | #define PARAM_2 2 |
| 22 | #define PARAM_3 3 |
| 23 | |
| 24 | struct xtest_session { |
| 25 | ADBG_Case_t *c; |
| 26 | TEEC_Session session; |
| 27 | TEEC_Context context; |
| 28 | }; |
| 29 | |
| 30 | /* Compares two memories and checks if their length and content is the same */ |
| 31 | #define EXPECT_SHARED_MEM_BUFFER(c, exp_buf, exp_blen, op, param_num, shrm) \ |
| 32 | do { \ |
| 33 | if ((exp_buf) == NULL) { \ |
| 34 | ADBG_EXPECT((c), exp_blen, \ |
| 35 | (op)->params[(param_num)].memref.size); \ |
| 36 | } else { \ |
| 37 | ADBG_EXPECT_COMPARE_POINTER((c), (shrm), ==, \ |
| 38 | (op)->params[(param_num)].memref.parent); \ |
| 39 | ADBG_EXPECT_BUFFER((c), (exp_buf), (exp_blen), \ |
| 40 | (shrm)->buffer, \ |
| 41 | (op)->params[(param_num)].memref.size); \ |
| 42 | } \ |
| 43 | } while (0) |
| 44 | |
| 45 | /* |
| 46 | * Compares the content of the memory cells in OP with the expected value |
| 47 | * contained. |
| 48 | */ |
| 49 | #define EXPECT_OP_TMP_MEM_BUFFER(c, exp_buf, exp_blen, op, param_num, buf) \ |
| 50 | do { \ |
| 51 | if ((exp_buf) == NULL) { \ |
| 52 | ADBG_EXPECT((c), exp_blen, \ |
| 53 | (op)->params[(param_num)].tmpref.size); \ |
| 54 | } else { \ |
| 55 | ADBG_EXPECT_COMPARE_POINTER((c), (buf), ==, \ |
| 56 | (op)->params[(param_num)].tmpref.buffer); \ |
| 57 | ADBG_EXPECT_BUFFER((c), (exp_buf), (exp_blen), \ |
| 58 | (buf), \ |
| 59 | (op)->params[(param_num)].memref.size); \ |
| 60 | } \ |
| 61 | } while (0) |
| 62 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 63 | /* Registers the TEEC_SharedMemory to the TEE. */ |
| 64 | static TEEC_Result RegisterSharedMemory(TEEC_Context *ctx, |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 65 | TEEC_SharedMemory *shm, size_t size, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 66 | uint32_t flags) |
| 67 | { |
| 68 | shm->flags = flags; |
| 69 | shm->size = size; |
| 70 | return TEEC_RegisterSharedMemory(ctx, shm); |
| 71 | } |
| 72 | |
| 73 | /* Allocates shared memory inside of the TEE. */ |
| 74 | static TEEC_Result AllocateSharedMemory(TEEC_Context *ctx, |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 75 | TEEC_SharedMemory *shm, size_t size, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 76 | uint32_t flags) |
| 77 | { |
| 78 | shm->flags = flags; |
| 79 | shm->size = size; |
| 80 | return TEEC_AllocateSharedMemory(ctx, shm); |
| 81 | } |
| 82 | |
| 83 | static void CloseSession_null(struct xtest_session *cs) |
| 84 | { |
| 85 | Do_ADBG_BeginSubCase(cs->c, "CloseSession_null"); |
| 86 | { |
| 87 | /* In reality doesn't test anything. */ |
| 88 | TEEC_CloseSession(NULL); |
| 89 | } |
| 90 | Do_ADBG_EndSubCase(cs->c, "CloseSession_null"); |
| 91 | } |
| 92 | |
| 93 | static void Allocate_In(struct xtest_session *cs) |
| 94 | { |
| 95 | Do_ADBG_BeginSubCase(cs->c, "Allocate_In"); |
| 96 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 97 | TEEC_SharedMemory shm = { }; |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 98 | size_t size = 1024; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 99 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 100 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 101 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 102 | goto out; |
| 103 | |
| 104 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 105 | AllocateSharedMemory(&cs->context, &shm, size, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 106 | TEEC_MEM_INPUT))) |
| 107 | goto out_final; |
| 108 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 109 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 110 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 111 | TEEC_FinalizeContext(&cs->context); |
| 112 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 113 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 114 | Do_ADBG_EndSubCase(cs->c, "Allocate_In"); |
| 115 | } |
| 116 | |
| 117 | static void Allocate_out_of_memory(struct xtest_session *cs) |
| 118 | { |
| 119 | Do_ADBG_BeginSubCase(cs->c, "Allocate_out_of_memory"); |
| 120 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 121 | TEEC_SharedMemory shm = { }; |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 122 | size_t SIZE_OVER_MEMORY_CAPACITY = SIZE_MAX; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 123 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 124 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 125 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 126 | goto out; |
| 127 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 128 | ADBG_EXPECT_TEEC_RESULT(cs->c, TEEC_ERROR_OUT_OF_MEMORY, |
| 129 | AllocateSharedMemory(&cs->context, &shm, |
| 130 | SIZE_OVER_MEMORY_CAPACITY, |
| 131 | TEEC_MEM_INPUT)); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 132 | TEEC_FinalizeContext(&cs->context); |
| 133 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 134 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 135 | Do_ADBG_EndSubCase(cs->c, "Allocate_out_of_memory"); |
| 136 | } |
| 137 | |
| 138 | static void OpenSession_error_notExistingTA(struct xtest_session *cs) |
| 139 | { |
| 140 | Do_ADBG_BeginSubCase(cs->c, "OpenSession_error_notExistingTA"); |
| 141 | { |
| 142 | TEEC_UUID NONEXISTING_TA_UUID = { 0x534D1192, 0x6143, 0x234C, |
| 143 | { 0x47, 0x55, 0x53, 0x52, |
| 144 | 0x54, 0x4F, 0x4F, 0x59 } }; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 145 | uint32_t ret_orig = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 146 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 147 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 148 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 149 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 150 | |
| 151 | ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_SUCCESS, !=, |
| 152 | TEEC_OpenSession(&cs->context, &cs->session, |
| 153 | &NONEXISTING_TA_UUID, |
| 154 | TEEC_LOGIN_PUBLIC, NULL, NULL, |
| 155 | &ret_orig)); |
| 156 | ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_ORIGIN_TRUSTED_APP, !=, |
| 157 | ret_orig); |
| 158 | TEEC_FinalizeContext(&cs->context); |
| 159 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 160 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 161 | Do_ADBG_EndSubCase(cs->c, "OpenSession_error_notExistingTA"); |
| 162 | } |
| 163 | |
| 164 | static void Allocate_InOut(struct xtest_session *cs) |
| 165 | { |
| 166 | Do_ADBG_BeginSubCase(cs->c, "Allocate_InOut"); |
| 167 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 168 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 169 | uint8_t val[] = { 54, 76, 98, 32 }; |
| 170 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 171 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 172 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 173 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 174 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 175 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 176 | AllocateSharedMemory(&cs->context, &shm, sizeof(val), |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 177 | TEEC_MEM_INPUT | TEEC_MEM_OUTPUT))) |
| 178 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 179 | |
| 180 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 181 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 182 | TEEC_FinalizeContext(&cs->context); |
| 183 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 184 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 185 | Do_ADBG_EndSubCase(cs->c, "Allocate_InOut"); |
| 186 | } |
| 187 | |
| 188 | static void Register_In(struct xtest_session *cs) |
| 189 | { |
| 190 | Do_ADBG_BeginSubCase(cs->c, "Register_In"); |
| 191 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 192 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 193 | uint8_t val[] = { 32, 65, 43, 21, 98 }; |
| 194 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 195 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 196 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 197 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 198 | |
| 199 | shm.buffer = val; |
| 200 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 201 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 202 | RegisterSharedMemory(&cs->context, &shm, sizeof(val), |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 203 | TEEC_MEM_INPUT))) |
| 204 | goto out_final; |
| 205 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 206 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 207 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 208 | TEEC_FinalizeContext(&cs->context); |
| 209 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 210 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 211 | Do_ADBG_EndSubCase(cs->c, "Register_In"); |
| 212 | } |
| 213 | |
| 214 | static void Register_notZeroLength_Out(struct xtest_session *cs) |
| 215 | { |
| 216 | Do_ADBG_BeginSubCase(cs->c, "Register_notZeroLength_Out"); |
| 217 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 218 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 219 | uint8_t val[] = { 56, 67, 78, 99 }; |
| 220 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 221 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 222 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 223 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 224 | |
| 225 | shm.buffer = val; |
| 226 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 227 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 228 | RegisterSharedMemory(&cs->context, &shm, sizeof(val), |
| 229 | TEEC_MEM_OUTPUT))) |
| 230 | goto out_final; |
| 231 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 232 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 233 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 234 | TEEC_FinalizeContext(&cs->context); |
| 235 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 236 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 237 | Do_ADBG_EndSubCase(cs->c, "Register_notZeroLength_Out"); |
| 238 | } |
| 239 | |
| 240 | static void Register_InOut(struct xtest_session *cs) |
| 241 | { |
| 242 | Do_ADBG_BeginSubCase(cs->c, "Register_InOut"); |
| 243 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 244 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 245 | uint8_t val[] = { 54, 76, 23, 98, 255, 23, 86 }; |
| 246 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 247 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 248 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 249 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 250 | |
| 251 | shm.buffer = val; |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 252 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 253 | RegisterSharedMemory(&cs->context, &shm, sizeof(val), |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 254 | TEEC_MEM_INPUT | TEEC_MEM_OUTPUT))) |
| 255 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 256 | |
| 257 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 258 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 259 | TEEC_FinalizeContext(&cs->context); |
| 260 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 261 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 262 | Do_ADBG_EndSubCase(cs->c, "Register_InOut"); |
| 263 | } |
| 264 | |
| 265 | static void Register_zeroLength_Out(struct xtest_session *cs) |
| 266 | { |
| 267 | Do_ADBG_BeginSubCase(cs->c, "Register_zeroLength_Out"); |
| 268 | { |
| 269 | uint8_t val[] = { 65, 76, 98, 32 }; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 270 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 271 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 272 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 273 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 274 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 275 | |
| 276 | shm.buffer = val; |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 277 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 278 | RegisterSharedMemory(&cs->context, &shm, 0, |
| 279 | TEEC_MEM_OUTPUT))) |
| 280 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 281 | |
| 282 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 283 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 284 | TEEC_FinalizeContext(&cs->context); |
| 285 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 286 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 287 | Do_ADBG_EndSubCase(cs->c, "Register_zeroLength_Out"); |
| 288 | } |
| 289 | |
| 290 | static void Allocate_Out(struct xtest_session *cs) |
| 291 | { |
| 292 | Do_ADBG_BeginSubCase(cs->c, "Allocate_Out"); |
| 293 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 294 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 295 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 296 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 297 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 298 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 299 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 300 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 301 | AllocateSharedMemory(&cs->context, &shm, 0, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 302 | TEEC_MEM_OUTPUT))) |
| 303 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 304 | |
| 305 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 306 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 307 | TEEC_FinalizeContext(&cs->context); |
| 308 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 309 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 310 | Do_ADBG_EndSubCase(cs->c, "Allocate_Out"); |
| 311 | } |
| 312 | |
| 313 | static void FinalizeContext_null(struct xtest_session *cs) |
| 314 | { |
| 315 | Do_ADBG_BeginSubCase(cs->c, "FinalizeContext_null"); |
| 316 | { |
| 317 | TEEC_FinalizeContext(NULL); |
| 318 | } |
| 319 | Do_ADBG_EndSubCase(cs->c, "FinalizeContext_null"); |
| 320 | } |
| 321 | |
| 322 | static void InitializeContext_NotExistingTEE(struct xtest_session *cs) |
| 323 | { |
| 324 | Do_ADBG_BeginSubCase(cs->c, "InitializeContext_NotExistingTEE"); |
| 325 | { |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 326 | if (!ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_SUCCESS, !=, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 327 | TEEC_InitializeContext("Invalid TEE name", |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 328 | &cs->context))) |
| 329 | TEEC_FinalizeContext(&cs->context); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 330 | } |
| 331 | Do_ADBG_EndSubCase(cs->c, "InitializeContext_NotExistingTEE"); |
| 332 | } |
| 333 | |
| 334 | static void AllocateThenRegister_SameMemory(struct xtest_session *cs) |
| 335 | { |
| 336 | Do_ADBG_BeginSubCase(cs->c, "AllocateThenRegister_SameMemory"); |
| 337 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 338 | TEEC_SharedMemory shm = { }; |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 339 | size_t size_allocation = 32; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 340 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 341 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 342 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 343 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 344 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 345 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 346 | AllocateSharedMemory(&cs->context, &shm, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 347 | size_allocation, TEEC_MEM_INPUT))) |
| 348 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 349 | |
| 350 | ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 351 | RegisterSharedMemory(&cs->context, &shm, |
| 352 | size_allocation, TEEC_MEM_INPUT)); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 353 | |
| 354 | TEEC_ReleaseSharedMemory(&shm); |
| 355 | out_final: |
| 356 | TEEC_FinalizeContext(&cs->context); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 357 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 358 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 359 | Do_ADBG_EndSubCase(cs->c, "AllocateThenRegister_SameMemory"); |
| 360 | } |
| 361 | |
| 362 | static void AllocateSameMemory_twice(struct xtest_session *cs) |
| 363 | { |
| 364 | Do_ADBG_BeginSubCase(cs->c, "AllocateSameMemory_twice"); |
| 365 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 366 | TEEC_SharedMemory shm = { }; |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 367 | size_t size_allocation = 32; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 368 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 369 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 370 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 371 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 372 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 373 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 374 | AllocateSharedMemory(&cs->context, &shm, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 375 | size_allocation, TEEC_MEM_INPUT))) |
| 376 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 377 | |
| 378 | ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 379 | AllocateSharedMemory(&cs->context, &shm, |
| 380 | size_allocation, TEEC_MEM_INPUT)); |
| 381 | |
| 382 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 383 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 384 | TEEC_FinalizeContext(&cs->context); |
| 385 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 386 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 387 | Do_ADBG_EndSubCase(cs->c, "AllocateSameMemory_twice"); |
| 388 | } |
| 389 | |
| 390 | static void RegisterSameMemory_twice(struct xtest_session *cs) |
| 391 | { |
| 392 | Do_ADBG_BeginSubCase(cs->c, "RegisterSameMemory_twice"); |
| 393 | { |
| 394 | uint8_t val[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 395 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 396 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 397 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 398 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 399 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 400 | |
| 401 | shm.buffer = val; |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 402 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 403 | RegisterSharedMemory(&cs->context, &shm, sizeof(val), |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 404 | TEEC_MEM_INPUT))) |
| 405 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 406 | |
| 407 | ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 408 | RegisterSharedMemory(&cs->context, &shm, sizeof(val), |
| 409 | TEEC_MEM_INPUT)); |
| 410 | |
| 411 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 412 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 413 | TEEC_FinalizeContext(&cs->context); |
| 414 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 415 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 416 | Do_ADBG_EndSubCase(cs->c, "RegisterSameMemory_twice"); |
| 417 | } |
| 418 | |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 419 | #ifndef MIN |
| 420 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
| 421 | #endif |
| 422 | |
| 423 | static void Allocate_sharedMemory_32k(struct xtest_session *cs) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 424 | { |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 425 | Do_ADBG_BeginSubCase(cs->c, "Allocate_sharedMemory_32k"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 426 | { |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 427 | size_t size = MIN(32 * 1024, |
| 428 | TEEC_CONFIG_SHAREDMEM_MAX_SIZE); |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 429 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 430 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 431 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 432 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 433 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 434 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 435 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 436 | AllocateSharedMemory(&cs->context, &shm, size, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 437 | TEEC_MEM_INPUT))) |
| 438 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 439 | |
| 440 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 441 | out_final: |
| 442 | TEEC_FinalizeContext(&cs->context); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 443 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 444 | out: |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 445 | Do_ADBG_EndSubCase(cs->c, "Allocate_sharedMemory_32k"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 448 | #define SHM_32K_SIZE MIN(32 * 1024, TEEC_CONFIG_SHAREDMEM_MAX_SIZE) |
| 449 | |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 450 | static void Register_sharedMemory_32k(struct xtest_session *cs) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 451 | { |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 452 | Do_ADBG_BeginSubCase(cs->c, "Register_sharedMemory_32k"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 453 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 454 | uint8_t val[SHM_32K_SIZE] = { }; |
| 455 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 456 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 457 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 458 | TEEC_InitializeContext(xtest_tee_name, &cs->context))) |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 459 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 460 | |
| 461 | shm.buffer = val; |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 462 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 463 | RegisterSharedMemory(&cs->context, &shm, SHM_32K_SIZE, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 464 | TEEC_MEM_INPUT))) |
| 465 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 466 | |
| 467 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 468 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 469 | TEEC_FinalizeContext(&cs->context); |
| 470 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 471 | out: |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 472 | Do_ADBG_EndSubCase(cs->c, "Register_sharedMemory_32k"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static void xtest_teec_TEE(ADBG_Case_t *c) |
| 476 | { |
| 477 | struct xtest_session connection = { c }; |
| 478 | |
| 479 | CloseSession_null(&connection); |
| 480 | |
| 481 | Allocate_In(&connection); |
| 482 | |
| 483 | Allocate_out_of_memory(&connection); |
| 484 | |
| 485 | OpenSession_error_notExistingTA(&connection); |
| 486 | |
| 487 | Allocate_InOut(&connection); |
| 488 | |
| 489 | Register_In(&connection); |
| 490 | |
| 491 | Register_notZeroLength_Out(&connection); |
| 492 | |
| 493 | Register_InOut(&connection); |
| 494 | |
| 495 | Register_zeroLength_Out(&connection); |
| 496 | |
| 497 | Allocate_Out(&connection); |
| 498 | |
| 499 | FinalizeContext_null(&connection); |
| 500 | |
| 501 | InitializeContext_NotExistingTEE(&connection); |
| 502 | |
| 503 | AllocateThenRegister_SameMemory(&connection); |
| 504 | |
| 505 | AllocateSameMemory_twice(&connection); |
| 506 | |
| 507 | RegisterSameMemory_twice(&connection); |
| 508 | |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 509 | Allocate_sharedMemory_32k(&connection); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 510 | |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 511 | Register_sharedMemory_32k(&connection); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Jens Wiklander | 74abfe3 | 2017-01-03 14:17:47 +0100 | [diff] [blame] | 514 | ADBG_CASE_DEFINE(regression, 5006, xtest_teec_TEE, |
Jens Wiklander | 25a57fe | 2016-12-26 21:46:24 +0100 | [diff] [blame] | 515 | "Tests for Global platform TEEC"); |