2014-12-18 18:50:47 +00:00
|
|
|
/*
|
2016-02-11 19:48:58 +00:00
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
2014-12-18 18:50:47 +00:00
|
|
|
* 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
|
2014-12-18 18:50:47 +00:00
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2014-09-10 01:54:53 +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>
|
|
|
|
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/config.h>
|
|
|
|
#include <osquery/core.h>
|
|
|
|
#include <osquery/database.h>
|
|
|
|
#include <osquery/filesystem.h>
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2016-05-11 21:16:32 +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
|
|
|
|
2015-07-29 06:08:15 +00:00
|
|
|
/// Cleanup/stop function for tests and benchmarks.
|
2016-03-05 17:29:51 +00:00
|
|
|
void shutdownTesting();
|
2015-07-29 06:08:15 +00:00
|
|
|
|
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";
|
|
|
|
|
2015-07-29 06:08:15 +00:00
|
|
|
/// Tests can be run from within the source or build directory.
|
|
|
|
/// The test initializer will attempt to discovery the current working path.
|
2015-05-23 19:40:36 +00:00
|
|
|
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.
|
2015-07-29 06:08:15 +00:00
|
|
|
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();
|
2016-02-06 01:10:35 +00:00
|
|
|
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()
|
2015-03-22 21:58:00 +00:00
|
|
|
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
|
2015-03-22 21:58:00 +00:00
|
|
|
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
|
2015-03-22 21:58:00 +00:00
|
|
|
// 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
|
2015-03-22 21:58:00 +00:00
|
|
|
// 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
|
2015-03-22 21:58:00 +00:00
|
|
|
// 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();
|
2015-03-22 21:58:00 +00:00
|
|
|
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
|
2015-03-22 21:58:00 +00:00
|
|
|
// 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
|
|
|
|
2014-08-17 08:44:22 +00:00
|
|
|
// generate content for a PEM-encoded certificate
|
|
|
|
std::string getCACertificateContent();
|
|
|
|
|
2014-08-02 03:46:22 +00:00
|
|
|
// 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();
|
|
|
|
|
2014-08-02 03:46:22 +00:00
|
|
|
// generate the expected data that getEtcHostsContent() should parse into
|
2015-03-22 21:58:00 +00:00
|
|
|
QueryData getEtcHostsExpectedResults();
|
2014-08-02 03:46:22 +00:00
|
|
|
|
2015-04-23 00:49:27 +00:00
|
|
|
// generate the expected data that getEtcProtocolsContent() should parse into
|
|
|
|
QueryData getEtcProtocolsExpectedResults();
|
|
|
|
|
2015-01-21 21:36:55 +00:00
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
2015-01-21 21:36:55 +00:00
|
|
|
// 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
|
|
|
}
|
2016-05-11 21:16:32 +00:00
|
|
|
|