2017-12-19 00:04:06 +00:00
|
|
|
/**
|
2016-02-11 19:48:58 +00:00
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
2015-04-27 09:12:58 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-12-19 00:04:06 +00:00
|
|
|
* This source code is licensed under both the Apache 2.0 license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2015-04-27 09:12:58 +00:00
|
|
|
*/
|
|
|
|
|
2016-05-11 21:16:32 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Shlwapi.h>
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2015-04-27 09:12:58 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2015-09-03 19:22:42 +00:00
|
|
|
#include <osquery/logger.h>
|
|
|
|
|
2016-05-11 21:16:32 +00:00
|
|
|
#include "osquery/core/process.h"
|
2016-09-12 16:46:52 +00:00
|
|
|
#include "osquery/tests/test_util.h"
|
2016-05-11 21:16:32 +00:00
|
|
|
|
|
|
|
namespace osquery {
|
|
|
|
|
|
|
|
/// This is exposed for process_tests.cpp. Without exporting this variable, we
|
|
|
|
/// would need to use more complicated measures to determine the current
|
|
|
|
/// executing file's path.
|
|
|
|
std::string kProcessTestExecPath;
|
|
|
|
|
|
|
|
/// This is the expected module name of the launcher process.
|
2016-09-12 16:46:52 +00:00
|
|
|
const char* kOsqueryTestModuleName = "osquery_tests.exe";
|
2016-05-11 21:16:32 +00:00
|
|
|
|
|
|
|
/// These are the expected arguments for our test worker process.
|
2016-09-12 16:46:52 +00:00
|
|
|
const char* kExpectedWorkerArgs[] = {
|
2017-11-20 15:24:55 +00:00
|
|
|
nullptr, "--socket", "fake-socket", nullptr};
|
2016-05-14 02:47:45 +00:00
|
|
|
const size_t kExpectedWorkerArgsCount =
|
2016-09-12 16:46:52 +00:00
|
|
|
(sizeof(osquery::kExpectedWorkerArgs) / sizeof(char*)) - 1;
|
2016-05-11 21:16:32 +00:00
|
|
|
|
|
|
|
/// These are the expected arguments for our test extensions process.
|
2017-11-20 15:24:55 +00:00
|
|
|
const char* kExpectedExtensionArgs[] = {nullptr,
|
2017-03-25 01:47:23 +00:00
|
|
|
"--verbose",
|
2016-09-12 16:46:52 +00:00
|
|
|
"--socket",
|
|
|
|
"socket-name",
|
|
|
|
"--timeout",
|
|
|
|
"100",
|
|
|
|
"--interval",
|
|
|
|
"5",
|
|
|
|
nullptr};
|
2016-05-14 02:47:45 +00:00
|
|
|
const size_t kExpectedExtensionArgsCount =
|
2016-09-12 16:46:52 +00:00
|
|
|
(sizeof(osquery::kExpectedExtensionArgs) / sizeof(char*)) - 1;
|
2016-05-11 21:16:32 +00:00
|
|
|
|
2016-09-12 16:46:52 +00:00
|
|
|
static bool compareArguments(char* result[],
|
2016-05-11 21:16:32 +00:00
|
|
|
unsigned int result_nelms,
|
2016-09-12 16:46:52 +00:00
|
|
|
const char* expected[],
|
2016-05-11 21:16:32 +00:00
|
|
|
unsigned int expected_nelms) {
|
|
|
|
if (result_nelms != expected_nelms) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < expected_nelms; i++) {
|
|
|
|
if (strlen(result[i]) != strlen(expected[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(result[i], expected[i], strlen(expected[i])) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-30 05:25:49 +00:00
|
|
|
} // namespace osquery
|
2016-05-11 21:16:32 +00:00
|
|
|
|
2016-09-12 16:46:52 +00:00
|
|
|
int workerMain(int argc, char* argv[]) {
|
2016-05-11 21:16:32 +00:00
|
|
|
if (!osquery::compareArguments(argv,
|
|
|
|
argc,
|
|
|
|
osquery::kExpectedWorkerArgs,
|
2016-05-14 02:47:45 +00:00
|
|
|
osquery::kExpectedWorkerArgsCount)) {
|
2016-05-11 21:16:32 +00:00
|
|
|
return ERROR_COMPARE_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto process = osquery::PlatformProcess::getLauncherProcess();
|
2017-10-30 05:25:49 +00:00
|
|
|
if (process == nullptr) {
|
2016-05-11 21:16:32 +00:00
|
|
|
return ERROR_LAUNCHER_PROCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
CHAR buffer[1024] = {0};
|
|
|
|
DWORD size = 1024;
|
|
|
|
if (!QueryFullProcessImageNameA(process->nativeHandle(), 0, buffer, &size)) {
|
|
|
|
return ERROR_QUERY_PROCESS_IMAGE;
|
|
|
|
}
|
|
|
|
PathStripPathA(buffer);
|
|
|
|
|
|
|
|
if (strlen(buffer) != strlen(osquery::kOsqueryTestModuleName)) {
|
|
|
|
return ERROR_IMAGE_NAME_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(buffer, osquery::kOsqueryTestModuleName, strlen(buffer)) != 0) {
|
|
|
|
return ERROR_LAUNCHER_MISMATCH;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (process->nativeHandle() != getppid()) {
|
|
|
|
return ERROR_LAUNCHER_MISMATCH;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return WORKER_SUCCESS_CODE;
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:46:52 +00:00
|
|
|
int extensionMain(int argc, char* argv[]) {
|
2016-05-11 21:16:32 +00:00
|
|
|
if (!osquery::compareArguments(argv,
|
|
|
|
argc,
|
|
|
|
osquery::kExpectedExtensionArgs,
|
2016-05-14 02:47:45 +00:00
|
|
|
osquery::kExpectedExtensionArgsCount)) {
|
2016-05-11 21:16:32 +00:00
|
|
|
return ERROR_COMPARE_ARGUMENT;
|
|
|
|
}
|
|
|
|
return EXTENSION_SUCCESS_CODE;
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:46:52 +00:00
|
|
|
int main(int argc, char* argv[]) {
|
2017-11-20 15:24:55 +00:00
|
|
|
osquery::kProcessTestExecPath = argv[0];
|
|
|
|
osquery::kExpectedExtensionArgs[0] = argv[0];
|
|
|
|
osquery::kExpectedWorkerArgs[0] = argv[0];
|
|
|
|
|
2016-05-11 21:16:32 +00:00
|
|
|
if (auto val = osquery::getEnvVar("OSQUERY_WORKER")) {
|
|
|
|
return workerMain(argc, argv);
|
|
|
|
} else if ((val = osquery::getEnvVar("OSQUERY_EXTENSION"))) {
|
|
|
|
return extensionMain(argc, argv);
|
|
|
|
}
|
2015-04-29 19:36:24 +00:00
|
|
|
|
|
|
|
osquery::initTesting();
|
2015-04-27 09:12:58 +00:00
|
|
|
testing::InitGoogleTest(&argc, argv);
|
|
|
|
// Optionally enable Goggle Logging
|
|
|
|
// google::InitGoogleLogging(argv[0]);
|
2016-03-05 17:29:51 +00:00
|
|
|
auto result = RUN_ALL_TESTS();
|
|
|
|
|
|
|
|
osquery::shutdownTesting();
|
|
|
|
return result;
|
2015-04-27 09:12:58 +00:00
|
|
|
}
|