osquery-1/osquery/core/test_util.h

110 lines
3.8 KiB
C
Raw Normal View History

/*
* Copyright (c) 2014, 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>
#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
2015-04-27 21:57:04 +00:00
namespace pt = boost::property_tree;
2014-08-15 07:25:30 +00:00
namespace osquery {
2014-07-31 00:35:19 +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";
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-06-30 21:20:04 +00:00
2015-04-29 19:36:24 +00:00
#ifdef DARWIN
2015-06-30 21:20:04 +00:00
const std::string kTestWorkingDirectory = "/private/tmp/osquery-tests";
2015-04-29 19:36:24 +00:00
#else
2015-06-30 21:20:04 +00:00
const std::string kTestWorkingDirectory = "/tmp/osquery-tests";
2015-04-29 19:36:24 +00:00
#endif
/// 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-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();
// remove the small directory structure used for testing
void tearDownMockFileStructure();
2014-08-15 07:25:30 +00:00
}