Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Few unit tests for the fopen(3) family.



details:   https://anonhg.NetBSD.org/src/rev/921d207cd983
branches:  trunk
changeset: 769471:921d207cd983
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Sun Sep 11 07:31:20 2011 +0000

description:
Few unit tests for the fopen(3) family.

diffstat:

 distrib/sets/lists/tests/mi    |    4 +-
 tests/lib/libc/stdio/Makefile  |    3 +-
 tests/lib/libc/stdio/t_fopen.c |  409 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 414 insertions(+), 2 deletions(-)

diffs (truncated from 455 to 300 lines):

diff -r c17312565adc -r 921d207cd983 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Sun Sep 11 06:02:20 2011 +0000
+++ b/distrib/sets/lists/tests/mi       Sun Sep 11 07:31:20 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.379 2011/09/11 05:15:55 jruoho Exp $
+# $NetBSD: mi,v 1.380 2011/09/11 07:31:20 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -438,6 +438,7 @@
 ./usr/libdata/debug/usr/tests/lib/libc/stdio/t_clearerr.debug          tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/stdio/t_fflush.debug            tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/stdio/t_fmemopen.debug          tests-lib-debug         debug,atf
+./usr/libdata/debug/usr/tests/lib/libc/stdio/t_fopen.debug             tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/stdio/t_format.debug            tests-obsolete          obsolete
 ./usr/libdata/debug/usr/tests/lib/libc/stdio/t_popen.debug             tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/stdio/t_printf.debug            tests-lib-debug         debug,atf
@@ -2023,6 +2024,7 @@
 ./usr/tests/lib/libc/stdio/t_clearerr          tests-lib-tests         atf
 ./usr/tests/lib/libc/stdio/t_fflush            tests-lib-tests         atf
 ./usr/tests/lib/libc/stdio/t_fmemopen          tests-lib-tests         atf
+./usr/tests/lib/libc/stdio/t_fopen             tests-lib-tests         atf
 ./usr/tests/lib/libc/stdio/t_format            tests-obsolete          obsolete
 ./usr/tests/lib/libc/stdio/t_popen             tests-lib-tests         atf
 ./usr/tests/lib/libc/stdio/t_printf            tests-lib-tests         atf
diff -r c17312565adc -r 921d207cd983 tests/lib/libc/stdio/Makefile
--- a/tests/lib/libc/stdio/Makefile     Sun Sep 11 06:02:20 2011 +0000
+++ b/tests/lib/libc/stdio/Makefile     Sun Sep 11 07:31:20 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.6 2011/09/11 05:15:55 jruoho Exp $
+# $NetBSD: Makefile,v 1.7 2011/09/11 07:31:20 jruoho Exp $
 
 .include <bsd.own.mk>
 
@@ -7,6 +7,7 @@
 TESTS_C+=      t_clearerr
 TESTS_C+=      t_fflush
 TESTS_C+=      t_fmemopen
+TESTS_C+=      t_fopen
 TESTS_C+=      t_popen
 TESTS_C+=      t_printf
 TESTS_C+=      t_scanf
diff -r c17312565adc -r 921d207cd983 tests/lib/libc/stdio/t_fopen.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/stdio/t_fopen.c    Sun Sep 11 07:31:20 2011 +0000
@@ -0,0 +1,409 @@
+/*     $NetBSD: t_fopen.c,v 1.1 2011/09/11 07:31:20 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_fopen.c,v 1.1 2011/09/11 07:31:20 jruoho Exp $");
+
+#include <atf-c.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <paths.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+static const char *path = "fopen";
+
+ATF_TC_WITH_CLEANUP(fdopen_err);
+ATF_TC_HEAD(fdopen_err, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test errors from fdopen(3)");
+}
+
+ATF_TC_BODY(fdopen_err, tc)
+{
+       int fd;
+
+       fd = open(path, O_RDONLY | O_CREAT);
+       ATF_REQUIRE(fd >= 0);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "w") == NULL);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "a") == NULL);
+
+       ATF_REQUIRE(close(fd) == 0);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EBADF, fdopen(fd, "r") == NULL);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EBADF, fdopen(-1, "w+") == NULL);
+
+       (void)unlink(path);
+}
+
+ATF_TC_CLEANUP(fdopen_err, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fdopen_seek);
+ATF_TC_HEAD(fdopen_seek, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test stream position with fdopen(3)");
+}
+
+ATF_TC_BODY(fdopen_seek, tc)
+{
+       FILE *f;
+       int fd;
+
+       /*
+        * Verify that the file position associated
+        * with the stream corresponds with the offset
+        * set earlier for the file descriptor.
+        */
+       fd = open(path, O_RDWR | O_CREAT);
+
+       ATF_REQUIRE(fd >= 0);
+       ATF_REQUIRE(write(fd, "garbage", 7) == 7);
+       ATF_REQUIRE(lseek(fd, 3, SEEK_SET) == 3);
+
+       f = fdopen(fd, "r+");
+
+       ATF_REQUIRE(f != NULL);
+       ATF_REQUIRE(ftell(f) == 3);
+       ATF_REQUIRE(fclose(f) == 0);
+
+       (void)fclose(f);
+       (void)close(fd);
+       (void)unlink(path);
+}
+
+ATF_TC_CLEANUP(fdopen_seek, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fopen_err);
+ATF_TC_HEAD(fopen_err, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test errors from fopen(3)");
+}
+
+ATF_TC_BODY(fopen_err, tc)
+{
+       static const char *mode[] = {
+               "x", "xr", "xr", "+r+", "R", "W+", " aXX", "Xr", " r+", "" };
+
+       char buf[PATH_MAX + 1];
+       size_t i;
+       FILE *f;
+
+       f = fopen(path, "w+");
+
+       ATF_REQUIRE(f != NULL);
+       ATF_REQUIRE(fclose(f) == 0);
+
+       /*
+        * Note that also "invalid" characters
+        * may follow the mode-string whenever
+        * the first character is valid.
+        */
+       for (i = 0; i < __arraycount(mode); i++) {
+
+               errno = 0;
+               f = fopen(path, mode[i]);
+
+               if (f == NULL && errno == EINVAL)
+                       continue;
+
+               if (f != NULL)
+                       (void)fclose(f);
+
+               atf_tc_fail_nonfatal("opened file as '%s'",  mode[i]);
+       }
+
+       (void)unlink(path);
+       (void)memset(buf, 'x', sizeof(buf));
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EISDIR, fopen("/usr/bin", "w") == NULL);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENOENT, fopen("/a/b/c/d/e/f", "r") == NULL);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENAMETOOLONG, fopen(buf, "r+") == NULL);
+}
+
+ATF_TC_CLEANUP(fopen_err, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fopen_append);
+ATF_TC_HEAD(fopen_append, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test that append-mode works");
+}
+
+ATF_TC_BODY(fopen_append, tc)
+{
+       char buf[15];
+       FILE *f;
+
+       (void)memset(buf, 'x', sizeof(buf));
+
+       f = fopen(path, "w+");
+
+       ATF_REQUIRE(f != NULL);
+       ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
+       ATF_REQUIRE(fclose(f) == 0);
+
+       f = fopen(path, "a");
+       ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
+       ATF_REQUIRE(fclose(f) == 0);
+
+       f = fopen(path, "r");
+       ATF_REQUIRE(fread(buf, 1, sizeof(buf), f) == 14);
+       ATF_REQUIRE(strncmp(buf, "garbagegarbage", 14) == 0);
+
+       ATF_REQUIRE(fclose(f) == 0);
+       ATF_REQUIRE(unlink(path) == 0);
+}
+
+ATF_TC_CLEANUP(fopen_append, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fopen_mode);
+ATF_TC_HEAD(fopen_mode, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test fopen(3) modes");
+}
+
+ATF_TC_BODY(fopen_mode, tc)
+{
+       size_t i;
+       FILE *f;
+
+       static const char *mode[] = {
+               "r", "r+", "w", "w+", "a", "a+",
+               "rb", "r+b", "wb", "w+b", "ab", "a+b"
+               "re", "r+e", "we", "w+e", "ae", "a+e"
+               "rf", "r+f", "wf", "w+f", "af", "a+f"
+       };
+
+       f = fopen(path, "w+");
+
+       ATF_REQUIRE(f != NULL);
+       ATF_REQUIRE(fclose(f) == 0);
+
+       /*
+        * Verify that various modes work.
+        */
+       for (i = 0; i < __arraycount(mode); i++) {
+
+               f = fopen(path, mode[i]);
+
+               if (f != NULL) {
+                       ATF_REQUIRE(fclose(f) == 0);
+                       continue;
+               }
+
+               atf_tc_fail_nonfatal("failed to open file as %s",  mode[i]);
+       }
+
+       (void)unlink(path);
+}
+
+ATF_TC_CLEANUP(fopen_mode, tc)
+{
+       (void)unlink(path);



Home | Main Index | Thread Index | Old Index