VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTTemp.cpp@ 23644

Last change on this file since 23644 was 20606, checked in by vboxsync, 16 years ago

IPRT/testcase: Use RTTestInitAndCreate.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1/* $Id: tstRTTemp.cpp 20606 2009-06-15 23:49:07Z 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*******************************************************************************/
51static char g_szTempPath[RTPATH_MAX - 50];
52
53
54static 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
100int main()
101{
102 RTTEST hTest;
103 int rc = RTTestInitAndCreate("tstRTTemp", &hTest);
104 if (rc)
105 return rc;
106 RTTestBanner(hTest);
107
108 /*
109 * Get the temp directory (this is essential to the testcase).
110 */
111 RTTESTI_CHECK_RC(rc = RTPathTemp(g_szTempPath, sizeof(g_szTempPath)), VINF_SUCCESS);
112 if (RT_FAILURE(rc))
113 return RTTestSummaryAndDestroy(hTest);
114
115 /*
116 * Create N temporary directories using RTDirCreateTemp.
117 */
118 tstDirCreateTemp("RTDirCreateTemp #1 (standard)", "rtRTTemp-XXXXXX", 128, false /*fSkipXCheck*/);
119 tstDirCreateTemp("RTDirCreateTemp #2 (long)", "rtRTTemp-XXXXXXXXXXXXXXXXX", 128, false /*fSkipXCheck*/);
120 tstDirCreateTemp("RTDirCreateTemp #3 (short)", "rtRTTemp-XX", 128, false /*fSkipXCheck*/);
121 tstDirCreateTemp("RTDirCreateTemp #4 (very short)", "rtRTTemp-X", 26+10, false /*fSkipXCheck*/);
122 tstDirCreateTemp("RTDirCreateTemp #5 (in-name)", "rtRTTemp-XXXt", 2, false /*fSkipXCheck*/);
123 tstDirCreateTemp("RTDirCreateTemp #6 (in-name)", "XXX-rtRTTemp", 2, false /*fSkipXCheck*/);
124 tstDirCreateTemp("RTDirCreateTemp #7 (in-name)", "rtRTTemp-XXXXXXXXX.tmp", 128, false /*fSkipXCheck*/);
125 tstDirCreateTemp("RTDirCreateTemp #8 (in-name)", "rtRTTemp-XXXXXXX-X.tmp", 128, true /*fSkipXCheck*/);
126 tstDirCreateTemp("RTDirCreateTemp #9 (in-name)", "rtRTTemp-XXXXXX-XX.tmp", 128, true /*fSkipXCheck*/);
127
128 /*
129 * Summary.
130 */
131 return RTTestSummaryAndDestroy(hTest);
132}
133
134
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