-
Notifications
You must be signed in to change notification settings - Fork 346
/
fapi-io.c
381 lines (315 loc) · 9.19 KB
/
fapi-io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* SPDX-License-Identifier: BSD-2-Clause */
/*******************************************************************************
* Copyright 2018, Fraunhofer SIT sponsored by Infineon Technologies AG
* All rights reserved.
******************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdarg.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <json-c/json_util.h>
#include <json-c/json_tokener.h>
#include <setjmp.h>
#include <cmocka.h>
#include <errno.h>
#include "ifapi_io.h"
#include "util/aux_util.h"
#define LOGMODULE tests
#include "util/log.h"
#define EXIT_SKIP 77
/*
* The unit tests will simulate error codes which can be returned by the
* system calls for file system IO.
*/
/* Global variables to trigger wrapper functions. If set to false the
* real function will be called. */
bool wrap_fcntl_test = false;
bool wrap_malloc_test = false;
bool wrap_read_test = false;
char _mock_stream; /**< stream will be used to activate wrapper.*/
#define MOCK_STREAM ((FILE *)(&_mock_stream))
/*
* Wrapper functions for file system io.
*/
int
__real_stat(const char *pathname, struct stat *statbuf, ...);
int
__wrap_stat(const char *pathname, struct stat *statbuf, ...)
{
if (strcmp(pathname, "tss_unit_dummyf")) {
return __real_stat(pathname, statbuf);
}
statbuf->st_mode = R_OK;
return 0;
}
FILE *
__real_fopen(const char *pathname, const char* mode, ...);
FILE *
__wrap_fopen(const char *pathname, const char* mode, ...)
{
if (strcmp(pathname, "tss_unit_dummyf")) {
return __real_fopen(pathname, mode);
}
return mock_ptr_type(FILE*);
}
int
__real_fclose(FILE *stream, ...);
int
__wrap_fclose(FILE *stream, ...)
{
if (stream != MOCK_STREAM) {
return __real_fclose(stream);
}
return mock_type(int);
}
int
__real_fseek(FILE *stream, long offset, int whence, ...);
int
__wrap_fseek(FILE *stream, long offset, int whence, ...)
{
if (stream != MOCK_STREAM) {
return __real_fseek(stream, offset, whence);
}
return mock_type(int);
}
long
__real_ftell(FILE *stream, ...);
long
__wrap_ftell(FILE *stream, ...)
{
if (stream != MOCK_STREAM) {
return __real_ftell(stream);
}
return mock_type(int);
}
int
__real_fcntl(int fd, int cmd, ...);
int
__wrap_fcntl(int fd, int cmd, ...)
{
if (wrap_fcntl_test)
return mock_type(int);
else
return __real_fcntl(fd, cmd);
}
void *
__real_malloc(size_t size, ...);
void *
__wrap_malloc(size_t size, ...)
{
if (wrap_malloc_test) {
return mock_ptr_type (void*);
} else {
return __real_malloc(size);
}
}
int
__real_fileno(FILE *stream, ...);
int
__wrap_fileno(FILE *stream, ...)
{
if (stream != MOCK_STREAM) {
return __real_fileno(stream);
}
return 1;
}
ssize_t
__real_read(int fd, void *buf, size_t count);
ssize_t
__wrap_read(int fd, void *buf, size_t count, ...) {
if (!wrap_read_test) {
return __real_read(fd, buf, count);
}
return mock_type(ssize_t);
}
/*
* The return codes for error cases which can be occur in the
* function: ifapi_io_read_async will be checked.
*/
static void
check_io_read_async(void **state) {
IFAPI_IO io;
TSS2_RC r;
char *dmy_buf = "dummy";
memset(&io, 0, sizeof(IFAPI_IO));
io.char_rbuffer = &dmy_buf[0];
r = ifapi_io_read_async(&io, "tss_unit_dummyf");
assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
io.char_rbuffer = NULL;
will_return(__wrap_fopen, NULL);
r = ifapi_io_read_async(&io, "tss_unit_dummyf");
assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
will_return(__wrap_fopen, NULL);
io.char_buffer = "dummy";
r = ifapi_io_read_async(&io, "tss_unit_dummyf");
assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
wrap_fcntl_test = true;
will_return(__wrap_fopen, MOCK_STREAM);
will_return(__wrap_fcntl, -1);
will_return_always(__wrap_fclose, 0);
errno = EAGAIN;
io.char_buffer = NULL;
r = ifapi_io_read_async(&io, "tss_unit_dummyf");
assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
will_return(__wrap_fopen, MOCK_STREAM);
will_return(__wrap_fopen, MOCK_STREAM);
will_return(__wrap_fcntl, 0);
will_return(__wrap_fseek, 0);
will_return(__wrap_ftell, 1);
will_return(__wrap_malloc, NULL);
errno = 0;
io.char_buffer = NULL;
wrap_malloc_test = true;
r = ifapi_io_read_async(&io, "tss_unit_dummyf");
assert_int_equal(r, TSS2_FAPI_RC_MEMORY);
wrap_malloc_test = false;
will_return(__wrap_fopen, MOCK_STREAM);
will_return(__wrap_fopen, MOCK_STREAM);
will_return(__wrap_fcntl, 0);
will_return(__wrap_fseek, 0);
will_return(__wrap_ftell, 1);
will_return(__wrap_fcntl, 0);
will_return(__wrap_fcntl, -1);
errno = 0;
io.char_buffer = NULL;
r = ifapi_io_read_async(&io, "tss_unit_dummyf");
assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
wrap_fcntl_test = false;
}
/*
* The return codes for error cases which can be occur in the
* function: ifapi_io_read_finish will be checked.
*/
static void
check_io_read_finish(void **state) {
IFAPI_IO io;
TSS2_RC r;
uint8_t *buffer[10];
char io_char_buffer[10];
size_t count = 10;
memset(&io, 0, sizeof(IFAPI_IO));
wrap_read_test = true;
will_return(__wrap_read, -1);
// will_return_always(__wrap_fileno, 1);
will_return_always(__wrap_fclose, 0);
io.char_buffer = &io_char_buffer[0];
io.buffer_length = 10;
io.stream = MOCK_STREAM;
errno = EAGAIN;
r = ifapi_io_read_finish(&io, &buffer[0], &count);
assert_int_equal(r, TSS2_FAPI_RC_TRY_AGAIN);
will_return(__wrap_read, -1);
errno = 0;
r = ifapi_io_read_finish(&io, &buffer[0], &count);
assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
will_return(__wrap_read, 8);
errno = 0;
r = ifapi_io_read_finish(&io, &buffer[0], &count);
assert_int_equal(r, TSS2_FAPI_RC_TRY_AGAIN);
will_return(__wrap_read, 10);
errno = 0;
r = ifapi_io_read_finish(&io, &buffer[0], &count);
assert_int_equal(r, TSS2_RC_SUCCESS);
will_return(__wrap_read, 10);
errno = 0;
r = ifapi_io_read_finish(&io, NULL, &count);
assert_int_equal(r, TSS2_RC_SUCCESS);
wrap_read_test = false;
}
/*
* The return codes for error cases which can be occur in the
* function: ifapi_io_write_async will be checked.
*/
static void
check_io_write_async(void **state) {
IFAPI_IO io;
TSS2_RC r;
uint8_t buffer[5] = { 1, 2, 3, 4, 5 };
char *char_buffer = "dummy";
will_return_always(__wrap_fclose, 0);
// will_return_always(__wrap_fileno, 1);
memset(&io, 0, sizeof(IFAPI_IO));
io.char_rbuffer = &char_buffer[0];
r = ifapi_io_write_async(&io, "tss_unit_dummyf", NULL, 0);
assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
io.char_rbuffer = NULL;
will_return(__wrap_malloc, NULL);
wrap_malloc_test = true;
r = ifapi_io_write_async(&io, "tss_unit_dummyf", NULL, 0);
assert_int_equal(r, TSS2_FAPI_RC_MEMORY);
wrap_malloc_test = false;
will_return(__wrap_fopen, NULL);
r = ifapi_io_write_async(&io, "tss_unit_dummyf", &buffer[0], 5);
assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
wrap_fcntl_test = true;
will_return(__wrap_fopen, MOCK_STREAM);
will_return(__wrap_fcntl, -1);
errno = EAGAIN;
r = ifapi_io_write_async(&io, "tss_unit_dummyf", &buffer[0], 5);
assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
io.char_rbuffer = NULL;
will_return(__wrap_fopen, MOCK_STREAM);
will_return(__wrap_fcntl, 0);
will_return(__wrap_fcntl, 0);
will_return(__wrap_fcntl, -1);
errno = 0;
r = ifapi_io_write_async(&io, "tss_unit_dummyf", &buffer[0], 5);
assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
wrap_fcntl_test = false;
}
bool wrap_write_test = false;
ssize_t
__real_write(int fd, void *buf, size_t count);
ssize_t
__wrap_write(int fd, void *buf, size_t count, ...) {
if (!wrap_write_test) {
return __real_read(fd, buf, count);
}
return mock_type(ssize_t);
}
/*
* The return codes for error cases which can be occur in the
* function: ifapi_io_write_finish will be checked.
*/
static void
check_io_write_finish(void **state) {
IFAPI_IO io;
TSS2_RC r;
//uint8_t buffer[5] = { 1, 2, 3, 4, 5 };
memset(&io, 0, sizeof(IFAPI_IO));
// will_return_always(__wrap_fileno, 1);
will_return_always(__wrap_fclose, 0);
wrap_write_test = true;
io.stream = MOCK_STREAM;
will_return(__wrap_write, -1);
errno = EAGAIN;
r = ifapi_io_write_finish(&io);
assert_int_equal(r, TSS2_FAPI_RC_TRY_AGAIN);
errno = 0;
will_return(__wrap_write, -1);
r = ifapi_io_write_finish(&io);
assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
wrap_write_test = false;
}
int
main(int argc, char *argv[])
{
#if _FILE_OFFSET_BITS == 64
// Would produce cmocka error
LOG_WARNING("_FILE_OFFSET == 64 would produce cmocka errors.");
return EXIT_SKIP;
#endif
const struct CMUnitTest tests[] = {
cmocka_unit_test(check_io_read_async),
cmocka_unit_test(check_io_read_finish),
cmocka_unit_test(check_io_write_async),
cmocka_unit_test(check_io_write_finish),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}