VirtualBox

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


Ignore:
Timestamp:
Sep 27, 2015 8:38:16 PM (9 years ago)
Author:
vboxsync
Message:

tstRTProcCreateEx: Use RTProcQueryUsernameA to get the the user name - that works everywhere. Drop the windows specific fun.

File:
1 edited

Legend:

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

    r57906 r57917  
    339339static int tstRTCreateProcEx5Child(int argc, char **argv)
    340340{
    341     int rc = RTR3InitExeNoArguments(0);
     341    int rc = RTR3InitExe(argc, &argv, 0);
    342342    if (RT_FAILURE(rc))
    343343        return RTMsgInitFailure(rc);
    344344
     345    uint32_t cErrors = 0;
     346
     347    /* Check that the OS thinks we're running as the user we're supposed to. */
     348    char *pszUser;
     349    rc = RTProcQueryUsernameA(NIL_RTPROCESS, &pszUser);
     350    if (RT_SUCCESS(rc))
     351    {
    345352#ifdef RT_OS_WINDOWS
    346     char szUser[_1K];
    347     DWORD cbLen = sizeof(szUser);
    348     /** @todo Does not yet handle ERROR_MORE_DATA for user names longer than 32767. */
    349     if (!GetUserName(szUser, &cbLen))
    350     {
    351         RTPrintf("GetUserName failed with last error=%ld\n", GetLastError());
    352         return RTEXITCODE_FAILURE;
    353     }
    354 # if 0 /* Does not work on NT4 (yet). */
    355     DWORD cbSid = 0;
    356     DWORD cbDomain = 0;
    357     SID_NAME_USE sidUse;
    358     /* First try to figure out how much space for SID + domain name we need. */
    359     BOOL bRet = LookupAccountName(NULL /* current system*/,
    360                                   szUser,
    361                                   NULL,
    362                                   &cbSid,
    363                                   NULL,
    364                                   &cbDomain,
    365                                   &sidUse);
    366     if (!bRet)
    367     {
    368         DWORD dwErr = GetLastError();
    369         if (dwErr != ERROR_INSUFFICIENT_BUFFER)
    370         {
    371             RTPrintf("LookupAccountName(1) failed with last error=%ld\n", dwErr);
    372             return RTEXITCODE_FAILURE;
    373         }
    374     }
    375 
    376     /* Now try getting the real SID + domain name. */
    377     SID *pSid = (SID *)RTMemAlloc(cbSid);
    378     AssertPtr(pSid);
    379     char *pszDomain = (char *)RTMemAlloc(cbDomain); /* Size in TCHAR! */
    380     AssertPtr(pszDomain);
    381 
    382     if (!LookupAccountName(NULL /* Current system */,
    383                            szUser,
    384                            pSid,
    385                            &cbSid,
    386                            pszDomain,
    387                            &cbDomain,
    388                            &sidUse))
    389     {
    390         RTPrintf("LookupAccountName(2) failed with last error=%ld\n", GetLastError());
    391         return RTEXITCODE_FAILURE;
    392     }
    393     RTMemFree(pSid);
    394     RTMemFree(pszDomain);
    395 # endif
     353        if (RTStrICmp(pszUser, argv[2]) != 0)
    396354#else
    397     /** @todo Lookup UID/effective UID, maybe GID? */
     355        if (RTStrCmp(pszUser, argv[2]) != 0)
    398356#endif
    399     return RTEXITCODE_SUCCESS;
     357        {
     358            RTStrmPrintf(g_pStdErr, "child4: user name is '%s', expected '%s'\n", pszUser, argv[2]);
     359            cErrors++;
     360        }
     361        RTStrFree(pszUser);
     362    }
     363    else
     364    {
     365        RTStrmPrintf(g_pStdErr, "child4: RTProcQueryUsernameA failed: %Rrc\n", rc);
     366        cErrors++;
     367    }
     368
     369    return cErrors == 0 ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
    400370}
    401371
     
    403373{
    404374    RTTestISubF("As user \"%s\" with password \"%s\"", pszUser, pszPassword);
    405 
    406     const char * apszArgs[3] =
     375    RTTESTI_CHECK_RETV(pszUser && *pszUser);
     376
     377    const char * apszArgs[] =
    407378    {
    408379        "test", /* user name */
    409380        "--testcase-child-5",
     381        pszUser,
    410382        NULL
    411383    };
    412384
     385    /* Test for invalid logons. */
    413386    RTPROCESS hProc;
    414 
    415     /* Test for invalid logons. */
    416387    int rc = RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL, NULL, NULL,
    417388                            "non-existing-user", "wrong-password", &hProc);
     
    422393    RTTESTI_CHECK_RC(RTProcCreateEx("non-existing-app", apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
    423394                                    NULL, NULL, NULL, NULL, &hProc), VERR_FILE_NOT_FOUND);
     395
    424396    /* Test a (hopefully) valid user/password logon (given by parameters of this function). */
    425397    RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
     
    679651    if (argc >= 5 && !strcmp(argv[1], "--testcase-child-4"))
    680652        return tstRTCreateProcEx4Child(argc, argv);
    681     if (argc == 2 && !strcmp(argv[1], "--testcase-child-5"))
     653    if (argc >= 2 && !strcmp(argv[1], "--testcase-child-5"))
    682654        return tstRTCreateProcEx5Child(argc, argv);
    683655    if (argc >= 2 && !strcmp(argv[1], "--testcase-child-6"))
    684656        return tstRTCreateProcEx6Child(argc, argv);
    685 
    686657
    687658    /*
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