KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sch_io_database_cycle.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
23#include <lib_symbol.h>
26
27// We need access to private members of SCH_IO_DATABASE to drive the cycle-detection
28// state directly. The plugin declares SCH_IO_DATABASE_CYCLE_DETECTION_FIXTURE as a friend.
30
31
40{
41public:
43 {
44 // s_inProgressLoads is static per thread, make sure any prior tests tidied up
45 // after themselves.
46 BOOST_REQUIRE_MESSAGE( SCH_IO_DATABASE::s_inProgressLoads.empty(),
47 "s_inProgressLoads is not empty at test start" );
48 }
49
51 {
52 // Do not leak our in-progress loads into subsequent tests.
54 }
55
58 std::unique_ptr<LIB_SYMBOL> InvokeWithCycle( SCH_IO_DATABASE& aPlugin,
59 const wxString& aSymbolName,
60 const DATABASE_LIB_TABLE& aTable,
61 const DATABASE_CONNECTION::ROW& aRow,
62 const wxString& aInProgressKey )
63 {
64 aPlugin.s_inProgressLoads.insert( aInProgressKey );
65 return aPlugin.loadSymbolFromRow( aSymbolName, aTable, aRow );
66 }
67
70 std::unique_ptr<LIB_SYMBOL> Invoke( SCH_IO_DATABASE& aPlugin,
71 const wxString& aSymbolName,
72 const DATABASE_LIB_TABLE& aTable,
73 const DATABASE_CONNECTION::ROW& aRow )
74 {
75 return aPlugin.loadSymbolFromRow( aSymbolName, aTable, aRow );
76 }
77
78 bool IsInProgressEmpty( const SCH_IO_DATABASE& aPlugin ) const
79 {
80 return aPlugin.s_inProgressLoads.empty();
81 }
82
83 std::size_t InProgressSize( const SCH_IO_DATABASE& aPlugin ) const
84 {
85 return aPlugin.s_inProgressLoads.size();
86 }
87};
88
89
90BOOST_AUTO_TEST_SUITE( SchIoDatabaseCycle )
91
92
93// m_adapter is deliberately left null: if the cycle short-circuit fails, loadSymbolFromRow
94// dereferences the null adapter and crashes the test. So this fails on revert of the fix.
95BOOST_AUTO_TEST_CASE( DetectsAlreadyInProgressLibId )
96{
97 SCH_IO_DATABASE plugin;
98
100 table.name = "SelfRefDB";
101 table.table = "Parts";
102 table.key_col = "Part ID";
103 table.symbols_col = "Symbols";
104
106 row[table.symbols_col] = std::string( "SelfRefDB:R-001" );
107
109
110 std::unique_ptr<LIB_SYMBOL> symbol =
111 fixture.InvokeWithCycle( plugin, wxS( "R-001" ), table, row, wxS( "SelfRefDB:R-001" ) );
112
113 BOOST_REQUIRE( symbol );
114 BOOST_CHECK( symbol->GetName() == wxS( "R-001" ) );
115
116 // The re-insert of the already-seeded key must observe it as present and so must not erase
117 // the fixture's entry on the way out.
118 BOOST_CHECK_EQUAL( fixture.InProgressSize( plugin ), 1u );
119}
120
121
122// A leaked in-progress entry would make a later load of the same LIB_ID a false positive cycle.
123// Drive two loads through a real (empty) adapter and require the set to be empty after each, so
124// a broken RAII removal is caught.
125BOOST_AUTO_TEST_CASE( RaiiGuardBalancesInProgressSet )
126{
127 LIBRARY_MANAGER libManager;
128 SYMBOL_LIBRARY_ADAPTER adapter( libManager );
129
130 SCH_IO_DATABASE plugin;
131 plugin.SetLibraryManagerAdapter( &adapter );
132
134 table.name = "SelfRefDB";
135 table.table = "Parts";
136 table.key_col = "Part ID";
137 table.symbols_col = "Symbols";
138
140 // Valid LIB_ID that will be tracked. The adapter has no libraries loaded so its LoadSymbol
141 // returns null without ever re-entering SCH_IO_DATABASE.
142 row[table.symbols_col] = std::string( "SomeLib:R-001" );
143
145
146 std::unique_ptr<LIB_SYMBOL> symbol = fixture.Invoke( plugin, wxS( "R-001" ), table, row );
147
148 BOOST_REQUIRE( symbol );
149 BOOST_CHECK( fixture.IsInProgressEmpty( plugin ) );
150
151 // A second call on the same row must not see leftover state from the first.
152 symbol = fixture.Invoke( plugin, wxS( "R-001" ), table, row );
153 BOOST_REQUIRE( symbol );
154 BOOST_CHECK( fixture.IsInProgressEmpty( plugin ) );
155}
156
157
std::map< std::string, std::any > ROW
Regression test for https://gitlab.com/kicad/code/kicad/-/issues/24249.
std::unique_ptr< LIB_SYMBOL > InvokeWithCycle(SCH_IO_DATABASE &aPlugin, const wxString &aSymbolName, const DATABASE_LIB_TABLE &aTable, const DATABASE_CONNECTION::ROW &aRow, const wxString &aInProgressKey)
Drive loadSymbolFromRow with aInProgressKey already marked in-progress, simulating the second entry o...
std::size_t InProgressSize(const SCH_IO_DATABASE &aPlugin) const
bool IsInProgressEmpty(const SCH_IO_DATABASE &aPlugin) const
std::unique_ptr< LIB_SYMBOL > Invoke(SCH_IO_DATABASE &aPlugin, const wxString &aSymbolName, const DATABASE_LIB_TABLE &aTable, const DATABASE_CONNECTION::ROW &aRow)
Drive loadSymbolFromRow without pre-seeding the in-progress set; the plugin inserts and (via its RAII...
A KiCad database library provides both symbol and footprint metadata, so there are "shim" plugins on ...
static thread_local std::unordered_set< wxString > s_inProgressLoads
LIB_IDs whose resolution is in flight on the calling thread, used to break self-referential cycles wh...
void SetLibraryManagerAdapter(SYMBOL_LIBRARY_ADAPTER *aAdapter) override
Some library plugins need to interface with other loaded libraries.
std::unique_ptr< LIB_SYMBOL > loadSymbolFromRow(const wxString &aSymbolName, const DATABASE_LIB_TABLE &aTable, const DATABASE_CONNECTION::ROW &aRow)
An interface to the global shared library manager that is schematic-specific and linked to one projec...
static bool empty(const wxTextEntryBase *aCtrl)
A database library table will be mapped to a sub-library provided by the database library entry in th...
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(DetectsAlreadyInProgressLibId)
BOOST_CHECK_EQUAL(result, "25.4")