1 | /* $Id: tstRTVfs.cpp 57638 2015-09-07 10:59:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - IPRT Virtual File System (VFS) API
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2015 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/filesystem.h>
|
---|
32 | #include <iprt/vfs.h>
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/test.h>
|
---|
35 | #include <iprt/file.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Structures and Typedefs *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 |
|
---|
42 | static const char *StandardHandleToString(RTHANDLESTD enmHandle)
|
---|
43 | {
|
---|
44 | switch (enmHandle)
|
---|
45 | {
|
---|
46 | case RTHANDLESTD_INPUT: return "STDIN";
|
---|
47 | case RTHANDLESTD_OUTPUT: return "STDOUT";
|
---|
48 | case RTHANDLESTD_ERROR: return "STDERR";
|
---|
49 | default: break;
|
---|
50 | }
|
---|
51 |
|
---|
52 | return "unknown";
|
---|
53 | }
|
---|
54 |
|
---|
55 | static void tstVfsIoFromStandardHandle(RTTEST hTest, RTHANDLESTD enmHandle)
|
---|
56 | {
|
---|
57 | RTTestSubF(hTest, "RTVfsIoStrmFromStdHandle(%s)", StandardHandleToString(enmHandle));
|
---|
58 |
|
---|
59 | RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
60 | int rc = RTVfsIoStrmFromStdHandle(enmHandle, 0, true /*fLeaveOpen*/, &hVfsIos);
|
---|
61 | if (RT_SUCCESS(rc))
|
---|
62 | {
|
---|
63 | if ( enmHandle == RTHANDLESTD_OUTPUT
|
---|
64 | || enmHandle == RTHANDLESTD_ERROR)
|
---|
65 | {
|
---|
66 | char szTmp[80];
|
---|
67 | size_t cchTmp = RTStrPrintf(szTmp, sizeof(szTmp), "Test output to %s\n", StandardHandleToString(enmHandle));
|
---|
68 |
|
---|
69 | size_t cbWritten;
|
---|
70 | RTTESTI_CHECK_RC(rc = RTVfsIoStrmWrite(hVfsIos, szTmp, cchTmp, true /*fBlocking*/, &cbWritten), VINF_SUCCESS);
|
---|
71 | if (RT_SUCCESS(rc))
|
---|
72 | RTTESTI_CHECK(cbWritten == cchTmp);
|
---|
73 | }
|
---|
74 |
|
---|
75 | uint32_t cRefs = RTVfsIoStrmRelease(hVfsIos);
|
---|
76 | RTTESTI_CHECK_MSG(cRefs == 0, ("cRefs=%#x\n", cRefs));
|
---|
77 | }
|
---|
78 | else
|
---|
79 | RTTestFailed(hTest, "Error creating VFS I/O stream for %s: %Rrc\n", StandardHandleToString(enmHandle), rc);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 |
|
---|
84 | int main(int argc, char **argv)
|
---|
85 | {
|
---|
86 | /*
|
---|
87 | * Initialize IPRT and create the test.
|
---|
88 | */
|
---|
89 | RTTEST hTest;
|
---|
90 | int rc = RTTestInitAndCreate("tstRTVfs", &hTest);
|
---|
91 | if (rc)
|
---|
92 | return rc;
|
---|
93 | RTTestBanner(hTest);
|
---|
94 |
|
---|
95 | tstVfsIoFromStandardHandle(hTest, RTHANDLESTD_INPUT);
|
---|
96 | tstVfsIoFromStandardHandle(hTest, RTHANDLESTD_OUTPUT);
|
---|
97 | tstVfsIoFromStandardHandle(hTest, RTHANDLESTD_ERROR);
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Summary
|
---|
101 | */
|
---|
102 | return RTTestSummaryAndDestroy(hTest);
|
---|
103 | }
|
---|
104 |
|
---|