Source-Changes-HG archive

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

[src/trunk]: src/tests/lib/libc/stdio Add 2 new tests in t_fopen



details:   https://anonhg.NetBSD.org/src/rev/bcb04f83cd3b
branches:  trunk
changeset: 448599:bcb04f83cd3b
user:      kamil <kamil%NetBSD.org@localhost>
date:      Tue Feb 05 17:30:19 2019 +0000

description:
Add 2 new tests in t_fopen

Added:
 - fopen_nullptr (without COMPAT_10)
 - fopen_nullptr_compat10 (with COMPAT_10)

PR kern/53948

Reviewed by <mgorny>

diffstat:

 tests/lib/libc/stdio/t_fopen.c |  118 ++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 116 insertions(+), 2 deletions(-)

diffs (153 lines):

diff -r 451106e8a6a2 -r bcb04f83cd3b tests/lib/libc/stdio/t_fopen.c
--- a/tests/lib/libc/stdio/t_fopen.c    Tue Feb 05 17:13:37 2019 +0000
+++ b/tests/lib/libc/stdio/t_fopen.c    Tue Feb 05 17:30:19 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_fopen.c,v 1.5 2017/11/06 23:06:55 kre Exp $ */
+/*     $NetBSD: t_fopen.c,v 1.6 2019/02/05 17:30:19 kamil Exp $ */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -29,14 +29,18 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_fopen.c,v 1.5 2017/11/06 23:06:55 kre Exp $");
+__RCSID("$NetBSD: t_fopen.c,v 1.6 2019/02/05 17:30:19 kamil Exp $");
 
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/module.h>
 #include <atf-c.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
 #include <paths.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -287,6 +291,114 @@
        (void)unlink(path);
 }
 
+static void
+check_kernel_modular(void)
+{
+       int err;
+
+       err = modctl(MODCTL_EXISTS, 0);
+       if (err == 0) return;
+       if (errno == ENOSYS)
+               atf_tc_skip("Kernel does not have 'options MODULAR'.");
+       if (errno == EPERM)
+               return; /* Module loading can be administratively forbidden */
+       ATF_REQUIRE_EQ_MSG(errno, 0, "unexpected error %d from "
+           "modctl(MODCTL_EXISTS, 0)", errno);
+}
+
+static bool
+is_module_present(const char *name)
+{
+       bool found;
+       size_t len;
+       int count;
+       struct iovec iov;
+       modstat_t *ms;
+
+       for (len = 8192; ;) {
+               iov.iov_base = malloc(len);
+               iov.iov_len = len;
+
+               errno = 0;
+
+               if (modctl(MODCTL_STAT, &iov) != 0) {
+                       fprintf(stderr, "modctl(MODCTL_STAT) failed: %s\n",
+                           strerror(errno));
+                       atf_tc_fail("Failed to query module status");
+               }
+               if (len >= iov.iov_len)
+                       break;
+               free(iov.iov_base);
+               len = iov.iov_len;
+       }
+
+       found = false;
+       count = *(int *)iov.iov_base;
+       ms = (modstat_t *)((char *)iov.iov_base + sizeof(int));
+       while (count > 0) {
+               if (strcmp(ms->ms_name, name) == 0) {
+                       found = true;
+                       break;
+               }
+               ms++;
+               count--;
+       }
+
+       free(iov.iov_base);
+
+       return found;
+}
+
+#define COMPAT10_MODNAME "compat_10"
+
+ATF_TC(fopen_nullptr);
+ATF_TC_HEAD(fopen_nullptr, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test fopen(3) with NULL path (without "
+           COMPAT10_MODNAME ")");
+}
+
+ATF_TC_BODY(fopen_nullptr, tc)
+{
+       bool compat10;
+
+       check_kernel_modular();
+       compat10 = is_module_present(COMPAT10_MODNAME);
+
+       if (compat10)
+               atf_tc_skip("Kernel does have the " COMPAT10_MODNAME
+                   " module loaded into the kernel");
+
+       /* NULL shall trigger error */
+       ATF_REQUIRE_ERRNO(EFAULT, fopen(NULL, "r") == NULL);
+}
+
+ATF_TC(fopen_nullptr_compat10);
+ATF_TC_HEAD(fopen_nullptr_compat10, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test fopen(3) with NULL path (with "
+           COMPAT10_MODNAME ")");
+}
+
+ATF_TC_BODY(fopen_nullptr_compat10, tc)
+{
+       FILE *fp;
+       bool compat10;
+
+       check_kernel_modular();
+       compat10 = is_module_present(COMPAT10_MODNAME);
+
+       if (!compat10)
+               atf_tc_skip("Kernel does not have the " COMPAT10_MODNAME
+                   " module loaded into the kernel");
+
+       /* NULL is translated to "." and shall success */
+       fp = fopen(NULL, "r");
+
+       ATF_REQUIRE(fp != NULL);
+       ATF_REQUIRE(fclose(fp) == 0);
+}
+
 ATF_TC(fopen_perm);
 ATF_TC_HEAD(fopen_perm, tc)
 {
@@ -482,6 +594,8 @@
        ATF_TP_ADD_TC(tp, fopen_append);
        ATF_TP_ADD_TC(tp, fopen_err);
        ATF_TP_ADD_TC(tp, fopen_mode);
+       ATF_TP_ADD_TC(tp, fopen_nullptr);
+       ATF_TP_ADD_TC(tp, fopen_nullptr_compat10);
        ATF_TP_ADD_TC(tp, fopen_perm);
        ATF_TP_ADD_TC(tp, fopen_regular);
        ATF_TP_ADD_TC(tp, fopen_symlink);



Home | Main Index | Thread Index | Old Index