| 1 |
/* |
| 2 |
* This file is part of the flashrom project. |
| 3 |
* |
| 4 |
* Copyright 2022 Google LLC |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; version 2 of the License. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "io_mock.h" |
| 17 |
#include "wraps.h" |
| 18 |
|
| 19 |
#include "io_real.h" |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
static int io_real_open(void *state, const char *pathname, int flags, mode_t mode) |
| 23 |
{ |
| 24 |
LOG_ME; |
| 25 |
return __real_open(pathname, flags, mode); |
| 26 |
} |
| 27 |
|
| 28 |
static FILE *io_real_fopen(void *state, const char *pathname, const char *mode) |
| 29 |
{ |
| 30 |
LOG_ME; |
| 31 |
return __real_fopen(pathname, mode); |
| 32 |
} |
| 33 |
|
| 34 |
static FILE *io_real_fdopen(void *state, int fd, const char *mode) |
| 35 |
{ |
| 36 |
LOG_ME; |
| 37 |
return __real_fdopen(fd, mode); |
| 38 |
} |
| 39 |
|
| 40 |
static size_t io_real_fwrite(void *state, const void *ptr, size_t size, size_t nmemb, FILE *fp) |
| 41 |
{ |
| 42 |
return __real_fwrite(ptr, size, nmemb, fp); |
| 43 |
} |
| 44 |
|
| 45 |
static int io_real_fclose(void *state, FILE *fp) |
| 46 |
{ |
| 47 |
LOG_ME; |
| 48 |
return __real_fclose(fp); |
| 49 |
} |
| 50 |
|
| 51 |
/* Mock ios that defer to the real io functions. |
| 52 |
* These exist so that code coverage can print to real files on disk. |
| 53 |
*/ |
| 54 |
static const struct io_mock real_io = { |
| 55 |
.iom_open = io_real_open, |
| 56 |
.iom_fopen = io_real_fopen, |
| 57 |
.iom_fwrite = io_real_fwrite, |
| 58 |
.iom_fdopen = io_real_fdopen, |
| 59 |
.iom_fclose = io_real_fclose, |
| 60 |
}; |
| 61 |
|
| 62 |
/* Return 0 if string ends with suffix. */ |
| 63 |
static int check_suffix(const char *string, const char *suffix) |
| 64 |
{ |
| 65 |
int len_l = strlen(string); |
| 66 |
int len_r = strlen(suffix); |
| 67 |
if (len_l > len_r) |
| 68 |
return strcmp(string + len_l - len_r, suffix); |
| 69 |
return 1; |
| 70 |
} |
| 71 |
|
| 72 |
void maybe_unmock_io(const char *pathname) |
| 73 |
{ |
| 74 |
const char *gcov_suffix = ".gcda"; |
| 75 |
const char *llvm_cov_suffix = ".profraw"; |
| 76 |
if (!check_suffix(pathname, gcov_suffix) || !check_suffix(pathname, llvm_cov_suffix)) |
| 77 |
io_mock_register(&real_io); |
| 78 |
} |