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:
Mark Mentovai 2017-03-18 00:18:20 -04:00 committed by Commit Bot
parent 88bc09fb86
commit 14138936b5
12 changed files with 225 additions and 57 deletions

View File

@ -23,9 +23,9 @@
'dependencies': [
'client.gyp:crashpad_client',
'../handler/handler.gyp:crashpad_handler',
'../test/test.gyp:crashpad_gmock_main',
'../test/test.gyp:crashpad_test',
'../third_party/gtest/gmock.gyp:gmock',
'../third_party/gtest/gmock.gyp:gmock_main',
'../third_party/gtest/gtest.gyp:gtest',
'../third_party/mini_chromium/mini_chromium.gyp:base',
'../util/util.gyp:crashpad_util',

View File

@ -23,9 +23,9 @@
'dependencies': [
'minidump.gyp:crashpad_minidump',
'../snapshot/snapshot_test.gyp:crashpad_snapshot_test_lib',
'../test/test.gyp:crashpad_gtest_main',
'../test/test.gyp:crashpad_test',
'../third_party/gtest/gtest.gyp:gtest',
'../third_party/gtest/gtest.gyp:gtest_main',
'../third_party/mini_chromium/mini_chromium.gyp:base',
'../util/util.gyp:crashpad_util',
],

View File

@ -57,9 +57,9 @@
'snapshot.gyp:crashpad_snapshot_api',
'../client/client.gyp:crashpad_client',
'../compat/compat.gyp:crashpad_compat',
'../test/test.gyp:crashpad_gtest_main',
'../test/test.gyp:crashpad_test',
'../third_party/gtest/gtest.gyp:gtest',
'../third_party/gtest/gtest.gyp:gtest_main',
'../third_party/mini_chromium/mini_chromium.gyp:base',
'../util/util.gyp:crashpad_util',
],

23
test/gmock_main.cc Normal file
View 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
View 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
View 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
View 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 programs `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 programs `main()`
//! functions 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_

View 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

View File

@ -42,6 +42,8 @@
'mac/mach_errors.h',
'mac/mach_multiprocess.cc',
'mac/mach_multiprocess.h',
'main_arguments.cc',
'main_arguments.h',
'multiprocess.h',
'multiprocess_exec.h',
'multiprocess_exec_posix.cc',
@ -63,6 +65,11 @@
'win/win_multiprocess.cc',
'win/win_multiprocess.h',
],
'direct_dependent_settings': {
'include_dirs': [
'..',
],
},
'conditions': [
['OS=="mac"', {
'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',
],
},
],
}

View File

@ -22,10 +22,10 @@
'type': 'executable',
'dependencies': [
'crashpad_test_test_multiprocess_exec_test_child',
'test.gyp:crashpad_gmock_main',
'test.gyp:crashpad_test',
'../compat/compat.gyp:crashpad_compat',
'../third_party/gtest/gmock.gyp:gmock',
'../third_party/gtest/gmock.gyp:gmock_main',
'../third_party/gtest/gtest.gyp:gtest',
'../third_party/mini_chromium/mini_chromium.gyp:base',
'../util/util.gyp:crashpad_util',
@ -36,6 +36,7 @@
'sources': [
'hex_string_test.cc',
'mac/mach_multiprocess_test.cc',
'main_arguments_test.cc',
'multiprocess_exec_test.cc',
'multiprocess_posix_test.cc',
'paths_test.cc',

View File

@ -15,28 +15,21 @@
#include "util/posix/process_info.h"
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include "base/files/scoped_file.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "gtest/gtest.h"
#include "test/errors.h"
#include "test/main_arguments.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 test {
namespace {
@ -100,51 +93,29 @@ void TestProcessSelfOrClone(const ProcessInfo& process_info) {
std::vector<std::string> argv;
ASSERT_TRUE(process_info.Arguments(&argv));
// gtest argv processing scrambles argv, but it leaves argc and argv[0]
// intact, so test those.
const std::vector<std::string>& expect_argv = GetMainArguments();
#if defined(OS_MACOSX)
int expect_argc = *_NSGetArgc();
char** expect_argv = *_NSGetArgv();
#elif defined(OS_LINUX) || defined(OS_ANDROID)
std::vector<std::string> expect_arg_vector;
{
base::ScopedFILE cmdline(fopen("/proc/self/cmdline", "re"));
ASSERT_NE(nullptr, cmdline.get()) << ErrnoMessage("fopen");
// expect_argv always contains the initial view of the arguments at the time
// the program was invoked. argv may contain this view, or it may contain the
// current view of arguments after gtest argv processing. argv may be a subset
// of expect_argv.
//
// gtest argv processing always leaves argv[0] intact, so this can be checked
// directly.
ASSERT_FALSE(expect_argv.empty());
ASSERT_FALSE(argv.empty());
EXPECT_EQ(expect_argv[0], argv[0]);
int expect_arg_char;
std::string expect_arg_string;
while ((expect_arg_char = fgetc(cmdline.get())) != EOF) {
if (expect_arg_char != '\0') {
expect_arg_string.append(1, expect_arg_char);
} else {
expect_arg_vector.push_back(expect_arg_string);
expect_arg_string.clear();
}
}
ASSERT_EQ(0, ferror(cmdline.get())) << ErrnoMessage("fgetc");
ASSERT_TRUE(expect_arg_string.empty());
EXPECT_LE(argv.size(), expect_argv.size());
// Everything else in argv should have a match in expect_argv too, but things
// may have moved around.
for (size_t arg_index = 1; arg_index < argv.size(); ++arg_index) {
const std::string& arg = argv[arg_index];
SCOPED_TRACE(
base::StringPrintf("arg_index %zu, arg %s", arg_index, arg.c_str()));
EXPECT_NE(expect_argv.end(), std::find(argv.begin(), argv.end(), arg));
}
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) {

View File

@ -24,9 +24,9 @@
'util.gyp:crashpad_util',
'../client/client.gyp:crashpad_client',
'../compat/compat.gyp:crashpad_compat',
'../test/test.gyp:crashpad_gmock_main',
'../test/test.gyp:crashpad_test',
'../third_party/gtest/gmock.gyp:gmock',
'../third_party/gtest/gmock.gyp:gmock_main',
'../third_party/gtest/gtest.gyp:gtest',
'../third_party/mini_chromium/mini_chromium.gyp:base',
'../third_party/zlib/zlib.gyp:zlib',