KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue24477_http_network_loss.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
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your 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
14 * GNU 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, you may find one here:
18 * https://www.gnu.org/licenses/gpl-3.0.en.html
19 */
20
33
34#include <boost/test/unit_test.hpp>
35
36#include <cstring>
37
38#include <wx/filename.h>
39#include <wx/wfstream.h>
40
44#include <lib_symbol.h>
46#include <ki_exception.h>
47
48
49BOOST_AUTO_TEST_SUITE( Issue24477HttpNetworkLoss )
50
51
52
57static wxFileName writeUnreachableHttpLib()
58{
59 wxFileName settings( wxFileName::CreateTempFileName( "kicad_qa_issue24477_" ) );
60 wxRemoveFile( settings.GetFullPath() );
61 settings.SetExt( "kicad_httplib" );
62
63 wxFFileOutputStream out( settings.GetFullPath() );
64 BOOST_REQUIRE( out.IsOk() );
65
66 const char* json =
67 "{\n"
68 " \"meta\": { \"version\": 1.0 },\n"
69 " \"name\": \"Unreachable HTTP Library\",\n"
70 " \"source\": {\n"
71 " \"type\": \"REST_API\",\n"
72 " \"api_version\": \"v1\",\n"
73 " \"root_url\": \"http://localhost:1/\",\n"
74 " \"token\": \"\"\n"
75 " }\n"
76 "}\n";
77
78 out.WriteAll( json, std::strlen( json ) );
79 out.Close();
80
81 return settings;
82}
83
84
87BOOST_AUTO_TEST_CASE( SubLibraryQueriesSurviveNetworkLoss )
88{
89 wxFileName settings = writeUnreachableHttpLib();
90
91 LIBRARY_MANAGER manager;
92 SYMBOL_LIBRARY_ADAPTER adapter( manager );
93
94 SCH_IO_HTTP_LIB plugin;
95 plugin.SetLibraryManagerAdapter( &adapter );
96
97 // Opening the library caches its settings and then fails to connect, exactly as it does
98 // while a schematic referencing the library is opened with the network down. This leaves
99 // the plugin with valid settings but a null connection, the state the tree build hit.
100 std::vector<LIB_SYMBOL*> symbols;
101 BOOST_CHECK_THROW( plugin.EnumerateSymbolLib( symbols, settings.GetFullPath() ), IO_ERROR );
102
103 // GetSubLibraryNames previously dereferenced the null connection. It must now fail
104 // gracefully and leave the result empty.
105 std::vector<wxString> names = { wxS( "stale" ) };
106 BOOST_REQUIRE_NO_THROW( plugin.GetSubLibraryNames( names ) );
107 BOOST_CHECK( names.empty() );
108
109 // GetSubLibraryDescription dereferenced the connection with no settings/connection
110 // guard at all. It must now return empty rather than crash.
111 wxString desc;
112 BOOST_REQUIRE_NO_THROW( desc = plugin.GetSubLibraryDescription( wxS( "anything" ) ) );
113 BOOST_CHECK( desc.IsEmpty() );
114
115 wxRemoveFile( settings.GetFullPath() );
116}
117
118
121BOOST_AUTO_TEST_CASE( ConnectionReportsInvalidEndpointOnNetworkLoss )
122{
123 HTTP_LIB_SOURCE source;
124 source.root_url = "http://localhost:1/v1/";
125 source.token = "";
126 source.api_version = "v1";
127
128 HTTP_LIB_CONNECTION conn( source, true );
129
130 BOOST_CHECK( !conn.IsValidEndpoint() );
131 BOOST_CHECK( !conn.GetLastError().empty() );
132}
133
134
std::string GetLastError() const
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
A KiCad HTTP library provides both symbol and footprint metadata, so there are "shim" plugins on both...
wxString GetSubLibraryDescription(const wxString &aName) override
Gets a description of a sublibrary.
void SetLibraryManagerAdapter(SYMBOL_LIBRARY_ADAPTER *aAdapter) override
Some library plugins need to interface with other loaded libraries.
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
void GetSubLibraryNames(std::vector< wxString > &aNames) override
Retrieves a list of sub-libraries in this library.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
nlohmann::json json
Definition gerbview.cpp:49
std::string api_version
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(SubLibraryQueriesSurviveNetworkLoss)
The sub-library queries used while building the library tree must not crash when the HTTP endpoint is...
static wxFileName writeUnreachableHttpLib()
Issue #24477.