Source-Changes-HG archive

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

[src/trunk]: src Add few basic tests for realpath(3).



details:   https://anonhg.NetBSD.org/src/rev/5ce8de9ebc28
branches:  trunk
changeset: 778124:5ce8de9ebc28
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Sat Mar 17 16:40:14 2012 +0000

description:
Add few basic tests for realpath(3).

diffstat:

 distrib/sets/lists/tests/mi     |    4 +-
 tests/lib/libc/gen/Makefile     |    3 +-
 tests/lib/libc/gen/t_realpath.c |  152 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 157 insertions(+), 2 deletions(-)

diffs (198 lines):

diff -r 6cad6d468698 -r 5ce8de9ebc28 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Sat Mar 17 16:35:02 2012 +0000
+++ b/distrib/sets/lists/tests/mi       Sat Mar 17 16:40:14 2012 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.443 2012/03/17 16:35:02 jruoho Exp $
+# $NetBSD: mi,v 1.444 2012/03/17 16:40:14 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -394,6 +394,7 @@
 ./usr/libdata/debug/usr/tests/lib/libc/gen/t_pause.debug               tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/gen/t_raise.debug               tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/gen/t_randomid.debug            tests-lib-debug         debug,atf
+./usr/libdata/debug/usr/tests/lib/libc/gen/t_realpath.debug            tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/gen/t_rbstress.debug            tests-obsolete          obsolete
 ./usr/libdata/debug/usr/tests/lib/libc/gen/t_setdomainname.debug       tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/gen/t_sethostname.debug         tests-lib-debug         debug,atf
@@ -2029,6 +2030,7 @@
 ./usr/tests/lib/libc/gen/t_pause               tests-lib-tests         atf
 ./usr/tests/lib/libc/gen/t_raise               tests-lib-tests         atf
 ./usr/tests/lib/libc/gen/t_randomid            tests-lib-tests         atf
+./usr/tests/lib/libc/gen/t_realpath            tests-lib-tests         atf
 ./usr/tests/lib/libc/gen/t_rbstress            tests-obsolete          obsolete
 ./usr/tests/lib/libc/gen/t_setdomainname       tests-lib-tests         atf
 ./usr/tests/lib/libc/gen/t_sethostname         tests-lib-tests         atf
diff -r 6cad6d468698 -r 5ce8de9ebc28 tests/lib/libc/gen/Makefile
--- a/tests/lib/libc/gen/Makefile       Sat Mar 17 16:35:02 2012 +0000
+++ b/tests/lib/libc/gen/Makefile       Sat Mar 17 16:40:14 2012 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.34 2012/02/13 21:03:08 martin Exp $
+# $NetBSD: Makefile,v 1.35 2012/03/17 16:40:14 jruoho Exp $
 
 .include <bsd.own.mk>
 
@@ -26,6 +26,7 @@
 TESTS_C+=      t_pause
 TESTS_C+=      t_raise
 TESTS_C+=      t_randomid
+TESTS_C+=      t_realpath
 TESTS_C+=      t_setdomainname
 TESTS_C+=      t_sethostname
 TESTS_C+=      t_siginfo
diff -r 6cad6d468698 -r 5ce8de9ebc28 tests/lib/libc/gen/t_realpath.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/gen/t_realpath.c   Sat Mar 17 16:40:14 2012 +0000
@@ -0,0 +1,152 @@
+/* $NetBSD: t_realpath.c,v 1.1 2012/03/17 16:40:14 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2012 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_realpath.c,v 1.1 2012/03/17 16:40:14 jruoho Exp $");
+
+#include <sys/param.h>
+
+#include <atf-c.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static const struct {
+       const char *path;
+       const char *result;
+} paths[] = {
+
+       { "/",                  "/"             },
+       { "///////",            "/"             },
+       { "",                   NULL            },
+       { "       ",            NULL            },
+       { "/      ",            NULL            },
+       { "      /",            NULL            },
+       { "/etc///",            "/etc"          },
+       { "///////etc",         "/etc"          },
+       { "/a/b/c/d/e",         NULL            },
+       { "    /usr/bin    ",   NULL            },
+       { "\\//////usr//bin",   NULL            },
+       { "//usr//bin//",       "/usr/bin"      },
+       { "//////usr//bin//",   "/usr/bin"      },
+       { "/usr/bin//////////", "/usr/bin"      },
+};
+
+ATF_TC(realpath_basic);
+ATF_TC_HEAD(realpath_basic, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "A basic test of realpath(3)");
+}
+
+ATF_TC_BODY(realpath_basic, tc)
+{
+       char buf[MAXPATHLEN];
+       char *ptr;
+       size_t i;
+
+       for (i = 0; i < __arraycount(paths); i++) {
+
+               (void)memset(buf, '\0', sizeof(buf));
+
+               ptr = realpath(paths[i].path, buf);
+
+               if (ptr == NULL && paths[i].result == NULL)
+                       continue;
+
+               if (ptr == NULL && paths[i].result != NULL)
+                       atf_tc_fail("realpath failed for '%s'", paths[i].path);
+
+               if (strcmp(paths[i].result, buf) != 0)
+                       atf_tc_fail("expected '%s', got '%s'",
+                           paths[i].result, buf);
+       }
+}
+
+ATF_TC(realpath_huge);
+ATF_TC_HEAD(realpath_huge, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test huge path with realpath(3)");
+}
+
+ATF_TC_BODY(realpath_huge, tc)
+{
+       char result[MAXPATHLEN] = { 0 };
+       char buffer[MAXPATHLEN] = { 0 };
+
+       (void)memset(buffer, '/', sizeof(buffer));
+
+       ATF_CHECK(realpath(buffer, result) != NULL);
+       ATF_CHECK(strlen(result) == 1);
+       ATF_CHECK(result[0] == '/');
+}
+
+ATF_TC(realpath_symlink);
+ATF_TC_HEAD(realpath_symlink, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test symbolic link with realpath(3)");
+}
+
+ATF_TC_BODY(realpath_symlink, tc)
+{
+       char path[MAXPATHLEN] = { 0 };
+       char slnk[MAXPATHLEN] = { 0 };
+       char resb[MAXPATHLEN] = { 0 };
+       int fd;
+
+       (void)getcwd(path, sizeof(path));
+       (void)getcwd(slnk, sizeof(slnk));
+
+       (void)strlcat(path, "/realpath", sizeof(path));
+       (void)strlcat(slnk, "/symbolic", sizeof(slnk));
+
+       fd = open(path, O_RDONLY | O_CREAT, 0600);
+
+       ATF_REQUIRE(fd >= 0);
+       ATF_REQUIRE(symlink(path, slnk) == 0);
+       ATF_REQUIRE(close(fd) == 0);
+
+       ATF_REQUIRE(realpath(slnk, resb) != NULL);
+       ATF_REQUIRE(strcmp(resb, path) == 0);
+
+       ATF_REQUIRE(unlink(path) == 0);
+       ATF_REQUIRE(unlink(slnk) == 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+       ATF_TP_ADD_TC(tp, realpath_basic);
+       ATF_TP_ADD_TC(tp, realpath_huge);
+       ATF_TP_ADD_TC(tp, realpath_symlink);
+
+       return atf_no_error();
+}



Home | Main Index | Thread Index | Old Index