Skip to content

Commit

Permalink
test: simplify the supplicant and root object
Browse files Browse the repository at this point in the history
- only create single thread.
- move struct supplicant to common.c to be used internally.
- replace test_supplicant_start with test_get_root.

Signed-off-by: Amirreza Zarrabi <[email protected]>
  • Loading branch information
qc-azarrabi committed Jan 22, 2025
1 parent 4056048 commit 927a3ac
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 85 deletions.
77 changes: 35 additions & 42 deletions tests/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <sys/ioctl.h>
#include "tests_private.h"

struct supplicant {
pthread_t thread;
struct qcomtee_object *root;
};

/* op is TEE_IOC_SUPPL_RECV or TEE_IOC_SUPPL_SEND. */
static int tee_call(int fd, int op, struct tee_ioctl_buf_data *buf_data)
{
Expand All @@ -19,83 +24,71 @@ static int tee_call(int fd, int op, struct tee_ioctl_buf_data *buf_data)
return ret;
}

/* arg is the root object. */
/* arg is the instance of supplicant. */
static void *test_supplicant_worker(void *arg)
{
struct supplicant *sup = (struct supplicant *)arg;

while (1) {
pthread_testcancel();
if (qcomtee_object_process_one((struct qcomtee_object *)arg,
tee_call)) {
PRINT("qcomtee_object_process_one.\n");
if (qcomtee_object_process_one(sup->root, tee_call))
break;
}
}

PRINT("qcomtee_object_process_one.\n");

return NULL;
}

/* arg is the instance of supplicant*/
/* arg is the instance of supplicant. */
static void test_supplicant_release(void *arg)
{
struct supplicant *sup = (struct supplicant *)arg;
int i;

/* Here, we are sure there is no QTEE or callback object. In other
* words, there should not be anyone calling qcomtee_object_invoke
* or any request pending from QTEE. We issue the pthread_cancel
* and wait for the thread to get cancelled at pthread_testcancel or
* get cancelled in tee_call asynchronously.
/* Here, we are sure there is no QTEE or callback object.
* We issue the pthread_cancel and wait for the thread to get cancelled
* at pthread_testcancel or in tee_call.
*/

for (i = 0; i < sup->pthreads_num; i++) {
if (!sup->pthreads[i].state)
pthread_cancel(sup->pthreads[i].thread);
if (sup->thread) {
pthread_cancel(sup->thread);
pthread_join(sup->thread, NULL);
}

for (i = 0; i < sup->pthreads_num; i++)
if (!sup->pthreads[i].state)
pthread_join(sup->pthreads[i].thread, NULL);

PRINT("test_supplicant_worker killed.\n");
free(sup);
}

struct supplicant *test_supplicant_start(int pthreads_num)
struct qcomtee_object *test_get_root(void)
{
int i, success = 0;
struct qcomtee_object *root;
struct supplicant *sup;
pthread_t thread;

if (pthreads_num > SUPPLICANT_THREADS)
return NULL;

/* INIT all threads as SUPPLICANT_DEAD. */
sup = calloc(1, sizeof(*sup));
if (!sup)
return NULL;
return QCOMTEE_OBJECT_NULL;

/* Start a fresh namespace. */
sup->root =
qcomtee_object_root_init(DEV_TEE, test_supplicant_release, sup);
if (sup->root == QCOMTEE_OBJECT_NULL)
root = qcomtee_object_root_init(DEV_TEE, test_supplicant_release, sup);
if (root == QCOMTEE_OBJECT_NULL)
goto failed_out;

sup->pthreads_num = pthreads_num;
/* Start supplicant threads. */
for (i = 0; i < sup->pthreads_num; i++) {
if (!pthread_create(&sup->pthreads[i].thread, NULL,
test_supplicant_worker, sup->root)) {
sup->pthreads[i].state = SUPPLICANT_RUNNING;
success = 1;
}
}
sup->root = root;

/* Start a supplicant thread. */
if (pthread_create(&thread, NULL, test_supplicant_worker, sup))
goto failed_out;

sup->thread = thread;

return root;

/* Success, if at least one thread has been started. */
if (success)
return sup;
failed_out:
qcomtee_object_refs_dec(root);
free(sup);

return NULL;
return QCOMTEE_OBJECT_NULL;
}

struct qcomtee_object *test_get_client_env_object(struct qcomtee_object *root)
Expand Down
16 changes: 7 additions & 9 deletions tests/diagnostics.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@ struct diagnostics_heap_info {
void test_print_diagnostics_info(void)
{
struct diagnostics_heap_info heap_info;
struct qcomtee_object *client_env_object, *service_object;
struct qcomtee_object *root, *client_env_object, *service_object;
struct qcomtee_param params[1];
qcomtee_result_t result;

struct supplicant *sup;

/* Start a fresh namespace with supplicant. */
sup = test_supplicant_start(1);
if (!sup) {
PRINT("test_supplicant_start.\n");
/* Get root + supplicant. */
root = test_get_root();
if (root == QCOMTEE_OBJECT_NULL) {
PRINT("test_get_root.\n");
return;
}

client_env_object = test_get_client_env_object(sup->root);
client_env_object = test_get_client_env_object(root);
if (client_env_object == QCOMTEE_OBJECT_NULL) {
PRINT("test_get_client_env_object.\n");
goto dec_root_object;
Expand Down Expand Up @@ -66,5 +64,5 @@ void test_print_diagnostics_info(void)
dec_client_env_object:
qcomtee_object_refs_dec(client_env_object);
dec_root_object:
qcomtee_object_refs_dec(sup->root);
qcomtee_object_refs_dec(root);
}
16 changes: 7 additions & 9 deletions tests/ta_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,16 @@ static struct ta test_load_ta(struct qcomtee_object *service_object,
void test_load_sample_ta(const char *pathname, int cmd)
{
struct ta test_ta;
struct qcomtee_object *client_env_object, *service_object;
struct qcomtee_object *root, *client_env_object, *service_object;

struct supplicant *sup;

/* Start a fresh namespace with supplicant. */
sup = test_supplicant_start(1);
if (!sup) {
PRINT("test_supplicant_start.\n");
/* Get root + supplicant. */
root = test_get_root();
if (root == QCOMTEE_OBJECT_NULL) {
PRINT("test_get_root.\n");
return;
}

client_env_object = test_get_client_env_object(sup->root);
client_env_object = test_get_client_env_object(root);
if (client_env_object == QCOMTEE_OBJECT_NULL) {
PRINT("test_get_client_env_object.\n");
goto dec_root_object;
Expand Down Expand Up @@ -146,5 +144,5 @@ void test_load_sample_ta(const char *pathname, int cmd)
dec_client_env_object:
qcomtee_object_refs_dec(client_env_object);
dec_root_object:
qcomtee_object_refs_dec(sup->root);
qcomtee_object_refs_dec(root);
}
29 changes: 4 additions & 25 deletions tests/tests_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,11 @@
#define DEV_TEE "/dev/tee0"

/**
* @def SUPPLICANT_THREADS
* @brief Maximum number of threads in a supplicant.
*/
#define SUPPLICANT_THREADS 4

#define SUPPLICANT_DEAD 0
#define SUPPLICANT_RUNNING 1
struct supplicant {
int pthreads_num;
struct {
int state;
pthread_t thread;
} pthreads[SUPPLICANT_THREADS];
struct qcomtee_object *root;
};

/**
* @brief Start a supplicant.
*
* Number of threads should be [1 to @ref SUPPLICANT_THREADS].
*
* @param pthreads_num Number of threads.
* @return On success, returns instance of @ref supplicant;
* Otherwise, return NULL.
* @brief Get a root object.
* @return On success, returns the object;
* Otherwise, returns @ref QCOMTEE_OBJECT_NULL.
*/
struct supplicant *test_supplicant_start(int pthreads_num);
struct qcomtee_object *test_get_root(void);

/**
* @brief Get a client environment object.
Expand Down

0 comments on commit 927a3ac

Please sign in to comment.