]> git.saurik.com Git - apple/libc.git/blob - tests/realpath.c
Libc-1244.50.9.tar.gz
[apple/libc.git] / tests / realpath.c
1 /* $NetBSD: t_realpath.c,v 1.2 2012/03/27 07:54:58 njoly Exp $ */
2
3 /*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 *
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.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_realpath.c,v 1.2 2012/03/27 07:54:58 njoly Exp $");
33
34 #include <sys/param.h>
35
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 #include <darwintest.h>
43 #include <darwintest_utils.h>
44
45 struct testcase {
46 const char *path;
47 const char *result;
48 };
49
50 static const struct testcase paths[] = {
51 { "/", "/" },
52 { "///////", "/" },
53 { "", NULL },
54 { " ", NULL },
55 { "/ ", NULL },
56 { " /", NULL },
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" },
65 };
66
67 T_DECL(realpath_basic, "Resolve various short directory paths")
68 {
69 char buf[MAXPATHLEN];
70 char *ptr;
71 size_t i;
72
73 size_t num_cases = sizeof(paths) / sizeof(struct testcase);
74 for (i = 0; i < num_cases; i++) {
75
76 (void)memset(buf, '\0', sizeof(buf));
77
78 ptr = realpath(paths[i].path, buf);
79
80 if (paths[i].result == NULL) {
81 T_EXPECT_NULL(ptr, NULL);
82 } else {
83 T_EXPECT_EQ_STR(ptr, paths[i].result, "Real path matches expected path");
84 }
85 }
86 }
87
88 T_DECL(realpath_trailing, "Trailing . and .. should fail after non-directories")
89 {
90 char result[MAXPATHLEN] = { 0 };
91
92 T_EXPECT_NULL(realpath("/usr/null/.", result), NULL);
93 T_EXPECT_NULL(realpath("/usr/null/..", result), NULL);
94 }
95
96 T_DECL(realpath_huge, "Test realpath with maximum path size")
97 {
98 char result[MAXPATHLEN] = { 0 };
99 char buffer[MAXPATHLEN] = { 0 };
100
101 (void)memset(buffer, '/', sizeof(buffer) - 1);
102
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);
106 }
107
108 T_DECL(realpath_symlink, "Resolve symlinked paths")
109 {
110 char *resolved_tmpdir;
111 char path[MAXPATHLEN] = { 0 };
112 char slnk[MAXPATHLEN] = { 0 };
113 char resb[MAXPATHLEN] = { 0 };
114 int fd;
115
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");
121
122 fd = open(path, O_RDONLY | O_CREAT, 0600);
123
124 T_WITH_ERRNO;
125 T_ASSERT_GE(fd, 0, NULL);
126 T_ASSERT_POSIX_ZERO(symlink(path, slnk), NULL);
127 T_ASSERT_POSIX_ZERO(close(fd), NULL);
128
129 T_ASSERT_NOTNULL(realpath(slnk, resb), NULL);
130 T_ASSERT_EQ_STR(resb, path, NULL);
131
132 T_ASSERT_POSIX_ZERO(unlink(path), NULL);
133 T_ASSERT_POSIX_ZERO(unlink(slnk), NULL);
134 free(resolved_tmpdir);
135 }