This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project gfxprim.git.
The branch, master has been updated via 929e7070a0d18580bbf3eeb17b02803099acbb5f (commit) from 1883d7d07231829d270253b3f97fc8562047a12c (commit)
Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below.
- Log ----------------------------------------------------------------- http://repo.or.cz/w/gfxprim.git/commit/929e7070a0d18580bbf3eeb17b02803099acb...
commit 929e7070a0d18580bbf3eeb17b02803099acbb5f Author: Cyril Hrubis metan@ucw.cz Date: Fri Aug 24 18:51:07 2012 +0200
tests: Add code to fail fclose() and fopen() libcalls.
diff --git a/tests/framework/Makefile b/tests/framework/Makefile index 529919e..9482807 100644 --- a/tests/framework/Makefile +++ b/tests/framework/Makefile @@ -14,7 +14,7 @@ APPS=test
ALL+=libtst_preload.so
-libtst_preload.so: tst_preload.o tst_alloc_barriers.o +libtst_preload.so: tst_preload.o tst_alloc_barriers.o tst_preload_FILE.o gcc -Wl,-soname -Wl,tst_preload.so --shared -ldl -fPIC $^ -o $@
CLEAN+=libtst_preload.so diff --git a/tests/framework/test.c b/tests/framework/test.c index 0c908f5..f10d404 100644 --- a/tests/framework/test.c +++ b/tests/framework/test.c @@ -23,9 +23,12 @@ #include <stdlib.h> #include <unistd.h> #include <stdio.h> +#include <errno.h> +#include <string.h>
#include "tst_test.h" #include "tst_alloc_barriers.h" +#include "tst_preload_FILE.h"
int success_fn(void) { @@ -135,6 +138,54 @@ int barrier_allocation(void) return TST_SUCCESS; }
+int fail_FILE(void) +{ + struct tst_fail_FILE failures[] = { + {.path = "test_fail_fopen", .call = TST_FAIL_FOPEN, .err = EPERM}, + {.path = "test_fail_fclose", .call = TST_FAIL_FCLOSE, .err = ENOSPC}, + {.path = NULL} + }; + + tst_fail_FILE_register(failures); + + int fail = 0; + FILE *f; + + f = fopen("test_fail_fclose", "w"); + + if (f == NULL) { + tst_report(0, "Failed to open 'test_fail_fclose' for writing: %s", + strerror(errno)); + fail = 1; + } + + tst_report(0, "Correctly opened 'test_fail_fclose'"); + + int ret = fclose(f); + + if (ret == 0 || errno != ENOSPC) { + tst_report(0, "Failed to fail to close 'test_fail_fclose'"); + fail = 1; + } + + tst_report(0, "Correctly failed to close 'test_fail_fclose'"); + + f = fopen("test_fail_fopen", "w"); + + if (f != NULL && errno != EPERM) { + tst_report(0, "Failed to fail to open 'test_fail_fopen'"); + fclose(f); + fail = 1; + } + + tst_report(0, "Correctly failed to open 'test_fail_fopen'"); + + if (fail) + return TST_FAILED; + + return TST_SUCCESS; +} + const struct tst_suite suite = { .suite_name = "Testing Framework Example", .tests = { @@ -148,6 +199,7 @@ const struct tst_suite suite = { {.name = "Mem Ok test", .tst_fn = malloc_ok_fn, .flags = TST_MALLOC_CHECK}, {.name = "Double free()", .tst_fn = double_free}, {.name = "Barrier allocation", .tst_fn = barrier_allocation}, + {.name = "Failed FILE", .tst_fn = fail_FILE, .flags = TST_TMPDIR}, {.name = NULL}, } }; diff --git a/tests/framework/tst_preload_FILE.c b/tests/framework/tst_preload_FILE.c new file mode 100644 index 0000000..2365b6b --- /dev/null +++ b/tests/framework/tst_preload_FILE.c @@ -0,0 +1,129 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim 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. * + * * + * Gfxprim 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 gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2012 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#define _GNU_SOURCE +#include <stdio.h> +#include <dlfcn.h> +#include <errno.h> +#include <string.h> + +#include "tst_test.h" +#include "tst_preload_FILE.h" + +struct tst_fail_FILE *failures = NULL; + +void tst_fail_FILE_register(struct tst_fail_FILE *self) +{ + failures = self; +} + +static struct tst_fail_FILE *failure_by_path(const char *path, + enum tst_file_call call) +{ + unsigned int i; + + if (failures == NULL) + return NULL; + + for (i = 0; failures[i].path != NULL; i++) + if (failures[i].call == call && + !strcmp(path, failures[i].path)) + return &failures[i]; + + return NULL; +} + +void failures_init_FILE(const char *path, FILE *f) +{ + unsigned int i; + + if (failures == NULL) + return; + + //TODO: warn on f not NULL + for (i = 0; failures[i].path != NULL; i++) + if (!strcmp(path, failures[i].path)) + failures[i].f = f; +} + +static struct tst_fail_FILE *failure_by_FILE(FILE *f, enum tst_file_call call) +{ + unsigned int i; + + if (failures == NULL) + return NULL; + + for (i = 0; failures[i].path != NULL; i++) + if (failures[i].call == call && f == failures[i].f) + return &failures[i]; + + return NULL; +} + +FILE *fopen(const char *path, const char *mode) +{ + static FILE *(*real_fopen)(const char *, const char *); + + if (!real_fopen) + real_fopen = dlsym(RTLD_NEXT, "fopen"); + + struct tst_fail_FILE *failure = failure_by_path(path, TST_FAIL_FOPEN); + + if (failure) { + if (failure->err) + errno = failure->err; + + return NULL; + } + + + FILE *f = real_fopen(path, mode); + + failures_init_FILE(path, f); + + return f; +} + +int fclose(FILE *fp) +{ + static int (*real_fclose)(FILE *); + + if (!real_fclose) + real_fclose = dlsym(RTLD_NEXT, "fclose"); + + struct tst_fail_FILE *failure = failure_by_FILE(fp, TST_FAIL_FCLOSE); + + /* + * We close the file here correctly, we can because when fclose() has + * failed any further access results in undefined behavior. + */ + if (failure) { + real_fclose(fp); + + if (failure->err) + errno = failure->err; + + return EOF; + } + + return real_fclose(fp); +} diff --git a/tests/framework/tst_preload_FILE.h b/tests/framework/tst_preload_FILE.h new file mode 100644 index 0000000..0c1da0a --- /dev/null +++ b/tests/framework/tst_preload_FILE.h @@ -0,0 +1,60 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim 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. * + * * + * Gfxprim 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 gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2012 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#ifndef TST_PRELOAD_FILE_H +#define TST_PRELOAD_FILE_H + +enum tst_file_call { + TST_FAIL_FOPEN = 0x01, + TST_FAIL_FCLOSE = 0x02, +}; + +/* + * Describes which call on which file and how should fail. + */ +struct tst_fail_FILE { + /* + * File path to be failed, this is matched exactly against the path + * passed to fopen(). + * + * TODO: should we support regexps? + */ + const char *path; + + /* pointer to opened file, don't touch */ + FILE *f; + + /* if not zero, errno is set to this */ + int err; + + /* + * Which call should be failed. + */ + enum tst_file_call call; +}; + +/* + * Registers NULL-terminated FILE fail table, NULL == no table. + */ +void tst_fail_FILE_register(struct tst_fail_FILE *self); + +#endif /* TST_PRELOAD_FILE_H */
-----------------------------------------------------------------------
Summary of changes: tests/framework/Makefile | 2 +- tests/framework/test.c | 52 ++++++++ tests/framework/tst_preload_FILE.c | 129 ++++++++++++++++++++ .../framework/tst_preload_FILE.h | 42 ++++--- 4 files changed, 209 insertions(+), 16 deletions(-) create mode 100644 tests/framework/tst_preload_FILE.c copy include/backends/GP_BackendVirtual.h => tests/framework/tst_preload_FILE.h (71%)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos@gmail.com if you want to unsubscribe, or site admin admin@repo.or.cz if you receive no reply.