VirtualBox

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

Last change on this file since 59692 was 57838, checked in by vboxsync, 9 years ago

tstRTSymlink: Warning.

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