KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_api_e2e.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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "api_e2e_utils.h"
21
22#include <utility>
23
24#include <boost/test/unit_test.hpp>
25#include <qa_utils/file_utils.h>
26
27#include <magic_enum.hpp>
28
29#include <wx/filefn.h>
30#include <wx/filename.h>
31
32#include <api/board/board.pb.h>
33#include <api/board/board_commands.pb.h>
34#include <api/board/board_rules.pb.h>
35#include <pcb_field.h>
36
37
39{
40public:
42 m_tempRoot( "kicad_api_e2e" )
43 {
44 }
45
46 bool Create( wxString* aError, const wxString& aRulesName = wxS( "api_kitchen_sink.kicad_dru" ) )
47 {
48 wxString testDataDir = wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() );
49 wxFileName srcPcb( testDataDir, wxS( "api_kitchen_sink.kicad_pcb" ) );
50 wxFileName srcPro( testDataDir, wxS( "api_kitchen_sink.kicad_pro" ) );
51 wxFileName srcDru( testDataDir + wxFileName::GetPathSeparator() + aRulesName );
52
53 wxFileName dstPcb( m_tempRoot.PathStr(), srcPcb.GetFullName() );
54 wxFileName dstPro( m_tempRoot.PathStr(), srcPro.GetFullName() );
55 wxFileName dstDru( m_tempRoot.PathStr(), wxS( "api_kitchen_sink.kicad_dru" ) );
56
57 if( !wxCopyFile( srcPcb.GetFullPath(), dstPcb.GetFullPath(), true )
58 || !wxCopyFile( srcPro.GetFullPath(), dstPro.GetFullPath(), true )
59 || !wxCopyFile( srcDru.GetFullPath(), dstDru.GetFullPath(), true ) )
60 {
61 if( aError )
62 *aError = wxS( "Failed to copy fixture files into temporary directory" );
63
64 return false;
65 }
66
67 m_boardPath = dstPcb.GetFullPath();
68 return true;
69 }
70
71 const wxString& BoardPath() const { return m_boardPath; }
72
73private:
75 wxString m_boardPath;
76};
77
78
79bool SendGetBoardDesignRules( API_TEST_CLIENT& aClient, const kiapi::common::types::DocumentSpecifier& aDocument,
80 kiapi::board::commands::BoardDesignRulesResponse* aOut, wxString* aError = nullptr )
81{
82 kiapi::board::commands::GetBoardDesignRules request;
83 *request.mutable_board() = aDocument;
84
85 kiapi::common::ApiResponse response;
86
87 if( !aClient.SendCommand( request, &response ) )
88 {
89 if( aError )
90 *aError = aClient.LastError();
91
92 return false;
93 }
94
95 if( response.status().status() != kiapi::common::AS_OK )
96 {
97 if( aError )
98 *aError = response.status().error_message();
99
100 return false;
101 }
102
103 if( !response.message().UnpackTo( aOut ) )
104 {
105 if( aError )
106 *aError = wxS( "Failed to unpack BoardDesignRulesResponse" );
107
108 return false;
109 }
110
111 return true;
112}
113
114
115bool SendGetCustomDesignRules( API_TEST_CLIENT& aClient, const kiapi::common::types::DocumentSpecifier& aDocument,
116 kiapi::board::commands::CustomRulesResponse* aOut, wxString* aError = nullptr )
117{
118 kiapi::board::commands::GetCustomDesignRules request;
119 *request.mutable_board() = aDocument;
120
121 kiapi::common::ApiResponse response;
122
123 if( !aClient.SendCommand( request, &response ) )
124 {
125 if( aError )
126 *aError = aClient.LastError();
127
128 return false;
129 }
130
131 if( response.status().status() != kiapi::common::AS_OK )
132 {
133 if( aError )
134 *aError = response.status().error_message();
135
136 return false;
137 }
138
139 if( !response.message().UnpackTo( aOut ) )
140 {
141 if( aError )
142 *aError = wxS( "Failed to unpack CustomRulesResponse" );
143
144 return false;
145 }
146
147 return true;
148}
149
150
151BOOST_AUTO_TEST_SUITE( ApiE2E )
152
153
155{
156 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
157
158 BOOST_CHECK_MESSAGE( Client().GetVersion(), "GetVersion failed: " + Client().LastError() );
159}
160
161
163{
164 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
165
166 kiapi::common::types::DocumentSpecifier document;
167 wxString testDataDir = wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() );
168 wxFileName boardPath( testDataDir, wxS( "api_kitchen_sink.kicad_pcb" ) );
169
170 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( boardPath.GetFullPath(), &document ),
171 "OpenDocument failed: " + Client().LastError() );
172
173 BOOST_REQUIRE( document.type() == kiapi::common::types::DOCTYPE_PCB );
174 BOOST_REQUIRE( boardPath.GetFullName().Matches( document.board_filename() ) );
175
176 int footprintCount = 0;
177
178 BOOST_REQUIRE_MESSAGE( Client().GetItemsCount( document, kiapi::common::types::KOT_PCB_FOOTPRINT, &footprintCount ),
179 "GetItems failed: " + Client().LastError() );
180
181 BOOST_CHECK_GT( footprintCount, 0 );
182}
183
184
185// An unset page_size must be refused because mapping it asserts, and a debug server blocks on the assert
186BOOST_FIXTURE_TEST_CASE( SetPageSettingsRequiresPageSize, API_SERVER_E2E_FIXTURE )
187{
188 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
189
190 kiapi::common::types::DocumentSpecifier document;
191 wxFileName boardPath( wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() ), wxS( "api_kitchen_sink.kicad_pcb" ) );
192
193 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( boardPath.GetFullPath(), &document ),
194 "OpenDocument failed: " + Client().LastError() );
195
196 kiapi::common::commands::SetPageSettings request;
197 request.mutable_document()->CopyFrom( document );
198 request.mutable_page_settings()->set_orientation( kiapi::common::types::PO_PORTRAIT );
199
200 kiapi::common::ApiResponse response;
201 BOOST_REQUIRE_MESSAGE( Client().SendCommand( request, &response ),
202 "SetPageSettings failed to send: " + Client().LastError() );
203 BOOST_CHECK( response.status().status() == kiapi::common::AS_BAD_REQUEST );
204 BOOST_CHECK_EQUAL( response.status().error_message(), "SetPageSettings requires page_size" );
205}
206
207
208// A footprint stripped of its reference or value is a state neither the editor nor the const
209// field accessors can cope with, so the deletion has to be refused outright
210BOOST_FIXTURE_TEST_CASE( DeleteMandatoryFieldIsRefused, API_SERVER_E2E_FIXTURE )
211{
212 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
213
214 kiapi::common::types::DocumentSpecifier document;
215 wxFileName boardPath( wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() ), wxS( "api_kitchen_sink.kicad_pcb" ) );
216
217 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( boardPath.GetFullPath(), &document ),
218 "OpenDocument failed: " + Client().LastError() );
219
220 FOOTPRINT footprint( nullptr );
221
222 BOOST_REQUIRE_MESSAGE( Client().GetFirstFootprint( document, &footprint ),
223 "GetFirstFootprint failed: " + Client().LastError() );
224
225 const PCB_FIELD* reference = std::as_const( footprint ).GetField( FIELD_T::REFERENCE );
226 BOOST_REQUIRE( reference );
227
228 kiapi::common::commands::DeleteItems request;
229 *request.mutable_header()->mutable_document() = document;
230 request.add_item_ids()->set_value( reference->m_Uuid.AsStdString() );
231
232 kiapi::common::ApiResponse response;
233
234 BOOST_REQUIRE_MESSAGE( Client().SendCommand( request, &response ),
235 "DeleteItems failed: " + Client().LastError() );
236 BOOST_REQUIRE_EQUAL( response.status().status(), kiapi::common::AS_OK );
237
238 kiapi::common::commands::DeleteItemsResponse deleteResponse;
239
240 BOOST_REQUIRE( response.message().UnpackTo( &deleteResponse ) );
241 BOOST_REQUIRE_EQUAL( deleteResponse.deleted_items_size(), 1 );
242 BOOST_CHECK_EQUAL( deleteResponse.deleted_items( 0 ).status(),
243 kiapi::common::commands::ItemDeletionStatus::IDS_IMMUTABLE );
244
245 FOOTPRINT after( nullptr );
246
247 BOOST_REQUIRE_MESSAGE( Client().GetFirstFootprint( document, &after ),
248 "GetFirstFootprint after delete failed: " + Client().LastError() );
249
250 const PCB_FIELD* survivor = std::as_const( after ).GetField( FIELD_T::REFERENCE );
251
252 BOOST_REQUIRE( survivor );
253 BOOST_CHECK( survivor->m_Uuid == reference->m_Uuid );
254}
255
256
258{
259 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
260
261 wxString testDataDir = wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() );
262 wxFileName boardPathA( testDataDir, wxS( "api_kitchen_sink.kicad_pcb" ) );
263 wxFileName boardPathB( testDataDir, wxS( "padstacks.kicad_pcb" ) );
264
265 kiapi::common::types::DocumentSpecifier documentA;
266
267 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( boardPathA.GetFullPath(), &documentA ),
268 "OpenDocument for first board failed: " + Client().LastError() );
269
270 FOOTPRINT footprintA( nullptr );
271
272 BOOST_REQUIRE_MESSAGE( Client().GetFirstFootprint( documentA, &footprintA ),
273 "GetFirstFootprint for first board failed: " + Client().LastError() );
274
275 // Switching projects requires an explicit close
276 kiapi::common::ApiStatusCode closeStatusA = kiapi::common::AS_UNKNOWN;
277
278 BOOST_REQUIRE_MESSAGE( Client().CloseDocument( &documentA, &closeStatusA ),
279 "CloseDocument for first board failed: " + Client().LastError() );
280 BOOST_CHECK_EQUAL( closeStatusA, kiapi::common::AS_OK );
281
282 kiapi::common::types::DocumentSpecifier documentB;
283
284 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( boardPathB.GetFullPath(), &documentB ),
285 "OpenDocument for second board failed: " + Client().LastError() );
286
287 FOOTPRINT footprintB( nullptr );
288
289 BOOST_REQUIRE_MESSAGE( Client().GetFirstFootprint( documentB, &footprintB ),
290 "GetFirstFootprint for second board failed: " + Client().LastError() );
291
292 BOOST_CHECK_NE( footprintA.Similarity( footprintB ), 1.0 );
293
294 kiapi::common::ApiStatusCode closeStatus = kiapi::common::AS_UNKNOWN;
295
296 BOOST_REQUIRE_MESSAGE( Client().CloseDocument( &documentB, &closeStatus ),
297 "CloseDocument failed: " + Client().LastError() );
298 BOOST_CHECK_EQUAL( closeStatus, kiapi::common::AS_OK );
299
300 BOOST_REQUIRE_MESSAGE( Client().CloseDocument( nullptr, &closeStatus ),
301 "CloseDocument after already closed failed: " + Client().LastError() );
302 BOOST_CHECK_EQUAL( closeStatus, kiapi::common::AS_BAD_REQUEST );
303}
304
305
307{
308 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
309
310 kiapi::common::types::DocumentSpecifier document;
311 wxFileName boardPath( wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() ), wxS( "api_kitchen_sink.kicad_pcb" ) );
312
313 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( boardPath.GetFullPath(), &document ),
314 "OpenDocument failed: " + Client().LastError() );
315
316 kiapi::board::commands::BoardDesignRulesResponse rulesResponse;
317 wxString error;
318
319 BOOST_REQUIRE_MESSAGE( SendGetBoardDesignRules( Client(), document, &rulesResponse, &error ),
320 "GetBoardDesignRules failed: " + error );
321
322 BOOST_CHECK_GE( rulesResponse.rules().constraints().min_clearance().value_nm(), 0 );
323 BOOST_CHECK_GE( rulesResponse.rules().constraints().min_track_width().value_nm(), 0 );
324 BOOST_CHECK( rulesResponse.custom_rules_status() == kiapi::board::commands::CRS_VALID );
325}
326
327
329{
330 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
331
332 TEMP_KITCHEN_SINK_COPY fixtureCopy;
333 wxString copyError;
334
335 BOOST_REQUIRE_MESSAGE( fixtureCopy.Create( &copyError ), copyError );
336
337 kiapi::common::types::DocumentSpecifier document;
338
339 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( fixtureCopy.BoardPath(), &document ),
340 "OpenDocument failed: " + Client().LastError() );
341
342 kiapi::board::commands::BoardDesignRulesResponse getResponse;
343
344 BOOST_REQUIRE_MESSAGE( SendGetBoardDesignRules( Client(), document, &getResponse, &copyError ),
345 "Initial GetBoardDesignRules failed: " + copyError );
346
347 int newWidth = getResponse.rules().constraints().min_track_width().value_nm() + 1000;
348
349 kiapi::board::commands::SetBoardDesignRules setRequest;
350 *setRequest.mutable_board() = document;
351 *setRequest.mutable_rules()->mutable_constraints() = getResponse.rules().constraints();
352 setRequest.mutable_rules()->mutable_constraints()->mutable_min_track_width()->set_value_nm( newWidth );
353
354 kiapi::common::ApiResponse setApiResponse;
355
356 BOOST_REQUIRE_MESSAGE( Client().SendCommand( setRequest, &setApiResponse ),
357 "SetBoardDesignRules failed to send: " + Client().LastError() );
358 BOOST_REQUIRE( setApiResponse.status().status() == kiapi::common::AS_OK );
359
360 kiapi::board::commands::BoardDesignRulesResponse setResponse;
361 BOOST_REQUIRE( setApiResponse.message().UnpackTo( &setResponse ) );
362
363 BOOST_CHECK_EQUAL( setResponse.rules().constraints().min_track_width().value_nm(), newWidth );
364
365 kiapi::board::commands::BoardDesignRulesResponse verifyResponse;
366
367 BOOST_REQUIRE_MESSAGE( SendGetBoardDesignRules( Client(), document, &verifyResponse, &copyError ),
368 "Verification GetBoardDesignRules failed: " + copyError );
369
370 BOOST_CHECK_EQUAL( verifyResponse.rules().constraints().min_track_width().value_nm(), newWidth );
371}
372
373
374BOOST_FIXTURE_TEST_CASE( SetBoardDesignRules_ValidationFailure, API_SERVER_E2E_FIXTURE )
375{
376 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
377
378 TEMP_KITCHEN_SINK_COPY fixtureCopy;
379 wxString copyError;
380
381 BOOST_REQUIRE_MESSAGE( fixtureCopy.Create( &copyError ), copyError );
382
383 kiapi::common::types::DocumentSpecifier document;
384
385 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( fixtureCopy.BoardPath(), &document ),
386 "OpenDocument failed: " + Client().LastError() );
387
388 kiapi::board::commands::BoardDesignRulesResponse getResponse;
389
390 BOOST_REQUIRE_MESSAGE( SendGetBoardDesignRules( Client(), document, &getResponse, &copyError ),
391 "Initial GetBoardDesignRules failed: " + copyError );
392
393 kiapi::board::commands::SetBoardDesignRules setRequest;
394 *setRequest.mutable_board() = document;
395 *setRequest.mutable_rules()->mutable_constraints() = getResponse.rules().constraints();
396 setRequest.mutable_rules()->mutable_constraints()->mutable_min_track_width()->set_value_nm( 1000000000 );
397
398 kiapi::common::ApiResponse response;
399
400 BOOST_REQUIRE_MESSAGE( Client().SendCommand( setRequest, &response ),
401 "SetBoardDesignRules failed to send: " + Client().LastError() );
402
403 BOOST_CHECK( response.status().status() == kiapi::common::AS_BAD_REQUEST );
404 BOOST_CHECK( !response.status().error_message().empty() );
405}
406
407
408BOOST_FIXTURE_TEST_CASE( SetBoardDesignRules_SeverityOverrides, API_SERVER_E2E_FIXTURE )
409{
410 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
411
412 TEMP_KITCHEN_SINK_COPY fixtureCopy;
413 wxString copyError;
414
415 BOOST_REQUIRE_MESSAGE( fixtureCopy.Create( &copyError ), copyError );
416
417 kiapi::common::types::DocumentSpecifier document;
418
419 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( fixtureCopy.BoardPath(), &document ),
420 "OpenDocument failed: " + Client().LastError() );
421
422 kiapi::board::commands::BoardDesignRulesResponse getResponse;
423 BOOST_REQUIRE_MESSAGE( SendGetBoardDesignRules( Client(), document, &getResponse, &copyError ),
424 "Initial GetBoardDesignRules failed: " + copyError );
425
426 auto applySeverityAndVerify = [&]( kiapi::common::types::RuleSeverity aSeverity )
427 {
428 kiapi::board::commands::SetBoardDesignRules setRequest;
429 *setRequest.mutable_board() = document;
430
431 kiapi::board::DrcSeveritySetting* setting = setRequest.mutable_rules()->add_severities();
432 setting->set_rule_type( kiapi::board::DrcErrorType::DRCET_UNCONNECTED_ITEMS );
433 setting->set_severity( aSeverity );
434
435 kiapi::common::ApiResponse setApiResponse;
436 BOOST_REQUIRE_MESSAGE( Client().SendCommand( setRequest, &setApiResponse ),
437 "SetBoardDesignRules failed to send: " + Client().LastError() );
438 BOOST_REQUIRE( setApiResponse.status().status() == kiapi::common::AS_OK );
439
440 kiapi::board::commands::BoardDesignRulesResponse verifyResponse;
441 BOOST_REQUIRE_MESSAGE( SendGetBoardDesignRules( Client(), document, &verifyResponse, &copyError ),
442 "Verification GetBoardDesignRules failed: " + copyError );
443
444 bool found = false;
445
446 for( const kiapi::board::DrcSeveritySetting& severity : verifyResponse.rules().severities() )
447 {
448 if( severity.rule_type() == kiapi::board::DrcErrorType::DRCET_UNCONNECTED_ITEMS )
449 {
450 found = true;
451 BOOST_CHECK( severity.severity() == aSeverity );
452 break;
453 }
454 }
455
456 BOOST_CHECK( found );
457 };
458
459 applySeverityAndVerify( kiapi::common::types::RS_IGNORE );
460 applySeverityAndVerify( kiapi::common::types::RS_ERROR );
461}
462
463
464BOOST_FIXTURE_TEST_CASE( GetCustomDesignRules_ReturnsFixtureRules, API_SERVER_E2E_FIXTURE )
465{
466 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
467
468 kiapi::common::types::DocumentSpecifier document;
469 wxFileName boardPath( wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() ), wxS( "api_kitchen_sink.kicad_pcb" ) );
470
471 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( boardPath.GetFullPath(), &document ),
472 "OpenDocument failed: " + Client().LastError() );
473
474 kiapi::board::commands::CustomRulesResponse response;
475 wxString error;
476
477 BOOST_REQUIRE_MESSAGE( SendGetCustomDesignRules( Client(), document, &response, &error ),
478 "GetCustomDesignRules failed: " + error );
479
480 BOOST_CHECK( response.status() == kiapi::board::commands::CRS_VALID );
481 BOOST_CHECK_GT( response.rules_size(), 0 );
482
483 bool foundFixtureRule = false;
484
485 for( const kiapi::board::CustomRule& rule : response.rules() )
486 {
487 if( rule.name() == "myrule" )
488 {
489 foundFixtureRule = true;
490 BOOST_CHECK_EQUAL( rule.condition(), "A.Name == 'test'" );
491 BOOST_CHECK_EQUAL( rule.constraints_size(), 2 );
492 BOOST_REQUIRE( !rule.comments().empty() );
493 break;
494 }
495 }
496
497 BOOST_CHECK( foundFixtureRule );
498}
499
500
501BOOST_FIXTURE_TEST_CASE( SetCustomDesignRules_RoundTripSingleRule, API_SERVER_E2E_FIXTURE )
502{
503 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
504
505 TEMP_KITCHEN_SINK_COPY fixtureCopy;
506 wxString copyError;
507
508 BOOST_REQUIRE_MESSAGE( fixtureCopy.Create( &copyError ), copyError );
509
510 kiapi::common::types::DocumentSpecifier document;
511
512 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( fixtureCopy.BoardPath(), &document ),
513 "OpenDocument failed: " + Client().LastError() );
514
515 kiapi::board::commands::CustomRulesResponse response;
516 wxString error;
517
518 BOOST_REQUIRE_MESSAGE( SendGetCustomDesignRules( Client(), document, &response, &error ),
519 "GetCustomDesignRules failed: " + error );
520
521 BOOST_CHECK( response.status() == kiapi::board::commands::CRS_VALID );
522 BOOST_CHECK_GT( response.rules_size(), 0 );
523
524 kiapi::board::commands::SetCustomDesignRules setRequest;
525 *setRequest.mutable_board() = document;
526
527 setRequest.mutable_rules()->CopyFrom( response.rules() );
528
529 kiapi::board::CustomRule* rule = setRequest.add_rules();
530 rule->set_name( "api_roundtrip_rule" );
531 rule->set_condition( "A.NetClass == 'HV'" );
532 rule->set_layer_mode( kiapi::board::CRLM_OUTER );
533 rule->set_severity( kiapi::common::types::RS_WARNING );
534
535 kiapi::board::CustomRuleConstraint* annular = rule->add_constraints();
536 annular->set_type( kiapi::board::CRCT_TRACK_WIDTH );
537 annular->mutable_numeric()->set_min( 3000000 );
538 annular->mutable_numeric()->set_opt( 4000000 );
539 annular->mutable_numeric()->set_max( 5000000 );
540
541 kiapi::board::CustomRuleConstraint* disallow = rule->add_constraints();
542 disallow->set_type( kiapi::board::CRCT_DISALLOW );
543 disallow->mutable_disallow()->add_types( kiapi::board::CRDT_BLIND_VIAS );
544
545 kiapi::common::ApiResponse setApiResponse;
546
547 BOOST_REQUIRE_MESSAGE( Client().SendCommand( setRequest, &setApiResponse ),
548 "SetCustomDesignRules failed to send: " + Client().LastError() );
549 BOOST_REQUIRE( setApiResponse.status().status() == kiapi::common::AS_OK );
550
551 kiapi::board::commands::CustomRulesResponse setResponse;
552 BOOST_REQUIRE( setApiResponse.message().UnpackTo( &setResponse ) );
553
554 int num_rules = setResponse.rules_size();
555 BOOST_CHECK( setResponse.status() == kiapi::board::commands::CRS_VALID );
556 BOOST_CHECK_EQUAL( num_rules, response.rules_size() + 1 );
557 BOOST_CHECK_EQUAL( setResponse.rules( num_rules - 1 ).name(), "api_roundtrip_rule" );
558}
559
560
561BOOST_FIXTURE_TEST_CASE( SetCustomDesignRules_RoundTripIsLossless, API_SERVER_E2E_FIXTURE )
562{
563 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
564
565 TEMP_KITCHEN_SINK_COPY fixtureCopy;
566 wxString copyError;
567
568 BOOST_REQUIRE_MESSAGE( fixtureCopy.Create( &copyError, wxS( "issue25846/custom_rules.kicad_dru" ) ),
569 copyError );
570
571 kiapi::common::types::DocumentSpecifier document;
572
573 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( fixtureCopy.BoardPath(), &document ),
574 "OpenDocument failed: " + Client().LastError() );
575
576 kiapi::board::commands::CustomRulesResponse getResponse;
577 wxString error;
578
579 BOOST_REQUIRE_MESSAGE( SendGetCustomDesignRules( Client(), document, &getResponse, &error ),
580 "GetCustomDesignRules failed: " + error );
581 BOOST_REQUIRE( getResponse.status() == kiapi::board::commands::CRS_VALID );
582 BOOST_REQUIRE_GT( getResponse.rules_size(), 0 );
583
584 bool sawReturnPath = false;
585
586 for( const kiapi::board::CustomRule& rule : getResponse.rules() )
587 {
588 for( const kiapi::board::CustomRuleConstraint& constraint : rule.constraints() )
589 {
590 if( constraint.type() != kiapi::board::CRCT_NET_CHAIN_RETURN_PATH )
591 continue;
592
593 sawReturnPath = true;
594 BOOST_CHECK_EQUAL( constraint.return_path().reference_layer(), "B.Cu" );
595 BOOST_CHECK_EQUAL( constraint.return_path().reference_net(), "GND" );
596 }
597 }
598
599 BOOST_CHECK( sawReturnPath );
600
601 kiapi::board::commands::SetCustomDesignRules setRequest;
602 *setRequest.mutable_board() = document;
603 setRequest.mutable_rules()->CopyFrom( getResponse.rules() );
604
605 kiapi::common::ApiResponse setApiResponse;
606
607 BOOST_REQUIRE_MESSAGE( Client().SendCommand( setRequest, &setApiResponse ),
608 "SetCustomDesignRules failed to send: " + Client().LastError() );
609 BOOST_REQUIRE_MESSAGE( setApiResponse.status().status() == kiapi::common::AS_OK,
610 setApiResponse.status().error_message() );
611
612 kiapi::board::commands::CustomRulesResponse setResponse;
613 BOOST_REQUIRE( setApiResponse.message().UnpackTo( &setResponse ) );
614 BOOST_REQUIRE( setResponse.status() == kiapi::board::commands::CRS_VALID );
615 BOOST_REQUIRE_EQUAL( setResponse.rules_size(), getResponse.rules_size() );
616
617 for( int ii = 0; ii < getResponse.rules_size(); ++ii )
618 {
619 BOOST_CHECK_MESSAGE( setResponse.rules( ii ).SerializeAsString() == getResponse.rules( ii ).SerializeAsString(),
620 getResponse.rules( ii ).name() << " changed in the round trip" );
621 }
622}
623
624
625BOOST_FIXTURE_TEST_CASE( OpenProjectWithBoardAndSchematic, API_SERVER_E2E_FIXTURE )
626{
627 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
628
629 wxString testDataDir = wxString::FromUTF8( KI_TEST::GetTestDataRootDir() ) + wxS( "cli/basic_test/" );
630
631 kiapi::common::types::DocumentSpecifier projectDoc;
632
633 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( testDataDir + wxS( "basic_test.kicad_pro" ),
634 kiapi::common::types::DOCTYPE_PROJECT, &projectDoc ),
635 "OpenDocument for project failed: " + Client().LastError() );
636
637 BOOST_REQUIRE( projectDoc.type() == kiapi::common::types::DOCTYPE_PROJECT );
638
639 kiapi::common::types::DocumentSpecifier boardDoc;
640
641 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( testDataDir + wxS( "basic_test.kicad_pcb" ),
642 kiapi::common::types::DOCTYPE_PCB, &boardDoc ),
643 "OpenDocument for board failed: " + Client().LastError() );
644
645 BOOST_REQUIRE( boardDoc.type() == kiapi::common::types::DOCTYPE_PCB );
646 BOOST_REQUIRE( !boardDoc.board_filename().empty() );
647
648 int footprintCount = 0;
649 BOOST_REQUIRE_MESSAGE( Client().GetItemsCount( boardDoc, kiapi::common::types::KOT_PCB_FOOTPRINT, &footprintCount ),
650 "GetItems for board failed: " + Client().LastError() );
651 BOOST_CHECK_GT( footprintCount, 0 );
652
653 kiapi::common::types::DocumentSpecifier schDoc;
654
655 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( testDataDir + wxS( "basic_test.kicad_sch" ),
656 kiapi::common::types::DOCTYPE_SCHEMATIC, &schDoc ),
657 "OpenDocument for schematic failed: " + Client().LastError() );
658
659 BOOST_REQUIRE( schDoc.type() == kiapi::common::types::DOCTYPE_SCHEMATIC );
660
661 // Verify the board is still open by querying it again.
662 footprintCount = 0;
663 BOOST_REQUIRE_MESSAGE( Client().GetItemsCount( boardDoc, kiapi::common::types::KOT_PCB_FOOTPRINT, &footprintCount ),
664 "GetItems for board after opening schematic failed: " + Client().LastError() );
665 BOOST_CHECK_GT( footprintCount, 0 );
666
667 // Verify that opening a document from a different project is rejected.
668 wxString otherDataDir = wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() );
669
670 kiapi::common::types::DocumentSpecifier otherBoardDoc;
671
673 !Client().OpenDocument( otherDataDir + wxS( "api_kitchen_sink.kicad_pcb" ),
674 kiapi::common::types::DOCTYPE_PCB, &otherBoardDoc ),
675 "OpenDocument for a different-project board should have failed" );
676
677 BOOST_CHECK( Client().LastError().Contains( wxS( "already open" ) ) );
678}
679
680
682{
683 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
684
685 auto test =
686 [&]( kiapi::common::types::DocumentType aType, const wxString& aExpectedExt )
687 {
688 KI_TEST::SCOPED_TEMP_DIR tempDir( "kicad_api_e2e_createdoc" );
689
690 wxString tempFn = tempDir.ChildPathStr( "new_doc" );
691 wxFileName fileWithExt( tempDir.ChildPathStr( "new_doc" + aExpectedExt ) );
692 wxFileName projectWithExt( tempDir.ChildPathStr( "new_doc.kicad_pro" ) );
693
694 kiapi::common::types::DocumentSpecifier document;
695
696 BOOST_REQUIRE_MESSAGE( Client().CreateDocument( tempFn, aType, &document ),
697 "CreateDocument failed: " + Client().LastError() );
698
699 BOOST_CHECK( document.type() == aType );
700 BOOST_CHECK( document.project().name() == projectWithExt.GetName() );
701 BOOST_CHECK( document.project().path() == projectWithExt.GetPath( wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME ) );
702
703 kiapi::common::ApiResponse response;
704 kiapi::common::commands::GetDocumentModifiedState state;
705 state.mutable_document()->CopyFrom( document );
706 BOOST_CHECK( Client().SendCommand( state, &response ) );
707
708 // Just creating the document doesn't save it
709 BOOST_CHECK( !wxFileName( fileWithExt ).FileExists() );
710 BOOST_CHECK( !wxFileName( projectWithExt ).FileExists() );
711
712 kiapi::common::commands::SaveDocument save;
713 save.mutable_document()->CopyFrom( document );
714 BOOST_CHECK( Client().SendCommand( save, &response ) );
715
716 BOOST_CHECK( wxFileName( fileWithExt ).FileExists() );
717 BOOST_CHECK( wxFileName( projectWithExt ).FileExists() );
718 };
719
720 std::map<kiapi::common::types::DocumentType, wxString> cases = {
721 { kiapi::common::types::DOCTYPE_PCB, wxS( ".kicad_pcb" )},
722 { kiapi::common::types::DOCTYPE_SCHEMATIC, wxS( ".kicad_sch" ) }
723 };
724
725 for( const auto& [docType, ext] : cases )
726 {
727 BOOST_TEST_CONTEXT( magic_enum::enum_name( docType ) )
728 {
729 test( docType, ext );
730 }
731 }
732}
733
734
735BOOST_FIXTURE_TEST_CASE( CreateDocumentRejectsUnsavedModifications, API_SERVER_E2E_FIXTURE )
736{
737 using namespace kiapi::common::commands;
738
739 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
740
741 auto test =
742 [&]( kiapi::common::types::DocumentType aType, const wxString& aExistingPath )
743 {
744 KI_TEST::SCOPED_TEMP_DIR tempDir( "kicad_api_e2e_createdoc" );
745 wxString docFileName = tempDir.ChildPathStr( "new_doc" );
746
747 kiapi::common::types::DocumentSpecifier existingDoc;
748
749 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( aExistingPath, aType, &existingDoc ),
750 "OpenDocument failed: " + Client().LastError() );
751
752 SetPageSettings modify;
753 modify.mutable_document()->CopyFrom( existingDoc );
754 modify.mutable_page_settings()->set_page_size( kiapi::common::types::PS_A4 );
755 modify.mutable_page_settings()->set_orientation( kiapi::common::types::PO_PORTRAIT );
756 kiapi::common::ApiResponse response;
757 BOOST_CHECK( Client().SendCommand( modify, &response ) );
758
759 GetDocumentModifiedState stateQuery;
760 stateQuery.mutable_document()->CopyFrom( existingDoc );
761 BOOST_CHECK( Client().SendCommand( stateQuery, &response ) );
762 GetDocumentModifiedStateResponse state;
763 BOOST_CHECK( response.message().UnpackTo( &state ) );
764 BOOST_CHECK( state.state() == DocumentModifiedState::DMS_MODIFIED );
765
766 kiapi::common::types::DocumentSpecifier newDoc;
767
768 BOOST_REQUIRE( !Client().CreateDocument( docFileName, aType, &newDoc ) );
769
770 BOOST_CHECK( Client().LastError().Contains( wxS( "save or revert" ) ) );
771
772 kiapi::common::commands::RevertDocument revert;
773 revert.mutable_document()->CopyFrom( existingDoc );
774 BOOST_CHECK( Client().SendCommand( revert, &response ) );
775
776 BOOST_REQUIRE( Client().CreateDocument( docFileName, aType, &newDoc ) );
777
778 BOOST_CHECK( Client().CloseAllDocuments() );
779 };
780
781 std::map<kiapi::common::types::DocumentType, wxString> cases = {
782 { kiapi::common::types::DOCTYPE_PCB,
783 wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() ) + wxS( "api_kitchen_sink.kicad_pcb" ) },
784 { kiapi::common::types::DOCTYPE_SCHEMATIC,
785 wxString::FromUTF8( KI_TEST::GetEeschemaTestDataDir() ) + wxS( "api_kitchen_sink.kicad_sch" ) }
786 };
787
788 for( const auto& [docType, path] : cases )
789 {
790 BOOST_TEST_CONTEXT( magic_enum::enum_name( docType ) )
791 {
792 test( docType, path );
793 }
794 }
795}
796
797
799{
800 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
801
802 auto test =
803 [&]( kiapi::common::types::DocumentType aType, const wxString& aSourcePath,
804 const wxString& aSourceName, const wxString& aDocumentExt )
805 {
806 KI_TEST::SCOPED_TEMP_DIR tempDir( "kicad_api_e2e_saveas" );
807
808 wxString sourceCopy = tempDir.ChildPathStr( aSourceName );
809 BOOST_REQUIRE( wxCopyFile( aSourcePath, sourceCopy, true ) );
810
811 kiapi::common::types::DocumentSpecifier document;
812
813 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( sourceCopy, aType, &document ),
814 "OpenDocument failed: " + Client().LastError() );
815
816 wxFileName newDocFile( tempDir.ChildPathStr( "renamed" + aDocumentExt ) );
817 wxFileName newProjectFile( tempDir.ChildPathStr( "renamed.kicad_pro" ) );
818
819 kiapi::common::commands::SaveDocumentAs saveAs;
820 saveAs.mutable_document()->CopyFrom( document );
821 saveAs.set_path( tempDir.ChildPathStr( "renamed" ).ToStdString() );
822
823 kiapi::common::ApiResponse response;
824 BOOST_REQUIRE_MESSAGE( Client().SendCommand( saveAs, &response ),
825 "SaveDocumentAs failed to send: " + Client().LastError() );
826 BOOST_REQUIRE( response.status().status() == kiapi::common::AS_OK );
827
828 BOOST_CHECK( newDocFile.FileExists() );
829 BOOST_CHECK( newProjectFile.FileExists() );
830
831 kiapi::common::commands::GetOpenDocuments openDocs;
832 openDocs.set_type( aType );
833 BOOST_REQUIRE( Client().SendCommand( openDocs, &response ) );
834
835 kiapi::common::commands::GetOpenDocumentsResponse docsResponse;
836 BOOST_REQUIRE( response.message().UnpackTo( &docsResponse ) );
837 BOOST_REQUIRE_EQUAL( docsResponse.documents_size(), 1 );
838
839 kiapi::common::types::DocumentSpecifier renamedDoc = docsResponse.documents( 0 );
840
841 // TODO(JE) clean up
842 // pcb reports the project directory without a trailing separator, schematic via
843 // PackProject which uses GetProjectPath() including the separator
844 wxString expectedProjectPath = ( aType == kiapi::common::types::DOCTYPE_PCB )
845 ? newProjectFile.GetPath()
846 : newProjectFile.GetPath( wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME );
847
848 BOOST_CHECK_EQUAL( renamedDoc.project().path(), expectedProjectPath.ToStdString() );
849 BOOST_CHECK_EQUAL( renamedDoc.project().name(), newProjectFile.GetName().ToStdString() );
850
851 if( aType == kiapi::common::types::DOCTYPE_PCB )
852 BOOST_CHECK_EQUAL( renamedDoc.board_filename(), newDocFile.GetFullName().ToStdString() );
853
854 // The old specifier must now be rejected: the document identity has changed
855 kiapi::common::commands::GetDocumentModifiedState stateQuery;
856 stateQuery.mutable_document()->CopyFrom( document );
857 BOOST_CHECK( Client().SendCommand( stateQuery, &response ) );
858 BOOST_CHECK( response.status().status() != kiapi::common::AS_OK );
859
860 // The new specifier works, and saving to the new location succeeds
861 stateQuery.mutable_document()->CopyFrom( renamedDoc );
862 BOOST_CHECK( Client().SendCommand( stateQuery, &response ) );
863 BOOST_REQUIRE( response.status().status() == kiapi::common::AS_OK );
864
865 kiapi::common::commands::GetDocumentModifiedStateResponse state;
866 BOOST_REQUIRE( response.message().UnpackTo( &state ) );
867 BOOST_CHECK( state.state() == kiapi::common::commands::DocumentModifiedState::DMS_UNMODIFIED );
868
869 kiapi::common::commands::SaveDocument save;
870 save.mutable_document()->CopyFrom( renamedDoc );
871 BOOST_CHECK( Client().SendCommand( save, &response ) );
872 BOOST_CHECK( response.status().status() == kiapi::common::AS_OK );
873
874 // Closing with the new specifier must succeed; the server's bookkeeping follows the rename
875 kiapi::common::ApiStatusCode closeStatus = kiapi::common::AS_UNKNOWN;
876 BOOST_REQUIRE_MESSAGE( Client().CloseDocument( &renamedDoc, &closeStatus ),
877 "CloseDocument after SaveAs failed: " + Client().LastError() );
878 BOOST_CHECK_EQUAL( closeStatus, kiapi::common::AS_OK );
879 };
880
881 struct SaveAsCase
882 {
883 wxString sourcePath;
884 wxString sourceName;
885 wxString documentExt;
886 };
887
888 std::map<kiapi::common::types::DocumentType, SaveAsCase> cases = {
889 { kiapi::common::types::DOCTYPE_PCB,
890 { wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() ) + wxS( "api_kitchen_sink.kicad_pcb" ),
891 wxS( "api_kitchen_sink.kicad_pcb" ), wxS( ".kicad_pcb" ) } },
892 { kiapi::common::types::DOCTYPE_SCHEMATIC,
893 { wxString::FromUTF8( KI_TEST::GetEeschemaTestDataDir() ) + wxS( "api_kitchen_sink.kicad_sch" ),
894 wxS( "api_kitchen_sink.kicad_sch" ), wxS( ".kicad_sch" ) } }
895 };
896
897 for( const auto& [docType, source] : cases )
898 {
899 BOOST_TEST_CONTEXT( magic_enum::enum_name( docType ) )
900 {
901 test( docType, source.sourcePath, source.sourceName, source.documentExt );
902 }
903 }
904}
905
906
907BOOST_FIXTURE_TEST_CASE( SaveDocumentAsRejectsBadRequests, API_SERVER_E2E_FIXTURE )
908{
909 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
910
911 wxString sourcePath = wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() ) + wxS( "api_kitchen_sink.kicad_pcb" );
912
913 KI_TEST::SCOPED_TEMP_DIR tempDir( "kicad_api_e2e_saveas_reject" );
914
915 kiapi::common::types::DocumentSpecifier document;
916
917 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( sourcePath, &document ),
918 "OpenDocument failed: " + Client().LastError() );
919
920 kiapi::common::ApiResponse response;
921
922 // An empty path is invalid
923 kiapi::common::commands::SaveDocumentAs emptyPath;
924 emptyPath.mutable_document()->CopyFrom( document );
925 BOOST_CHECK( Client().SendCommand( emptyPath, &response ) );
926 BOOST_CHECK( response.status().status() == kiapi::common::AS_BAD_REQUEST );
927 BOOST_CHECK( wxString( response.status().error_message() ).Contains( wxS( "path is required" ) ) );
928
929 // A stale document specifier is rejected by validation before any file is written
930 kiapi::common::commands::SaveDocumentAs staleDoc;
931 staleDoc.mutable_document()->CopyFrom( document );
932 staleDoc.mutable_document()->set_board_filename( "no_such_board.kicad_pcb" );
933 staleDoc.set_path( tempDir.ChildPathStr( "never_written" ).ToStdString() );
934 BOOST_CHECK( Client().SendCommand( staleDoc, &response ) );
935 BOOST_CHECK( response.status().status() == kiapi::common::AS_BAD_REQUEST );
936
937 BOOST_CHECK( !wxFileName( tempDir.ChildPathStr( "never_written.kicad_pcb" ) ).FileExists() );
938
939 Client().CloseAllDocuments();
940}
941
942
943BOOST_FIXTURE_TEST_CASE( SaveDocumentAsSwitchesProject, API_SERVER_E2E_FIXTURE )
944{
945 BOOST_REQUIRE_MESSAGE( Start(), LastError() );
946
947 wxString sourcePath = wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() ) + wxS( "api_kitchen_sink.kicad_pcb" );
948
949 KI_TEST::SCOPED_TEMP_DIR tempDir( "kicad_api_e2e_saveas_project" );
950
951 kiapi::common::types::DocumentSpecifier document;
952
953 BOOST_REQUIRE_MESSAGE( Client().OpenDocument( sourcePath, &document ),
954 "OpenDocument failed: " + Client().LastError() );
955
956 // Save into a new subdirectory: the document and its project move there
957 wxString subdir = tempDir.CreateChildDirStr( "subdir" );
958
959 kiapi::common::commands::SaveDocumentAs saveAs;
960 saveAs.mutable_document()->CopyFrom( document );
961 saveAs.set_path( ( subdir + wxS( "/moved" ) ).ToStdString() );
962
963 kiapi::common::ApiResponse response;
964 BOOST_REQUIRE_MESSAGE( Client().SendCommand( saveAs, &response ),
965 "SaveDocumentAs failed to send: " + Client().LastError() );
966 BOOST_REQUIRE( response.status().status() == kiapi::common::AS_OK );
967
968 BOOST_CHECK( wxFileName( subdir + wxS( "/moved.kicad_pcb" ) ).FileExists() );
969 BOOST_CHECK( wxFileName( subdir + wxS( "/moved.kicad_pro" ) ).FileExists() );
970
971 // The project must have switched
972 kiapi::common::types::DocumentSpecifier otherBoard;
973 BOOST_REQUIRE_MESSAGE( !Client().OpenDocument( sourcePath, &otherBoard ),
974 "Opening a board from the old project directory should have failed" );
975 BOOST_CHECK( Client().LastError().Contains( wxS( "already open" ) ) );
976
977 Client().CloseAllDocuments();
978}
979
981
Per-test fixture for API E2E tests.
const wxString & LastError() const
bool SendCommand(const T &aMessage, kiapi::common::ApiResponse *aResponse)
const KIID m_Uuid
Definition eda_item.h:599
double Similarity(const BOARD_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
std::string AsStdString() const
Definition kiid.cpp:270
wxString ChildPathStr(const wxString &aName) const
Get the path to a direct child of the temporary directory as a wxString, without creating the child.
wxString CreateChildDirStr(const wxString &aName) const
Create and return the path to a direct child directory as a wxString.
const wxString & BoardPath() const
bool Create(wxString *aError, const wxString &aRulesName=wxS("api_kitchen_sink.kicad_dru"))
KI_TEST::SCOPED_TEMP_DIR m_tempRoot
static std::string ToStdString(const wxString &aStr)
std::string source
std::string GetTestDataRootDir()
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
@ REFERENCE
Field Reference of part, i.e. "IC21".
bool SendGetCustomDesignRules(API_TEST_CLIENT &aClient, const kiapi::common::types::DocumentSpecifier &aDocument, kiapi::board::commands::CustomRulesResponse *aOut, wxString *aError=nullptr)
BOOST_FIXTURE_TEST_CASE(ServerStartsAndResponds, API_SERVER_E2E_FIXTURE)
bool SendGetBoardDesignRules(API_TEST_CLIENT &aClient, const kiapi::common::types::DocumentSpecifier &aDocument, kiapi::board::commands::BoardDesignRulesResponse *aOut, wxString *aError=nullptr)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
std::string path
BOOST_TEST_CONTEXT("Test Clearance")