1 | /* $Id: tstRTVfs.cpp 57610 2015-09-03 13:29:20Z 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.alldomusa.eu.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 int tstVfsIoFromStandardHandle(RTTEST hTest, RTHANDLESTD enmHandle)
|
---|
56 | {
|
---|
57 | RTTestPrintf(hTest, RTTESTLVL_SUB_TEST, "Testing: %s\n", standardHandleToString(enmHandle));
|
---|
58 |
|
---|
59 | RTVFSIOSTREAM hVfs = NIL_RTVFSIOSTREAM;
|
---|
60 | int rc = RTVfsIoStrmFromStdHandle(enmHandle, 0, true /*fLeaveOpen*/, &hVfs);
|
---|
61 | if (RT_SUCCESS(rc))
|
---|
62 | {
|
---|
63 | bool fOutput = enmHandle == RTHANDLESTD_OUTPUT
|
---|
64 | || enmHandle == RTHANDLESTD_ERROR;
|
---|
65 | if (fOutput)
|
---|
66 | {
|
---|
67 | RTTestPrintf(hTest, RTTESTLVL_SUB_TEST, "Output for %s:\n", standardHandleToString(enmHandle));
|
---|
68 |
|
---|
69 | char *pszBufWritten;
|
---|
70 | int cchBuf = RTStrAPrintf(&pszBufWritten, "Testing %s\n", standardHandleToString(enmHandle));
|
---|
71 | Assert(cchBuf);
|
---|
72 | AssertPtr(pszBufWritten);
|
---|
73 |
|
---|
74 | size_t cbWritten;
|
---|
75 | rc = RTVfsIoStrmWrite(hVfs, pszBufWritten, strlen(pszBufWritten), true /*fBlocking*/, &cbWritten);
|
---|
76 | if (RT_SUCCESS(rc))
|
---|
77 | {
|
---|
78 | rc = cbWritten == strlen(pszBufWritten) ? VINF_SUCCESS : VERR_NOT_EQUAL;
|
---|
79 | /** @todo Compare written + read output. */
|
---|
80 | }
|
---|
81 |
|
---|
82 | RTStrFree(pszBufWritten);
|
---|
83 | }
|
---|
84 | else
|
---|
85 | {
|
---|
86 |
|
---|
87 | }
|
---|
88 | }
|
---|
89 | else
|
---|
90 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Error creating VFS I/O stream for %s: %Rrc\n",
|
---|
91 | standardHandleToString(enmHandle), rc);
|
---|
92 |
|
---|
93 |
|
---|
94 | if (RT_FAILURE(rc))
|
---|
95 | RTTestFailed(hTest, "Testing %s failed: %Rrc\n", standardHandleToString(enmHandle), rc);
|
---|
96 |
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 | int main(int argc, char **argv)
|
---|
101 | {
|
---|
102 | /*
|
---|
103 | * Initialize IPRT and create the test.
|
---|
104 | */
|
---|
105 | RTTEST hTest;
|
---|
106 | int rc = RTTestInitAndCreate("tstRTVfs", &hTest);
|
---|
107 | if (rc)
|
---|
108 | return rc;
|
---|
109 | RTTestBanner(hTest);
|
---|
110 |
|
---|
111 | do
|
---|
112 | {
|
---|
113 | rc = tstVfsIoFromStandardHandle(hTest, RTHANDLESTD_INPUT);
|
---|
114 | RTTESTI_CHECK_BREAK(rc == VINF_SUCCESS);
|
---|
115 |
|
---|
116 | rc = tstVfsIoFromStandardHandle(hTest, RTHANDLESTD_OUTPUT);
|
---|
117 | RTTESTI_CHECK_BREAK(rc == VINF_SUCCESS);
|
---|
118 |
|
---|
119 | rc = tstVfsIoFromStandardHandle(hTest, RTHANDLESTD_ERROR);
|
---|
120 | RTTESTI_CHECK_BREAK(rc == VINF_SUCCESS);
|
---|
121 |
|
---|
122 | } while (0);
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * Summary
|
---|
126 | */
|
---|
127 | return RTTestSummaryAndDestroy(hTest);
|
---|
128 | }
|
---|
129 |
|
---|