KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_remote_provider_client.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <stdexcept>
23
24#include <oauth/oauth_session.h>
27#include <wx/utils.h>
28
29
30namespace
31{
32wxFileName schemaPath()
33{
34 wxFileName schemaFile = wxFileName::DirName( wxString::FromUTF8( QA_SRC_ROOT ) );
35 schemaFile.AppendDir( wxS( "resources" ) );
36 schemaFile.AppendDir( wxS( "schemas" ) );
37 schemaFile.SetFullName( wxS( "kicad-remote-provider-metadata-v1.schema.json" ) );
38 return schemaFile;
39}
40
41
42nlohmann::json providerMetadataJson( const wxString& aAuthType = wxS( "oauth2" ) )
43{
44 nlohmann::json auth = { { "type", aAuthType.ToStdString() } };
45
46 if( aAuthType == wxS( "oauth2" ) )
47 {
48 auth["metadata_url"] = "https://provider.example.test/.well-known/oauth-authorization-server";
49 auth["client_id"] = "kicad-desktop";
50 auth["scopes"] = nlohmann::json::array( { "openid", "parts.read" } );
51 }
52
53 return nlohmann::json{
54 { "provider_name", "Acme Parts" },
55 { "provider_version", "1.0.0" },
56 { "api_base_url", "https://provider.example.test/api" },
57 { "panel_url", "https://provider.example.test/app" },
58 { "session_bootstrap_url", "https://provider.example.test/session/bootstrap" },
59 { "auth", auth },
60 { "capabilities",
61 { { "web_ui_v1", true },
62 { "parts_v1", true },
63 { "direct_downloads_v1", true },
64 { "inline_payloads_v1", true } } },
65 { "max_download_bytes", 10485760 },
66 { "supported_asset_types", nlohmann::json::array( { "symbol", "footprint", "3dmodel" } ) },
67 { "parts", { { "endpoint_template", "/v1/parts/{part_id}" } } }
68 };
69}
70
71
72REMOTE_PROVIDER_METADATA parseProviderMetadata( const wxString& aAuthType = wxS( "oauth2" ) )
73{
74 wxString error;
75 std::optional<REMOTE_PROVIDER_METADATA> metadata =
76 REMOTE_PROVIDER_METADATA::FromJson( providerMetadataJson( aAuthType ), schemaPath(), error );
77
78 if( !metadata.has_value() )
79 throw std::runtime_error( error.ToStdString() );
80
81 return *metadata;
82}
83
84
85nlohmann::json authServerMetadataJson()
86{
87 return nlohmann::json{ { "issuer", "https://provider.example.test" },
88 { "authorization_endpoint", "https://provider.example.test/oauth/authorize" },
89 { "token_endpoint", "https://provider.example.test/oauth/token" },
90 { "revocation_endpoint", "https://provider.example.test/oauth/revoke" } };
91}
92
93nlohmann::json manifestJson()
94{
95 return nlohmann::json{
96 { "part_id", "acme-res-10k" },
97 { "display_name", "10k Resistor" },
98 { "summary", "10k 0603 thick film resistor" },
99 { "license", "CC-BY-4.0" },
100 { "assets", nlohmann::json::array(
101 { { { "asset_type", "symbol" },
102 { "name", "acme-res-10k.kicad_sym" },
103 { "content_type", "application/x-kicad-symbol" },
104 { "size_bytes", 2048 },
105 { "sha256", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" },
106 { "download_url", "https://provider.example.test/downloads/acme-res-10k.kicad_sym" },
107 { "required", true } },
108 { { "asset_type", "footprint" },
109 { "name", "R_0603.pretty" },
110 { "content_type", "application/x-kicad-footprint" },
111 { "size_bytes", 4096 },
112 { "sha256", "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789" },
113 { "download_url", "https://provider.example.test/downloads/R_0603.pretty" },
114 { "required", false } } } ) }
115 };
116}
117
118
119wxString dumpJson( const nlohmann::json& aJson )
120{
121 return wxString::FromUTF8( aJson.dump().c_str() );
122}
123struct BUILD_DIR_FIXTURE
124{
125 BUILD_DIR_FIXTURE()
126 {
127 m_wasSet = wxGetEnv( wxS( "KICAD_RUN_FROM_BUILD_DIR" ), &m_oldValue );
128
129 if( !m_wasSet )
130 wxSetEnv( wxS( "KICAD_RUN_FROM_BUILD_DIR" ), wxS( "1" ) );
131 }
132
133 ~BUILD_DIR_FIXTURE()
134 {
135 if( !m_wasSet )
136 wxUnsetEnv( wxS( "KICAD_RUN_FROM_BUILD_DIR" ) );
137 }
138
139 bool m_wasSet;
140 wxString m_oldValue;
141};
142
143} // namespace
144
145
146BOOST_FIXTURE_TEST_SUITE( RemoteProviderClientTests, BUILD_DIR_FIXTURE )
147
148BOOST_AUTO_TEST_CASE( DiscoveryFetchesWellKnownMetadata )
149{
150 std::vector<wxString> requestedUrls;
151
153 [&]( const REMOTE_PROVIDER_HTTP_REQUEST& aRequest, REMOTE_PROVIDER_HTTP_RESPONSE& aResponse,
154 wxString& aError )
155 {
156 wxUnusedVar( aError );
157 requestedUrls.push_back( aRequest.url );
158 aResponse.status_code = 200;
159 aResponse.body = dumpJson( providerMetadataJson() );
160 return true;
161 } );
162
165
166 BOOST_REQUIRE( client.DiscoverProvider( wxString( "https://provider.example.test" ), metadata, error ) );
167 BOOST_CHECK_EQUAL( requestedUrls.size(), 1U );
168 BOOST_CHECK_EQUAL( requestedUrls.front(),
169 wxString( "https://provider.example.test/.well-known/kicad-remote-provider" ) );
170 BOOST_CHECK_EQUAL( metadata.provider_name, wxString( "Acme Parts" ) );
171}
172
173BOOST_AUTO_TEST_CASE( OAuthServerMetadataFetchParsesRfc8414Fields )
174{
175 REMOTE_PROVIDER_METADATA metadata = parseProviderMetadata();
176
178 [&]( const REMOTE_PROVIDER_HTTP_REQUEST& aRequest, REMOTE_PROVIDER_HTTP_RESPONSE& aResponse,
179 wxString& aError )
180 {
181 wxUnusedVar( aError );
182 BOOST_CHECK_EQUAL( aRequest.url,
183 wxString( "https://provider.example.test/.well-known/oauth-authorization-server" ) );
184 aResponse.status_code = 200;
185 aResponse.body = dumpJson( authServerMetadataJson() );
186 return true;
187 } );
188
191
192 BOOST_REQUIRE( client.FetchOAuthServerMetadata( metadata, authMetadata, error ) );
193 BOOST_CHECK_EQUAL( authMetadata.authorization_endpoint,
194 wxString( "https://provider.example.test/oauth/authorize" ) );
195 BOOST_CHECK_EQUAL( authMetadata.token_endpoint, wxString( "https://provider.example.test/oauth/token" ) );
196}
197
198BOOST_AUTO_TEST_CASE( ManifestParsesDigestSizeAndDownloadUrls )
199{
200 REMOTE_PROVIDER_METADATA metadata = parseProviderMetadata( wxS( "none" ) );
201
203 [&]( const REMOTE_PROVIDER_HTTP_REQUEST& aRequest, REMOTE_PROVIDER_HTTP_RESPONSE& aResponse,
204 wxString& aError )
205 {
206 wxUnusedVar( aError );
207 BOOST_CHECK( aRequest.method == REMOTE_PROVIDER_HTTP_METHOD::GET );
208 BOOST_CHECK_EQUAL( aRequest.url,
209 wxString( "https://provider.example.test/api/v1/parts/acme-res-10k" ) );
210 aResponse.status_code = 200;
211 aResponse.body = dumpJson( manifestJson() );
212 return true;
213 } );
214
217
218 BOOST_REQUIRE( client.FetchManifest( metadata, wxString( "acme-res-10k" ), wxString(), manifest, error ) );
219 BOOST_CHECK_EQUAL( manifest.part_id, wxString( "acme-res-10k" ) );
220 BOOST_REQUIRE_EQUAL( manifest.assets.size(), 2U );
221 BOOST_CHECK_EQUAL( manifest.assets.front().size_bytes, 2048LL );
222 BOOST_CHECK_EQUAL( manifest.assets.front().sha256,
223 wxString( "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" ) );
224 BOOST_CHECK( manifest.assets.front().required );
225}
226
227BOOST_AUTO_TEST_CASE( AuthorizationCodeExchangeParsesTokens )
228{
229 REMOTE_PROVIDER_METADATA metadata = parseProviderMetadata();
231 oauth.authorization_endpoint = wxString( "https://provider.example.test/oauth/authorize" );
232 oauth.token_endpoint = wxString( "https://provider.example.test/oauth/token" );
233
235 [&]( const REMOTE_PROVIDER_HTTP_REQUEST& aRequest, REMOTE_PROVIDER_HTTP_RESPONSE& aResponse,
236 wxString& aError )
237 {
238 wxUnusedVar( aError );
239 BOOST_CHECK( aRequest.method == REMOTE_PROVIDER_HTTP_METHOD::POST );
240 BOOST_CHECK_EQUAL( aRequest.url, oauth.token_endpoint );
241 BOOST_CHECK( aRequest.body.Contains( wxString( "grant_type=authorization_code" ) ) );
242 BOOST_CHECK( aRequest.body.Contains( wxString( "code=test-code" ) ) );
243 aResponse.status_code = 200;
244 aResponse.body = dumpJson( {
245 { "access_token", "access-123" },
246 { "refresh_token", "refresh-123" },
247 { "token_type", "Bearer" },
248 { "scope", "openid parts.read" },
249 { "expires_in", 3600 }
250 } );
251 return true;
252 } );
253
254 OAUTH_SESSION session;
255 session.client_id = metadata.auth.client_id;
256 session.redirect_uri = wxString( "http://127.0.0.1:9000/oauth/callback" );
257 session.code_verifier = wxString( "verifier" );
258 OAUTH_TOKEN_SET tokens;
260
261 BOOST_REQUIRE( client.ExchangeAuthorizationCode( oauth, session, wxString( "test-code" ), tokens, error ) );
262 BOOST_CHECK_EQUAL( tokens.access_token, wxString( "access-123" ) );
263 BOOST_CHECK_EQUAL( tokens.refresh_token, wxString( "refresh-123" ) );
264 BOOST_CHECK_EQUAL( tokens.token_type, wxString( "Bearer" ) );
265}
266
267BOOST_AUTO_TEST_CASE( RefreshTokenExchangeParsesTokens )
268{
270 oauth.token_endpoint = wxString( "https://provider.example.test/oauth/token" );
271
273 [&]( const REMOTE_PROVIDER_HTTP_REQUEST& aRequest, REMOTE_PROVIDER_HTTP_RESPONSE& aResponse,
274 wxString& aError )
275 {
276 wxUnusedVar( aError );
277 BOOST_CHECK( aRequest.body.Contains( wxString( "grant_type=refresh_token" ) ) );
278 BOOST_CHECK( aRequest.body.Contains( wxString( "refresh_token=refresh-123" ) ) );
279 aResponse.status_code = 200;
280 aResponse.body = dumpJson( {
281 { "access_token", "access-456" },
282 { "refresh_token", "refresh-456" },
283 { "token_type", "Bearer" },
284 { "scope", "openid parts.read" },
285 { "expires_in", 3600 }
286 } );
287 return true;
288 } );
289
290 OAUTH_TOKEN_SET tokens;
292 BOOST_REQUIRE( client.RefreshAccessToken( oauth, wxString( "kicad-desktop" ),
293 wxString( "refresh-123" ), tokens, error ) );
294 BOOST_CHECK_EQUAL( tokens.access_token, wxString( "access-456" ) );
295 BOOST_CHECK_EQUAL( tokens.refresh_token, wxString( "refresh-456" ) );
296}
297
298BOOST_AUTO_TEST_CASE( RevokeTokenPostsToRevocationEndpoint )
299{
301 oauth.revocation_endpoint = wxString( "https://provider.example.test/oauth/revoke" );
302
304 [&]( const REMOTE_PROVIDER_HTTP_REQUEST& aRequest, REMOTE_PROVIDER_HTTP_RESPONSE& aResponse,
305 wxString& aError )
306 {
307 wxUnusedVar( aError );
308 BOOST_CHECK( aRequest.body.Contains( wxString( "token=access-123" ) ) );
309 aResponse.status_code = 200;
310 aResponse.body = wxString( "{}" );
311 return true;
312 } );
313
315 BOOST_CHECK( client.RevokeToken( oauth, wxString( "kicad-desktop" ), wxString( "access-123" ), error ) );
316}
317
wxString client_id
REMOTE_PROVIDER_HTTP_METHOD method
static std::optional< REMOTE_PROVIDER_METADATA > FromJson(const nlohmann::json &aJson, wxString &aError)
REMOTE_PROVIDER_AUTH_METADATA auth
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(DiscoveryFetchesWellKnownMetadata)
BOOST_CHECK_EQUAL(result, "25.4")