VirtualBox

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


Ignore:
Timestamp:
Jul 16, 2013 12:37:23 PM (12 years ago)
Author:
vboxsync
Message:

IPRT/localipc-win.cpp: Update; enhanced testcase, now is passing completely.

File:
1 edited

Legend:

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

    r47168 r47185  
    3434#include <iprt/path.h>
    3535#include <iprt/process.h>
     36#include <iprt/rand.h>
    3637#include <iprt/string.h>
    3738#include <iprt/test.h>
     
    328329} LOCALIPCTESTMSG, *PLOCALIPCTESTMSG;
    329330
    330 static DECLCALLBACK(int) testSessionDataThread(RTTHREAD hSelf, void *pvUser)
    331 {
    332     PLOCALIPCTHREADCTX pCtx = (PLOCALIPCTHREADCTX)pvUser;
     331static int testSessionDataReadTestMsg(RTTEST hTest, RTLOCALIPCSESSION hSession,
     332                                      void *pvBuffer, size_t cbBuffer, const char *pszMsg)
     333{
     334    AssertPtrReturn(pvBuffer, VERR_INVALID_POINTER);
     335    AssertPtrReturn(pszMsg, VERR_INVALID_POINTER);
     336
     337    void *pvBufCur = pvBuffer;
     338    size_t cbReadTotal = 0;
     339    for (;;)
     340    {
     341        size_t cbRead = RTRandU32Ex(1, sizeof(LOCALIPCTESTMSG) - cbReadTotal); /* Force a bit of fragmentation. */
     342        RTTEST_CHECK_BREAK(hTest, cbRead);
     343        RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionRead(hSession, pvBufCur,
     344                                                           cbBuffer,
     345                                                           &cbRead), VINF_SUCCESS);
     346        RTTEST_CHECK_BREAK(hTest, cbRead);
     347        pvBufCur     = (uint8_t *)pvBufCur + cbRead; /* Advance. */
     348        cbReadTotal += cbRead;
     349        RTTEST_CHECK_BREAK(hTest, cbReadTotal <= cbBuffer);
     350        if (cbReadTotal >= sizeof(LOCALIPCTESTMSG)) /* Got a complete test message? */
     351        {
     352            RTTEST_CHECK_BREAK(hTest, cbReadTotal == sizeof(LOCALIPCTESTMSG));
     353            PLOCALIPCTESTMSG pMsg = (PLOCALIPCTESTMSG)pvBuffer;
     354            RTTEST_CHECK_BREAK(hTest, pMsg != NULL);
     355            RTTEST_CHECK_BREAK(hTest, !RTStrCmp(pMsg->szOp, pszMsg));
     356            break;
     357        }
     358        /* Try receiving next part of the message in another round. */
     359    }
     360
     361    return !RTTestErrorCount(hTest) ? VINF_SUCCESS : VERR_GENERAL_FAILURE /* Doesn't matter */;
     362}
     363
     364static int testSessionDataThreadWorker(PLOCALIPCTHREADCTX pCtx)
     365{
    333366    AssertPtr(pCtx);
     367
     368    size_t cbScratchBuf = _1K; /** @todo Make this random in future. */
     369    uint8_t *pvScratchBuf = (uint8_t*)RTMemAlloc(cbScratchBuf);
     370    RTTEST_CHECK_RET(pCtx->hTest, pvScratchBuf != NULL, VERR_NO_MEMORY);
    334371
    335372    do
     
    337374        /* Note: At the moment we only support one client per run. */
    338375        RTTestPrintf(pCtx->hTest, RTTESTLVL_INFO, "testSessionDataThread: Listening for incoming connections ...\n");
    339         RTLOCALIPCSESSION ipcSession;
    340         RTTEST_CHECK_RC_BREAK(pCtx->hTest, RTLocalIpcServerListen(pCtx->hServer, &ipcSession), VINF_SUCCESS);
     376        RTLOCALIPCSESSION hSession;
     377        RTTEST_CHECK_RC_BREAK(pCtx->hTest, RTLocalIpcServerListen(pCtx->hServer, &hSession), VINF_SUCCESS);
    341378        RTTestPrintf(pCtx->hTest, RTTESTLVL_INFO, "testSessionDataThread: Got new client connection\n");
    342379        uint32_t cRounds = 256; /** @todo Use RTRand(). */
    343         RTTEST_CHECK_RC_BREAK(pCtx->hTest, RTLocalIpcSessionWrite(ipcSession, &cRounds, sizeof(cRounds)), VINF_SUCCESS);
     380        /* Write how many rounds we're going to send data. */
     381        RTTEST_CHECK_RC_BREAK(pCtx->hTest, RTLocalIpcSessionWrite(hSession, &cRounds, sizeof(cRounds)), VINF_SUCCESS);
    344382        RTTestPrintf(pCtx->hTest, RTTESTLVL_INFO, "testSessionDataThread: Written number of rounds\n");
    345383        for (uint32_t i = 0; i < cRounds; i++)
     
    348386            RTTEST_CHECK_BREAK(pCtx->hTest, RTStrPrintf(msg.szOp, sizeof(msg.szOp),
    349387                                                        "YayIGotRound%RU32FromTheServer", i) > 0);
    350             RTTEST_CHECK_RC_BREAK(pCtx->hTest, RTLocalIpcSessionWrite(ipcSession, &msg, sizeof(msg)), VINF_SUCCESS);
    351         }
    352         RTThreadSleep(10 * 1000); /* Fudge. */
    353         RTTEST_CHECK_RC_BREAK(pCtx->hTest, RTLocalIpcSessionClose(ipcSession), VINF_SUCCESS);
    354         RTTestPrintf(pCtx->hTest, RTTESTLVL_INFO, "testSessionDataThread: Data successfully written\n");
     388            RTTEST_CHECK_RC_BREAK(pCtx->hTest, RTLocalIpcSessionWrite(hSession, &msg, sizeof(msg)), VINF_SUCCESS);
     389        }
     390        if (!RTTestErrorCount(pCtx->hTest))
     391            RTTestPrintf(pCtx->hTest, RTTESTLVL_INFO, "testSessionDataThread: Data successfully written\n");
     392        /* Try to receive the same amount of rounds from the client. */
     393        for (uint32_t i = 0; i < cRounds; i++)
     394        {
     395            RTTEST_CHECK_RC_BREAK(pCtx->hTest, RTLocalIpcSessionWaitForData(hSession, RT_INDEFINITE_WAIT),
     396                                  VINF_SUCCESS);
     397            char szMsg[32];
     398            RTTEST_CHECK_BREAK(pCtx->hTest, RTStrPrintf(szMsg, sizeof(szMsg), "YayIGotRound%RU32FromTheClient", i) > 0);
     399            RTTEST_CHECK_RC_BREAK(pCtx->hTest, testSessionDataReadTestMsg(pCtx->hTest, hSession,
     400                                                                          pvScratchBuf, cbScratchBuf,
     401                                                                          szMsg), VINF_SUCCESS);
     402            if (RTTestErrorCount(pCtx->hTest))
     403                break;
     404        }
     405        if (!RTTestErrorCount(pCtx->hTest))
     406            RTTestPrintf(pCtx->hTest, RTTESTLVL_INFO, "testSessionDataThread: Data successfully read\n");
     407        RTTEST_CHECK_RC_BREAK(pCtx->hTest, RTLocalIpcSessionClose(hSession), VINF_SUCCESS);
    355408
    356409    } while (0);
    357410
     411    RTMemFree(pvScratchBuf);
    358412    return !RTTestErrorCount(pCtx->hTest) ? VINF_SUCCESS : VERR_GENERAL_FAILURE /* Doesn't matter */;
    359413}
    360414
    361 static RTEXITCODE testSessionDataChild(int argc, char **argv, RTTEST hTest)
    362 {
    363     uint8_t *pvScratchBuf = (uint8_t*)RTMemAlloc(_1K);
     415static DECLCALLBACK(int) testSessionDataThread(RTTHREAD hSelf, void *pvUser)
     416{
     417    PLOCALIPCTHREADCTX pCtx = (PLOCALIPCTHREADCTX)pvUser;
     418    AssertPtr(pCtx);
     419
     420    return testSessionDataThreadWorker(pCtx);
     421}
     422
     423static int testSessionDataChildWorker(RTTEST hTest)
     424{
     425    size_t cbScratchBuf = _1K; /** @todo Make this random in future. */
     426    uint8_t *pvScratchBuf = (uint8_t*)RTMemAlloc(cbScratchBuf);
    364427    RTTEST_CHECK_RET(hTest, pvScratchBuf != NULL, RTEXITCODE_FAILURE);
    365428
     
    367430    {
    368431        RTThreadSleep(2000); /* Fudge. */
    369         RTLOCALIPCSESSION clientSession;
    370         RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionConnect(&clientSession, "tstRTLocalIpcSessionData",
     432        RTLOCALIPCSESSION hSession;
     433        RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionConnect(&hSession, "tstRTLocalIpcSessionData",
    371434                                                              0 /* Flags */), VINF_SUCCESS);
    372435        /* Get number of rounds we want to read/write. */
    373         uint32_t cRounds = 0; size_t cbRead;
    374         RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionWaitForData(clientSession, RT_INDEFINITE_WAIT),
     436        uint32_t cRounds = 0;
     437        RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionWaitForData(hSession, RT_INDEFINITE_WAIT),
    375438                                                                  VINF_SUCCESS);
    376         RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionRead(clientSession, &cRounds, sizeof(cRounds),
     439        RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionRead(hSession, &cRounds, sizeof(cRounds),
    377440                                                           NULL /* Get exactly sizeof(cRounds) bytes */), VINF_SUCCESS);
    378441        RTTEST_CHECK_BREAK(hTest, cRounds == 256); /** @todo Check for != 0 when using RTRand(). */
    379         /* Process all rounds. */
    380         size_t uOffScratchBuf = 0;
    381         char szStr[32];
    382         for (uint32_t i = 0; i < 1; cRounds++)
    383         {
    384             RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionWaitForData(clientSession, RT_INDEFINITE_WAIT),
     442        /* Receive all rounds. */
     443        for (uint32_t i = 0; i < cRounds; i++)
     444        {
     445            RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionWaitForData(hSession, RT_INDEFINITE_WAIT),
    385446                                                                      VINF_SUCCESS);
    386             RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionRead(clientSession, pvScratchBuf,
    387                                                                sizeof(*pvScratchBuf) - uOffScratchBuf,
    388                                                                &cbRead), VINF_SUCCESS);
    389 
    390             uOffScratchBuf += cbRead;
    391             RTTEST_CHECK_BREAK(hTest, uOffScratchBuf <= sizeof(*pvScratchBuf));
    392             if (uOffScratchBuf >= sizeof(LOCALIPCTESTMSG)) /* Got a complete test message? */
    393             {
    394                 PLOCALIPCTESTMSG pMsg = (PLOCALIPCTESTMSG)pvScratchBuf;
    395                 RTTEST_CHECK_BREAK(hTest, pMsg != NULL);
    396                 RTTEST_CHECK_BREAK(hTest, RTStrPrintf(szStr, sizeof(szStr), "YayIGotRound%RU32FromTheServer", i) > 0);
    397                 RTTEST_CHECK_BREAK(hTest, !RTStrICmp(pMsg->szOp, szStr));
    398                 memcpy(pvScratchBuf, (void*)pvScratchBuf[sizeof(LOCALIPCTESTMSG)], cbRead);
    399             }
     447            char szMsg[32];
     448            RTTEST_CHECK_BREAK(hTest, RTStrPrintf(szMsg, sizeof(szMsg), "YayIGotRound%RU32FromTheServer", i) > 0);
     449            RTTEST_CHECK_RC_BREAK(hTest, testSessionDataReadTestMsg(hTest, hSession,
     450                                                                    pvScratchBuf, cbScratchBuf,
     451                                                                    szMsg), VINF_SUCCESS);
    400452            if (RTTestErrorCount(hTest))
    401453                break;
    402454        }
    403         RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionClose(clientSession), VINF_SUCCESS);
     455        /* Send all rounds back to the server. */
     456        for (uint32_t i = 0; i < cRounds; i++)
     457        {
     458            LOCALIPCTESTMSG msg;
     459            RTTEST_CHECK_BREAK(hTest, RTStrPrintf(msg.szOp, sizeof(msg.szOp),
     460                                                  "YayIGotRound%RU32FromTheClient", i) > 0);
     461            RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionWrite(hSession, &msg, sizeof(msg)), VINF_SUCCESS);
     462        }
     463        RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionClose(hSession), VINF_SUCCESS);
    404464
    405465    } while (0);
    406466
    407467    RTMemFree(pvScratchBuf);
    408     return !RTTestErrorCount(hTest) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
     468    return !RTTestErrorCount(hTest) ? VINF_SUCCESS : VERR_GENERAL_FAILURE /* Doesn't matter */;
     469}
     470
     471static DECLCALLBACK(int) testSessionDataChildAsThread(RTTHREAD hSelf, void *pvUser)
     472{
     473    PRTTEST phTest = (PRTTEST)pvUser;
     474    AssertPtr(phTest);
     475    return testSessionDataChildWorker(*phTest);
     476}
     477
     478static RTEXITCODE testSessionDataChild(int argc, char **argv, RTTEST hTest)
     479{
     480    return RT_SUCCESS(testSessionDataChildWorker(hTest)) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
    409481}
    410482
     
    419491    {
    420492        LOCALIPCTHREADCTX threadCtx = { ipcServer, hTest };
    421 
     493#if 0
     494        /* Run server + client in threads instead of fork'ed processes (useful for debugging). */
     495        RTTHREAD hThreadServer, hThreadClient;
     496        rc = RTThreadCreate(&hThreadServer, testSessionDataThread,
     497                            &threadCtx, 0 /* Stack */, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tstIpc4");
     498        if (RT_SUCCESS(rc))
     499            rc = RTThreadCreate(&hThreadClient, testSessionDataChildAsThread,
     500                                &hTest, 0 /* Stack */, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tstIpc5");
     501        if (RT_SUCCESS(rc))
     502        {
     503            do
     504            {
     505                int threadRc;
     506                RTTEST_CHECK_RC(hTest, RTThreadWait(hThreadServer,
     507                                                    5 * 60 * 1000 /* 5 minutes timeout */, &threadRc), VINF_SUCCESS);
     508                RTTEST_CHECK_RC_BREAK(hTest, threadRc, VINF_SUCCESS);
     509                RTTEST_CHECK_RC(hTest, RTThreadWait(hThreadClient,
     510                                                    5 * 60 * 1000 /* 5 minutes timeout */, &threadRc), VINF_SUCCESS);
     511                RTTEST_CHECK_RC_BREAK(hTest, threadRc, VINF_SUCCESS);
     512
     513            } while (0);
     514        }
     515#else
    422516        /* Spawn a simple worker thread and let it listen for incoming connections.
    423517         * In the meanwhile we try to cancel the server and see what happens. */
     
    451545        else
    452546            RTTestFailed(hTest, "Unable to create thread for cancelling server, rc=%Rrc\n", rc);
     547#endif
    453548    }
    454549    else
    455550        RTTestFailed(hTest, "Unable to create IPC server, rc=%Rrc\n", rc);
    456551
    457     return VINF_SUCCESS;
     552    return !RTTestErrorCount(hTest) ? VINF_SUCCESS : VERR_GENERAL_FAILURE /* Doesn't matter */;
    458553}
    459554
     
    536631    if (RTTestErrorCount(hTest) == 0)
    537632    {
    538         /*RTTESTI_CHECK_RC_RET(testServerListenAndCancel(hTest, szExecPath), VINF_SUCCESS, 1);
     633        RTTESTI_CHECK_RC_RET(testServerListenAndCancel(hTest, szExecPath), VINF_SUCCESS, 1);
    539634        RTTESTI_CHECK_RC_RET(testSessionConnection(hTest, szExecPath), VINF_SUCCESS, 1);
    540         RTTESTI_CHECK_RC_RET(testSessionWait(hTest, szExecPath), VINF_SUCCESS, 1);*/
     635        RTTESTI_CHECK_RC_RET(testSessionWait(hTest, szExecPath), VINF_SUCCESS, 1);
    541636        RTTESTI_CHECK_RC_RET(testSessionData(hTest, szExecPath), VINF_SUCCESS, 1);
    542637    }
Note: See TracChangeset for help on using the changeset viewer.

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