VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTDirCreateUniqueNumbered.cpp@ 100762

Last change on this file since 100762 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1/* $Id: tstRTDirCreateUniqueNumbered.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Unique directory creation.
4 */
5
6/*
7 * Copyright (C) 2011-2023 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
43#include <iprt/err.h>
44#include <iprt/path.h>
45#include <iprt/mem.h>
46#include <iprt/string.h>
47#include <iprt/test.h>
48
49
50/*********************************************************************************************************************************
51* Global Variables *
52*********************************************************************************************************************************/
53static char g_szTempPath[RTPATH_MAX - 50];
54
55
56static void tst1(size_t cTest, size_t cchDigits, char chSep)
57{
58 RTTestISubF("tst #%u (digits: %u; sep: %c)", cTest, cchDigits, chSep ? chSep : ' ');
59
60 /* We try to create max possible + one. */
61 size_t cTimes = 1;
62 for (size_t i = 0; i < cchDigits; ++i)
63 cTimes *= 10;
64
65 /* Allocate the result array. */
66 char **papszNames = (char **)RTMemTmpAllocZ((cTimes + 1) * sizeof(char *));
67 RTTESTI_CHECK_RETV(papszNames != NULL);
68
69 int rc = VERR_INTERNAL_ERROR;
70 /* The test loop. */
71 size_t i;
72 for (i = 0; i < cTimes + 1; i++)
73 {
74 char szName[RTPATH_MAX];
75 RTTESTI_CHECK_RC(rc = RTPathAppend(strcpy(szName, g_szTempPath), sizeof(szName), "RTDirCreateUniqueNumbered"), VINF_SUCCESS);
76 if (RT_FAILURE(rc))
77 break;
78
79 rc = RTDirCreateUniqueNumbered(szName, sizeof(szName), 0700, cchDigits, chSep);
80 if (rc != VINF_SUCCESS)
81 {
82 /* Random selection (system) isn't 100% predictable, so we must give a little
83 leeway for the 2+ digit tests. (Using random is essential for performance.) */
84 if (cchDigits == 1 || rc != VERR_ALREADY_EXISTS || i < cTimes - 1)
85 RTTestIFailed("RTDirCreateUniqueNumbered(%s) call #%u -> %Rrc\n", szName, i, rc);
86 break;
87 }
88
89 RTTESTI_CHECK(papszNames[i] = RTStrDup(szName));
90 if (!papszNames[i])
91 break;
92
93 RTTestIPrintf(RTTESTLVL_DEBUG, "%s\n", papszNames[i]);
94 }
95
96 /* Try to create one more, which shouldn't be possible. */
97 if (RT_SUCCESS(rc) && i == cTimes + 1)
98 {
99 char szName[RTPATH_MAX];
100 RTTESTI_CHECK_RC(rc = RTPathAppend(strcpy(szName, g_szTempPath), sizeof(szName), "RTDirCreateUniqueNumbered"), VINF_SUCCESS);
101 if (RT_SUCCESS(rc))
102 RTTESTI_CHECK_RC(rc = RTDirCreateUniqueNumbered(szName, sizeof(szName), 0700, cchDigits, chSep), VERR_ALREADY_EXISTS);
103 }
104
105 /* cleanup */
106 while (i-- > 0)
107 {
108 RTTESTI_CHECK_RC(RTDirRemove(papszNames[i]), VINF_SUCCESS);
109 RTStrFree(papszNames[i]);
110 }
111 RTMemTmpFree(papszNames);
112}
113
114
115int main()
116{
117 RTTEST hTest;
118 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTDirCreateUniqueNumbered", &hTest);
119 if (rcExit)
120 return rcExit;
121 RTTestBanner(hTest);
122
123 /*
124 * Get the temp directory (this is essential to the testcase).
125 */
126 int rc;
127 RTTESTI_CHECK_RC(rc = RTPathTemp(g_szTempPath, sizeof(g_szTempPath)), VINF_SUCCESS);
128 if (RT_FAILURE(rc))
129 return RTTestSummaryAndDestroy(hTest);
130
131 /*
132 * Create some test directories.
133 */
134 tst1(1, 1, '\0');
135 tst1(2, 1, '-');
136 tst1(3, 2, '\0');
137 tst1(4, 2, '-');
138
139 /*
140 * Summary.
141 */
142 return RTTestSummaryAndDestroy(hTest);
143}
144
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