1 | /* $Id: tstSSLCertDownloads.cpp 58252 2015-10-14 15:51:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Simple cURL testcase.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.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/err.h>
|
---|
32 | #include <iprt/mem.h>
|
---|
33 | #include <iprt/test.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | /*
|
---|
37 | * It's a private class we're testing, so we must include the source.
|
---|
38 | * Better than emedding the testcase as public function in the class.
|
---|
39 | */
|
---|
40 | #include "UINetworkReply.cpp"
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 | /*static*/ void UINetworkReplyPrivateThread::testIt(RTTEST hTest)
|
---|
45 | {
|
---|
46 | QNetworkRequest Dummy;
|
---|
47 | UINetworkReplyPrivateThread TestObj(Dummy, UINetworkRequestType_GET);
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * Do the first setup things that UINetworkReplyPrivateThread::run.
|
---|
51 | */
|
---|
52 | RTTESTI_CHECK_RC(RTHttpCreate(&TestObj.m_hHttp), VINF_SUCCESS);
|
---|
53 | RTTESTI_CHECK_RC(TestObj.applyProxyRules(), VINF_SUCCESS);
|
---|
54 |
|
---|
55 | /*
|
---|
56 | * Duplicate much of downloadMissingCertificates, but making sure we
|
---|
57 | * can both get the certificate from the ZIP file(s) and the first
|
---|
58 | * PEM URL (as the rest are currently busted).
|
---|
59 | */
|
---|
60 | RTTestISub("SSL Cert downloading");
|
---|
61 | RTCRSTORE hStore;
|
---|
62 | RTTESTI_CHECK_RC(RTCrStoreCreateInMem(&hStore, RT_ELEMENTS(s_aCerts) * 2), VINF_SUCCESS);
|
---|
63 |
|
---|
64 | int rc;
|
---|
65 |
|
---|
66 | /* ZIP files: */
|
---|
67 | for (uint32_t iUrl = 0; iUrl < RT_ELEMENTS(s_apszRootsZipUrls); iUrl++)
|
---|
68 | {
|
---|
69 | RTTestIPrintf(RTTESTLVL_ALWAYS, "URL: %s\n", s_apszRootsZipUrls[iUrl]);
|
---|
70 | void *pvRootsZip;
|
---|
71 | size_t cbRootsZip;
|
---|
72 | rc = RTHttpGetBinary(TestObj.m_hHttp, s_apszRootsZipUrls[iUrl], &pvRootsZip, &cbRootsZip);
|
---|
73 | if (RT_SUCCESS(rc))
|
---|
74 | {
|
---|
75 | for (uint32_t i = 0; i < RT_ELEMENTS(s_aCerts); i++)
|
---|
76 | {
|
---|
77 | CERTINFO const *pInfo = (CERTINFO const *)s_aCerts[i].pvUser;
|
---|
78 | if (pInfo->pszZipFile)
|
---|
79 | {
|
---|
80 | void *pvFile;
|
---|
81 | size_t cbFile;
|
---|
82 | RTTESTI_CHECK_RC_BREAK(RTZipPkzipMemDecompress(&pvFile, &cbFile, pvRootsZip, cbRootsZip, pInfo->pszZipFile),
|
---|
83 | VINF_SUCCESS);
|
---|
84 | RTTESTI_CHECK_RC(convertVerifyAndAddPemCertificateToStore(hStore, pvFile, cbFile, &TestObj.s_aCerts[i]),
|
---|
85 | VINF_SUCCESS);
|
---|
86 | RTMemFree(pvFile);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | RTHttpFreeResponse(pvRootsZip);
|
---|
90 | }
|
---|
91 | else if ( rc != VERR_HTTP_PROXY_NOT_FOUND
|
---|
92 | && rc != VERR_HTTP_COULDNT_CONNECT)
|
---|
93 | RTTestIFailed("%Rrc on '%s'", rc, s_apszRootsZipUrls[iUrl]); /* code or link broken */
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* PEM files: */
|
---|
97 | for (uint32_t i = 0; i < RT_ELEMENTS(s_aCerts); i++)
|
---|
98 | {
|
---|
99 | uint32_t iUrl = 0; /* First URL must always work. */
|
---|
100 | UINetworkReplyPrivateThread::CERTINFO const *pInfo;
|
---|
101 | pInfo = (UINetworkReplyPrivateThread::CERTINFO const *)s_aCerts[i].pvUser;
|
---|
102 | if (pInfo->apszUrls[iUrl])
|
---|
103 | {
|
---|
104 | RTTestIPrintf(RTTESTLVL_ALWAYS, "URL: %s\n", pInfo->apszUrls[iUrl]);
|
---|
105 | void *pvResponse;
|
---|
106 | size_t cbResponse;
|
---|
107 | rc = RTHttpGetBinary(TestObj.m_hHttp, pInfo->apszUrls[iUrl], &pvResponse, &cbResponse);
|
---|
108 | if (RT_SUCCESS(rc))
|
---|
109 | {
|
---|
110 | RTTESTI_CHECK_RC_OK(convertVerifyAndAddPemCertificateToStore(hStore, pvResponse, cbResponse,
|
---|
111 | &TestObj.s_aCerts[i]));
|
---|
112 | RTHttpFreeResponse(pvResponse);
|
---|
113 | }
|
---|
114 | else if ( rc != VERR_HTTP_PROXY_NOT_FOUND
|
---|
115 | && rc != VERR_HTTP_COULDNT_CONNECT)
|
---|
116 | RTTestIFailed("%Rrc on '%s'", rc, pInfo->apszUrls[iUrl]); /* code or link broken */
|
---|
117 | }
|
---|
118 | }
|
---|
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);
|
---|
141 |
|
---|
142 | RTTESTI_CHECK(RTCrStoreRelease(hStore) == 0);
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * We're done.
|
---|
146 | */
|
---|
147 | //RTTESTI_CHECK_RC(RTHttpDestroy(TestObj.m_hHttp), VINF_SUCCESS);
|
---|
148 | RTHttpDestroy(TestObj.m_hHttp);
|
---|
149 | TestObj.m_hHttp = NIL_RTHTTP;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | int main()
|
---|
154 | {
|
---|
155 | RTTEST hTest;
|
---|
156 | RTEXITCODE rcExit = RTTestInitAndCreate("tstSSLCertDownloads", &hTest);
|
---|
157 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
158 | return rcExit;
|
---|
159 | RTTestBanner(hTest);
|
---|
160 |
|
---|
161 | UINetworkReplyPrivateThread::testIt(hTest);
|
---|
162 |
|
---|
163 | return RTTestSummaryAndDestroy(hTest);
|
---|
164 | }
|
---|
165 |
|
---|