KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_navlib_safe_init.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
21#include <navlib_safe_init.h>
22#include <stdexcept>
23
24#if !defined( __WINDOWS__ )
25#include <sys/wait.h>
26#include <unistd.h>
27#endif
28
29BOOST_AUTO_TEST_SUITE( NavlibSafeInit )
30
31
32BOOST_AUTO_TEST_CASE( SuccessfulInit )
33{
34 bool called = false;
35
36 bool result = SafeNavlibInit( [&called]()
37 {
38 called = true;
39 } );
40
41 BOOST_CHECK( result );
42 BOOST_CHECK( called );
43 BOOST_CHECK( !NavlibDriverCrashed() );
44}
45
46
47BOOST_AUTO_TEST_CASE( ExceptionHandling )
48{
49 bool result = SafeNavlibInit( []()
50 {
51 throw std::runtime_error( "test exception" );
52 } );
53
54 BOOST_CHECK( !result );
55 BOOST_CHECK( !NavlibDriverCrashed() );
56}
57
58
59BOOST_AUTO_TEST_CASE( SystemErrorHandling )
60{
61 bool result = SafeNavlibInit( []()
62 {
63 throw std::system_error( std::make_error_code( std::errc::connection_refused ),
64 "test system error" );
65 } );
66
67 BOOST_CHECK( !result );
68 BOOST_CHECK( !NavlibDriverCrashed() );
69}
70
71
72BOOST_AUTO_TEST_CASE( UnknownExceptionHandling )
73{
74 bool result = SafeNavlibInit( []()
75 {
76 throw 42;
77 } );
78
79 BOOST_CHECK( !result );
80 BOOST_CHECK( !NavlibDriverCrashed() );
81}
82
83
84#if !defined( __WINDOWS__ )
85BOOST_AUTO_TEST_CASE( AbortRecovery )
86{
87 // Test that SafeNavlibInit recovers from abort() by running the test in a
88 // child process. The parent checks the child's exit status.
89 pid_t pid = fork();
90
91 if( pid == 0 )
92 {
93 // Child process: test that SafeNavlibInit catches abort()
94 bool result = SafeNavlibInit( []()
95 {
96 abort();
97 } );
98
99 // If we get here, recovery worked
100 _exit( result ? 1 : 0 );
101 }
102 else
103 {
104 BOOST_REQUIRE( pid > 0 );
105
106 int status = 0;
107 waitpid( pid, &status, 0 );
108
109 // The child should have exited normally (not killed by signal) with code 0
110 // (indicating SafeNavlibInit returned false after catching the abort)
111 BOOST_CHECK_MESSAGE( WIFEXITED( status ),
112 "Child process should exit normally after abort recovery" );
113
114 if( WIFEXITED( status ) )
115 {
116 BOOST_CHECK_EQUAL( WEXITSTATUS( status ), 0 );
117 }
118 }
119}
120#endif
121
122
bool SafeNavlibInit(const std::function< void()> &aInitFunc)
Attempt to run the given function, recovering from both C++ exceptions and abort() calls triggered by...
bool NavlibDriverCrashed()
Returns true if a previous SafeNavlibInit call detected a driver crash.
Safe initialization wrapper for the 3Dconnexion navlib SDK.
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(SuccessfulInit)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")