QueryData usersFromContext(const QueryContext& context, bool all) { QueryData users; if (context.hasConstraint("uid", EQUALS)) { context.forEachConstraint( "uid", EQUALS, ([&users](const std::string& expr) { auto user = SQL::selectAllFrom("users", "uid", EQUALS, expr); users.insert(users.end(), user.begin(), user.end()); })); } else if (!all) { users = SQL::selectAllFrom( "users", "uid", EQUALS, std::to_string(platformGetUid())); } else { users = SQL::selectAllFrom("users"); } return users; }
void initTesting() { osquery::kToolType = ToolType::TEST; registryAndPluginInit(); // Allow unit test execution from anywhere in the osquery source/build tree. while (osquery::kTestDataPath != "/") { if (!fs::exists(osquery::kTestDataPath)) { osquery::kTestDataPath = osquery::kTestDataPath.substr(3, osquery::kTestDataPath.size()); } else { break; } } // Seed the random number generator, some tests generate temporary files // ports, sockets, etc using random numbers. std::srand(static_cast<unsigned int>( 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"; #ifndef WIN32 FLAGS_modules_autoload = kTestWorkingDirectory + "unittests-mod.load"; #endif 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 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<unsigned int>( 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(); }