VirtualBox

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

Last change on this file since 62724 was 62723, checked in by vboxsync, 9 years ago

RTDirCreateUniqueNumbered: Changed the implementation to count from zero and only do 20 sequential tries before switching to random numbers. Also, don't bother retrying more than 10000 times. Corrected the cchDigits parameter (nobody counts in signed int!).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1/* $Id: tstRTDirCreateUniqueNumbered.cpp 62723 2016-07-30 00:02:01Z 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*********************************************************************************************************************************/
42static char g_szTempPath[RTPATH_MAX - 50];
43
44
45static 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 * 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 RTTESTI_CHECK_RC(rc = RTDirCreateUniqueNumbered(szName, sizeof(szName), 0700, cchDigits, chSep), VINF_SUCCESS);
69 if (RT_FAILURE(rc))
70 {
71 RTTestIFailed("RTDirCreateUniqueNumbered(%s) call #%u -> %Rrc\n", szName, i, rc);
72 break;
73 }
74
75 RTTESTI_CHECK(papszNames[i] = RTStrDup(szName));
76 if (!papszNames[i])
77 break;
78
79 RTTestIPrintf(RTTESTLVL_DEBUG, "%s\n", papszNames[i]);
80 }
81
82 /* Try to create one more, which shouldn't be possible. */
83 if (RT_SUCCESS(rc))
84 {
85 char szName[RTPATH_MAX];
86 RTTESTI_CHECK_RC(rc = RTPathAppend(strcpy(szName, g_szTempPath), sizeof(szName), "RTDirCreateUniqueNumbered"), VINF_SUCCESS);
87 if (RT_SUCCESS(rc))
88 RTTESTI_CHECK_RC(rc = RTDirCreateUniqueNumbered(szName, sizeof(szName), 0700, cchDigits, chSep), VERR_ALREADY_EXISTS);
89 }
90
91 /* cleanup */
92 while (i-- > 0)
93 {
94 RTTESTI_CHECK_RC(RTDirRemove(papszNames[i]), VINF_SUCCESS);
95 RTStrFree(papszNames[i]);
96 }
97 RTMemTmpFree(papszNames);
98}
99
100
101int main()
102{
103 RTTEST hTest;
104 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTDirCreateUniqueNumbered", &hTest);
105 if (rcExit)
106 return rcExit;
107 RTTestBanner(hTest);
108
109 /*
110 * Get the temp directory (this is essential to the testcase).
111 */
112 int rc;
113 RTTESTI_CHECK_RC(rc = RTPathTemp(g_szTempPath, sizeof(g_szTempPath)), VINF_SUCCESS);
114 if (RT_FAILURE(rc))
115 return RTTestSummaryAndDestroy(hTest);
116
117 /*
118 * Create some test directories.
119 */
120 tst1(1, 1, '\0');
121 tst1(2, 1, '-');
122 tst1(3, 2, '\0');
123 tst1(4, 2, '-');
124
125 /*
126 * Summary.
127 */
128 return RTTestSummaryAndDestroy(hTest);
129}
130
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