VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTSymlink.cpp@ 97242

Last change on this file since 97242 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1/* $Id: tstRTSymlink.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Symbolic Links.
4 */
5
6/*
7 * Copyright (C) 2010-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/symlink.h>
42
43#include <iprt/test.h>
44#include <iprt/dir.h>
45#include <iprt/err.h>
46#include <iprt/file.h>
47#include <iprt/param.h>
48#include <iprt/path.h>
49#include <iprt/process.h>
50#include <iprt/string.h>
51#include <iprt/initterm.h>
52
53
54static void test1Worker(RTTEST hTest, const char *pszBaseDir,
55 const char *pszTarget, RTSYMLINKTYPE enmType, bool fDangling)
56{
57 char szPath1[RTPATH_MAX];
58 char szPath2[RTPATH_MAX];
59 size_t cchTarget = strlen(pszTarget);
60 char szPath3[RTPATH_MAX];
61
62 RTStrCopy(szPath3, sizeof(szPath3), pszTarget);
63
64#ifdef RT_OS_WINDOWS
65 /* see RTSymlinkCreate in symlink-win.cpp */
66 char c;
67 char *psz = szPath3;
68 while ((c = *psz) != '\0')
69 {
70 if (c == '/')
71 *psz = '\\';
72 psz++;
73 }
74#endif
75
76 /* Create it.*/
77 RTTESTI_CHECK_RC_OK_RETV(RTPathJoin(szPath1, sizeof(szPath1), pszBaseDir, "tstRTSymlink-link-1"));
78 RTSymlinkDelete(szPath1, 0); /* clean up previous run */
79 int rc = RTSymlinkCreate(szPath1, pszTarget, enmType, 0);
80 if (rc == VERR_NOT_SUPPORTED)
81 {
82 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "VERR_NOT_SUPPORTED - skipping\n");
83 return;
84 }
85 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
86
87 /* Check the predicate functions. */
88 RTTESTI_CHECK(RTSymlinkExists(szPath1));
89 RTTESTI_CHECK(RTSymlinkIsDangling(szPath1) == fDangling);
90
91 /* Read it. */
92 memset(szPath2, 0xff, sizeof(szPath2));
93 szPath2[sizeof(szPath2) - 1] = '\0';
94 RTTESTI_CHECK_RC(RTSymlinkRead(szPath1, szPath2, sizeof(szPath2), 0), VINF_SUCCESS);
95 RTTESTI_CHECK_MSG(strcmp(szPath2, szPath3) == 0, ("got=\"%s\" expected=\"%s\"", szPath2, szPath3));
96
97 memset(szPath2, 0xff, sizeof(szPath2));
98 szPath2[sizeof(szPath2) - 1] = '\0';
99 RTTESTI_CHECK_RC(RTSymlinkRead(szPath1, szPath2, cchTarget + 1, 0), VINF_SUCCESS);
100 RTTESTI_CHECK_MSG(strcmp(szPath2, szPath3) == 0, ("got=\"%s\" expected=\"%s\"", szPath2, szPath3));
101
102 memset(szPath2, 0xff, sizeof(szPath2));
103 szPath2[sizeof(szPath2) - 1] = '\0';
104 RTTESTI_CHECK_RC(RTSymlinkRead(szPath1, szPath2, cchTarget, 0), VERR_BUFFER_OVERFLOW);
105 RTTESTI_CHECK_MSG( strncmp(szPath2, szPath3, cchTarget - 1) == 0
106 && szPath2[cchTarget - 1] == '\0',
107 ("got=\"%s\" expected=\"%.*s\"", szPath2, cchTarget - 1, szPath3));
108
109 /* Other APIs that have to handle symlinks carefully. */
110 RTFSOBJINFO ObjInfo;
111 RTTESTI_CHECK_RC(rc = RTPathQueryInfo(szPath1, &ObjInfo, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
112 if (RT_SUCCESS(rc))
113 RTTESTI_CHECK(RTFS_IS_SYMLINK(ObjInfo.Attr.fMode));
114 RTTESTI_CHECK_RC(rc = RTPathQueryInfoEx(szPath1, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
115 if (RT_SUCCESS(rc))
116 RTTESTI_CHECK(RTFS_IS_SYMLINK(ObjInfo.Attr.fMode));
117
118 if (!fDangling)
119 {
120 RTTESTI_CHECK_RC(rc = RTPathQueryInfoEx(szPath1, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK), VINF_SUCCESS);
121 if (RT_SUCCESS(rc))
122 RTTESTI_CHECK(!RTFS_IS_SYMLINK(ObjInfo.Attr.fMode));
123 else
124 RT_ZERO(ObjInfo);
125
126 if (enmType == RTSYMLINKTYPE_DIR)
127 {
128 RTTESTI_CHECK(RTDirExists(szPath1));
129 RTTESTI_CHECK(RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode));
130 }
131 else if (enmType == RTSYMLINKTYPE_FILE)
132 {
133 RTTESTI_CHECK(RTFileExists(szPath1));
134 RTTESTI_CHECK(RTFS_IS_FILE(ObjInfo.Attr.fMode));
135 }
136
137 /** @todo Check more APIs */
138 }
139
140 /* Finally, the removal of the symlink. */
141 RTTESTI_CHECK_RC(RTSymlinkDelete(szPath1, 0), VINF_SUCCESS);
142 RTTESTI_CHECK_RC(RTSymlinkDelete(szPath1, 0), VERR_FILE_NOT_FOUND);
143}
144
145
146static void test1(RTTEST hTest, const char *pszBaseDir)
147{
148 char szPath1[RTPATH_MAX];
149
150 /*
151 * Making some assumptions about how we are executed from to start with...
152 */
153 RTTestISub("Negative RTSymlinkRead, Exists & IsDangling");
154 char szExecDir[RTPATH_MAX];
155 RTTESTI_CHECK_RC_OK_RETV(RTPathExecDir(szExecDir, sizeof(szExecDir)));
156 RTTESTI_CHECK(RTDirExists(szExecDir));
157
158 char szExecFile[RTPATH_MAX];
159 RTTESTI_CHECK_RETV(RTProcGetExecutablePath(szExecFile, sizeof(szExecFile)) != NULL);
160 RTTESTI_CHECK(RTFileExists(szExecFile));
161
162 RTTESTI_CHECK(!RTSymlinkExists(szExecFile));
163 RTTESTI_CHECK(!RTSymlinkExists(szExecDir));
164 RTTESTI_CHECK(!RTSymlinkIsDangling(szExecFile));
165 RTTESTI_CHECK(!RTSymlinkIsDangling(szExecDir));
166 RTTESTI_CHECK(!RTSymlinkExists("/"));
167 RTTESTI_CHECK(!RTSymlinkIsDangling("/"));
168 RTTESTI_CHECK(!RTSymlinkExists("/some/non-existing/directory/name/iprt"));
169 RTTESTI_CHECK(!RTSymlinkExists("/some/non-existing/directory/name/iprt/"));
170 RTTESTI_CHECK(!RTSymlinkIsDangling("/some/non-existing/directory/name/iprt"));
171 RTTESTI_CHECK(!RTSymlinkIsDangling("/some/non-existing/directory/name/iprt/"));
172
173 RTTESTI_CHECK_RC(RTSymlinkRead(szExecFile, szPath1, sizeof(szPath1), 0), VERR_NOT_SYMLINK);
174 RTTESTI_CHECK_RC(RTSymlinkRead(szExecDir, szPath1, sizeof(szPath1), 0), VERR_NOT_SYMLINK);
175
176 /*
177 * Do some symlinking. ASSUME they are supported on the test file system.
178 */
179 RTTestISub("Basics");
180 RTTESTI_CHECK_RETV(RTDirExists(pszBaseDir));
181 test1Worker(hTest, pszBaseDir, szExecFile, RTSYMLINKTYPE_FILE, false /*fDangling*/);
182 test1Worker(hTest, pszBaseDir, szExecDir, RTSYMLINKTYPE_DIR, false /*fDangling*/);
183 test1Worker(hTest, pszBaseDir, szExecFile, RTSYMLINKTYPE_UNKNOWN, false /*fDangling*/);
184 test1Worker(hTest, pszBaseDir, szExecDir, RTSYMLINKTYPE_UNKNOWN, false /*fDangling*/);
185
186 /*
187 * Create a few dangling links.
188 */
189 RTTestISub("Dangling links");
190 test1Worker(hTest, pszBaseDir, "../dangle/dangle", RTSYMLINKTYPE_FILE, true /*fDangling*/);
191 test1Worker(hTest, pszBaseDir, "../dangle/dangle", RTSYMLINKTYPE_DIR, true /*fDangling*/);
192 test1Worker(hTest, pszBaseDir, "../dangle/dangle", RTSYMLINKTYPE_UNKNOWN, true /*fDangling*/);
193 test1Worker(hTest, pszBaseDir, "../dangle/dangle/", RTSYMLINKTYPE_UNKNOWN, true /*fDangling*/);
194}
195
196
197int main()
198{
199 RTTEST hTest;
200 int rc = RTTestInitAndCreate("tstRTSymlink", &hTest);
201 if (rc)
202 return rc;
203 RTTestBanner(hTest);
204
205 test1(hTest, ".");
206
207 return RTTestSummaryAndDestroy(hTest);
208}
209
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette