1// Copyright 2016 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <fcntl.h>
6#include <limits.h>
7#include <stdint.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <sys/stat.h>
11#include <unistd.h>
12
13#include <zircon/syscalls.h>
14#include <unittest/unittest.h>
15
16#include "filesystems.h"
17
18constexpr char kName[] = "::my_file";
19constexpr char kTestNameDotDot[] = "::foo/../bar/../my_file";
20constexpr char kTestNameDot[] = "::././././my_file";
21constexpr char kTestNameBothDots[] = "::foo//.././/./././my_file";
22
23static bool terminator(char c) { return c == 0 || c == '/'; }
24
25static bool is_resolved(const char* path) {
26    // Check that there are no ".", "//", or ".." components.
27    // We assume there are no symlinks, since symlinks are not
28    // yet supported on Fuchsia.
29    while (true) {
30        if (path[0] == 0) {
31            return true;
32        } else if (path[0] == '.' && terminator(path[1])) {
33            return false;
34        } else if (path[0] == '/' && path[1] == '/') {
35            return false;
36        } else if (path[0] == '.' && path[1] == '.' && terminator(path[2])) {
37            return false;
38        }
39        if ((path = strchr(path, '/')) == NULL) {
40            return true;
41        }
42        path += 1;
43    }
44}
45
46bool test_realpath_absolute(void) {
47    BEGIN_TEST;
48
49    int fd = open(kName, O_RDWR | O_CREAT, 0644);
50    ASSERT_GT(fd, 0);
51
52    struct stat sb;
53    ASSERT_EQ(stat(kName, &sb), 0);
54
55    // Find the real path of the file (since, due to linker magic, we
56    // actually don't know it).
57    char buf[PATH_MAX];
58    ASSERT_EQ(realpath(kName, buf), buf);
59
60    // Confirm that for (resolvable) cases of realpath, the name
61    // can be cleaned.
62    char buf2[PATH_MAX];
63    ASSERT_EQ(realpath(kTestNameDotDot, buf2), buf2);
64    ASSERT_EQ(strcmp(buf, buf2), 0, "Name (with ..) did not resolve");
65    ASSERT_TRUE(is_resolved(buf2));
66
67    ASSERT_EQ(realpath(kTestNameDot, buf2), buf2);
68    ASSERT_EQ(strcmp(buf, buf2), 0, "Name (with .) did not resolve");
69    ASSERT_TRUE(is_resolved(buf2));
70
71    ASSERT_EQ(realpath(kTestNameBothDots, buf2), buf2);
72    ASSERT_EQ(strcmp(buf, buf2), 0, "Name (with . and ..) did not resolve");
73    ASSERT_TRUE(is_resolved(buf2));
74
75    // Clean up
76    ASSERT_EQ(close(fd), 0);
77    ASSERT_EQ(unlink(kName), 0);
78    END_TEST;
79}
80
81constexpr char kNameDir[] = "::my_dir";
82constexpr char kNameFile[] = "::my_dir/my_file";
83constexpr char kTestRelativeDotDot[] = "../my_dir/../my_dir/my_file";
84constexpr char kTestRelativeDot[] = "./././my_file";
85constexpr char kTestRelativeBothDots[] = "./..//my_dir/.././///././my_dir/./my_file";
86
87bool test_realpath_relative(void) {
88    BEGIN_TEST;
89
90    ASSERT_EQ(mkdir(kNameDir, 0666), 0);
91    int fd = open(kNameFile, O_RDWR | O_CREAT, 0644);
92    ASSERT_GT(fd, 0);
93    close(fd);
94
95    struct stat sb;
96    ASSERT_EQ(stat(kNameFile, &sb), 0);
97
98    // Find the real path of the file (since, due to linker magic, we
99    // actually don't know it).
100    char buf[PATH_MAX];
101    ASSERT_EQ(realpath(kNameFile, buf), buf);
102
103    char cwd[PATH_MAX];
104    ASSERT_NONNULL(getcwd(cwd, sizeof(cwd)));
105    ASSERT_EQ(chdir(kNameDir), 0);
106
107    char buf2[PATH_MAX];
108    ASSERT_EQ(realpath(kTestRelativeDotDot, buf2), buf2);
109    ASSERT_EQ(strcmp(buf, buf2), 0, "Name (with ..) did not resolve");
110    ASSERT_TRUE(is_resolved(buf2));
111
112    ASSERT_EQ(realpath(kTestRelativeDot, buf2), buf2);
113    ASSERT_EQ(strcmp(buf, buf2), 0, "Name (with .) did not resolve");
114    ASSERT_TRUE(is_resolved(buf2));
115
116    ASSERT_EQ(realpath(kTestRelativeBothDots, buf2), buf2);
117    ASSERT_EQ(strcmp(buf, buf2), 0, "Name (with . and ..) did not resolve");
118    ASSERT_TRUE(is_resolved(buf2));
119
120    // Test the longest possible path name
121
122    // Extract the current working directory name ("my_dir/my_file" - "my_file")
123    size_t cwd_len = strlen(buf) - strlen("my_file");
124    char bufmax[PATH_MAX + 1];
125    bufmax[0] = '.';
126    size_t len = 1;
127    // When realpath completes, it should return a result of the
128    // form "CWD + '/' + "my_file".
129    //
130    // Ensure that our (uncanonicalized) path, including the CWD,
131    // can fit within PATH_MAX (but just barely).
132    while (len != PATH_MAX - cwd_len - strlen("my_file") - 1) {
133        bufmax[len++] = '/';
134    }
135    memcpy(bufmax + len, "my_file", strlen("my_file"));
136    bufmax[len + strlen("my_file")] = 0;
137    ASSERT_EQ(strlen(bufmax), PATH_MAX - cwd_len - 1);
138
139    ASSERT_EQ(realpath(bufmax, buf2), buf2);
140    ASSERT_EQ(strcmp(buf, buf2), 0, "Name (longest path) did not resolve");
141    ASSERT_TRUE(is_resolved(buf2));
142
143    // Try a name that is too long (same as the last one, but just
144    // add a single additional "/").
145    bufmax[len++] = '/';
146    strcpy(bufmax + len, "my_file");
147    ASSERT_NULL(realpath(bufmax, buf2));
148
149    // Clean up
150    ASSERT_EQ(chdir(cwd), 0, "Could not return to original cwd");
151    ASSERT_EQ(unlink(kNameFile), 0);
152    END_TEST;
153}
154
155RUN_FOR_ALL_FILESYSTEMS(realpath_tests,
156    RUN_TEST_MEDIUM(test_realpath_absolute)
157    RUN_TEST_MEDIUM(test_realpath_relative)
158)
159