osquery-1/osquery/tests/test_util.h

164 lines
5.3 KiB
C
Raw Normal View History

/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
2015-04-23 00:49:27 +00:00
* 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.
*
*/
2014-07-31 00:35:19 +00:00
#pragma once
2014-07-31 00:35:19 +00:00
#include <string>
#include <utility>
#include <vector>
2015-09-07 18:09:06 +00:00
#include <boost/noncopyable.hpp>
2014-07-31 00:35:19 +00:00
#include <boost/property_tree/ptree.hpp>
#include <osquery/config.h>
#include <osquery/core.h>
#include <osquery/database.h>
#include <osquery/filesystem.h>
2014-07-31 00:35:19 +00:00
#include "osquery/core/process.h"
2015-04-27 21:57:04 +00:00
namespace pt = boost::property_tree;
2014-08-15 07:25:30 +00:00
namespace osquery {
2015-07-23 23:42:46 +00:00
/// Init function for tests and benchmarks.
void initTesting();
2014-07-31 00:35:19 +00:00
/// Cleanup/stop function for tests and benchmarks.
void shutdownTesting();
2015-04-29 19:36:24 +00:00
/// Any SQL-dependent tests should use kTestQuery for a pre-populated example.
const std::string kTestQuery = "SELECT * FROM test_table";
/// Tests can be run from within the source or build directory.
/// The test initializer will attempt to discovery the current working path.
extern std::string kTestDataPath;
2015-04-29 19:36:24 +00:00
/// Tests should limit intermediate input/output to a working directory.
/// Config data, logging results, and intermediate database/caching usage.
extern std::string kTestWorkingDirectory;
2015-04-29 19:36:24 +00:00
/// A fake directory tree should be used for filesystem iterator testing.
2015-06-30 21:20:04 +00:00
const std::string kFakeDirectoryName = "fstree";
extern std::string kFakeDirectory;
2015-02-14 00:52:11 +00:00
2015-11-02 18:33:20 +00:00
// Get an example generate config with one static source name to JSON content.
std::map<std::string, std::string> getTestConfigMap();
pt::ptree getExamplePacksConfig();
pt::ptree getUnrestrictedPack();
pt::ptree getRestrictedPack();
2015-11-02 18:33:20 +00:00
pt::ptree getPackWithDiscovery();
pt::ptree getPackWithValidDiscovery();
pt::ptree getPackWithFakeVersion();
2015-04-27 21:57:04 +00:00
ScheduledQuery getOsqueryScheduledQuery();
2014-07-31 00:35:19 +00:00
// getTestDBExpectedResults returns the results of kTestQuery of the table that
// initially gets returned from createTestDB()
QueryData getTestDBExpectedResults();
2014-07-31 00:35:19 +00:00
// Starting with the dataset returned by createTestDB(), getTestDBResultStream
// returns a vector of std::pair's where pair.first is the query that would
// need to be performed on the dataset to make the results be pair.second
std::vector<std::pair<std::string, QueryData> > getTestDBResultStream();
2014-07-31 00:35:19 +00:00
// getSerializedRow() return an std::pair where pair->first is a string which
// should serialize to pair->second. pair->second should deserialize
2014-07-31 00:35:19 +00:00
// to pair->first
2015-04-27 21:57:04 +00:00
std::pair<pt::ptree, Row> getSerializedRow();
2014-07-31 00:35:19 +00:00
// getSerializedQueryData() return an std::pair where pair->first is a string
// which should serialize to pair->second. pair->second should
2014-07-31 00:35:19 +00:00
// deserialize to pair->first
2015-04-27 21:57:04 +00:00
std::pair<pt::ptree, QueryData> getSerializedQueryData();
std::pair<std::string, QueryData> getSerializedQueryDataJSON();
2014-07-31 00:35:19 +00:00
// getSerializedDiffResults() return an std::pair where pair->first is a string
// which should serialize to pair->second. pair->second should
2014-07-31 00:35:19 +00:00
// deserialize to pair->first
2015-04-27 21:57:04 +00:00
std::pair<pt::ptree, DiffResults> getSerializedDiffResults();
std::pair<std::string, DiffResults> getSerializedDiffResultsJSON();
2014-07-31 00:35:19 +00:00
2015-04-27 21:57:04 +00:00
// getSerializedQueryLogItem() return an std::pair where pair->first
// is a string which should serialize to pair->second. pair->second
2014-07-31 00:35:19 +00:00
// should deserialize to pair->first
2015-04-27 21:57:04 +00:00
std::pair<pt::ptree, QueryLogItem> getSerializedQueryLogItem();
std::pair<std::string, QueryLogItem> getSerializedQueryLogItemJSON();
2014-07-31 00:35:19 +00:00
// generate content for a PEM-encoded certificate
std::string getCACertificateContent();
// generate the content that would be found in an /etc/hosts file
std::string getEtcHostsContent();
2015-04-23 00:49:27 +00:00
// generate the content that would be found in an /etc/protocols file
std::string getEtcProtocolsContent();
// generate the expected data that getEtcHostsContent() should parse into
QueryData getEtcHostsExpectedResults();
2015-04-23 00:49:27 +00:00
// generate the expected data that getEtcProtocolsContent() should parse into
QueryData getEtcProtocolsExpectedResults();
// the three items that you need to test osquery::splitString
2014-08-04 21:12:06 +00:00
struct SplitStringTestData {
std::string test_string;
2014-08-04 23:08:49 +00:00
std::string delim;
2014-08-04 21:12:06 +00:00
std::vector<std::string> test_vector;
};
// generate a set of test data to test osquery::splitString
2014-08-04 21:12:06 +00:00
std::vector<SplitStringTestData> generateSplitStringTestData();
2015-02-14 00:52:11 +00:00
// generate a small directory structure for testing
void createMockFileStructure();
2016-01-21 08:23:05 +00:00
2015-02-14 00:52:11 +00:00
// remove the small directory structure used for testing
void tearDownMockFileStructure();
2015-09-07 18:09:06 +00:00
class TLSServerRunner : private boost::noncopyable {
public:
/// Create a singleton TLS server runner.
static TLSServerRunner& instance() {
static TLSServerRunner instance;
return instance;
}
2016-01-21 08:23:05 +00:00
/// Set associated flags for testing client TLS usage.
static void setClientConfig();
/// Unset or restore associated flags for testing client TLS usage.
static void unsetClientConfig();
2015-09-07 18:09:06 +00:00
/// TCP port accessor.
static const std::string& port() { return instance().port_; }
2016-01-21 08:23:05 +00:00
2015-09-07 18:09:06 +00:00
/// Start the server if it hasn't started already.
static void start();
2016-01-21 08:23:05 +00:00
2015-09-07 18:09:06 +00:00
/// Stop the service when the process exits.
static void stop();
private:
2016-01-21 08:23:05 +00:00
/// Current server PID.
pid_t server_{0};
2015-09-07 18:09:06 +00:00
2016-01-21 08:23:05 +00:00
/// Current server TLS port.
2015-09-07 18:09:06 +00:00
std::string port_;
2016-01-21 08:23:05 +00:00
private:
std::string tls_hostname_;
std::string enroll_tls_endpoint_;
std::string tls_server_certs_;
std::string enroll_secret_path_;
2015-09-07 18:09:06 +00:00
};
2014-08-15 07:25:30 +00:00
}