VirtualBox

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

Last change on this file since 43538 was 39612, checked in by vboxsync, 13 years ago

IPRT/*: add _NO_SYMLINKS flags to certain functions

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/* $Id: tstRTSymlink.cpp 39612 2011-12-14 14:19:55Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Symbolic Links.
4 */
5
6/*
7 * Copyright (C) 2010 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
51 /* Create it.*/
52 RTTESTI_CHECK_RC_OK_RETV(RTPathJoin(szPath1, sizeof(szPath1), pszBaseDir, "tstRTSymlink-link-1"));
53 RTSymlinkDelete(szPath1, 0); /* clean up previous run */
54 RTTESTI_CHECK_RC_RETV(RTSymlinkCreate(szPath1, pszTarget, RTSYMLINKTYPE_FILE, 0), VINF_SUCCESS);
55
56 /* Check the predicate functions. */
57 RTTESTI_CHECK(RTSymlinkExists(szPath1));
58 RTTESTI_CHECK(RTSymlinkIsDangling(szPath1) == fDangling);
59
60 /* Read it. */
61 memset(szPath2, 0xff, sizeof(szPath2));
62 szPath2[sizeof(szPath2) - 1] = '\0';
63 RTTESTI_CHECK_RC(RTSymlinkRead(szPath1, szPath2, sizeof(szPath2), 0), VINF_SUCCESS);
64 RTTESTI_CHECK_MSG(strcmp(szPath2, pszTarget) == 0, ("got=\"%s\" expected=\"%s\"", szPath2, pszTarget));
65
66 memset(szPath2, 0xff, sizeof(szPath2));
67 szPath2[sizeof(szPath2) - 1] = '\0';
68 RTTESTI_CHECK_RC(RTSymlinkRead(szPath1, szPath2, cchTarget + 1, 0), VINF_SUCCESS);
69 RTTESTI_CHECK_MSG(strcmp(szPath2, pszTarget) == 0, ("got=\"%s\" expected=\"%s\"", szPath2, pszTarget));
70
71 memset(szPath2, 0xff, sizeof(szPath2));
72 szPath2[sizeof(szPath2) - 1] = '\0';
73 RTTESTI_CHECK_RC(RTSymlinkRead(szPath1, szPath2, cchTarget, 0), VERR_BUFFER_OVERFLOW);
74 RTTESTI_CHECK_MSG( strncmp(szPath2, pszTarget, cchTarget - 1) == 0
75 && szPath2[cchTarget - 1] == '\0',
76 ("got=\"%s\" expected=\"%.*s\"", szPath2, cchTarget - 1, pszTarget));
77
78 /* Other APIs that have to handle symlinks carefully. */
79 int rc;
80 RTFSOBJINFO ObjInfo;
81 RTTESTI_CHECK_RC(rc = RTPathQueryInfo(szPath1, &ObjInfo, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
82 if (RT_SUCCESS(rc))
83 RTTESTI_CHECK(RTFS_IS_SYMLINK(ObjInfo.Attr.fMode));
84 RTTESTI_CHECK_RC(rc = RTPathQueryInfoEx(szPath1, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
85 if (RT_SUCCESS(rc))
86 RTTESTI_CHECK(RTFS_IS_SYMLINK(ObjInfo.Attr.fMode));
87
88 if (!fDangling)
89 {
90 RTTESTI_CHECK_RC(rc = RTPathQueryInfoEx(szPath1, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK), VINF_SUCCESS);
91 if (RT_SUCCESS(rc))
92 RTTESTI_CHECK(!RTFS_IS_SYMLINK(ObjInfo.Attr.fMode));
93 else
94 RT_ZERO(ObjInfo);
95
96 if (enmType == RTSYMLINKTYPE_DIR)
97 {
98 RTTESTI_CHECK(RTDirExists(szPath1));
99 RTTESTI_CHECK(RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode));
100 }
101 else if (enmType == RTSYMLINKTYPE_FILE)
102 {
103 RTTESTI_CHECK(RTFileExists(szPath1));
104 RTTESTI_CHECK(RTFS_IS_FILE(ObjInfo.Attr.fMode));
105 }
106
107 /** @todo Check more APIs */
108 }
109
110 /* Finally, the removal of the symlink. */
111 RTTESTI_CHECK_RC(RTSymlinkDelete(szPath1, 0), VINF_SUCCESS);
112 RTTESTI_CHECK_RC(RTSymlinkDelete(szPath1, 0), VERR_FILE_NOT_FOUND);
113}
114
115
116static void test1(RTTEST hTest, const char *pszBaseDir)
117{
118 char szPath1[RTPATH_MAX];
119 char szPath2[RTPATH_MAX];
120
121 /*
122 * Making some assumptions about how we are executed from to start with...
123 */
124 RTTestISub("Negative RTSymlinkRead, RTSymlinkExists and RTSymlinkIsDangling");
125 char szExecDir[RTPATH_MAX];
126 RTTESTI_CHECK_RC_OK_RETV(RTPathExecDir(szExecDir, sizeof(szExecDir)));
127 size_t cchExecDir = strlen(szExecDir);
128 RTTESTI_CHECK(RTDirExists(szExecDir));
129
130 char szExecFile[RTPATH_MAX];
131 RTTESTI_CHECK_RETV(RTProcGetExecutablePath(szExecFile, sizeof(szExecFile)) != NULL);
132 size_t cchExecFile = strlen(szExecFile);
133 RTTESTI_CHECK(RTFileExists(szExecFile));
134
135 RTTESTI_CHECK(!RTSymlinkExists(szExecFile));
136 RTTESTI_CHECK(!RTSymlinkExists(szExecDir));
137 RTTESTI_CHECK(!RTSymlinkIsDangling(szExecFile));
138 RTTESTI_CHECK(!RTSymlinkIsDangling(szExecDir));
139 RTTESTI_CHECK(!RTSymlinkExists("/"));
140 RTTESTI_CHECK(!RTSymlinkIsDangling("/"));
141 RTTESTI_CHECK(!RTSymlinkExists("/some/non-existing/directory/name/iprt"));
142 RTTESTI_CHECK(!RTSymlinkExists("/some/non-existing/directory/name/iprt/"));
143 RTTESTI_CHECK(!RTSymlinkIsDangling("/some/non-existing/directory/name/iprt"));
144 RTTESTI_CHECK(!RTSymlinkIsDangling("/some/non-existing/directory/name/iprt/"));
145
146 RTTESTI_CHECK_RC(RTSymlinkRead(szExecFile, szPath1, sizeof(szPath1), 0), VERR_NOT_SYMLINK);
147 RTTESTI_CHECK_RC(RTSymlinkRead(szExecDir, szPath1, sizeof(szPath1), 0), VERR_NOT_SYMLINK);
148
149 /*
150 * Do some symlinking. ASSUME they are supported on the test file system.
151 */
152 RTTestISub("Basics");
153 RTTESTI_CHECK_RETV(RTDirExists(pszBaseDir));
154 test1Worker(hTest, pszBaseDir, szExecFile, RTSYMLINKTYPE_FILE, false /*fDangling*/);
155 test1Worker(hTest, pszBaseDir, szExecDir, RTSYMLINKTYPE_DIR, false /*fDangling*/);
156 test1Worker(hTest, pszBaseDir, szExecFile, RTSYMLINKTYPE_UNKNOWN, false /*fDangling*/);
157 test1Worker(hTest, pszBaseDir, szExecDir, RTSYMLINKTYPE_UNKNOWN, false /*fDangling*/);
158
159 /*
160 * Create a few dangling links.
161 */
162 RTTestISub("Dangling links");
163 test1Worker(hTest, pszBaseDir, "../dangle/dangle", RTSYMLINKTYPE_FILE, true /*fDangling*/);
164 test1Worker(hTest, pszBaseDir, "../dangle/dangle", RTSYMLINKTYPE_DIR, true /*fDangling*/);
165 test1Worker(hTest, pszBaseDir, "../dangle/dangle", RTSYMLINKTYPE_UNKNOWN, true /*fDangling*/);
166 test1Worker(hTest, pszBaseDir, "../dangle/dangle/", RTSYMLINKTYPE_UNKNOWN, true /*fDangling*/);
167}
168
169
170int main(int argc, char **argv)
171{
172 RTTEST hTest;
173 int rc = RTTestInitAndCreate("tstRTSymlink", &hTest);
174 if (rc)
175 return rc;
176 RTTestBanner(hTest);
177
178 test1(hTest, ".");
179
180 return RTTestSummaryAndDestroy(hTest);
181}
182
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