2015-04-27 09:12:58 +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.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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"
|
|
|
|
#include "osquery/core/testing.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[] = {
|
|
|
|
"worker-test", "--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.
|
2016-09-12 16:46:52 +00:00
|
|
|
const char* kExpectedExtensionArgs[] = {"osquery extension: extension-test",
|
|
|
|
"--socket",
|
|
|
|
"socket-name",
|
|
|
|
"--timeout",
|
|
|
|
"100",
|
|
|
|
"--interval",
|
|
|
|
"5",
|
|
|
|
"--verbose",
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
if (process.get() == nullptr) {
|
|
|
|
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[]) {
|
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);
|
|
|
|
}
|
2016-09-12 16:46:52 +00:00
|
|
|
osquery::registryAndPluginInit();
|
2016-05-11 21:16:32 +00:00
|
|
|
osquery::kProcessTestExecPath = argv[0];
|
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
|
|
|
}
|