1 | /* $Id: tstRTTemp.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Temporary files and directories.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2024 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/dir.h>
|
---|
42 | #include <iprt/file.h>
|
---|
43 | #include <iprt/path.h>
|
---|
44 |
|
---|
45 | #include <iprt/errcore.h>
|
---|
46 | #include <iprt/initterm.h>
|
---|
47 | #include <iprt/mem.h>
|
---|
48 | #include <iprt/param.h>
|
---|
49 | #include <iprt/path.h>
|
---|
50 | #include <iprt/stream.h>
|
---|
51 | #include <iprt/string.h>
|
---|
52 | #include <iprt/test.h>
|
---|
53 |
|
---|
54 |
|
---|
55 | /*********************************************************************************************************************************
|
---|
56 | * Global Variables *
|
---|
57 | *********************************************************************************************************************************/
|
---|
58 | static char g_szTempPath[RTPATH_MAX - 50];
|
---|
59 |
|
---|
60 |
|
---|
61 | static void tstObjectCreateTemp(const char *pszSubTest, const char *pszTemplate, bool fFile, RTFMODE fMode, unsigned cTimes, bool fSkipXCheck)
|
---|
62 | {
|
---|
63 | RTTestISub(pszSubTest);
|
---|
64 | const char *pcszAPI = fFile ? "RTFileCreateTemp" : "RTDirCreateTemp";
|
---|
65 |
|
---|
66 | /* Allocate the result array. */
|
---|
67 | char **papszNames = (char **)RTMemTmpAllocZ(cTimes * sizeof(char *));
|
---|
68 | RTTESTI_CHECK_RETV(papszNames != NULL);
|
---|
69 |
|
---|
70 | /* The test loop. */
|
---|
71 | unsigned i;
|
---|
72 | for (i = 0; i < cTimes; i++)
|
---|
73 | {
|
---|
74 | int rc;
|
---|
75 | char szName[RTPATH_MAX];
|
---|
76 | RTFMODE fModeFinal;
|
---|
77 |
|
---|
78 | RTTESTI_CHECK_RC(rc = RTPathAppend(strcpy(szName, g_szTempPath), sizeof(szName), pszTemplate), VINF_SUCCESS);
|
---|
79 | if (RT_FAILURE(rc))
|
---|
80 | break;
|
---|
81 |
|
---|
82 | RTTESTI_CHECK(papszNames[i] = RTStrDup(szName));
|
---|
83 | if (!papszNames[i])
|
---|
84 | break;
|
---|
85 |
|
---|
86 | rc = fFile
|
---|
87 | ? RTFileCreateTemp(papszNames[i], fMode)
|
---|
88 | : RTDirCreateTemp(papszNames[i], fMode);
|
---|
89 | if (rc != VINF_SUCCESS)
|
---|
90 | {
|
---|
91 | RTTestIFailed("%s(%s, %#o) call #%u -> %Rrc\n", pcszAPI, szName, (int)fMode, i, rc);
|
---|
92 | RTStrFree(papszNames[i]);
|
---|
93 | papszNames[i] = NULL;
|
---|
94 | break;
|
---|
95 | }
|
---|
96 | /* Check that the final permissions are not more permissive than
|
---|
97 | * the ones requested (less permissive is fine, c.f. umask etc.).
|
---|
98 | * I mask out the group as I am not sure how we deal with that on
|
---|
99 | * Windows. */
|
---|
100 | RTTESTI_CHECK_RC_OK(rc = RTPathGetMode(papszNames[i], &fModeFinal));
|
---|
101 | if (RT_SUCCESS(rc))
|
---|
102 | {
|
---|
103 | fModeFinal &= (RTFS_UNIX_IRWXU | RTFS_UNIX_IRWXO);
|
---|
104 | RTTESTI_CHECK_MSG((fModeFinal & ~fMode) == 0,
|
---|
105 | ("%s: szName %s\nfModeFinal ~= %#o, expected %#o\n",
|
---|
106 | pcszAPI, szName, fModeFinal, (int)fMode));
|
---|
107 | }
|
---|
108 | RTTestIPrintf(RTTESTLVL_DEBUG, "%s: %s\n", pcszAPI, papszNames[i]);
|
---|
109 | RTTESTI_CHECK_MSG(strlen(szName) == strlen(papszNames[i]), ("%s: szName %s\nReturned %s\n", pcszAPI, szName, papszNames[i]));
|
---|
110 | if (!fSkipXCheck)
|
---|
111 | RTTESTI_CHECK_MSG(strchr(RTPathFilename(papszNames[i]), 'X') == NULL, ("%s: szName %s\nReturned %s\n", pcszAPI, szName, papszNames[i]));
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* cleanup */
|
---|
115 | while (i-- > 0)
|
---|
116 | {
|
---|
117 | if (fFile)
|
---|
118 | RTTESTI_CHECK_RC(RTFileDelete(papszNames[i]), VINF_SUCCESS);
|
---|
119 | else
|
---|
120 | RTTESTI_CHECK_RC(RTDirRemove(papszNames[i]), VINF_SUCCESS);
|
---|
121 | RTStrFree(papszNames[i]);
|
---|
122 | }
|
---|
123 | RTMemTmpFree(papszNames);
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | static void tstFileCreateTemp(const char *pszSubTest, const char *pszTemplate, RTFMODE fMode, unsigned cTimes, bool fSkipXCheck)
|
---|
128 | {
|
---|
129 | tstObjectCreateTemp(pszSubTest, pszTemplate, true /* fFile */, fMode,
|
---|
130 | cTimes, fSkipXCheck);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | static void tstDirCreateTemp(const char *pszSubTest, const char *pszTemplate, RTFMODE fMode, unsigned cTimes, bool fSkipXCheck)
|
---|
135 | {
|
---|
136 | tstObjectCreateTemp(pszSubTest, pszTemplate, false /* fFile */, fMode,
|
---|
137 | cTimes, fSkipXCheck);
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | static void tstBothCreateTemp(const char *pszSubTest, const char *pszTemplate, RTFMODE fMode, unsigned cTimes, bool fSkipXCheck)
|
---|
142 | {
|
---|
143 | char pszSubTestLong[128];
|
---|
144 |
|
---|
145 | RTStrPrintf(pszSubTestLong, sizeof(pszSubTestLong), "RTFileCreateTemp %s",
|
---|
146 | pszSubTest);
|
---|
147 | tstFileCreateTemp(pszSubTestLong, pszTemplate, fMode, cTimes,
|
---|
148 | fSkipXCheck);
|
---|
149 | RTStrPrintf(pszSubTestLong, sizeof(pszSubTestLong), "RTDirCreateTemp %s",
|
---|
150 | pszSubTest);
|
---|
151 | tstDirCreateTemp(pszSubTestLong, pszTemplate, fMode, cTimes, fSkipXCheck);
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | int main()
|
---|
156 | {
|
---|
157 | RTTEST hTest;
|
---|
158 | int rc = RTTestInitAndCreate("tstRTTemp", &hTest);
|
---|
159 | if (rc)
|
---|
160 | return rc;
|
---|
161 | RTTestBanner(hTest);
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Get the temp directory (this is essential to the testcase).
|
---|
165 | */
|
---|
166 | RTTESTI_CHECK_RC(rc = RTPathTemp(g_szTempPath, sizeof(g_szTempPath)), VINF_SUCCESS);
|
---|
167 | if (RT_FAILURE(rc))
|
---|
168 | return RTTestSummaryAndDestroy(hTest);
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * Create N temporary files and directories using RT(File|Dir)CreateTemp.
|
---|
172 | */
|
---|
173 | tstBothCreateTemp("#1 (standard)", "rtRTTemp-XXXXXX", 0700, 128, false /*fSkipXCheck*/);
|
---|
174 | tstBothCreateTemp("#2 (long)", "rtRTTemp-XXXXXXXXXXXXXXXXX", 0700, 128, false /*fSkipXCheck*/);
|
---|
175 | tstBothCreateTemp("#3 (short)", "rtRTTemp-XX", 0777, 128, false /*fSkipXCheck*/);
|
---|
176 | tstBothCreateTemp("#4 (very short)", "rtRTTemp-X", 0100, 26+10, false /*fSkipXCheck*/);
|
---|
177 | tstBothCreateTemp("#5 (in-name)", "rtRTTemp-XXXt", 0301, 2, false /*fSkipXCheck*/);
|
---|
178 | tstBothCreateTemp("#6 (in-name)", "XXX-rtRTTemp", 0355, 2, false /*fSkipXCheck*/);
|
---|
179 | tstBothCreateTemp("#7 (in-name)", "rtRTTemp-XXXXXXXXX.tmp", 0755, 128, false /*fSkipXCheck*/);
|
---|
180 | tstBothCreateTemp("#8 (in-name)", "rtRTTemp-XXXXXXX-X.tmp", 0700, 128, true /*fSkipXCheck*/);
|
---|
181 | tstBothCreateTemp("#9 (in-name)", "rtRTTemp-XXXXXX-XX.tmp", 0700, 128, true /*fSkipXCheck*/);
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * Summary.
|
---|
185 | */
|
---|
186 | return RTTestSummaryAndDestroy(hTest);
|
---|
187 | }
|
---|
188 |
|
---|