mirror of
https://github.com/chromium/crashpad.git
synced 2024-12-27 23:41:02 +08:00
test: Compare ProcessInfo::Arguments() to main()’s argc/argv on POSIX
Previously on macOS, the test used an OS-specific library function to recover the original argc and argv. On Linux/Android, it essentially reimplemented the very code it was testing, which didn’t make for a very good test. The new approach is to save argc and argv in main() and base the comparison on that. Bug: crashpad:30 Test: crashpad_util_test ProcessInfo.*, crashpad_test_test MainArguments.* Change-Id: I578abed3b04ae10a22f79a193bbb8b6589276c97 Reviewed-on: https://chromium-review.googlesource.com/456798 Commit-Queue: Mark Mentovai <mark@chromium.org> Reviewed-by: Joshua Peraza <jperaza@chromium.org>
This commit is contained in:
parent
88bc09fb86
commit
14138936b5
@ -23,9 +23,9 @@
|
|||||||
'dependencies': [
|
'dependencies': [
|
||||||
'client.gyp:crashpad_client',
|
'client.gyp:crashpad_client',
|
||||||
'../handler/handler.gyp:crashpad_handler',
|
'../handler/handler.gyp:crashpad_handler',
|
||||||
|
'../test/test.gyp:crashpad_gmock_main',
|
||||||
'../test/test.gyp:crashpad_test',
|
'../test/test.gyp:crashpad_test',
|
||||||
'../third_party/gtest/gmock.gyp:gmock',
|
'../third_party/gtest/gmock.gyp:gmock',
|
||||||
'../third_party/gtest/gmock.gyp:gmock_main',
|
|
||||||
'../third_party/gtest/gtest.gyp:gtest',
|
'../third_party/gtest/gtest.gyp:gtest',
|
||||||
'../third_party/mini_chromium/mini_chromium.gyp:base',
|
'../third_party/mini_chromium/mini_chromium.gyp:base',
|
||||||
'../util/util.gyp:crashpad_util',
|
'../util/util.gyp:crashpad_util',
|
||||||
|
@ -23,9 +23,9 @@
|
|||||||
'dependencies': [
|
'dependencies': [
|
||||||
'minidump.gyp:crashpad_minidump',
|
'minidump.gyp:crashpad_minidump',
|
||||||
'../snapshot/snapshot_test.gyp:crashpad_snapshot_test_lib',
|
'../snapshot/snapshot_test.gyp:crashpad_snapshot_test_lib',
|
||||||
|
'../test/test.gyp:crashpad_gtest_main',
|
||||||
'../test/test.gyp:crashpad_test',
|
'../test/test.gyp:crashpad_test',
|
||||||
'../third_party/gtest/gtest.gyp:gtest',
|
'../third_party/gtest/gtest.gyp:gtest',
|
||||||
'../third_party/gtest/gtest.gyp:gtest_main',
|
|
||||||
'../third_party/mini_chromium/mini_chromium.gyp:base',
|
'../third_party/mini_chromium/mini_chromium.gyp:base',
|
||||||
'../util/util.gyp:crashpad_util',
|
'../util/util.gyp:crashpad_util',
|
||||||
],
|
],
|
||||||
|
@ -57,9 +57,9 @@
|
|||||||
'snapshot.gyp:crashpad_snapshot_api',
|
'snapshot.gyp:crashpad_snapshot_api',
|
||||||
'../client/client.gyp:crashpad_client',
|
'../client/client.gyp:crashpad_client',
|
||||||
'../compat/compat.gyp:crashpad_compat',
|
'../compat/compat.gyp:crashpad_compat',
|
||||||
|
'../test/test.gyp:crashpad_gtest_main',
|
||||||
'../test/test.gyp:crashpad_test',
|
'../test/test.gyp:crashpad_test',
|
||||||
'../third_party/gtest/gtest.gyp:gtest',
|
'../third_party/gtest/gtest.gyp:gtest',
|
||||||
'../third_party/gtest/gtest.gyp:gtest_main',
|
|
||||||
'../third_party/mini_chromium/mini_chromium.gyp:base',
|
'../third_party/mini_chromium/mini_chromium.gyp:base',
|
||||||
'../util/util.gyp:crashpad_util',
|
'../util/util.gyp:crashpad_util',
|
||||||
],
|
],
|
||||||
|
23
test/gmock_main.cc
Normal file
23
test/gmock_main.cc
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// Copyright 2017 The Crashpad Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "gmock/gmock.h"
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "test/main_arguments.h"
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
crashpad::test::InitializeMainArguments(argc, argv);
|
||||||
|
testing::InitGoogleMock(&argc, argv);
|
||||||
|
return RUN_ALL_TESTS();
|
||||||
|
}
|
22
test/gtest_main.cc
Normal file
22
test/gtest_main.cc
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Copyright 2017 The Crashpad Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "test/main_arguments.h"
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
crashpad::test::InitializeMainArguments(argc, argv);
|
||||||
|
testing::InitGoogleTest(&argc, argv);
|
||||||
|
return RUN_ALL_TESTS();
|
||||||
|
}
|
38
test/main_arguments.cc
Normal file
38
test/main_arguments.cc
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// Copyright 2017 The Crashpad Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "test/main_arguments.h"
|
||||||
|
|
||||||
|
#include "base/logging.h"
|
||||||
|
|
||||||
|
namespace crashpad {
|
||||||
|
namespace test {
|
||||||
|
|
||||||
|
const std::vector<std::string>* g_arguments;
|
||||||
|
|
||||||
|
void InitializeMainArguments(int argc, char* argv[]) {
|
||||||
|
CHECK(!g_arguments);
|
||||||
|
CHECK(argc);
|
||||||
|
CHECK(argv);
|
||||||
|
|
||||||
|
g_arguments = new const std::vector<std::string>(argv, argv + argc);
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<std::string>& GetMainArguments() {
|
||||||
|
CHECK(g_arguments);
|
||||||
|
return *g_arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace test
|
||||||
|
} // namespace crashpad
|
49
test/main_arguments.h
Normal file
49
test/main_arguments.h
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// Copyright 2017 The Crashpad Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#ifndef CRASHPAD_TEST_MAIN_ARGUMENTS_H_
|
||||||
|
#define CRASHPAD_TEST_MAIN_ARGUMENTS_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace crashpad {
|
||||||
|
namespace test {
|
||||||
|
|
||||||
|
//! \brief Saves the arguments to `main()` for later use.
|
||||||
|
//!
|
||||||
|
//! Call this function from a test program’s `main()` function so that tests
|
||||||
|
//! that require access to these variables can retrieve them from
|
||||||
|
//! GetMainArguments().
|
||||||
|
//!
|
||||||
|
//! The contents of \a argv, limited to \a argc elements, will be copied, so
|
||||||
|
//! that subsequent modifications to these variables by `main()` will not affect
|
||||||
|
//! the state returned by GetMainArguments().
|
||||||
|
//!
|
||||||
|
//! This function must be called exactly once during the lifetime of a test
|
||||||
|
//! program.
|
||||||
|
void InitializeMainArguments(int argc, char* argv[]);
|
||||||
|
|
||||||
|
//! \brief Retrieves pointers to the arguments to `main()`.
|
||||||
|
//!
|
||||||
|
//! Tests that need to access the original values of a test program’s `main()`
|
||||||
|
//! function’s parameters at process creation can use this function to retrieve
|
||||||
|
//! them, provided that `main()` called InitializeMainArguments() before making
|
||||||
|
//! any changes to its arguments.
|
||||||
|
const std::vector<std::string>& GetMainArguments();
|
||||||
|
|
||||||
|
} // namespace test
|
||||||
|
} // namespace crashpad
|
||||||
|
|
||||||
|
#endif // CRASHPAD_TEST_MAIN_ARGUMENTS_H_
|
34
test/main_arguments_test.cc
Normal file
34
test/main_arguments_test.cc
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// Copyright 2017 The Crashpad Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "test/main_arguments.h"
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
namespace crashpad {
|
||||||
|
namespace test {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
TEST(MainArguments, GetMainArguments) {
|
||||||
|
// Make sure that InitializeMainArguments() has been called and that
|
||||||
|
// GetMainArguments() provides reasonable values.
|
||||||
|
const std::vector<std::string>& arguments = GetMainArguments();
|
||||||
|
|
||||||
|
ASSERT_FALSE(arguments.empty());
|
||||||
|
EXPECT_FALSE(arguments[0].empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace test
|
||||||
|
} // namespace crashpad
|
@ -42,6 +42,8 @@
|
|||||||
'mac/mach_errors.h',
|
'mac/mach_errors.h',
|
||||||
'mac/mach_multiprocess.cc',
|
'mac/mach_multiprocess.cc',
|
||||||
'mac/mach_multiprocess.h',
|
'mac/mach_multiprocess.h',
|
||||||
|
'main_arguments.cc',
|
||||||
|
'main_arguments.h',
|
||||||
'multiprocess.h',
|
'multiprocess.h',
|
||||||
'multiprocess_exec.h',
|
'multiprocess_exec.h',
|
||||||
'multiprocess_exec_posix.cc',
|
'multiprocess_exec_posix.cc',
|
||||||
@ -63,6 +65,11 @@
|
|||||||
'win/win_multiprocess.cc',
|
'win/win_multiprocess.cc',
|
||||||
'win/win_multiprocess.h',
|
'win/win_multiprocess.h',
|
||||||
],
|
],
|
||||||
|
'direct_dependent_settings': {
|
||||||
|
'include_dirs': [
|
||||||
|
'..',
|
||||||
|
],
|
||||||
|
},
|
||||||
'conditions': [
|
'conditions': [
|
||||||
['OS=="mac"', {
|
['OS=="mac"', {
|
||||||
'link_settings': {
|
'link_settings': {
|
||||||
@ -87,5 +94,28 @@
|
|||||||
}],
|
}],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
'target_name': 'crashpad_gtest_main',
|
||||||
|
'type': 'static_library',
|
||||||
|
'dependencies': [
|
||||||
|
'crashpad_test',
|
||||||
|
'../third_party/gtest/gtest.gyp:gtest',
|
||||||
|
],
|
||||||
|
'sources': [
|
||||||
|
'gtest_main.cc',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'target_name': 'crashpad_gmock_main',
|
||||||
|
'type': 'static_library',
|
||||||
|
'dependencies': [
|
||||||
|
'crashpad_test',
|
||||||
|
'../third_party/gtest/gmock.gyp:gmock',
|
||||||
|
'../third_party/gtest/gtest.gyp:gtest',
|
||||||
|
],
|
||||||
|
'sources': [
|
||||||
|
'gmock_main.cc',
|
||||||
|
],
|
||||||
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
@ -22,10 +22,10 @@
|
|||||||
'type': 'executable',
|
'type': 'executable',
|
||||||
'dependencies': [
|
'dependencies': [
|
||||||
'crashpad_test_test_multiprocess_exec_test_child',
|
'crashpad_test_test_multiprocess_exec_test_child',
|
||||||
|
'test.gyp:crashpad_gmock_main',
|
||||||
'test.gyp:crashpad_test',
|
'test.gyp:crashpad_test',
|
||||||
'../compat/compat.gyp:crashpad_compat',
|
'../compat/compat.gyp:crashpad_compat',
|
||||||
'../third_party/gtest/gmock.gyp:gmock',
|
'../third_party/gtest/gmock.gyp:gmock',
|
||||||
'../third_party/gtest/gmock.gyp:gmock_main',
|
|
||||||
'../third_party/gtest/gtest.gyp:gtest',
|
'../third_party/gtest/gtest.gyp:gtest',
|
||||||
'../third_party/mini_chromium/mini_chromium.gyp:base',
|
'../third_party/mini_chromium/mini_chromium.gyp:base',
|
||||||
'../util/util.gyp:crashpad_util',
|
'../util/util.gyp:crashpad_util',
|
||||||
@ -36,6 +36,7 @@
|
|||||||
'sources': [
|
'sources': [
|
||||||
'hex_string_test.cc',
|
'hex_string_test.cc',
|
||||||
'mac/mach_multiprocess_test.cc',
|
'mac/mach_multiprocess_test.cc',
|
||||||
|
'main_arguments_test.cc',
|
||||||
'multiprocess_exec_test.cc',
|
'multiprocess_exec_test.cc',
|
||||||
'multiprocess_posix_test.cc',
|
'multiprocess_posix_test.cc',
|
||||||
'paths_test.cc',
|
'paths_test.cc',
|
||||||
|
@ -15,28 +15,21 @@
|
|||||||
#include "util/posix/process_info.h"
|
#include "util/posix/process_info.h"
|
||||||
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "base/files/scoped_file.h"
|
#include "base/strings/stringprintf.h"
|
||||||
#include "build/build_config.h"
|
#include "build/build_config.h"
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "test/errors.h"
|
#include "test/errors.h"
|
||||||
|
#include "test/main_arguments.h"
|
||||||
#include "util/misc/implicit_cast.h"
|
#include "util/misc/implicit_cast.h"
|
||||||
|
|
||||||
#if defined(OS_MACOSX)
|
|
||||||
#include <crt_externs.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(OS_LINUX) || defined(OS_ANDROID)
|
|
||||||
#include <sys/prctl.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace crashpad {
|
namespace crashpad {
|
||||||
namespace test {
|
namespace test {
|
||||||
namespace {
|
namespace {
|
||||||
@ -100,51 +93,29 @@ void TestProcessSelfOrClone(const ProcessInfo& process_info) {
|
|||||||
std::vector<std::string> argv;
|
std::vector<std::string> argv;
|
||||||
ASSERT_TRUE(process_info.Arguments(&argv));
|
ASSERT_TRUE(process_info.Arguments(&argv));
|
||||||
|
|
||||||
// gtest argv processing scrambles argv, but it leaves argc and argv[0]
|
const std::vector<std::string>& expect_argv = GetMainArguments();
|
||||||
// intact, so test those.
|
|
||||||
|
|
||||||
#if defined(OS_MACOSX)
|
// expect_argv always contains the initial view of the arguments at the time
|
||||||
int expect_argc = *_NSGetArgc();
|
// the program was invoked. argv may contain this view, or it may contain the
|
||||||
char** expect_argv = *_NSGetArgv();
|
// current view of arguments after gtest argv processing. argv may be a subset
|
||||||
#elif defined(OS_LINUX) || defined(OS_ANDROID)
|
// of expect_argv.
|
||||||
std::vector<std::string> expect_arg_vector;
|
//
|
||||||
{
|
// gtest argv processing always leaves argv[0] intact, so this can be checked
|
||||||
base::ScopedFILE cmdline(fopen("/proc/self/cmdline", "re"));
|
// directly.
|
||||||
ASSERT_NE(nullptr, cmdline.get()) << ErrnoMessage("fopen");
|
ASSERT_FALSE(expect_argv.empty());
|
||||||
|
ASSERT_FALSE(argv.empty());
|
||||||
|
EXPECT_EQ(expect_argv[0], argv[0]);
|
||||||
|
|
||||||
int expect_arg_char;
|
EXPECT_LE(argv.size(), expect_argv.size());
|
||||||
std::string expect_arg_string;
|
|
||||||
while ((expect_arg_char = fgetc(cmdline.get())) != EOF) {
|
// Everything else in argv should have a match in expect_argv too, but things
|
||||||
if (expect_arg_char != '\0') {
|
// may have moved around.
|
||||||
expect_arg_string.append(1, expect_arg_char);
|
for (size_t arg_index = 1; arg_index < argv.size(); ++arg_index) {
|
||||||
} else {
|
const std::string& arg = argv[arg_index];
|
||||||
expect_arg_vector.push_back(expect_arg_string);
|
SCOPED_TRACE(
|
||||||
expect_arg_string.clear();
|
base::StringPrintf("arg_index %zu, arg %s", arg_index, arg.c_str()));
|
||||||
}
|
EXPECT_NE(expect_argv.end(), std::find(argv.begin(), argv.end(), arg));
|
||||||
}
|
|
||||||
ASSERT_EQ(0, ferror(cmdline.get())) << ErrnoMessage("fgetc");
|
|
||||||
ASSERT_TRUE(expect_arg_string.empty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<const char*> expect_argv_storage;
|
|
||||||
for (const std::string& expect_arg_string : expect_arg_vector) {
|
|
||||||
expect_argv_storage.push_back(expect_arg_string.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
int expect_argc = expect_argv_storage.size();
|
|
||||||
const char* const* expect_argv =
|
|
||||||
!expect_argv_storage.empty() ? &expect_argv_storage[0] : nullptr;
|
|
||||||
#else
|
|
||||||
#error Obtain expect_argc and expect_argv correctly on your system.
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int argc = implicit_cast<int>(argv.size());
|
|
||||||
EXPECT_EQ(expect_argc, argc);
|
|
||||||
|
|
||||||
ASSERT_GE(expect_argc, 1);
|
|
||||||
ASSERT_GE(argc, 1);
|
|
||||||
|
|
||||||
EXPECT_EQ(std::string(expect_argv[0]), argv[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestSelfProcess(const ProcessInfo& process_info) {
|
void TestSelfProcess(const ProcessInfo& process_info) {
|
||||||
|
@ -24,9 +24,9 @@
|
|||||||
'util.gyp:crashpad_util',
|
'util.gyp:crashpad_util',
|
||||||
'../client/client.gyp:crashpad_client',
|
'../client/client.gyp:crashpad_client',
|
||||||
'../compat/compat.gyp:crashpad_compat',
|
'../compat/compat.gyp:crashpad_compat',
|
||||||
|
'../test/test.gyp:crashpad_gmock_main',
|
||||||
'../test/test.gyp:crashpad_test',
|
'../test/test.gyp:crashpad_test',
|
||||||
'../third_party/gtest/gmock.gyp:gmock',
|
'../third_party/gtest/gmock.gyp:gmock',
|
||||||
'../third_party/gtest/gmock.gyp:gmock_main',
|
|
||||||
'../third_party/gtest/gtest.gyp:gtest',
|
'../third_party/gtest/gtest.gyp:gtest',
|
||||||
'../third_party/mini_chromium/mini_chromium.gyp:base',
|
'../third_party/mini_chromium/mini_chromium.gyp:base',
|
||||||
'../third_party/zlib/zlib.gyp:zlib',
|
'../third_party/zlib/zlib.gyp:zlib',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user