1 | /* $Id: tstRTTemp.cpp 20111 2009-05-28 00:34:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Temporary files and directories.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <iprt/dir.h>
|
---|
35 | #include <iprt/file.h>
|
---|
36 | #include <iprt/path.h>
|
---|
37 |
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/initterm.h>
|
---|
40 | #include <iprt/mem.h>
|
---|
41 | #include <iprt/param.h>
|
---|
42 | #include <iprt/path.h>
|
---|
43 | #include <iprt/stream.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include <iprt/test.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Global Variables *
|
---|
50 | *******************************************************************************/
|
---|
51 | static char g_szTempPath[RTPATH_MAX - 50];
|
---|
52 |
|
---|
53 |
|
---|
54 | static void tstDirCreateTemp(const char *pszSubTest, const char *pszTemplate, unsigned cTimes, bool fSkipXCheck)
|
---|
55 | {
|
---|
56 | RTTestISub(pszSubTest);
|
---|
57 |
|
---|
58 | /* Allocate the result array. */
|
---|
59 | char **papszNames = (char **)RTMemTmpAllocZ(cTimes * sizeof(char *));
|
---|
60 | RTTESTI_CHECK_RETV(papszNames != NULL);
|
---|
61 |
|
---|
62 | /* The test loop. */
|
---|
63 | unsigned i;
|
---|
64 | for (i = 0; i < cTimes; i++)
|
---|
65 | {
|
---|
66 | int rc;
|
---|
67 | char szName[RTPATH_MAX];
|
---|
68 | RTTESTI_CHECK_RC(rc = RTPathAppend(strcpy(szName, g_szTempPath), sizeof(szName), pszTemplate), VINF_SUCCESS);
|
---|
69 | if (RT_FAILURE(rc))
|
---|
70 | break;
|
---|
71 |
|
---|
72 | RTTESTI_CHECK(papszNames[i] = RTStrDup(szName));
|
---|
73 | if (!papszNames[i])
|
---|
74 | break;
|
---|
75 |
|
---|
76 | rc = RTDirCreateTemp(papszNames[i]);
|
---|
77 | if (rc != VINF_SUCCESS)
|
---|
78 | {
|
---|
79 | RTTestIFailed("RTDirCreateTemp(%s) call #%u -> %Rrc\n", szName, i, rc);
|
---|
80 | RTStrFree(papszNames[i]);
|
---|
81 | papszNames[i] = NULL;
|
---|
82 | break;
|
---|
83 | }
|
---|
84 | RTTestIPrintf(RTTESTLVL_DEBUG, "%s\n", papszNames[i]);
|
---|
85 | RTTESTI_CHECK_MSG(strlen(szName) == strlen(papszNames[i]), ("szName %s\nReturned %s\n", szName, papszNames[i]));
|
---|
86 | if (!fSkipXCheck)
|
---|
87 | RTTESTI_CHECK_MSG(strchr(RTPathFilename(papszNames[i]), 'X') == NULL, ("szName %s\nReturned %s\n", szName, papszNames[i]));
|
---|
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 | int rc = RTR3Init();
|
---|
103 | if (RT_FAILURE(rc))
|
---|
104 | return 1;
|
---|
105 | RTTEST hTest;
|
---|
106 | rc = RTTestCreate("tstRTTemp", &hTest);
|
---|
107 | if (RT_FAILURE(rc))
|
---|
108 | return 1;
|
---|
109 | RTTestBanner(hTest);
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * Get the temp directory (this is essential to the testcase).
|
---|
113 | */
|
---|
114 | RTTESTI_CHECK_RC(rc = RTPathTemp(g_szTempPath, sizeof(g_szTempPath)), VINF_SUCCESS);
|
---|
115 | if (RT_FAILURE(rc))
|
---|
116 | return RTTestSummaryAndDestroy(hTest);
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Create N temporary directories using RTDirCreateTemp.
|
---|
120 | */
|
---|
121 | tstDirCreateTemp("RTDirCreateTemp #1 (standard)", "rtRTTemp-XXXXXX", 128, false /*fSkipXCheck*/);
|
---|
122 | tstDirCreateTemp("RTDirCreateTemp #2 (long)", "rtRTTemp-XXXXXXXXXXXXXXXXX", 128, false /*fSkipXCheck*/);
|
---|
123 | tstDirCreateTemp("RTDirCreateTemp #3 (short)", "rtRTTemp-XX", 128, false /*fSkipXCheck*/);
|
---|
124 | tstDirCreateTemp("RTDirCreateTemp #4 (very short)", "rtRTTemp-X", 26+10, false /*fSkipXCheck*/);
|
---|
125 | tstDirCreateTemp("RTDirCreateTemp #5 (in-name)", "rtRTTemp-XXXt", 2, false /*fSkipXCheck*/);
|
---|
126 | tstDirCreateTemp("RTDirCreateTemp #6 (in-name)", "XXX-rtRTTemp", 2, false /*fSkipXCheck*/);
|
---|
127 | tstDirCreateTemp("RTDirCreateTemp #7 (in-name)", "rtRTTemp-XXXXXXXXX.tmp", 128, false /*fSkipXCheck*/);
|
---|
128 | tstDirCreateTemp("RTDirCreateTemp #8 (in-name)", "rtRTTemp-XXXXXXX-X.tmp", 128, true /*fSkipXCheck*/);
|
---|
129 | tstDirCreateTemp("RTDirCreateTemp #9 (in-name)", "rtRTTemp-XXXXXX-XX.tmp", 128, true /*fSkipXCheck*/);
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * Summary.
|
---|
133 | */
|
---|
134 | return RTTestSummaryAndDestroy(hTest);
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|