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