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