1 /* $NetBSD: t_realpath.c,v 1.2 2012/03/27 07:54:58 njoly Exp $ */
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_realpath.c,v 1.2 2012/03/27 07:54:58 njoly Exp $");
34 #include <sys/param.h>
54 { "/etc///", "/etc" },
55 { "///////etc", "/etc" },
56 { "/a/b/c/d/e", NULL
},
57 { " /usr/bin ", NULL
},
58 { "\\//////usr//bin", NULL
},
59 { "//usr//bin//", "/usr/bin" },
60 { "//////usr//bin//", "/usr/bin" },
61 { "/usr/bin//////////", "/usr/bin" },
64 ATF_TC(realpath_basic
);
65 ATF_TC_HEAD(realpath_basic
, tc
)
67 atf_tc_set_md_var(tc
, "descr", "A basic test of realpath(3)");
70 ATF_TC_BODY(realpath_basic
, tc
)
76 for (i
= 0; i
< __arraycount(paths
); i
++) {
78 (void)memset(buf
, '\0', sizeof(buf
));
80 ptr
= realpath(paths
[i
].path
, buf
);
82 if (ptr
== NULL
&& paths
[i
].result
== NULL
)
85 if (ptr
== NULL
&& paths
[i
].result
!= NULL
)
86 atf_tc_fail("realpath failed for '%s'", paths
[i
].path
);
88 if (strcmp(paths
[i
].result
, buf
) != 0)
89 atf_tc_fail("expected '%s', got '%s'",
90 paths
[i
].result
, buf
);
94 ATF_TC(realpath_huge
);
95 ATF_TC_HEAD(realpath_huge
, tc
)
97 atf_tc_set_md_var(tc
, "descr", "Test huge path with realpath(3)");
100 ATF_TC_BODY(realpath_huge
, tc
)
102 char result
[MAXPATHLEN
] = { 0 };
103 char buffer
[MAXPATHLEN
] = { 0 };
105 (void)memset(buffer
, '/', sizeof(buffer
) - 1);
107 ATF_CHECK(realpath(buffer
, result
) != NULL
);
108 ATF_CHECK(strlen(result
) == 1);
109 ATF_CHECK(result
[0] == '/');
112 ATF_TC(realpath_symlink
);
113 ATF_TC_HEAD(realpath_symlink
, tc
)
115 atf_tc_set_md_var(tc
, "descr", "Test symbolic link with realpath(3)");
118 ATF_TC_BODY(realpath_symlink
, tc
)
120 char path
[MAXPATHLEN
] = { 0 };
121 char slnk
[MAXPATHLEN
] = { 0 };
122 char resb
[MAXPATHLEN
] = { 0 };
125 (void)getcwd(path
, sizeof(path
));
126 (void)getcwd(slnk
, sizeof(slnk
));
128 (void)strlcat(path
, "/realpath", sizeof(path
));
129 (void)strlcat(slnk
, "/symbolic", sizeof(slnk
));
131 fd
= open(path
, O_RDONLY
| O_CREAT
, 0600);
133 ATF_REQUIRE(fd
>= 0);
134 ATF_REQUIRE(symlink(path
, slnk
) == 0);
135 ATF_REQUIRE(close(fd
) == 0);
137 ATF_REQUIRE(realpath(slnk
, resb
) != NULL
);
138 ATF_REQUIRE(strcmp(resb
, path
) == 0);
140 ATF_REQUIRE(unlink(path
) == 0);
141 ATF_REQUIRE(unlink(slnk
) == 0);
147 ATF_TP_ADD_TC(tp
, realpath_basic
);
148 ATF_TP_ADD_TC(tp
, realpath_huge
);
149 ATF_TP_ADD_TC(tp
, realpath_symlink
);
151 return atf_no_error();