Skip to content

Commit 0523db8

Browse files
committed
test: update ta_load test to use memory object
Run unittest path_to_bin 0 cmd [use buffer] Run unittest path_to_bin 1 cmd [use memory object] Signed-off-by: Amirreza Zarrabi <[email protected]>
1 parent 077ef85 commit 0523db8

File tree

4 files changed

+131
-46
lines changed

4 files changed

+131
-46
lines changed

tests/common.c

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,14 @@ test_get_service_object(struct qcomtee_object *client_env_object, uint32_t uid)
150150

151151
/* File stuff. */
152152

153+
/* On error returns "0"; otherwise file size (which could be "0"). */
153154
size_t test_get_file_size(FILE *file)
154155
{
155156
ssize_t size;
156157

157158
if (fseek(file, 0, SEEK_END)) {
158159
PRINT("%s\n", strerror(errno));
159-
return -1;
160+
return 0;
160161
}
161162

162163
size = ftell(file);
@@ -167,55 +168,96 @@ size_t test_get_file_size(FILE *file)
167168

168169
if (fseek(file, 0, SEEK_SET)) {
169170
PRINT("%s\n", strerror(errno));
170-
return -1;
171+
return 0;
171172
}
172173

173174
return size;
174175
}
175176

176-
int test_read_file(const char *filename, char **buffer, size_t *size)
177+
/* On error returns "0"; otherwise file size (which could be "0"). */
178+
size_t test_get_file_size_by_filename(const char *filename)
179+
{
180+
FILE *file;
181+
size_t file_size;
182+
183+
file = fopen(filename, "r");
184+
if (!file) {
185+
PRINT("%s\n", strerror(errno));
186+
return 0;
187+
}
188+
189+
file_size = test_get_file_size(file);
190+
fclose(file);
191+
192+
return file_size;
193+
}
194+
195+
/**
196+
* @brief Read a file.
197+
*
198+
* If buffer is NULL, it allocated the buffer. Caller should free the buffer.
199+
*
200+
* @param filename Path to the file.
201+
* @param buffer Buffer to read the file.
202+
* @param size Size of the buffer.
203+
* @return On success, returns size of the file; Otherwise, returns 0.
204+
*/
205+
size_t test_read_file(const char *filename, char **buffer, size_t size)
177206
{
178-
int ret = -1;
179207
FILE *file;
180208
char *file_buf;
181209
size_t file_size;
182210

183211
file = fopen(filename, "r");
184212
if (!file) {
185213
PRINT("%s\n", strerror(errno));
186-
return -1;
214+
return 0;
187215
}
188216

189217
file_size = test_get_file_size(file);
190218
if (!file_size)
191219
goto out;
192220

193-
file_buf = malloc(file_size);
194-
if (!file_buf) {
195-
PRINT("malloc,\n");
196-
goto out;
221+
if (size > 0) {
222+
/* User provided the buffer; make sure it is large enough. */
223+
if (size < file_size) {
224+
PRINT("no space.\n");
225+
file_size = 0; /* Unable to read. */
226+
227+
goto out;
228+
}
229+
230+
file_buf = *buffer;
231+
} else {
232+
/* User did not provide the buffer; allocate one. */
233+
file_buf = malloc(file_size);
234+
if (!file_buf) {
235+
PRINT("malloc.\n");
236+
file_size = 0; /* Unable to read. */
237+
238+
goto out;
239+
}
197240
}
198241

199242
PRINT("File %s, size: %lu Bytes.\n", filename, file_size);
200243

201244
if (fread(file_buf, 1, file_size, file) != file_size) {
202245
PRINT("fread.\n");
203-
free(file_buf);
246+
if (size == 0)
247+
free(file_buf);
248+
file_size = 0; /* Unable to read. */
204249
goto out;
205250
}
206251

207252
*buffer = file_buf;
208-
*size = file_size;
209-
210-
ret = 0;
211253
out:
212254
fclose(file);
213255

214-
return ret;
256+
return file_size;
215257
}
216258

217-
int test_read_file2(const char *pathname, const char *name, char **buffer,
218-
size_t *size)
259+
size_t test_read_file2(const char *pathname, const char *name, char **buffer,
260+
size_t size)
219261
{
220262
char filename[1024] = { 0 };
221263
/* Hope it fits! */

tests/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static void usage(char *name)
1010
printf("OPTION are:\n"
1111
"\t-d - Run the TZ diagnostics test that prints basic info on TZ heaps.\n"
1212
"\t-l - Load the test TA and send command.\n"
13-
"\t\t%s -l <path to TA binary> <command>\n"
13+
"\t\t%s -l <path to TA binary> <buffer vs. memory object> <command>\n"
1414
"\t-h - Print this help message and exit\n\n",
1515
name);
1616
}
@@ -22,10 +22,10 @@ int main(int argc, char *argv[])
2222
test_print_diagnostics_info();
2323
break;
2424
case 'l':
25-
if (argc != 4)
25+
if (argc != 5)
2626
goto help;
2727

28-
test_load_sample_ta(argv[2], atoi(argv[3]));
28+
test_load_sample_ta(argv[2], atoi(argv[3]), atoi(argv[4]));
2929
break;
3030
help:
3131
case 'h':

tests/ta_load.c

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <time.h>
55
#include "tests_private.h"
66

7+
/* ROOT used in this test. */
8+
static struct qcomtee_object *root;
9+
710
static int test_ta_cmd_0(struct qcomtee_object *ta)
811
{
912
struct qcomtee_param params[2];
@@ -44,29 +47,71 @@ struct ta {
4447
struct qcomtee_object *ta;
4548
};
4649

47-
static struct ta test_load_ta(struct qcomtee_object *service_object,
50+
static struct ta test_load_ta(struct qcomtee_object *service_object, int use_mo,
4851
const char *pathname)
4952
{
5053
struct ta ta = { QCOMTEE_OBJECT_NULL, QCOMTEE_OBJECT_NULL };
5154
struct qcomtee_param params[2];
5255
qcomtee_result_t result;
53-
5456
char *buffer;
5557
size_t size;
56-
/* Read the TA file. */
57-
if (test_read_file2(pathname, TEST_TA, &buffer, &size))
58-
return ta;
59-
60-
/* INIT parameters and invoke object: */
61-
params[0].attr = QCOMTEE_UBUF_INPUT;
62-
params[0].ubuf.addr = buffer;
63-
params[0].ubuf.size = size;
64-
params[1].attr = QCOMTEE_OBJREF_OUTPUT;
65-
/* 0 is IAppLoader_OP_loadFromBuffer. */
66-
if (qcomtee_object_invoke(service_object, 0, params, 2, &result) ||
67-
(result != QCOMTEE_OK)) {
68-
PRINT("qcomtee_object_invoke.\n");
69-
goto failed_out;
58+
int ret;
59+
60+
if (!use_mo) {
61+
/* Read the TA file. */
62+
size = test_read_file2(pathname, TEST_TA, &buffer, 0);
63+
if (!size)
64+
return ta;
65+
66+
/* INIT parameters and invoke object: */
67+
params[0].attr = QCOMTEE_UBUF_INPUT;
68+
params[0].ubuf.addr = buffer;
69+
params[0].ubuf.size = size;
70+
params[1].attr = QCOMTEE_OBJREF_OUTPUT;
71+
/* 0 is IAppLoader_OP_loadFromBuffer. */
72+
ret = qcomtee_object_invoke(service_object, 0, params, 2,
73+
&result);
74+
75+
/* Free buffer allocated in test_read_file2. */
76+
free(buffer);
77+
78+
if (ret || (result != QCOMTEE_OK)) {
79+
PRINT("qcomtee_object_invoke.\n");
80+
return ta;
81+
}
82+
} else {
83+
struct qcomtee_object *mo;
84+
85+
size = test_get_file_size_by_filename(pathname);
86+
if (!size)
87+
return ta;
88+
89+
/* Allocate a memory object for the file contents. */
90+
if (qcomtee_memory_object_alloc(size, root, &mo))
91+
return ta;
92+
93+
buffer = qcomtee_memory_object_addr(mo);
94+
/* Read the TA file. */
95+
size = test_read_file2(pathname, TEST_TA, &buffer,
96+
qcomtee_memory_object_size(mo));
97+
if (!size)
98+
return ta;
99+
100+
/* INIT parameters and invoke object: */
101+
params[0].attr = QCOMTEE_OBJREF_INPUT;
102+
params[0].object = mo;
103+
params[1].attr = QCOMTEE_OBJREF_OUTPUT;
104+
/* 1 is IAppLoader_OP_loadFromRegion. */
105+
ret = qcomtee_object_invoke(service_object, 1, params, 2,
106+
&result);
107+
108+
/* The memoy object donated to QTEE; release it. QTEE releases it's copy. */
109+
qcomtee_memory_object_release(mo);
110+
111+
if (ret || (result != QCOMTEE_OK)) {
112+
PRINT("qcomtee_object_invoke.\n");
113+
return ta;
114+
}
70115
}
71116

72117
ta.ta_controller = params[1].object;
@@ -77,21 +122,18 @@ static struct ta test_load_ta(struct qcomtee_object *service_object,
77122
if (qcomtee_object_invoke(ta.ta_controller, 2, params, 1, &result) ||
78123
(result != QCOMTEE_OK)) {
79124
PRINT("qcomtee_object_invoke.\n");
80-
goto failed_out;
125+
return ta;
81126
}
82127

83128
ta.ta = params[0].object;
84129

85-
failed_out:
86-
free(buffer);
87-
88130
return ta;
89131
}
90132

91-
void test_load_sample_ta(const char *pathname, int cmd)
133+
void test_load_sample_ta(const char *pathname, int use_mo, int cmd)
92134
{
93135
struct ta test_ta;
94-
struct qcomtee_object *root, *client_env_object, *service_object;
136+
struct qcomtee_object *client_env_object, *service_object;
95137

96138
/* Get root + supplicant. */
97139
root = test_get_root();
@@ -114,7 +156,7 @@ void test_load_sample_ta(const char *pathname, int cmd)
114156
}
115157

116158
/* Load the TA. */
117-
test_ta = test_load_ta(service_object, pathname);
159+
test_ta = test_load_ta(service_object, use_mo, pathname);
118160
if (test_ta.ta == QCOMTEE_OBJECT_NULL) {
119161
PRINT("test_load_ta.\n");
120162
goto dec_service_object;

tests/tests_private.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ test_get_service_object(struct qcomtee_object *client_env_object, uint32_t uid);
4040

4141
/* File stuff. */
4242
size_t test_get_file_size(FILE *file);
43-
int test_read_file(const char *filename, char **buffer, size_t *size);
44-
int test_read_file2(const char *pathname, const char *name, char **buffer,
45-
size_t *size);
43+
size_t test_get_file_size_by_filename(const char *filename);
44+
size_t test_read_file(const char *filename, char **buffer, size_t size);
45+
size_t test_read_file2(const char *pathname, const char *name, char **buffer,
46+
size_t size);
4647

4748
#define PRINT(fmt, ...) \
4849
printf("[%s][%d] " fmt, __func__, __LINE__, ##__VA_ARGS__)
@@ -56,6 +57,6 @@ void test_print_diagnostics_info(void);
5657
/* ta_load.c. */
5758
#define TEST_TA "smcinvoke_skeleton_ta64.mbn"
5859

59-
void test_load_sample_ta(const char *pathname, int cmd);
60+
void test_load_sample_ta(const char *pathname, int use_mo, int cmd);
6061

6162
#endif // _TESTS_PRIVATE_H

0 commit comments

Comments
 (0)