]>
git.saurik.com Git - apple/libc.git/blob - tests/realpath.c
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>
42 #include <darwintest.h>
43 #include <darwintest_utils.h>
50 static const struct testcase paths
[] = {
57 { "/usr/bin///", "/usr/bin" },
58 { "///////usr", "/usr" },
59 { "/a/b/c/d/e", NULL
},
60 { " /usr/bin ", NULL
},
61 { "\\//////usr//bin", NULL
},
62 { "//usr//bin//", "/usr/bin" },
63 { "//////usr//bin//", "/usr/bin" },
64 { "/usr/bin//////////", "/usr/bin" },
67 T_DECL(realpath_basic
, "Resolve various short directory paths")
73 size_t num_cases
= sizeof(paths
) / sizeof(struct testcase
);
74 for (i
= 0; i
< num_cases
; i
++) {
76 (void)memset(buf
, '\0', sizeof(buf
));
78 ptr
= realpath(paths
[i
].path
, buf
);
80 if (paths
[i
].result
== NULL
) {
81 T_EXPECT_NULL(ptr
, NULL
);
83 T_EXPECT_EQ_STR(ptr
, paths
[i
].result
, "Real path matches expected path");
88 T_DECL(realpath_trailing
, "Trailing . and .. should fail after non-directories")
90 char result
[MAXPATHLEN
] = { 0 };
92 T_EXPECT_NULL(realpath("/usr/null/.", result
), NULL
);
93 T_EXPECT_NULL(realpath("/usr/null/..", result
), NULL
);
96 T_DECL(realpath_huge
, "Test realpath with maximum path size")
98 char result
[MAXPATHLEN
] = { 0 };
99 char buffer
[MAXPATHLEN
] = { 0 };
101 (void)memset(buffer
, '/', sizeof(buffer
) - 1);
103 T_EXPECT_NOTNULL(realpath(buffer
, result
), NULL
);
104 T_EXPECT_EQ(strlen(result
), (size_t) 1, NULL
);
105 T_EXPECT_EQ(result
[0], '/', NULL
);
108 T_DECL(realpath_symlink
, "Resolve symlinked paths")
110 char *resolved_tmpdir
;
111 char path
[MAXPATHLEN
] = { 0 };
112 char slnk
[MAXPATHLEN
] = { 0 };
113 char resb
[MAXPATHLEN
] = { 0 };
116 // We have to use the realpath for this symlink test.
117 // Otherwise, when trying to resolve a symlink, it won't match.
118 resolved_tmpdir
= realpath(dt_tmpdir(), NULL
);
119 snprintf(path
, sizeof(path
), "%s%s", resolved_tmpdir
, "/real");
120 snprintf(slnk
, sizeof(path
), "%s%s", resolved_tmpdir
, "/sym");
122 fd
= open(path
, O_RDONLY
| O_CREAT
, 0600);
125 T_ASSERT_GE(fd
, 0, NULL
);
126 T_ASSERT_POSIX_ZERO(symlink(path
, slnk
), NULL
);
127 T_ASSERT_POSIX_ZERO(close(fd
), NULL
);
129 T_ASSERT_NOTNULL(realpath(slnk
, resb
), NULL
);
130 T_ASSERT_EQ_STR(resb
, path
, NULL
);
132 T_ASSERT_POSIX_ZERO(unlink(path
), NULL
);
133 T_ASSERT_POSIX_ZERO(unlink(slnk
), NULL
);
134 free(resolved_tmpdir
);