VirtualBox

Changeset 27613 in vbox for trunk/src/VBox/Runtime/testcase


Ignore:
Timestamp:
Mar 23, 2010 1:12:54 AM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
59162
Message:

iprt: Added RTPipeFromNative, implemented the windows version and added tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/tstRTPipe.cpp

    r27553 r27613  
    3535#include <iprt/pipe.h>
    3636
     37#include <iprt/env.h>
    3738#include <iprt/err.h>
     39#include <iprt/initterm.h>
    3840#include <iprt/mem.h>
     41#include <iprt/message.h>
     42#include <iprt/param.h>
     43#include <iprt/process.h>
    3944#include <iprt/string.h>
    4045#include <iprt/test.h>
     46
     47
     48/*******************************************************************************
     49*   Global Variables                                                           *
     50*******************************************************************************/
     51static const char g_szTest4Message[] = "This is test #4, everything is working fine.\n\r";
     52static const char g_szTest5Message[] = "This is test #5, everything is working fine.\n\r";
     53
     54
     55static RTEXITCODE tstRTPipe5Child(const char *pszPipe)
     56{
     57    int rc = RTR3Init();
     58    if (RT_FAILURE(rc))
     59        return RTMsgInitFailure(rc);
     60
     61    int64_t iNative;
     62    rc = RTStrToInt64Full(pszPipe, 10, &iNative);
     63    if (RT_FAILURE(rc))
     64        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTStrToUInt64Full(%s) -> %Rrc\n", pszPipe, rc);
     65
     66    RTPIPE hPipe;
     67    rc = RTPipeFromNative(&hPipe, (RTHCINTPTR)iNative, RTPIPE_N_READ);
     68    if (RT_FAILURE(rc))
     69        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeFromNative(,%s,READ) -> %Rrc\n", pszPipe, rc);
     70
     71    ///
     72    char    szTmp[1024];
     73    size_t  cbRead = 0;
     74    rc = RTPipeReadBlocking(hPipe, szTmp, sizeof(szTmp) - 1, &cbRead);
     75    if (RT_FAILURE(rc))
     76        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeReadBlocking() -> %Rrc\n", rc);
     77    szTmp[cbRead] = '\0';
     78
     79    size_t cbRead2;
     80    char szTmp2[4];
     81    rc = RTPipeReadBlocking(hPipe, szTmp2, sizeof(szTmp2), &cbRead2);
     82    if (rc != VERR_BROKEN_PIPE)
     83        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeReadBlocking() -> %Rrc instead of VERR_BROKEN_PIPE\n", rc);
     84
     85    rc = RTPipeClose(hPipe);
     86    if (RT_FAILURE(rc))
     87        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeClose() -> %Rrc\n", rc);
     88
     89    if (memcmp(szTmp, g_szTest5Message, sizeof(g_szTest5Message)))
     90        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Message mismatch.\n:Expected '%s'\nGot     '%s'\n", g_szTest5Message, szTmp);
     91
     92    return RTEXITCODE_SUCCESS;
     93}
     94
     95static void tstRTPipe5(void)
     96{
     97    RTTestISub("Inherit non-standard pipe handle, read end");
     98
     99    char    szPathSelf[RTPATH_MAX];
     100    RTTESTI_CHECK_RETV(RTProcGetExecutableName(szPathSelf, sizeof(szPathSelf)) == szPathSelf);
     101
     102    RTPIPE  hPipeR;
     103    RTPIPE  hPipeW;
     104    RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_READ), VINF_SUCCESS);
     105
     106    RTHCINTPTR hNative = RTPipeToNative(hPipeR);
     107    RTTESTI_CHECK_RETV(hNative != -1);
     108
     109    char szNative[64];
     110    RTStrPrintf(szNative, sizeof(szNative), "%RHi", hNative);
     111    const char *papszArgs[4] = { szPathSelf, "--child-5", szNative, NULL };
     112
     113    RTPROCESS hChild;
     114    RTTESTI_CHECK_RC_RETV(RTProcCreate(szPathSelf, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hChild), VINF_SUCCESS);
     115    RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeR), VINF_SUCCESS);
     116
     117    RTTESTI_CHECK_RC(RTPipeWriteBlocking(hPipeW, g_szTest5Message, sizeof(g_szTest5Message) - 1, NULL), VINF_SUCCESS);
     118    int rc;
     119    RTTESTI_CHECK_RC(rc = RTPipeClose(hPipeW), VINF_SUCCESS);
     120    if (RT_FAILURE(rc))
     121        RTTESTI_CHECK_RC(RTProcTerminate(hChild), VINF_SUCCESS);
     122
     123    RTPROCSTATUS ProcStatus;
     124    RTTESTI_CHECK_RC(rc = RTProcWait(hChild, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
     125    if (RT_FAILURE(rc))
     126        return;
     127    RTTESTI_CHECK(   ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
     128                  && ProcStatus.iStatus   == 0);
     129}
     130
     131
     132static RTEXITCODE tstRTPipe4Child(const char *pszPipe)
     133{
     134    int rc = RTR3Init();
     135    if (RT_FAILURE(rc))
     136        return RTMsgInitFailure(rc);
     137
     138    int64_t iNative;
     139    rc = RTStrToInt64Full(pszPipe, 10, &iNative);
     140    if (RT_FAILURE(rc))
     141        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTStrToUInt64Full(%s) -> %Rrc\n", pszPipe, rc);
     142
     143    RTPIPE hPipe;
     144    rc = RTPipeFromNative(&hPipe, (RTHCINTPTR)iNative, RTPIPE_N_WRITE);
     145    if (RT_FAILURE(rc))
     146        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeFromNative(,%s,WRITE) -> %Rrc\n", pszPipe, rc);
     147
     148    rc = RTPipeWriteBlocking(hPipe, g_szTest4Message, sizeof(g_szTest4Message) - 1, NULL);
     149    if (RT_FAILURE(rc))
     150        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeWriteBlocking() -> %Rrc\n", rc);
     151
     152    rc = RTPipeClose(hPipe);
     153    if (RT_FAILURE(rc))
     154        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeClose() -> %Rrc\n", rc);
     155    return RTEXITCODE_SUCCESS;
     156}
     157
     158static void tstRTPipe4(void)
     159{
     160    RTTestISub("Inherit non-standard pipe handle, write end");
     161
     162    char    szPathSelf[RTPATH_MAX];
     163    RTTESTI_CHECK_RETV(RTProcGetExecutableName(szPathSelf, sizeof(szPathSelf)) == szPathSelf);
     164
     165    RTPIPE  hPipeR;
     166    RTPIPE  hPipeW;
     167    RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
     168
     169    RTHCINTPTR hNative = RTPipeToNative(hPipeW);
     170    RTTESTI_CHECK_RETV(hNative != -1);
     171
     172    char szNative[64];
     173    RTStrPrintf(szNative, sizeof(szNative), "%RHi", hNative);
     174    const char *papszArgs[4] = { szPathSelf, "--child-4", szNative, NULL };
     175
     176    RTPROCESS hChild;
     177    RTTESTI_CHECK_RC_RETV(RTProcCreate(szPathSelf, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hChild), VINF_SUCCESS);
     178    RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeW), VINF_SUCCESS);
     179
     180    char    szTmp[1024];
     181    size_t  cbRead = 0;
     182    int     rc;
     183    RTTESTI_CHECK_RC(rc = RTPipeReadBlocking(hPipeR, szTmp, sizeof(szTmp) - 1, &cbRead), VINF_SUCCESS);
     184    if (RT_FAILURE(rc))
     185        cbRead = 0;
     186    RTTESTI_CHECK_RETV(cbRead < sizeof(szTmp));
     187    szTmp[cbRead] = '\0';
     188
     189    size_t cbRead2;
     190    char szTmp2[4];
     191    RTTESTI_CHECK_RC(RTPipeReadBlocking(hPipeR, szTmp2, sizeof(szTmp2), &cbRead2), VERR_BROKEN_PIPE);
     192    RTTESTI_CHECK_RC(rc = RTPipeClose(hPipeR), VINF_SUCCESS);
     193    if (RT_FAILURE(rc))
     194        RTTESTI_CHECK_RC(RTProcTerminate(hChild), VINF_SUCCESS);
     195
     196    RTPROCSTATUS ProcStatus;
     197    RTTESTI_CHECK_RC(rc = RTProcWait(hChild, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
     198    if (RT_FAILURE(rc))
     199        return;
     200    RTTESTI_CHECK(   ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
     201                  && ProcStatus.iStatus   == 0);
     202    if (memcmp(szTmp, g_szTest4Message, sizeof(g_szTest4Message)))
     203        RTTestIFailed("Message mismatch.\n:Expected '%s'\nGot     '%s'\n", g_szTest4Message, szTmp);
     204}
    41205
    42206
     
    301465}
    302466
    303 int main()
    304 {
     467int main(int argc, char **argv)
     468{
     469    if (argc == 3 && !strcmp(argv[1], "--child-4"))
     470        return tstRTPipe4Child(argv[2]);
     471    if (argc == 3 && !strcmp(argv[1], "--child-5"))
     472        return tstRTPipe5Child(argv[2]);
     473
    305474    RTTEST hTest;
    306475    int rc = RTTestInitAndCreate("tstRTPipe", &hTest);
     
    324493
    325494        tstRTPipe3();
     495        tstRTPipe4();
     496        tstRTPipe5();
    326497    }
    327498
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette