33
33
#include " gtest/gtest-death-test.h"
34
34
35
35
#include < functional>
36
+ #include < memory>
37
+ #include < sstream>
38
+ #include < string>
36
39
#include < utility>
40
+ #include < vector>
37
41
38
42
#include " gtest/internal/custom/gtest.h"
39
43
#include " gtest/internal/gtest-port.h"
40
44
41
- #if GTEST_HAS_DEATH_TEST
45
+ #ifdef GTEST_HAS_DEATH_TEST
42
46
43
- #if GTEST_OS_MAC
47
+ #ifdef GTEST_OS_MAC
44
48
#include < crt_externs.h>
45
49
#endif // GTEST_OS_MAC
46
50
47
51
#include < errno.h>
48
52
#include < fcntl.h>
49
53
#include < limits.h>
50
54
51
- #if GTEST_OS_LINUX
55
+ #ifdef GTEST_OS_LINUX
52
56
#include < signal.h>
53
57
#endif // GTEST_OS_LINUX
54
58
55
59
#include < stdarg.h>
56
60
57
- #if GTEST_OS_WINDOWS
61
+ #ifdef GTEST_OS_WINDOWS
58
62
#include < windows.h>
59
63
#else
60
64
#include < sys/mman.h>
61
65
#include < sys/wait.h>
62
66
#endif // GTEST_OS_WINDOWS
63
67
64
- #if GTEST_OS_QNX
68
+ #ifdef GTEST_OS_QNX
65
69
#include < spawn.h>
66
70
#endif // GTEST_OS_QNX
67
71
68
- #if GTEST_OS_FUCHSIA
72
+ #ifdef GTEST_OS_FUCHSIA
69
73
#include < lib/fdio/fd.h>
70
74
#include < lib/fdio/io.h>
71
75
#include < lib/fdio/spawn.h>
@@ -131,13 +135,13 @@ GTEST_DEFINE_string_(
131
135
132
136
namespace testing {
133
137
134
- #if GTEST_HAS_DEATH_TEST
138
+ #ifdef GTEST_HAS_DEATH_TEST
135
139
136
140
namespace internal {
137
141
138
142
// Valid only for fast death tests. Indicates the code is running in the
139
143
// child process of a fast style death test.
140
- #if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA
144
+ #if !defined( GTEST_OS_WINDOWS) && !defined( GTEST_OS_FUCHSIA)
141
145
static bool g_in_fast_death_test_child = false ;
142
146
#endif
143
147
@@ -147,7 +151,7 @@ static bool g_in_fast_death_test_child = false;
147
151
// tests. IMPORTANT: This is an internal utility. Using it may break the
148
152
// implementation of death tests. User code MUST NOT use it.
149
153
bool InDeathTestChild () {
150
- #if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
154
+ #if defined( GTEST_OS_WINDOWS) || defined( GTEST_OS_FUCHSIA)
151
155
152
156
// On Windows and Fuchsia, death tests are thread-safe regardless of the value
153
157
// of the death_test_style flag.
@@ -169,7 +173,7 @@ ExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) {}
169
173
170
174
// ExitedWithCode function-call operator.
171
175
bool ExitedWithCode::operator ()(int exit_status) const {
172
- #if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
176
+ #if defined( GTEST_OS_WINDOWS) || defined( GTEST_OS_FUCHSIA)
173
177
174
178
return exit_status == exit_code_;
175
179
@@ -180,7 +184,7 @@ bool ExitedWithCode::operator()(int exit_status) const {
180
184
#endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
181
185
}
182
186
183
- #if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA
187
+ #if !defined( GTEST_OS_WINDOWS) && !defined( GTEST_OS_FUCHSIA)
184
188
// KilledBySignal constructor.
185
189
KilledBySignal::KilledBySignal (int signum) : signum_(signum) {}
186
190
@@ -207,7 +211,7 @@ namespace internal {
207
211
static std::string ExitSummary (int exit_code) {
208
212
Message m;
209
213
210
- #if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
214
+ #if defined( GTEST_OS_WINDOWS) || defined( GTEST_OS_FUCHSIA)
211
215
212
216
m << " Exited with exit status " << exit_code;
213
217
@@ -234,7 +238,7 @@ bool ExitedUnsuccessfully(int exit_status) {
234
238
return !ExitedWithCode (0 )(exit_status);
235
239
}
236
240
237
- #if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA
241
+ #if !defined( GTEST_OS_WINDOWS) && !defined( GTEST_OS_FUCHSIA)
238
242
// Generates a textual failure message when a death test finds more than
239
243
// one thread running, or cannot determine the number of threads, prior
240
244
// to executing the given statement. It is the responsibility of the
@@ -263,7 +267,7 @@ static const char kDeathTestReturned = 'R';
263
267
static const char kDeathTestThrew = ' T' ;
264
268
static const char kDeathTestInternalError = ' I' ;
265
269
266
- #if GTEST_OS_FUCHSIA
270
+ #ifdef GTEST_OS_FUCHSIA
267
271
268
272
// File descriptor used for the pipe in the child process.
269
273
static const int kFuchsiaReadPipeFd = 3 ;
@@ -621,7 +625,21 @@ bool DeathTestImpl::Passed(bool status_ok) {
621
625
return success;
622
626
}
623
627
624
- #if GTEST_OS_WINDOWS
628
+ #ifndef GTEST_OS_WINDOWS
629
+ // Note: The return value points into args, so the return value's lifetime is
630
+ // bound to that of args.
631
+ static std::unique_ptr<char *[]> CreateArgvFromArgs (
632
+ std::vector<std::string>& args) {
633
+ auto result = std::make_unique<char *[]>(args.size () + 1 );
634
+ for (size_t i = 0 ; i < args.size (); ++i) {
635
+ result[i] = &args[i][0 ];
636
+ }
637
+ result[args.size ()] = nullptr ; // extra null terminator
638
+ return result;
639
+ }
640
+ #endif
641
+
642
+ #ifdef GTEST_OS_WINDOWS
625
643
// WindowsDeathTest implements death tests on Windows. Due to the
626
644
// specifics of starting new processes on Windows, death tests there are
627
645
// always threadsafe, and Google Test considers the
@@ -808,7 +826,7 @@ DeathTest::TestRole WindowsDeathTest::AssumeRole() {
808
826
return OVERSEE_TEST;
809
827
}
810
828
811
- #elif GTEST_OS_FUCHSIA
829
+ #elif defined( GTEST_OS_FUCHSIA)
812
830
813
831
class FuchsiaDeathTest : public DeathTestImpl {
814
832
public:
@@ -836,36 +854,6 @@ class FuchsiaDeathTest : public DeathTestImpl {
836
854
zx::socket stderr_socket_;
837
855
};
838
856
839
- // Utility class for accumulating command-line arguments.
840
- class Arguments {
841
- public:
842
- Arguments () { args_.push_back (nullptr ); }
843
-
844
- ~Arguments () {
845
- for (std::vector<char *>::iterator i = args_.begin (); i != args_.end ();
846
- ++i) {
847
- free (*i);
848
- }
849
- }
850
- void AddArgument (const char * argument) {
851
- args_.insert (args_.end () - 1 , posix::StrDup (argument));
852
- }
853
-
854
- template <typename Str>
855
- void AddArguments (const ::std::vector<Str>& arguments) {
856
- for (typename ::std::vector<Str>::const_iterator i = arguments.begin ();
857
- i != arguments.end (); ++i) {
858
- args_.insert (args_.end () - 1 , posix::StrDup (i->c_str ()));
859
- }
860
- }
861
- char * const * Argv () { return &args_[0 ]; }
862
-
863
- int size () { return static_cast <int >(args_.size ()) - 1 ; }
864
-
865
- private:
866
- std::vector<char *> args_;
867
- };
868
-
869
857
// Waits for the child in a death test to exit, returning its exit
870
858
// status, or 0 if no child process exists. As a side effect, sets the
871
859
// outcome data member.
@@ -986,10 +974,10 @@ DeathTest::TestRole FuchsiaDeathTest::AssumeRole() {
986
974
kInternalRunDeathTestFlag + " =" + file_ +
987
975
" |" + StreamableToString (line_) + " |" +
988
976
StreamableToString (death_test_index);
989
- Arguments args;
990
- args. AddArguments ( GetInjectableArgvs () );
991
- args.AddArgument (filter_flag. c_str () );
992
- args.AddArgument (internal_flag. c_str () );
977
+
978
+ std::vector<std::string> args = GetInjectableArgvs ();
979
+ args.push_back (filter_flag);
980
+ args.push_back (internal_flag);
993
981
994
982
// Build the pipe for communication with the child.
995
983
zx_status_t status;
@@ -1041,8 +1029,14 @@ DeathTest::TestRole FuchsiaDeathTest::AssumeRole() {
1041
1029
GTEST_DEATH_TEST_CHECK_ (status == ZX_OK);
1042
1030
1043
1031
// Spawn the child process.
1044
- status = fdio_spawn_etc (child_job, FDIO_SPAWN_CLONE_ALL, args.Argv ()[0 ],
1045
- args.Argv (), nullptr , 2 , spawn_actions,
1032
+ // Note: The test component must have `fuchsia.process.Launcher` declared
1033
+ // in its manifest. (Fuchsia integration tests require creating a
1034
+ // "Fuchsia Test Component" which contains a "Fuchsia Component Manifest")
1035
+ // Launching processes is a privileged operation in Fuchsia, and the
1036
+ // declaration indicates that the ability is required for the component.
1037
+ std::unique_ptr<char *[]> argv = CreateArgvFromArgs (args);
1038
+ status = fdio_spawn_etc (child_job, FDIO_SPAWN_CLONE_ALL, argv[0 ], argv.get (),
1039
+ nullptr , 2 , spawn_actions,
1046
1040
child_process_.reset_and_get_address (), nullptr );
1047
1041
GTEST_DEATH_TEST_CHECK_ (status == ZX_OK);
1048
1042
@@ -1134,7 +1128,7 @@ DeathTest::TestRole NoExecDeathTest::AssumeRole() {
1134
1128
LogToStderr ();
1135
1129
// Event forwarding to the listeners of event listener API mush be shut
1136
1130
// down in death test subprocesses.
1137
- GetUnitTestImpl ()->listeners ()->SuppressEventForwarding ();
1131
+ GetUnitTestImpl ()->listeners ()->SuppressEventForwarding (true );
1138
1132
g_in_fast_death_test_child = true ;
1139
1133
return EXECUTE_TEST;
1140
1134
} else {
@@ -1173,42 +1167,14 @@ class ExecDeathTest : public ForkingDeathTest {
1173
1167
const int line_;
1174
1168
};
1175
1169
1176
- // Utility class for accumulating command-line arguments.
1177
- class Arguments {
1178
- public:
1179
- Arguments () { args_.push_back (nullptr ); }
1180
-
1181
- ~Arguments () {
1182
- for (std::vector<char *>::iterator i = args_.begin (); i != args_.end ();
1183
- ++i) {
1184
- free (*i);
1185
- }
1186
- }
1187
- void AddArgument (const char * argument) {
1188
- args_.insert (args_.end () - 1 , posix::StrDup (argument));
1189
- }
1190
-
1191
- template <typename Str>
1192
- void AddArguments (const ::std::vector<Str>& arguments) {
1193
- for (typename ::std::vector<Str>::const_iterator i = arguments.begin ();
1194
- i != arguments.end (); ++i) {
1195
- args_.insert (args_.end () - 1 , posix::StrDup (i->c_str ()));
1196
- }
1197
- }
1198
- char * const * Argv () { return &args_[0 ]; }
1199
-
1200
- private:
1201
- std::vector<char *> args_;
1202
- };
1203
-
1204
1170
// A struct that encompasses the arguments to the child process of a
1205
1171
// threadsafe-style death test process.
1206
1172
struct ExecDeathTestArgs {
1207
1173
char * const * argv; // Command-line arguments for the child's call to exec
1208
1174
int close_fd; // File descriptor to close; the read end of a pipe
1209
1175
};
1210
1176
1211
- #if GTEST_OS_QNX
1177
+ #ifdef GTEST_OS_QNX
1212
1178
extern " C" char ** environ;
1213
1179
#else // GTEST_OS_QNX
1214
1180
// The main function for a threadsafe-style death test child process.
@@ -1289,7 +1255,7 @@ static pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) {
1289
1255
ExecDeathTestArgs args = {argv, close_fd};
1290
1256
pid_t child_pid = -1 ;
1291
1257
1292
- #if GTEST_OS_QNX
1258
+ #ifdef GTEST_OS_QNX
1293
1259
// Obtains the current directory and sets it to be closed in the child
1294
1260
// process.
1295
1261
const int cwd_fd = open (" ." , O_RDONLY);
@@ -1320,7 +1286,7 @@ static pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) {
1320
1286
GTEST_DEATH_TEST_CHECK_SYSCALL_ (close (cwd_fd));
1321
1287
1322
1288
#else // GTEST_OS_QNX
1323
- #if GTEST_OS_LINUX
1289
+ #ifdef GTEST_OS_LINUX
1324
1290
// When a SIGPROF signal is received while fork() or clone() are executing,
1325
1291
// the process may hang. To avoid this, we ignore SIGPROF here and re-enable
1326
1292
// it after the call to fork()/clone() is complete.
@@ -1367,11 +1333,10 @@ static pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) {
1367
1333
#endif // GTEST_HAS_CLONE
1368
1334
1369
1335
if (use_fork && (child_pid = fork ()) == 0 ) {
1370
- ExecDeathTestChildMain (&args);
1371
- _exit (0 );
1336
+ _exit (ExecDeathTestChildMain (&args));
1372
1337
}
1373
1338
#endif // GTEST_OS_QNX
1374
- #if GTEST_OS_LINUX
1339
+ #ifdef GTEST_OS_LINUX
1375
1340
GTEST_DEATH_TEST_CHECK_SYSCALL_ (
1376
1341
sigaction (SIGPROF, &saved_sigprof_action, nullptr ));
1377
1342
#endif // GTEST_OS_LINUX
@@ -1410,10 +1375,9 @@ DeathTest::TestRole ExecDeathTest::AssumeRole() {
1410
1375
StreamableToString (line_) + " |" +
1411
1376
StreamableToString (death_test_index) + " |" +
1412
1377
StreamableToString (pipe_fd[1 ]);
1413
- Arguments args;
1414
- args.AddArguments (GetArgvsForDeathTestChildProcess ());
1415
- args.AddArgument (filter_flag.c_str ());
1416
- args.AddArgument (internal_flag.c_str ());
1378
+ std::vector<std::string> args = GetArgvsForDeathTestChildProcess ();
1379
+ args.push_back (filter_flag);
1380
+ args.push_back (internal_flag);
1417
1381
1418
1382
DeathTest::set_last_death_test_message (" " );
1419
1383
@@ -1422,7 +1386,8 @@ DeathTest::TestRole ExecDeathTest::AssumeRole() {
1422
1386
// is necessary.
1423
1387
FlushInfoLog ();
1424
1388
1425
- const pid_t child_pid = ExecDeathTestSpawnChild (args.Argv (), pipe_fd[0 ]);
1389
+ std::unique_ptr<char *[]> argv = CreateArgvFromArgs (args);
1390
+ const pid_t child_pid = ExecDeathTestSpawnChild (argv.get (), pipe_fd[0 ]);
1426
1391
GTEST_DEATH_TEST_CHECK_SYSCALL_ (close (pipe_fd[1 ]));
1427
1392
set_child_pid (child_pid);
1428
1393
set_read_fd (pipe_fd[0 ]);
@@ -1463,14 +1428,14 @@ bool DefaultDeathTestFactory::Create(const char* statement,
1463
1428
}
1464
1429
}
1465
1430
1466
- #if GTEST_OS_WINDOWS
1431
+ #ifdef GTEST_OS_WINDOWS
1467
1432
1468
1433
if (GTEST_FLAG_GET (death_test_style) == " threadsafe" ||
1469
1434
GTEST_FLAG_GET (death_test_style) == " fast" ) {
1470
1435
*test = new WindowsDeathTest (statement, std::move (matcher), file, line);
1471
1436
}
1472
1437
1473
- #elif GTEST_OS_FUCHSIA
1438
+ #elif defined( GTEST_OS_FUCHSIA)
1474
1439
1475
1440
if (GTEST_FLAG_GET (death_test_style) == " threadsafe" ||
1476
1441
GTEST_FLAG_GET (death_test_style) == " fast" ) {
@@ -1497,7 +1462,7 @@ bool DefaultDeathTestFactory::Create(const char* statement,
1497
1462
return true ;
1498
1463
}
1499
1464
1500
- #if GTEST_OS_WINDOWS
1465
+ #ifdef GTEST_OS_WINDOWS
1501
1466
// Recreates the pipe and event handles from the provided parameters,
1502
1467
// signals the event, and returns a file descriptor wrapped around the pipe
1503
1468
// handle. This function is called in the child process only.
@@ -1564,7 +1529,7 @@ static int GetStatusFileDescriptor(unsigned int parent_process_id,
1564
1529
// initialized from the GTEST_FLAG(internal_run_death_test) flag if
1565
1530
// the flag is specified; otherwise returns NULL.
1566
1531
InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag () {
1567
- if (GTEST_FLAG_GET (internal_run_death_test) == " " ) return nullptr ;
1532
+ if (GTEST_FLAG_GET (internal_run_death_test). empty () ) return nullptr ;
1568
1533
1569
1534
// GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we
1570
1535
// can use it here.
@@ -1574,7 +1539,7 @@ InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() {
1574
1539
SplitString (GTEST_FLAG_GET (internal_run_death_test), ' |' , &fields);
1575
1540
int write_fd = -1 ;
1576
1541
1577
- #if GTEST_OS_WINDOWS
1542
+ #ifdef GTEST_OS_WINDOWS
1578
1543
1579
1544
unsigned int parent_process_id = 0 ;
1580
1545
size_t write_handle_as_size_t = 0 ;
@@ -1591,7 +1556,7 @@ InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() {
1591
1556
write_fd = GetStatusFileDescriptor (parent_process_id, write_handle_as_size_t ,
1592
1557
event_handle_as_size_t );
1593
1558
1594
- #elif GTEST_OS_FUCHSIA
1559
+ #elif defined( GTEST_OS_FUCHSIA)
1595
1560
1596
1561
if (fields.size () != 3 || !ParseNaturalNumber (fields[1 ], &line) ||
1597
1562
!ParseNaturalNumber (fields[2 ], &index )) {
0 commit comments