KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_remote_symbol_load_from_payload.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,
12 * but 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
28
30
31#include <lib_symbol.h>
33#include <sch_io/sch_io_mgr.h>
34
35#include <wx/filefn.h>
36#include <wx/filename.h>
37#include <wx/ffile.h>
38
39
40namespace
41{
42const char* kMinimalSymbolLib =
43 "(kicad_symbol_lib (version 20220914) (generator kicad_symbol_editor)\n"
44 " (symbol \"TestResistor\" (in_bom yes) (on_board yes)\n"
45 " (property \"Reference\" \"R\" (at 0 0 0)\n"
46 " (effects (font (size 1.27 1.27)))\n"
47 " )\n"
48 " (property \"Value\" \"TestResistor\" (at 0 0 0)\n"
49 " (effects (font (size 1.27 1.27)))\n"
50 " )\n"
51 " (property \"Footprint\" \"\" (at 0 0 0)\n"
52 " (effects (font (size 1.27 1.27)) hide)\n"
53 " )\n"
54 " (property \"Datasheet\" \"\" (at 0 0 0)\n"
55 " (effects (font (size 1.27 1.27)) hide)\n"
56 " )\n"
57 " (symbol \"TestResistor_0_1\"\n"
58 " (rectangle (start -1.27 -1.27) (end 1.27 1.27)\n"
59 " (stroke (width 0) (type default))\n"
60 " (fill (type background))\n"
61 " )\n"
62 " )\n"
63 " (symbol \"TestResistor_1_1\"\n"
64 " (pin passive line (at -3.81 0 0) (length 2.54)\n"
65 " (name \"PIN\" (effects (font (size 1.27 1.27))))\n"
66 " (number \"1\" (effects (font (size 1.27 1.27))))\n"
67 " )\n"
68 " )\n"
69 " )\n"
70 ")\n";
71
72
73wxString writeTempSymbolLib()
74{
75 wxString path = wxFileName::CreateTempFileName( wxS( "qa_remote_sym" ) );
76 wxFFile file( path, wxS( "wb" ) );
77 BOOST_REQUIRE( file.IsOpened() );
78 file.Write( kMinimalSymbolLib, strlen( kMinimalSymbolLib ) );
79 file.Close();
80 return path;
81}
82} // namespace
83
84
85BOOST_AUTO_TEST_SUITE( RemoteSymbolLoadFromPayload )
86
87
88BOOST_AUTO_TEST_CASE( CopiedSymbolSurvivesPluginDestruction )
89{
90 const wxString tempPath = writeTempSymbolLib();
91 std::unique_ptr<LIB_SYMBOL> symbol;
92
93 {
94 IO_RELEASER<SCH_IO> plugin( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
95 BOOST_REQUIRE( plugin );
96
97 LIB_SYMBOL* loaded = plugin->LoadSymbol( tempPath, wxS( "TestResistor" ) );
98 BOOST_REQUIRE( loaded );
99
100 // The fix for #23286: copy the symbol so it is independent of the
101 // plugin's cache, which is destroyed when the plugin goes out of scope.
102 symbol = std::make_unique<LIB_SYMBOL>( *loaded );
103 }
104
105 // Plugin and its cache have been destroyed. The copied symbol must still
106 // be valid and accessible without triggering a use-after-free.
107 BOOST_CHECK( symbol );
108 BOOST_CHECK( symbol->GetName() == wxS( "TestResistor" ) );
109
110 wxRemoveFile( tempPath );
111}
112
113
114BOOST_AUTO_TEST_CASE( LoadSymbolReturnsNullForMissingName )
115{
116 const wxString tempPath = writeTempSymbolLib();
117
118 IO_RELEASER<SCH_IO> plugin( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
119 BOOST_REQUIRE( plugin );
120
121 LIB_SYMBOL* loaded = plugin->LoadSymbol( tempPath, wxS( "NonExistentSymbol" ) );
122 BOOST_CHECK( loaded == nullptr );
123
124 wxRemoveFile( tempPath );
125}
126
127
Define a library symbol object.
Definition lib_symbol.h:83
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
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::string path
BOOST_AUTO_TEST_CASE(CopiedSymbolSurvivesPluginDestruction)