VirtualBox

Ignore:
Timestamp:
Sep 9, 2015 5:28:16 PM (9 years ago)
Author:
vboxsync
Message:

tstSSLCertDownloads: Don't use friend, use a static class function instead as that is simpler and shorter. Test the refreshCertificates method too, expecting it succeed without downloading anything.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/net
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkReply.cpp

    r57676 r57677  
    107107    static unsigned countCertsFound(bool const *pafFoundCerts);
    108108    static bool areAllCertsFound(bool const *pafFoundCerts);
    109     static void refreshCertificates(RTHTTP hHttp, RTCRSTORE hOldStore, bool *pafFoundCerts, const char *pszCaCertFile);
     109    static int refreshCertificates(RTHTTP hHttp, PRTCRSTORE phStore, bool *pafFoundCerts, const char *pszCaCertFile);
    110110    static void downloadMissingCertificates(RTCRSTORE hNewStore, bool *pafNewFoundCerts, RTHTTP hHttp,
    111111                                            PRTERRINFOSTATIC pStaticErrInfo);
     
    130130
    131131#ifdef VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS
    132     friend UINetworkReplyPrivateThreadTestcase;
     132public:
     133    static void testIt(RTTEST hTest);
    133134#endif
    134135};
     
    308309         */
    309310        if (fRefresh)
    310             refreshCertificates(m_hHttp, hCurStore, afCertsFound, pszCaCertFile);
     311            refreshCertificates(m_hHttp, &hCurStore, afCertsFound, pszCaCertFile);
    311312
    312313        RTCrStoreRelease(hCurStore);
     
    465466}
    466467
    467 /*static*/ void
    468 UINetworkReplyPrivateThread::refreshCertificates(RTHTTP hHttp, RTCRSTORE hOldStore, bool *pafOldFoundCerts,
     468/**
     469 * Refresh the certificates.
     470 *
     471 * @return  IPRT status code for the testcase.
     472 * @param   hHttp               The HTTP client instance.  (Can be NIL when
     473 *                              running the testcase.)
     474 * @param   phStore             On input, this holds the current store, so that
     475 *                              we can fish out wanted certificates from it.
     476 *                              On successful return, this is replaced with a
     477 *                              new store reflecting the refrehsed content of
     478 *                              @a pszCaCertFile.
     479 * @param   pafFoundCerts       On input, this holds the certificates found in
     480 *                              the current store.  On return, this reflects
     481 *                              what is current in the @a pszCaCertFile.  The
     482 *                              array runs parallel to s_aCerts.
     483 * @param   pszCaCertFile       Where to write the refreshed certificates if
     484 *                              we've managed to gather a collection that is at
     485 *                              least as good as the old one.
     486 */
     487/*static*/ int
     488UINetworkReplyPrivateThread::refreshCertificates(RTHTTP hHttp, PRTCRSTORE phStore, bool *pafFoundCerts,
    469489                                                 const char *pszCaCertFile)
    470490{
     
    472492     * Collect the standard assortment of SSL certificates.
    473493     */
    474     uint32_t  cHint = RTCrStoreCertCount(hOldStore);
     494    uint32_t  cHint = RTCrStoreCertCount(*phStore);
    475495    RTCRSTORE hNewStore;
    476496    int rc = RTCrStoreCreateInMem(&hNewStore, cHint > 32 && cHint < _32K ? cHint + 16 : 256);
     
    501521                rc = RTCrStoreCertAddWantedFromStore(hNewStore,
    502522                                                     RTCRCERTCTX_F_ADD_IF_NOT_FOUND | RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR,
    503                                                      hOldStore, s_aCerts, RT_ELEMENTS(s_aCerts), afNewFoundCerts);
     523                                                     *phStore, s_aCerts, RT_ELEMENTS(s_aCerts), afNewFoundCerts);
    504524                AssertLogRelRC(rc);
    505525                Assert(rc != VINF_SUCCESS || areAllCertsFound(afNewFoundCerts));
     
    525545             * If that didn't help, try download the certificates.
    526546             */
    527             if (rc != VINF_SUCCESS)
     547            if (rc != VINF_SUCCESS && hHttp != NIL_RTHTTP)
    528548                downloadMissingCertificates(hNewStore, afNewFoundCerts, hHttp, &StaticErrInfo);
    529549
     
    533553             */
    534554            if (   areAllCertsFound(afNewFoundCerts)
    535                 || countCertsFound(afNewFoundCerts) >= countCertsFound(pafOldFoundCerts) )
     555                || countCertsFound(afNewFoundCerts) >= countCertsFound(pafFoundCerts) )
    536556            {
    537557                rc = RTCrStoreCertExportAsPem(hNewStore, 0 /*fFlags*/, pszCaCertFile);
    538558                if (RT_SUCCESS(rc))
    539559                {
    540                     memcpy(pafOldFoundCerts, afNewFoundCerts, sizeof(afNewFoundCerts));
    541560                    LogRel(("refreshCertificates/#3: Found %u/%u SSL certs we/you trust (previously %u/%u).\n",
    542561                            countCertsFound(afNewFoundCerts), RTCrStoreCertCount(hNewStore),
    543                             countCertsFound(pafOldFoundCerts), RTCrStoreCertCount(hOldStore) ));
     562                            countCertsFound(pafFoundCerts), RTCrStoreCertCount(*phStore) ));
     563
     564                    memcpy(pafFoundCerts, afNewFoundCerts, sizeof(afNewFoundCerts));
     565                    RTCrStoreRelease(*phStore);
     566                    *phStore  = hNewStore;
     567                    hNewStore = NIL_RTCRSTORE;
    544568                }
    545569                else
    546570                {
    547                     RT_ZERO(pafOldFoundCerts);
     571                    RT_ZERO(pafFoundCerts);
    548572                    LogRel(("refreshCertificates/#3: RTCrStoreCertExportAsPem unexpectedly failed with %Rrc\n", rc));
    549573                }
     
    554578        RTCrStoreRelease(hNewStore);
    555579    }
     580    return rc;
    556581}
    557582
     
    697722}
    698723
     724#ifndef VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS
    699725
    700726/**
     
    803829
    804830
    805 
    806 /*
    807  *
    808  * Class UINetworkReply implementation.
    809  * Class UINetworkReply implementation.
    810  * Class UINetworkReply implementation.
    811  *
    812  */
    813 
    814 #ifndef VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS
     831/*********************************************************************************************************************************
     832*   Class UINetworkReply implementation.                                                                                         *
     833*********************************************************************************************************************************/
    815834
    816835UINetworkReply::UINetworkReply(const QNetworkRequest &request, UINetworkRequestType requestType)
  • trunk/src/VBox/Frontends/VirtualBox/src/net/tstSSLCertDownloads.cpp

    r57676 r57677  
    3434
    3535
    36 class UINetworkReplyPrivateThreadTestcase
    37 {
    38 public:
    39     UINetworkReplyPrivateThreadTestcase()
    40     {
    41     }
    42 
    43     static void testIt(RTTEST hTest);
    44 };
    45 
    46 
    4736/*
    4837 * It's a private class we're testing, so we must include the source.
     
    5342
    5443
    55 /*static*/ void UINetworkReplyPrivateThreadTestcase::testIt(RTTEST hTest)
     44/*static*/ void UINetworkReplyPrivateThread::testIt(RTTEST hTest)
    5645{
    5746    QNetworkRequest Dummy;
     
    6958     * PEM URL (as the rest are currently busted).
    7059     */
     60    RTTestISub("SSL Cert downloading");
    7161    RTCRSTORE hStore;
    72     RTTESTI_CHECK_RC(RTCrStoreCreateInMem(&hStore, RT_ELEMENTS(TestObj.s_aCerts) * 2), VINF_SUCCESS);
     62    RTTESTI_CHECK_RC(RTCrStoreCreateInMem(&hStore, RT_ELEMENTS(s_aCerts) * 2), VINF_SUCCESS);
    7363
    7464    int rc;
    7565
    7666    /* ZIP files: */
    77     for (uint32_t iUrl = 0; iUrl < RT_ELEMENTS(TestObj.s_apszRootsZipUrls); iUrl++)
     67    for (uint32_t iUrl = 0; iUrl < RT_ELEMENTS(s_apszRootsZipUrls); iUrl++)
    7868    {
    79         RTTestIPrintf(RTTESTLVL_ALWAYS, "URL: %s\n", TestObj.s_apszRootsZipUrls[iUrl]);
     69        RTTestIPrintf(RTTESTLVL_ALWAYS, "URL: %s\n", s_apszRootsZipUrls[iUrl]);
    8070        void   *pvRootsZip;
    8171        size_t  cbRootsZip;
    82         rc = RTHttpGetBinary(TestObj.m_hHttp, TestObj.s_apszRootsZipUrls[iUrl], &pvRootsZip, &cbRootsZip);
     72        rc = RTHttpGetBinary(TestObj.m_hHttp, s_apszRootsZipUrls[iUrl], &pvRootsZip, &cbRootsZip);
    8373        if (RT_SUCCESS(rc))
    8474        {
    85             for (uint32_t i = 0; i < RT_ELEMENTS(TestObj.s_aCerts); i++)
     75            for (uint32_t i = 0; i < RT_ELEMENTS(s_aCerts); i++)
    8676            {
    87                 UINetworkReplyPrivateThread::CERTINFO const *pInfo;
    88                 pInfo = (UINetworkReplyPrivateThread::CERTINFO const *)TestObj.s_aCerts[i].pvUser;
     77                CERTINFO const *pInfo = (CERTINFO const *)s_aCerts[i].pvUser;
    8978                if (pInfo->pszZipFile)
    9079                {
     
    9382                    RTTESTI_CHECK_RC_BREAK(RTZipPkzipMemDecompress(&pvFile, &cbFile, pvRootsZip, cbRootsZip, pInfo->pszZipFile),
    9483                                           VINF_SUCCESS);
    95                     RTTESTI_CHECK_RC(TestObj.convertVerifyAndAddPemCertificateToStore(hStore, pvFile, cbFile,
    96                                                                                       &TestObj.s_aCerts[i]), VINF_SUCCESS);
     84                    RTTESTI_CHECK_RC(convertVerifyAndAddPemCertificateToStore(hStore, pvFile, cbFile, &TestObj.s_aCerts[i]),
     85                                     VINF_SUCCESS);
    9786                    RTMemFree(pvFile);
    9887                }
     
    10291        else if (   rc != VERR_HTTP_PROXY_NOT_FOUND
    10392                 && rc != VERR_HTTP_COULDNT_CONNECT)
    104             RTTestIFailed("%Rrc on '%s'", rc, TestObj.s_apszRootsZipUrls[iUrl]); /* code or link broken */
     93            RTTestIFailed("%Rrc on '%s'", rc, s_apszRootsZipUrls[iUrl]); /* code or link broken */
    10594    }
    10695
    10796    /* PEM files: */
    108     for (uint32_t i = 0; i < RT_ELEMENTS(TestObj.s_aCerts); i++)
     97    for (uint32_t i = 0; i < RT_ELEMENTS(s_aCerts); i++)
    10998    {
    11099        uint32_t iUrl = 0; /* First URL must always work. */
    111100        UINetworkReplyPrivateThread::CERTINFO const *pInfo;
    112         pInfo = (UINetworkReplyPrivateThread::CERTINFO const *)TestObj.s_aCerts[i].pvUser;
     101        pInfo = (UINetworkReplyPrivateThread::CERTINFO const *)s_aCerts[i].pvUser;
    113102        if (pInfo->apszUrls[iUrl])
    114103        {
     
    119108            if (RT_SUCCESS(rc))
    120109            {
    121                 RTTESTI_CHECK_RC_OK(TestObj.convertVerifyAndAddPemCertificateToStore(hStore, pvResponse, cbResponse,
    122                                                                                      &TestObj.s_aCerts[i]));
     110                RTTESTI_CHECK_RC_OK(convertVerifyAndAddPemCertificateToStore(hStore, pvResponse, cbResponse,
     111                                                                             &TestObj.s_aCerts[i]));
    123112                RTHttpFreeResponse(pvResponse);
    124113            }
     
    128117        }
    129118    }
     119
     120    RTTESTI_CHECK(RTCrStoreRelease(hStore) == 0);
     121
     122    /*
     123     * Now check the gathering of certificates on the system doesn't crash.
     124     */
     125    RTTestISub("Refreshing certificates");
     126
     127    /* create an empty store and do the refresh operation, writing it to /dev/null. */
     128    RTTESTI_CHECK_RC(RTCrStoreCreateInMem(&hStore, RT_ELEMENTS(s_aCerts) * 2), VINF_SUCCESS);
     129    bool afFoundCerts[RT_ELEMENTS(s_aCerts)];
     130    RT_ZERO(afFoundCerts);
     131#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
     132    RTTESTI_CHECK_RC_OK(TestObj.refreshCertificates(NIL_RTHTTP, &hStore, afFoundCerts, "nul"));
     133#else
     134    RTTESTI_CHECK_RC_OK(TestObj.refreshCertificates(NIL_RTHTTP, &hStore, afFoundCerts, "/dev/null"));
     135#endif
     136
     137    /* Log how many certificates we found and require at least one. */
     138    uint32_t cCerts = RTCrStoreCertCount(hStore);
     139    RTTestIValue("certificates", cCerts, RTTESTUNIT_NONE);
     140    RTTESTI_CHECK(cCerts > 0);
    130141
    131142    RTTESTI_CHECK(RTCrStoreRelease(hStore) == 0);
     
    143154{
    144155    RTTEST hTest;
    145     RTEXITCODE rcExit = RTTestInitAndCreate("tstCertDownloads", &hTest);
     156    RTEXITCODE rcExit = RTTestInitAndCreate("tstSSLCertDownloads", &hTest);
    146157    if (rcExit != RTEXITCODE_SUCCESS)
    147158        return rcExit;
    148159    RTTestBanner(hTest);
    149160
    150     UINetworkReplyPrivateThreadTestcase::testIt(hTest);
     161    UINetworkReplyPrivateThread::testIt(hTest);
    151162
    152163    return RTTestSummaryAndDestroy(hTest);
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