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
46 std::unique_ptr<LIB_SYMBOL> InvokeWithCycle( SCH_IO_DATABASE& aPlugin,
47 const wxString& aSymbolName,
48 const DATABASE_LIB_TABLE& aTable,
49 const DATABASE_CONNECTION::ROW& aRow,
50 const wxString& aInProgressKey )
51 {
52 aPlugin.m_inProgressLoads.insert( aInProgressKey );
53 return aPlugin.loadSymbolFromRow( aSymbolName, aTable, aRow );
54 }
55
58 std::unique_ptr<LIB_SYMBOL> Invoke( SCH_IO_DATABASE& aPlugin,
59 const wxString& aSymbolName,
60 const DATABASE_LIB_TABLE& aTable,
61 const DATABASE_CONNECTION::ROW& aRow )
62 {
63 return aPlugin.loadSymbolFromRow( aSymbolName, aTable, aRow );
64 }
65
66 bool IsInProgressEmpty( const SCH_IO_DATABASE& aPlugin ) const
67 {
68 return aPlugin.m_inProgressLoads.empty();
69 }
70
71 std::size_t InProgressSize( const SCH_IO_DATABASE& aPlugin ) const
72 {
73 return aPlugin.m_inProgressLoads.size();
74 }
75};
76
77
78BOOST_AUTO_TEST_SUITE( SchIoDatabaseCycle )
79
80
81// m_adapter is deliberately left null: if the cycle short-circuit fails, loadSymbolFromRow
82// dereferences the null adapter and crashes the test. So this fails on revert of the fix.
83BOOST_AUTO_TEST_CASE( DetectsAlreadyInProgressLibId )
84{
85 SCH_IO_DATABASE plugin;
86
88 table.name = "SelfRefDB";
89 table.table = "Parts";
90 table.key_col = "Part ID";
91 table.symbols_col = "Symbols";
92
94 row[table.symbols_col] = std::string( "SelfRefDB:R-001" );
95
97
98 std::unique_ptr<LIB_SYMBOL> symbol =
99 fixture.InvokeWithCycle( plugin, wxS( "R-001" ), table, row, wxS( "SelfRefDB:R-001" ) );
100
101 BOOST_REQUIRE( symbol );
102 BOOST_CHECK( symbol->GetName() == wxS( "R-001" ) );
103
104 // The re-insert of the already-seeded key must observe it as present and so must not erase
105 // the fixture's entry on the way out.
106 BOOST_CHECK_EQUAL( fixture.InProgressSize( plugin ), 1u );
107}
108
109
110// A leaked in-progress entry would make a later load of the same LIB_ID a false positive cycle.
111// Drive two loads through a real (empty) adapter and require the set to be empty after each, so
112// a broken RAII removal is caught.
113BOOST_AUTO_TEST_CASE( RaiiGuardBalancesInProgressSet )
114{
115 LIBRARY_MANAGER libManager;
116 SYMBOL_LIBRARY_ADAPTER adapter( libManager );
117
118 SCH_IO_DATABASE plugin;
119 plugin.SetLibraryManagerAdapter( &adapter );
120
122 table.name = "SelfRefDB";
123 table.table = "Parts";
124 table.key_col = "Part ID";
125 table.symbols_col = "Symbols";
126
128 // Valid LIB_ID that will be tracked. The adapter has no libraries loaded so its LoadSymbol
129 // returns null without ever re-entering SCH_IO_DATABASE.
130 row[table.symbols_col] = std::string( "SomeLib:R-001" );
131
133 BOOST_REQUIRE( fixture.IsInProgressEmpty( plugin ) );
134
135 std::unique_ptr<LIB_SYMBOL> symbol = fixture.Invoke( plugin, wxS( "R-001" ), table, row );
136
137 BOOST_REQUIRE( symbol );
138 BOOST_CHECK( fixture.IsInProgressEmpty( plugin ) );
139
140 // A second call on the same row must not see leftover state from the first.
141 symbol = fixture.Invoke( plugin, wxS( "R-001" ), table, row );
142 BOOST_REQUIRE( symbol );
143 BOOST_CHECK( fixture.IsInProgressEmpty( plugin ) );
144}
145
146
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 ...
std::unordered_set< wxString > m_inProgressLoads
LIB_IDs whose resolution is in flight, used to break self-referential cycles where a row's Symbols co...
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...
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()
std::vector< std::vector< std::string > > table
BOOST_AUTO_TEST_CASE(DetectsAlreadyInProgressLibId)
BOOST_CHECK_EQUAL(result, "25.4")