virtestmock.c 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "virmock.h"
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
25 26
#include <execinfo.h>
#include <sys/file.h>
27
#include <sys/stat.h>
28 29 30 31
#include <sys/socket.h>
#ifdef HAVE_SYS_UN_H
# include <sys/un.h>
#endif
32 33 34

#include "internal.h"
#include "configmake.h"
35 36 37
#include "virstring.h"
#include "viralloc.h"
#include "virfile.h"
38 39 40 41

static int (*real_open)(const char *path, int flags, ...);
static FILE *(*real_fopen)(const char *path, const char *mode);
static int (*real_access)(const char *path, int mode);
42
static int (*real_connect)(int fd, const struct sockaddr *addr, socklen_t addrlen);
43

44 45 46 47 48
static const char *progname;
const char *output;

#define VIR_FILE_ACCESS_DEFAULT abs_builddir "/test_file_access.txt"

49 50 51 52 53 54 55 56
static void init_syms(void)
{
    if (real_open)
        return;

    VIR_MOCK_REAL_INIT(open);
    VIR_MOCK_REAL_INIT(fopen);
    VIR_MOCK_REAL_INIT(access);
57
    VIR_MOCK_REAL_INIT(connect);
58 59 60
}

static void
M
Michal Privoznik 已提交
61 62
printFile(const char *file,
          const char *func)
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
    FILE *fp;
    const char *testname = getenv("VIR_TEST_MOCK_TESTNAME");

    if (!progname) {
        progname = getenv("VIR_TEST_MOCK_PROGNAME");

        if (!progname)
            return;

        output = getenv("VIR_TEST_FILE_ACCESS_OUTPUT");
        if (!output)
            output = VIR_FILE_ACCESS_DEFAULT;
    }

    if (!(fp = real_fopen(output, "a"))) {
        fprintf(stderr, "Unable to open %s: %s\n", output, strerror(errno));
        abort();
    }

    if (flock(fileno(fp), LOCK_EX) < 0) {
        fprintf(stderr, "Unable to lock %s: %s\n", output, strerror(errno));
        fclose(fp);
        abort();
    }

    /* Now append the following line into the output file:
M
Michal Privoznik 已提交
90
     * $file: $progname: $func: $testname */
91

M
Michal Privoznik 已提交
92
    fprintf(fp, "%s: %s: %s", file, func, progname);
93 94 95 96 97 98 99 100 101
    if (testname)
        fprintf(fp, ": %s", testname);

    fputc('\n', fp);

    flock(fileno(fp), LOCK_UN);
    fclose(fp);
}

M
Michal Privoznik 已提交
102 103 104
#define CHECK_PATH(path) \
    checkPath(path, __FUNCTION__)

105
static void
M
Michal Privoznik 已提交
106 107
checkPath(const char *path,
          const char *func)
108 109 110 111 112 113 114 115 116
{
    char *fullPath = NULL;
    char *relPath = NULL;
    char *crippledPath = NULL;

    if (path[0] != '/' &&
        virAsprintfQuiet(&relPath, "./%s", path) < 0)
        goto error;

117 118 119 120 121
    /* Le sigh. virFileCanonicalizePath() expects @path to exist, otherwise
     * it will return an error. So if we are called over an non-existent
     * file, this could return an error. In that case do our best and hope
     * we will catch possible errors. */
    if ((fullPath = virFileCanonicalizePath(relPath ? relPath : path))) {
122 123 124 125 126 127 128 129 130
        path = fullPath;
    } else {
        /* Yeah, our worst nightmares just became true. Path does
         * not exist. Cut off the last component and retry. */
        if (VIR_STRDUP_QUIET(crippledPath, relPath ? relPath : path) < 0)
            goto error;

        virFileRemoveLastComponent(crippledPath);

131
        if ((fullPath = virFileCanonicalizePath(crippledPath)))
132 133 134 135
            path = fullPath;
    }


136 137
    if (!STRPREFIX(path, abs_top_srcdir) &&
        !STRPREFIX(path, abs_top_builddir)) {
M
Michal Privoznik 已提交
138
        printFile(path, func);
139 140 141 142 143 144 145 146 147 148
    }

    VIR_FREE(crippledPath);
    VIR_FREE(relPath);
    VIR_FREE(fullPath);

    return;
 error:
    fprintf(stderr, "Out of memory\n");
    abort();
149 150 151 152 153 154 155 156 157
}


int open(const char *path, int flags, ...)
{
    int ret;

    init_syms();

M
Michal Privoznik 已提交
158
    CHECK_PATH(path);
159 160 161 162 163

    if (flags & O_CREAT) {
        va_list ap;
        mode_t mode;
        va_start(ap, flags);
164
        mode = (mode_t) va_arg(ap, int);
165 166 167 168 169 170 171 172 173 174 175 176
        va_end(ap);
        ret = real_open(path, flags, mode);
    } else {
        ret = real_open(path, flags);
    }
    return ret;
}

FILE *fopen(const char *path, const char *mode)
{
    init_syms();

M
Michal Privoznik 已提交
177
    CHECK_PATH(path);
178 179 180 181 182 183 184 185 186

    return real_fopen(path, mode);
}


int access(const char *path, int mode)
{
    init_syms();

M
Michal Privoznik 已提交
187
    CHECK_PATH(path);
188 189 190 191 192

    return real_access(path, mode);
}


193 194 195 196 197
#define VIR_MOCK_STAT_HOOK \
    do { \
        init_syms(); \
        checkPath(path, "stat"); \
    } while (0)
198

199
#include "virmockstathelpers.c"
200

201
static int virMockStatRedirect(const char *path ATTRIBUTE_UNUSED, char **newpath ATTRIBUTE_UNUSED)
202
{
203
    return 0;
204
}
205 206 207 208 209 210 211 212 213 214


int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
    init_syms();

#ifdef HAVE_SYS_UN_H
    if (addrlen == sizeof(struct sockaddr_un)) {
        struct sockaddr_un *tmp = (struct sockaddr_un *) addr;
        if (tmp->sun_family == AF_UNIX)
M
Michal Privoznik 已提交
215
            CHECK_PATH(tmp->sun_path);
216 217 218 219 220
    }
#endif

    return real_connect(sockfd, addr, addrlen);
}