/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * * 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. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace fs = boost::filesystem; namespace osquery { /// Will be set with initTesting in test harness main. std::string kFakeDirectory; /// Will be set with initTesting in test harness main. std::string kTestWorkingDirectory; /// The relative path within the source repo to find test content. std::string kTestDataPath{"../../../tools/tests/"}; DECLARE_string(database_path); DECLARE_string(extensions_socket); DECLARE_string(extensions_autoload); DECLARE_string(enroll_tls_endpoint); DECLARE_bool(disable_logging); DECLARE_bool(disable_database); using chrono_clock = std::chrono::high_resolution_clock; void initTesting() { Config::setStartTime(getUnixTime()); kToolType = ToolType::TEST; if (osquery::isPlatform(PlatformType::TYPE_OSX)) { kTestWorkingDirectory = "/private/tmp/osquery-tests"; } else { kTestWorkingDirectory = (fs::temp_directory_path() / "osquery-tests").string(); } if (osquery::isPlatform(PlatformType::TYPE_WINDOWS)) { kTestDataPath = "../" + kTestDataPath; } registryAndPluginInit(); // Allow unit test execution from anywhere in the osquery source/build tree. if (fs::exists("test_data/test_inline_pack.conf")) { // If the execution occurs within the build/shared directory and shared // is pointing to a tmp build directory. See #3414. kTestDataPath = "test_data/"; } else if (fs::exists("../test_data/test_inline_pack.conf")) { // ctest executes from the osquery subdirectory. If this is a build/shared // link then the test_data directory links to the source repo. kTestDataPath = "../test_data/"; } else { while (kTestDataPath.find("tools") != 0) { if (!fs::exists(kTestDataPath + "test_inline_pack.conf")) { kTestDataPath = kTestDataPath.substr(3, kTestDataPath.size()); } else { break; } } } // The tests will fail randomly without test data. if (!fs::exists(kTestDataPath)) { throw std::runtime_error("Cannot find test data path"); } // Seed the random number generator, some tests generate temporary files // ports, sockets, etc using random numbers. std::srand(static_cast( chrono_clock::now().time_since_epoch().count())); // Set safe default values for path-based flags. // Specific unittests may edit flags temporarily. kTestWorkingDirectory += std::to_string(platformGetUid()) + "/"; kFakeDirectory = kTestWorkingDirectory + kFakeDirectoryName; fs::remove_all(kTestWorkingDirectory); fs::create_directories(kTestWorkingDirectory); FLAGS_database_path = kTestWorkingDirectory + "unittests.db"; FLAGS_extensions_socket = kTestWorkingDirectory + "unittests.em"; FLAGS_extensions_autoload = kTestWorkingDirectory + "unittests-ext.load"; FLAGS_disable_logging = true; FLAGS_disable_database = true; // Tests need a database plugin. // Set up the database instance for the unittests. DatabasePlugin::setAllowOpen(true); DatabasePlugin::initPlugin(); Initializer::platformSetup(); } void shutdownTesting() { DatabasePlugin::shutdown(); Initializer::platformTeardown(); } ScheduledQuery getOsqueryScheduledQuery() { ScheduledQuery sq; sq.query = "SELECT filename FROM fs WHERE path = '/bin' ORDER BY filename"; sq.interval = 5; return sq; } TableRows genRows(EventSubscriberPlugin* sub) { auto vtc = new VirtualTableContent(); QueryContext context(vtc); RowGenerator::pull_type generator(std::bind(&EventSubscriberPlugin::genTable, sub, std::placeholders::_1, std::move(context))); TableRows results; if (!generator) { delete vtc; return results; } while (generator) { results.push_back(generator.get()); generator(); } delete vtc; return results; } } // namespace osquery