blob: 448ee162ea836f45c9f741d70dd48712713b8252 [file] [log] [blame]
Pascal Brandc639ac82015-07-02 08:53:34 +02001/*
2 * Copyright (c) 2014, STMicroelectronics International N.V.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <stdio.h>
15#include <string.h>
16#include <stdlib.h>
17#include <sys/types.h>
18#include <unistd.h>
19
20#include "xtest_test.h"
21#include "xtest_helpers.h"
Pascal Brandc639ac82015-07-02 08:53:34 +020022#include "tee_api_defines.h"
23#include "tee_client_api.h"
24
25#define OFFSET0 0
26
27#define PARAM_0 0
28#define PARAM_1 1
29#define PARAM_2 2
30#define PARAM_3 3
31
32struct xtest_session {
33 ADBG_Case_t *c;
34 TEEC_Session session;
35 TEEC_Context context;
36};
37
38/* Compares two memories and checks if their length and content is the same */
39#define EXPECT_SHARED_MEM_BUFFER(c, exp_buf, exp_blen, op, param_num, shrm) \
40 do { \
41 if ((exp_buf) == NULL) { \
42 ADBG_EXPECT((c), exp_blen, \
43 (op)->params[(param_num)].memref.size); \
44 } else { \
45 ADBG_EXPECT_COMPARE_POINTER((c), (shrm), ==, \
46 (op)->params[(param_num)].memref.parent); \
47 ADBG_EXPECT_BUFFER((c), (exp_buf), (exp_blen), \
48 (shrm)->buffer, \
49 (op)->params[(param_num)].memref.size); \
50 } \
51 } while (0)
52
53/*
54 * Compares the content of the memory cells in OP with the expected value
55 * contained.
56 */
57#define EXPECT_OP_TMP_MEM_BUFFER(c, exp_buf, exp_blen, op, param_num, buf) \
58 do { \
59 if ((exp_buf) == NULL) { \
60 ADBG_EXPECT((c), exp_blen, \
61 (op)->params[(param_num)].tmpref.size); \
62 } else { \
63 ADBG_EXPECT_COMPARE_POINTER((c), (buf), ==, \
64 (op)->params[(param_num)].tmpref.buffer); \
65 ADBG_EXPECT_BUFFER((c), (exp_buf), (exp_blen), \
66 (buf), \
67 (op)->params[(param_num)].memref.size); \
68 } \
69 } while (0)
70
71/* Initiates the memory and allocates size uint32_t. */
72
73/* Registers the TEEC_SharedMemory to the TEE. */
74static TEEC_Result RegisterSharedMemory(TEEC_Context *ctx,
75 TEEC_SharedMemory *shm, uint32_t size,
76 uint32_t flags)
77{
78 shm->flags = flags;
79 shm->size = size;
80 return TEEC_RegisterSharedMemory(ctx, shm);
81}
82
83/* Allocates shared memory inside of the TEE. */
84static TEEC_Result AllocateSharedMemory(TEEC_Context *ctx,
85 TEEC_SharedMemory *shm, uint32_t size,
86 uint32_t flags)
87{
88 shm->flags = flags;
89 shm->size = size;
90 return TEEC_AllocateSharedMemory(ctx, shm);
91}
92
93static void CloseSession_null(struct xtest_session *cs)
94{
95 Do_ADBG_BeginSubCase(cs->c, "CloseSession_null");
96 {
97 /* In reality doesn't test anything. */
98 TEEC_CloseSession(NULL);
99 }
100 Do_ADBG_EndSubCase(cs->c, "CloseSession_null");
101}
102
103static void Allocate_In(struct xtest_session *cs)
104{
105 Do_ADBG_BeginSubCase(cs->c, "Allocate_In");
106 {
107 TEEC_SharedMemory shm;
108 uint32_t size = 1024;
109
Jens Wiklander72c55452016-10-27 20:44:35 +0200110 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
111 TEEC_InitializeContext(_device, &cs->context)))
112 goto out;
113
114 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200115 AllocateSharedMemory(&cs->context, &shm, size,
Jens Wiklander72c55452016-10-27 20:44:35 +0200116 TEEC_MEM_INPUT)))
117 goto out_final;
118
Pascal Brandc639ac82015-07-02 08:53:34 +0200119 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200120out_final:
Pascal Brandc639ac82015-07-02 08:53:34 +0200121 TEEC_FinalizeContext(&cs->context);
122 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200123out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200124 Do_ADBG_EndSubCase(cs->c, "Allocate_In");
125}
126
127static void Allocate_out_of_memory(struct xtest_session *cs)
128{
129 Do_ADBG_BeginSubCase(cs->c, "Allocate_out_of_memory");
130 {
131 TEEC_SharedMemory shm;
132 uint32_t SIZE_OVER_MEMORY_CAPACITY = INT32_MAX;
133
Jens Wiklander72c55452016-10-27 20:44:35 +0200134 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
135 TEEC_InitializeContext(_device, &cs->context)))
136 goto out;
137
Pascal Brandc639ac82015-07-02 08:53:34 +0200138 ADBG_EXPECT_TEEC_RESULT(cs->c, TEEC_ERROR_OUT_OF_MEMORY,
139 AllocateSharedMemory(&cs->context, &shm,
140 SIZE_OVER_MEMORY_CAPACITY,
141 TEEC_MEM_INPUT));
Pascal Brandc639ac82015-07-02 08:53:34 +0200142 TEEC_FinalizeContext(&cs->context);
143 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200144out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200145 Do_ADBG_EndSubCase(cs->c, "Allocate_out_of_memory");
146}
147
148static void OpenSession_error_notExistingTA(struct xtest_session *cs)
149{
150 Do_ADBG_BeginSubCase(cs->c, "OpenSession_error_notExistingTA");
151 {
152 TEEC_UUID NONEXISTING_TA_UUID = { 0x534D1192, 0x6143, 0x234C,
153 { 0x47, 0x55, 0x53, 0x52,
154 0x54, 0x4F, 0x4F, 0x59 } };
155 uint32_t ret_orig;
156
Jens Wiklander72c55452016-10-27 20:44:35 +0200157 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
158 TEEC_InitializeContext(_device, &cs->context)))
159 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200160
161 ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_SUCCESS, !=,
162 TEEC_OpenSession(&cs->context, &cs->session,
163 &NONEXISTING_TA_UUID,
164 TEEC_LOGIN_PUBLIC, NULL, NULL,
165 &ret_orig));
166 ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_ORIGIN_TRUSTED_APP, !=,
167 ret_orig);
168 TEEC_FinalizeContext(&cs->context);
169 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200170out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200171 Do_ADBG_EndSubCase(cs->c, "OpenSession_error_notExistingTA");
172}
173
174static void Allocate_InOut(struct xtest_session *cs)
175{
176 Do_ADBG_BeginSubCase(cs->c, "Allocate_InOut");
177 {
178 TEEC_SharedMemory shm;
179 uint8_t val[] = { 54, 76, 98, 32 };
180
Jens Wiklander72c55452016-10-27 20:44:35 +0200181 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
182 TEEC_InitializeContext(_device, &cs->context)))
183 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200184
Jens Wiklander72c55452016-10-27 20:44:35 +0200185 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200186 AllocateSharedMemory(&cs->context, &shm, sizeof(val),
Jens Wiklander72c55452016-10-27 20:44:35 +0200187 TEEC_MEM_INPUT | TEEC_MEM_OUTPUT)))
188 goto out_final;
Pascal Brandc639ac82015-07-02 08:53:34 +0200189
190 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200191out_final:
Pascal Brandc639ac82015-07-02 08:53:34 +0200192 TEEC_FinalizeContext(&cs->context);
193 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200194out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200195 Do_ADBG_EndSubCase(cs->c, "Allocate_InOut");
196}
197
198static void Register_In(struct xtest_session *cs)
199{
200 Do_ADBG_BeginSubCase(cs->c, "Register_In");
201 {
202 TEEC_SharedMemory shm;
203 uint8_t val[] = { 32, 65, 43, 21, 98 };
204
Jens Wiklander72c55452016-10-27 20:44:35 +0200205 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
206 TEEC_InitializeContext(_device, &cs->context)))
207 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200208
209 shm.buffer = val;
210
Jens Wiklander72c55452016-10-27 20:44:35 +0200211 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200212 RegisterSharedMemory(&cs->context, &shm, sizeof(val),
Jens Wiklander72c55452016-10-27 20:44:35 +0200213 TEEC_MEM_INPUT)))
214 goto out_final;
215
Pascal Brandc639ac82015-07-02 08:53:34 +0200216 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200217out_final:
Pascal Brandc639ac82015-07-02 08:53:34 +0200218 TEEC_FinalizeContext(&cs->context);
219 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200220out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200221 Do_ADBG_EndSubCase(cs->c, "Register_In");
222}
223
224static void Register_notZeroLength_Out(struct xtest_session *cs)
225{
226 Do_ADBG_BeginSubCase(cs->c, "Register_notZeroLength_Out");
227 {
228 TEEC_SharedMemory shm;
229 uint8_t val[] = { 56, 67, 78, 99 };
230
Jens Wiklander72c55452016-10-27 20:44:35 +0200231 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
232 TEEC_InitializeContext(_device, &cs->context)))
233 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200234
235 shm.buffer = val;
236
Jens Wiklander72c55452016-10-27 20:44:35 +0200237 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
238 RegisterSharedMemory(&cs->context, &shm, sizeof(val),
239 TEEC_MEM_OUTPUT)))
240 goto out_final;
241
Pascal Brandc639ac82015-07-02 08:53:34 +0200242 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200243out_final:
Pascal Brandc639ac82015-07-02 08:53:34 +0200244 TEEC_FinalizeContext(&cs->context);
245 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200246out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200247 Do_ADBG_EndSubCase(cs->c, "Register_notZeroLength_Out");
248}
249
250static void Register_InOut(struct xtest_session *cs)
251{
252 Do_ADBG_BeginSubCase(cs->c, "Register_InOut");
253 {
254 TEEC_SharedMemory shm;
255 uint8_t val[] = { 54, 76, 23, 98, 255, 23, 86 };
256
Jens Wiklander72c55452016-10-27 20:44:35 +0200257 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
258 TEEC_InitializeContext(_device, &cs->context)))
259 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200260
261 shm.buffer = val;
Jens Wiklander72c55452016-10-27 20:44:35 +0200262 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200263 RegisterSharedMemory(&cs->context, &shm, sizeof(val),
Jens Wiklander72c55452016-10-27 20:44:35 +0200264 TEEC_MEM_INPUT | TEEC_MEM_OUTPUT)))
265 goto out_final;
Pascal Brandc639ac82015-07-02 08:53:34 +0200266
267 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200268out_final:
Pascal Brandc639ac82015-07-02 08:53:34 +0200269 TEEC_FinalizeContext(&cs->context);
270 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200271out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200272 Do_ADBG_EndSubCase(cs->c, "Register_InOut");
273}
274
275static void Register_zeroLength_Out(struct xtest_session *cs)
276{
277 Do_ADBG_BeginSubCase(cs->c, "Register_zeroLength_Out");
278 {
279 uint8_t val[] = { 65, 76, 98, 32 };
280 TEEC_SharedMemory shm;
281
Jens Wiklander72c55452016-10-27 20:44:35 +0200282 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
283 TEEC_InitializeContext(_device, &cs->context)))
284 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200285
286 shm.buffer = val;
Jens Wiklander72c55452016-10-27 20:44:35 +0200287 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
288 RegisterSharedMemory(&cs->context, &shm, 0,
289 TEEC_MEM_OUTPUT)))
290 goto out_final;
Pascal Brandc639ac82015-07-02 08:53:34 +0200291
292 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200293out_final:
Pascal Brandc639ac82015-07-02 08:53:34 +0200294 TEEC_FinalizeContext(&cs->context);
295 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200296out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200297 Do_ADBG_EndSubCase(cs->c, "Register_zeroLength_Out");
298}
299
300static void Allocate_Out(struct xtest_session *cs)
301{
302 Do_ADBG_BeginSubCase(cs->c, "Allocate_Out");
303 {
304 TEEC_SharedMemory shm;
305
Jens Wiklander72c55452016-10-27 20:44:35 +0200306 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
307 TEEC_InitializeContext(_device, &cs->context)))
308 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200309
Jens Wiklander72c55452016-10-27 20:44:35 +0200310 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200311 AllocateSharedMemory(&cs->context, &shm, 0,
Jens Wiklander72c55452016-10-27 20:44:35 +0200312 TEEC_MEM_OUTPUT)))
313 goto out_final;
Pascal Brandc639ac82015-07-02 08:53:34 +0200314
315 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200316out_final:
Pascal Brandc639ac82015-07-02 08:53:34 +0200317 TEEC_FinalizeContext(&cs->context);
318 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200319out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200320 Do_ADBG_EndSubCase(cs->c, "Allocate_Out");
321}
322
323static void FinalizeContext_null(struct xtest_session *cs)
324{
325 Do_ADBG_BeginSubCase(cs->c, "FinalizeContext_null");
326 {
327 TEEC_FinalizeContext(NULL);
328 }
329 Do_ADBG_EndSubCase(cs->c, "FinalizeContext_null");
330}
331
332static void InitializeContext_NotExistingTEE(struct xtest_session *cs)
333{
334 Do_ADBG_BeginSubCase(cs->c, "InitializeContext_NotExistingTEE");
335 {
Jens Wiklander72c55452016-10-27 20:44:35 +0200336 if (!ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_SUCCESS, !=,
Pascal Brandc639ac82015-07-02 08:53:34 +0200337 TEEC_InitializeContext("Invalid TEE name",
Jens Wiklander72c55452016-10-27 20:44:35 +0200338 &cs->context)))
339 TEEC_FinalizeContext(&cs->context);
Pascal Brandc639ac82015-07-02 08:53:34 +0200340 }
341 Do_ADBG_EndSubCase(cs->c, "InitializeContext_NotExistingTEE");
342}
343
344static void AllocateThenRegister_SameMemory(struct xtest_session *cs)
345{
346 Do_ADBG_BeginSubCase(cs->c, "AllocateThenRegister_SameMemory");
347 {
348 TEEC_SharedMemory shm;
349 uint32_t size_allocation = 32;
350
Jens Wiklander72c55452016-10-27 20:44:35 +0200351 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
352 TEEC_InitializeContext(_device, &cs->context)))
353 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200354
Jens Wiklander72c55452016-10-27 20:44:35 +0200355 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200356 AllocateSharedMemory(&cs->context, &shm,
Jens Wiklander72c55452016-10-27 20:44:35 +0200357 size_allocation, TEEC_MEM_INPUT)))
358 goto out_final;
Pascal Brandc639ac82015-07-02 08:53:34 +0200359
360 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
361 RegisterSharedMemory(&cs->context, &shm,
362 size_allocation, TEEC_MEM_INPUT));
Jens Wiklander72c55452016-10-27 20:44:35 +0200363
364 TEEC_ReleaseSharedMemory(&shm);
365out_final:
366 TEEC_FinalizeContext(&cs->context);
Pascal Brandc639ac82015-07-02 08:53:34 +0200367 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200368out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200369 Do_ADBG_EndSubCase(cs->c, "AllocateThenRegister_SameMemory");
370}
371
372static void AllocateSameMemory_twice(struct xtest_session *cs)
373{
374 Do_ADBG_BeginSubCase(cs->c, "AllocateSameMemory_twice");
375 {
376 TEEC_SharedMemory shm;
377 uint32_t size_allocation = 32;
378
Jens Wiklander72c55452016-10-27 20:44:35 +0200379 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
380 TEEC_InitializeContext(_device, &cs->context)))
381 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200382
Jens Wiklander72c55452016-10-27 20:44:35 +0200383 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200384 AllocateSharedMemory(&cs->context, &shm,
Jens Wiklander72c55452016-10-27 20:44:35 +0200385 size_allocation, TEEC_MEM_INPUT)))
386 goto out_final;
Pascal Brandc639ac82015-07-02 08:53:34 +0200387
388 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
389 AllocateSharedMemory(&cs->context, &shm,
390 size_allocation, TEEC_MEM_INPUT));
391
392 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200393out_final:
Pascal Brandc639ac82015-07-02 08:53:34 +0200394 TEEC_FinalizeContext(&cs->context);
395 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200396out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200397 Do_ADBG_EndSubCase(cs->c, "AllocateSameMemory_twice");
398}
399
400static void RegisterSameMemory_twice(struct xtest_session *cs)
401{
402 Do_ADBG_BeginSubCase(cs->c, "RegisterSameMemory_twice");
403 {
404 uint8_t val[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
405 TEEC_SharedMemory shm;
406
Jens Wiklander72c55452016-10-27 20:44:35 +0200407 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
408 TEEC_InitializeContext(_device, &cs->context)))
409 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200410
411 shm.buffer = val;
Jens Wiklander72c55452016-10-27 20:44:35 +0200412 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200413 RegisterSharedMemory(&cs->context, &shm, sizeof(val),
Jens Wiklander72c55452016-10-27 20:44:35 +0200414 TEEC_MEM_INPUT)))
415 goto out_final;
Pascal Brandc639ac82015-07-02 08:53:34 +0200416
417 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
418 RegisterSharedMemory(&cs->context, &shm, sizeof(val),
419 TEEC_MEM_INPUT));
420
421 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200422out_final:
Pascal Brandc639ac82015-07-02 08:53:34 +0200423 TEEC_FinalizeContext(&cs->context);
424 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200425out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200426 Do_ADBG_EndSubCase(cs->c, "RegisterSameMemory_twice");
427}
428
429static void Allocate_sharedMemory_maxSize(struct xtest_session *cs)
430{
431 Do_ADBG_BeginSubCase(cs->c,
432 "Allocate_sharedMemory_MaxSize_Above_and_Below, allocate max size");
433 {
434 uint32_t size_max = TEEC_CONFIG_SHAREDMEM_MAX_SIZE;
435 TEEC_SharedMemory shm;
436
Jens Wiklander72c55452016-10-27 20:44:35 +0200437 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
438 TEEC_InitializeContext(_device, &cs->context)))
439 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200440
Jens Wiklander72c55452016-10-27 20:44:35 +0200441 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200442 AllocateSharedMemory(&cs->context, &shm, size_max,
Jens Wiklander72c55452016-10-27 20:44:35 +0200443 TEEC_MEM_INPUT)))
444 goto out_final;
Pascal Brandc639ac82015-07-02 08:53:34 +0200445
446 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200447out_final:
448 TEEC_FinalizeContext(&cs->context);
Pascal Brandc639ac82015-07-02 08:53:34 +0200449 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200450out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200451 Do_ADBG_EndSubCase(cs->c,
452 "Allocate_sharedMemory_MaxSize_Above_and_Below, allocate max size");
453}
454
455static void Allocate_sharedMemory_belowMaxSize(struct xtest_session *cs)
456{
457 Do_ADBG_BeginSubCase(cs->c,
458 "Allocate_sharedMemory_MaxSize_Above_and_Below, "
459 "allocate just below max size");
460 {
461 TEEC_SharedMemory shm;
462 uint32_t size_below = TEEC_CONFIG_SHAREDMEM_MAX_SIZE - 1;
463
Jens Wiklander72c55452016-10-27 20:44:35 +0200464 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
465 TEEC_InitializeContext(_device, &cs->context)))
466 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200467
Jens Wiklander72c55452016-10-27 20:44:35 +0200468 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200469 AllocateSharedMemory(&cs->context, &shm, size_below,
Jens Wiklander72c55452016-10-27 20:44:35 +0200470 TEEC_MEM_INPUT)))
471 goto out_final;
Pascal Brandc639ac82015-07-02 08:53:34 +0200472
473 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200474out_final:
Pascal Brandc639ac82015-07-02 08:53:34 +0200475 TEEC_FinalizeContext(&cs->context);
476 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200477out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200478 Do_ADBG_EndSubCase(cs->c,
479 "Allocate_sharedMemory_MaxSize_Above_and_Below, "
480 "allocate just below max size");
481}
482
483static void Allocate_sharedMemory_aboveMaxSize(struct xtest_session *cs)
484{
485 Do_ADBG_BeginSubCase(cs->c,
486 "Allocate_sharedMemory_MaxSize_Above_and_Below, "
487 "allocate just above max size");
488 {
489 TEEC_Result res;
490 TEEC_SharedMemory shm;
491 uint32_t size_above = TEEC_CONFIG_SHAREDMEM_MAX_SIZE + 1;
492
Jens Wiklander72c55452016-10-27 20:44:35 +0200493 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
494 TEEC_InitializeContext(_device, &cs->context)))
495 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200496
497 res = AllocateSharedMemory(&cs->context, &shm, size_above,
498 TEEC_MEM_INPUT);
499
500 ADBG_EXPECT_TRUE(cs->c, res == TEEC_ERROR_OUT_OF_MEMORY ||
501 res == TEEC_SUCCESS);
502
Jens Wiklander72c55452016-10-27 20:44:35 +0200503 if (res == TEEC_SUCCESS)
504 TEEC_ReleaseSharedMemory(&shm);
Pascal Brandc639ac82015-07-02 08:53:34 +0200505 TEEC_FinalizeContext(&cs->context);
506 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200507out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200508 Do_ADBG_EndSubCase(cs->c,
509 "Allocate_sharedMemory_MaxSize_Above_and_Below, "
510 "allocate just above max size");
511}
512
513static void Register_sharedMemory_maxSize(struct xtest_session *cs)
514{
515 Do_ADBG_BeginSubCase(cs->c, "Register_sharedMemory_maxSize");
516 {
517 uint32_t size_max = TEEC_CONFIG_SHAREDMEM_MAX_SIZE;
518 uint8_t val[size_max];
519 TEEC_SharedMemory shm;
520
Jens Wiklander72c55452016-10-27 20:44:35 +0200521 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
522 TEEC_InitializeContext(_device, &cs->context)))
523 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200524
525 shm.buffer = val;
Jens Wiklander72c55452016-10-27 20:44:35 +0200526 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200527 RegisterSharedMemory(&cs->context, &shm, size_max,
Jens Wiklander72c55452016-10-27 20:44:35 +0200528 TEEC_MEM_INPUT)))
529 goto out_final;
Pascal Brandc639ac82015-07-02 08:53:34 +0200530
531 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200532out_final:
Pascal Brandc639ac82015-07-02 08:53:34 +0200533 TEEC_FinalizeContext(&cs->context);
534 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200535out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200536 Do_ADBG_EndSubCase(cs->c, "Register_sharedMemory_maxSize");
537}
538
539static void Register_sharedMemory_aboveMaxSize(struct xtest_session *cs)
540{
541 Do_ADBG_BeginSubCase(cs->c, "Register_sharedMemory_aboveMaxSize");
542 {
543 TEEC_Result res;
544 uint32_t size_aboveMax = 0xffffffff;
545 uint8_t val[1];
546 TEEC_SharedMemory shm;
547
Jens Wiklander72c55452016-10-27 20:44:35 +0200548 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
549 TEEC_InitializeContext(_device, &cs->context)))
550 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200551
552 shm.buffer = val;
553 res = RegisterSharedMemory(&cs->context, &shm, size_aboveMax,
554 TEEC_MEM_INPUT);
555
556 ADBG_EXPECT_TRUE(cs->c, res == TEEC_ERROR_OUT_OF_MEMORY ||
557 res == TEEC_SUCCESS);
558
Jens Wiklander72c55452016-10-27 20:44:35 +0200559 if (res == TEEC_SUCCESS)
560 TEEC_ReleaseSharedMemory(&shm);
561 TEEC_FinalizeContext(&cs->context);
Pascal Brandc639ac82015-07-02 08:53:34 +0200562 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200563out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200564 Do_ADBG_EndSubCase(cs->c, "Register_sharedMemory_aboveMaxSize");
565}
566
567static void Register_sharedMemory_belowMaxSize(struct xtest_session *cs)
568{
569 Do_ADBG_BeginSubCase(cs->c, "Register_sharedMemory_belowMaxSize");
570 {
571 uint32_t size_belowMax = TEEC_CONFIG_SHAREDMEM_MAX_SIZE - 1;
572 uint8_t val[size_belowMax];
573 TEEC_SharedMemory shm;
574
Jens Wiklander72c55452016-10-27 20:44:35 +0200575 if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS,
576 TEEC_InitializeContext(_device, &cs->context)))
577 goto out;
Pascal Brandc639ac82015-07-02 08:53:34 +0200578
579 shm.buffer = val;
Jens Wiklander72c55452016-10-27 20:44:35 +0200580 if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200581 RegisterSharedMemory(&cs->context, &shm, size_belowMax,
Jens Wiklander72c55452016-10-27 20:44:35 +0200582 TEEC_MEM_INPUT)))
583 goto out_final;
Pascal Brandc639ac82015-07-02 08:53:34 +0200584
585 TEEC_ReleaseSharedMemory(&shm);
Jens Wiklander72c55452016-10-27 20:44:35 +0200586out_final:
587 TEEC_FinalizeContext(&cs->context);
Pascal Brandc639ac82015-07-02 08:53:34 +0200588 }
Jens Wiklander72c55452016-10-27 20:44:35 +0200589out:
Pascal Brandc639ac82015-07-02 08:53:34 +0200590 Do_ADBG_EndSubCase(cs->c, "Register_sharedMemory_belowMaxSize");
591}
592
593static void xtest_teec_TEE(ADBG_Case_t *c)
594{
595 struct xtest_session connection = { c };
596
597 CloseSession_null(&connection);
598
599 Allocate_In(&connection);
600
601 Allocate_out_of_memory(&connection);
602
603 OpenSession_error_notExistingTA(&connection);
604
605 Allocate_InOut(&connection);
606
607 Register_In(&connection);
608
609 Register_notZeroLength_Out(&connection);
610
611 Register_InOut(&connection);
612
613 Register_zeroLength_Out(&connection);
614
615 Allocate_Out(&connection);
616
617 FinalizeContext_null(&connection);
618
619 InitializeContext_NotExistingTEE(&connection);
620
621 AllocateThenRegister_SameMemory(&connection);
622
623 AllocateSameMemory_twice(&connection);
624
625 RegisterSameMemory_twice(&connection);
626
627 Allocate_sharedMemory_maxSize(&connection);
628
629 Allocate_sharedMemory_belowMaxSize(&connection);
630
631 Allocate_sharedMemory_aboveMaxSize(&connection);
632
633 Register_sharedMemory_maxSize(&connection);
634
635 Register_sharedMemory_aboveMaxSize(&connection);
636
637 Register_sharedMemory_belowMaxSize(&connection);
638}
639
640ADBG_CASE_DEFINE(XTEST_TEE_5006, xtest_teec_TEE,
Jens Wiklander25a57fe2016-12-26 21:46:24 +0100641 "Tests for Global platform TEEC");