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