From 5b786ce2212ce542e1a60e3328f2ada2f84671da Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 6 Jun 2016 17:07:14 +0200 Subject: [PATCH] Add unit test for tempfile() Signed-off-by: Joachim Nilsson --- tempfile.c | 11 +---------- tempfile.mk | 4 ---- tests/Makefile.am | 2 ++ tests/tempfile.c | 25 +++++++++++++++++++++++++ 4 files changed, 28 insertions(+), 14 deletions(-) delete mode 100644 tempfile.mk create mode 100644 tests/tempfile.c diff --git a/tempfile.c b/tempfile.c index ad94fab..dec3472 100644 --- a/tempfile.c +++ b/tempfile.c @@ -1,6 +1,6 @@ /* A secure tmpfile() replacement. * - * Copyright (c) 2015 Joachim Nilsson + * Copyright (c) 2015-2016 Joachim Nilsson * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -51,17 +51,8 @@ FILE *tempfile(void) #endif } -#ifdef UNITTEST -int main(void) -{ - FILE *fp = tempfile(); system("ls -lrt " - _PATH_TMP " | tail -10"); return fclose(fp); -} -#endif - /** * Local Variables: - * compile-command: "make V=1 -f tempfile.mk" * indent-tabs-mode: t * c-file-style: "linux" * End: diff --git a/tempfile.mk b/tempfile.mk deleted file mode 100644 index 61910a0..0000000 --- a/tempfile.mk +++ /dev/null @@ -1,4 +0,0 @@ -OBJS = -TARGET = tempfile.test clean - -include rules.mk diff --git a/tests/Makefile.am b/tests/Makefile.am index 8f92186..82edab1 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -4,10 +4,12 @@ CLEANFILES = *~ *.trs *.log TESTS = TESTS += makepath TESTS += pidfile +TESTS += tempfile TESTS += tree check_PROGRAMS = $(TESTS) # Ignore warnings about unused result, in e.g. write() CFLAGS = -W -Wall -Wextra -Wno-unused-result +CPPFLAGS = -D_GNU_SOURCE LDADD = -L../ ../libite.la diff --git a/tests/tempfile.c b/tests/tempfile.c new file mode 100644 index 0000000..371121a --- /dev/null +++ b/tests/tempfile.c @@ -0,0 +1,25 @@ +#include /* O_TMPFILE requires -D_GNU_SOURCE */ +#include +#include +#include +#include "check.h" + +#ifndef O_TMPFILE +#warning O_TMPFILE is missing, either too old GLIBC, Linux, or non-Linux system +#endif + +int main(void) +{ + FILE *fp; + + printf("Before tempfile():\n"); + system("ls -lrt " _PATH_TMP " | tail -3"); + + fp = tempfile(); + fail_unless(fp != NULL); + + printf("\nAfter tempfile(), should be same list:\n"); + system("ls -lrt " _PATH_TMP " | tail -3"); + + return fclose(fp); +}