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-01-30 01:33:41 +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
|
|
|
|
2015-09-07 18:09:06 +00:00
|
|
|
#include <chrono>
|
2014-07-31 00:35:19 +00:00
|
|
|
#include <deque>
|
2015-09-07 18:09:06 +00:00
|
|
|
#include <random>
|
2014-07-31 00:35:19 +00:00
|
|
|
#include <sstream>
|
2016-03-07 09:48:09 +00:00
|
|
|
#include <thread>
|
2015-07-23 23:42:46 +00:00
|
|
|
|
2015-09-07 18:09:06 +00:00
|
|
|
#include <signal.h>
|
2015-07-23 23:42:46 +00:00
|
|
|
#include <time.h>
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2015-02-14 00:52:11 +00:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2016-03-05 17:29:51 +00:00
|
|
|
#include <osquery/core.h>
|
|
|
|
#include <osquery/database.h>
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/filesystem.h>
|
2015-01-21 21:36:55 +00:00
|
|
|
#include <osquery/logger.h>
|
2016-03-07 09:48:09 +00:00
|
|
|
#include <osquery/sql.h>
|
2014-08-05 23:13:55 +00:00
|
|
|
|
2016-09-02 22:04:03 +00:00
|
|
|
#include "osquery/core/json.h"
|
2016-06-09 17:49:26 +00:00
|
|
|
#include "osquery/tests/test_util.h"
|
2014-12-17 05:35:26 +00:00
|
|
|
|
2015-06-30 18:48:11 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
2014-08-15 07:25:30 +00:00
|
|
|
namespace osquery {
|
2016-03-05 17:29:51 +00:00
|
|
|
|
2015-07-23 23:42:46 +00:00
|
|
|
std::string kFakeDirectory = "";
|
|
|
|
|
2015-07-29 06:08:15 +00:00
|
|
|
#ifdef DARWIN
|
|
|
|
std::string kTestWorkingDirectory = "/private/tmp/osquery-tests";
|
|
|
|
#else
|
2016-09-12 16:46:52 +00:00
|
|
|
std::string kTestWorkingDirectory =
|
|
|
|
(fs::temp_directory_path() / "osquery-tests").make_preferred().string();
|
2015-07-29 06:08:15 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/// Most tests will use binary or disk-backed content for parsing tests.
|
2016-09-20 21:18:58 +00:00
|
|
|
#if !defined(OSQUERY_BUILD_SDK) && !WIN32
|
2015-07-29 06:08:15 +00:00
|
|
|
std::string kTestDataPath = "../../../tools/tests/";
|
|
|
|
#else
|
|
|
|
std::string kTestDataPath = "../../../../tools/tests/";
|
|
|
|
#endif
|
|
|
|
|
2015-07-23 23:42:46 +00:00
|
|
|
DECLARE_string(database_path);
|
|
|
|
DECLARE_string(extensions_socket);
|
2016-09-12 16:46:52 +00:00
|
|
|
|
|
|
|
#ifndef WIN32
|
2015-07-23 23:42:46 +00:00
|
|
|
DECLARE_string(modules_autoload);
|
2016-09-12 16:46:52 +00:00
|
|
|
#endif
|
|
|
|
|
2015-07-23 23:42:46 +00:00
|
|
|
DECLARE_string(extensions_autoload);
|
2016-01-21 08:23:05 +00:00
|
|
|
DECLARE_string(enroll_tls_endpoint);
|
2015-07-23 23:42:46 +00:00
|
|
|
DECLARE_bool(disable_logging);
|
2016-05-20 15:39:04 +00:00
|
|
|
DECLARE_bool(disable_database);
|
2015-07-23 23:42:46 +00:00
|
|
|
|
|
|
|
typedef std::chrono::high_resolution_clock chrono_clock;
|
|
|
|
|
|
|
|
void initTesting() {
|
2016-08-10 03:27:42 +00:00
|
|
|
registryAndPluginInit();
|
2015-07-23 23:42:46 +00:00
|
|
|
// 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.
|
2016-09-12 16:46:52 +00:00
|
|
|
std::srand(static_cast<unsigned int>(
|
|
|
|
chrono_clock::now().time_since_epoch().count()));
|
2015-07-23 23:42:46 +00:00
|
|
|
|
|
|
|
// Set safe default values for path-based flags.
|
|
|
|
// Specific unittests may edit flags temporarily.
|
2016-09-12 16:46:52 +00:00
|
|
|
kTestWorkingDirectory += getUserId() + "/";
|
2015-07-29 06:08:15 +00:00
|
|
|
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";
|
2016-09-12 16:46:52 +00:00
|
|
|
|
|
|
|
#ifndef WIN32
|
2015-07-29 06:08:15 +00:00
|
|
|
FLAGS_modules_autoload = kTestWorkingDirectory + "unittests-mod.load";
|
2016-09-12 16:46:52 +00:00
|
|
|
#endif
|
|
|
|
|
2015-07-23 23:42:46 +00:00
|
|
|
FLAGS_disable_logging = true;
|
2016-05-20 15:39:04 +00:00
|
|
|
FLAGS_disable_database = true;
|
2015-07-23 23:42:46 +00:00
|
|
|
|
2016-03-05 17:29:51 +00:00
|
|
|
// Tests need a database plugin.
|
|
|
|
// Set up the database instance for the unittests.
|
|
|
|
DatabasePlugin::setAllowOpen(true);
|
2016-05-20 15:39:04 +00:00
|
|
|
DatabasePlugin::initPlugin();
|
2015-07-23 23:42:46 +00:00
|
|
|
}
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2016-09-12 16:46:52 +00:00
|
|
|
void shutdownTesting() {
|
|
|
|
DatabasePlugin::shutdown();
|
|
|
|
}
|
2016-03-05 17:29:51 +00:00
|
|
|
|
2015-11-02 18:33:20 +00:00
|
|
|
std::map<std::string, std::string> getTestConfigMap() {
|
|
|
|
std::string content;
|
2016-09-20 21:18:58 +00:00
|
|
|
readFile(fs::path(kTestDataPath) / "test_parse_items.conf", content);
|
2015-11-02 18:33:20 +00:00
|
|
|
std::map<std::string, std::string> config;
|
|
|
|
config["awesome"] = content;
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
pt::ptree getExamplePacksConfig() {
|
|
|
|
std::string content;
|
2016-09-20 21:18:58 +00:00
|
|
|
auto s = readFile(fs::path(kTestDataPath) / "test_inline_pack.conf", content);
|
2015-11-02 18:33:20 +00:00
|
|
|
assert(s.ok());
|
|
|
|
std::stringstream json;
|
|
|
|
json << content;
|
|
|
|
pt::ptree tree;
|
|
|
|
pt::read_json(json, tree);
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// no discovery queries, no platform restriction
|
|
|
|
pt::ptree getUnrestrictedPack() {
|
|
|
|
auto tree = getExamplePacksConfig();
|
|
|
|
auto packs = tree.get_child("packs");
|
|
|
|
return packs.get_child("unrestricted_pack");
|
|
|
|
}
|
|
|
|
|
2016-02-06 01:10:35 +00:00
|
|
|
// several restrictions (version, platform, shard)
|
|
|
|
pt::ptree getRestrictedPack() {
|
|
|
|
auto tree = getExamplePacksConfig();
|
|
|
|
auto packs = tree.get_child("packs");
|
|
|
|
return packs.get_child("restricted_pack");
|
|
|
|
}
|
|
|
|
|
2015-11-02 18:33:20 +00:00
|
|
|
/// 1 discovery query, darwin platform restriction
|
|
|
|
pt::ptree getPackWithDiscovery() {
|
|
|
|
auto tree = getExamplePacksConfig();
|
|
|
|
auto packs = tree.get_child("packs");
|
|
|
|
return packs.get_child("discovery_pack");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 1 discovery query which will always pass
|
|
|
|
pt::ptree getPackWithValidDiscovery() {
|
|
|
|
auto tree = getExamplePacksConfig();
|
|
|
|
auto packs = tree.get_child("packs");
|
|
|
|
return packs.get_child("valid_discovery_pack");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// no discovery queries, no platform restriction, fake version string
|
|
|
|
pt::ptree getPackWithFakeVersion() {
|
|
|
|
auto tree = getExamplePacksConfig();
|
|
|
|
auto packs = tree.get_child("packs");
|
|
|
|
return packs.get_child("fake_version_pack");
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:35:19 +00:00
|
|
|
QueryData getTestDBExpectedResults() {
|
|
|
|
QueryData d;
|
|
|
|
Row row1;
|
|
|
|
row1["username"] = "mike";
|
|
|
|
row1["age"] = "23";
|
|
|
|
d.push_back(row1);
|
|
|
|
Row row2;
|
|
|
|
row2["username"] = "matt";
|
|
|
|
row2["age"] = "24";
|
|
|
|
d.push_back(row2);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2016-05-20 15:39:04 +00:00
|
|
|
std::vector<std::pair<std::string, QueryData>> getTestDBResultStream() {
|
|
|
|
std::vector<std::pair<std::string, QueryData>> results;
|
2014-07-31 00:35:19 +00:00
|
|
|
|
|
|
|
std::string q2 =
|
2014-08-15 07:25:30 +00:00
|
|
|
"INSERT INTO test_table (username, age) VALUES (\"joe\", 25)";
|
2014-07-31 00:35:19 +00:00
|
|
|
QueryData d2;
|
|
|
|
Row row2_1;
|
|
|
|
row2_1["username"] = "mike";
|
|
|
|
row2_1["age"] = "23";
|
|
|
|
d2.push_back(row2_1);
|
|
|
|
Row row2_2;
|
|
|
|
row2_2["username"] = "matt";
|
|
|
|
row2_2["age"] = "24";
|
|
|
|
d2.push_back(row2_2);
|
|
|
|
Row row2_3;
|
|
|
|
row2_3["username"] = "joe";
|
|
|
|
row2_3["age"] = "25";
|
|
|
|
d2.push_back(row2_3);
|
|
|
|
results.push_back(std::make_pair(q2, d2));
|
|
|
|
|
|
|
|
std::string q3 = "UPDATE test_table SET age = 27 WHERE username = \"matt\"";
|
|
|
|
QueryData d3;
|
|
|
|
Row row3_1;
|
|
|
|
row3_1["username"] = "mike";
|
|
|
|
row3_1["age"] = "23";
|
|
|
|
d3.push_back(row3_1);
|
|
|
|
Row row3_2;
|
|
|
|
row3_2["username"] = "matt";
|
|
|
|
row3_2["age"] = "27";
|
|
|
|
d3.push_back(row3_2);
|
|
|
|
Row row3_3;
|
|
|
|
row3_3["username"] = "joe";
|
|
|
|
row3_3["age"] = "25";
|
|
|
|
d3.push_back(row3_3);
|
|
|
|
results.push_back(std::make_pair(q3, d3));
|
|
|
|
|
|
|
|
std::string q4 =
|
2014-08-15 07:25:30 +00:00
|
|
|
"DELETE FROM test_table WHERE username = \"matt\" AND age = 27";
|
2014-07-31 00:35:19 +00:00
|
|
|
QueryData d4;
|
|
|
|
Row row4_1;
|
|
|
|
row4_1["username"] = "mike";
|
|
|
|
row4_1["age"] = "23";
|
|
|
|
d4.push_back(row4_1);
|
|
|
|
Row row4_2;
|
|
|
|
row4_2["username"] = "joe";
|
|
|
|
row4_2["age"] = "25";
|
|
|
|
d4.push_back(row4_2);
|
|
|
|
results.push_back(std::make_pair(q4, d4));
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
ScheduledQuery getOsqueryScheduledQuery() {
|
2015-03-22 21:58:00 +00:00
|
|
|
ScheduledQuery sq;
|
|
|
|
sq.query = "SELECT filename FROM fs WHERE path = '/bin' ORDER BY filename";
|
|
|
|
sq.interval = 5;
|
|
|
|
return sq;
|
2014-07-31 00:35:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
std::pair<pt::ptree, Row> getSerializedRow() {
|
2014-07-31 00:35:19 +00:00
|
|
|
Row r;
|
|
|
|
r["foo"] = "bar";
|
|
|
|
r["meaning_of_life"] = "42";
|
|
|
|
pt::ptree arr;
|
|
|
|
arr.put<std::string>("foo", "bar");
|
|
|
|
arr.put<std::string>("meaning_of_life", "42");
|
|
|
|
return std::make_pair(arr, r);
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
std::pair<pt::ptree, QueryData> getSerializedQueryData() {
|
2014-07-31 00:35:19 +00:00
|
|
|
auto r = getSerializedRow();
|
|
|
|
QueryData q = {r.second, r.second};
|
|
|
|
pt::ptree arr;
|
|
|
|
arr.push_back(std::make_pair("", r.first));
|
|
|
|
arr.push_back(std::make_pair("", r.first));
|
|
|
|
return std::make_pair(arr, q);
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
std::pair<pt::ptree, DiffResults> getSerializedDiffResults() {
|
2014-07-31 00:35:19 +00:00
|
|
|
auto qd = getSerializedQueryData();
|
|
|
|
DiffResults diff_results;
|
|
|
|
diff_results.added = qd.second;
|
|
|
|
diff_results.removed = qd.second;
|
|
|
|
|
|
|
|
pt::ptree root;
|
|
|
|
root.add_child("removed", qd.first);
|
2016-07-20 19:25:10 +00:00
|
|
|
root.add_child("added", qd.first);
|
2014-07-31 00:35:19 +00:00
|
|
|
|
|
|
|
return std::make_pair(root, diff_results);
|
|
|
|
}
|
|
|
|
|
2015-03-22 21:58:00 +00:00
|
|
|
std::pair<std::string, DiffResults> getSerializedDiffResultsJSON() {
|
2014-07-31 00:35:19 +00:00
|
|
|
auto results = getSerializedDiffResults();
|
|
|
|
std::ostringstream ss;
|
|
|
|
pt::write_json(ss, results.first, false);
|
|
|
|
return std::make_pair(ss.str(), results.second);
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
std::pair<std::string, QueryData> getSerializedQueryDataJSON() {
|
|
|
|
auto results = getSerializedQueryData();
|
2014-07-31 00:35:19 +00:00
|
|
|
std::ostringstream ss;
|
|
|
|
pt::write_json(ss, results.first, false);
|
|
|
|
return std::make_pair(ss.str(), results.second);
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
std::pair<pt::ptree, QueryLogItem> getSerializedQueryLogItem() {
|
|
|
|
QueryLogItem i;
|
2014-07-31 00:35:19 +00:00
|
|
|
pt::ptree root;
|
|
|
|
auto dr = getSerializedDiffResults();
|
2015-04-27 21:57:04 +00:00
|
|
|
i.results = dr.second;
|
2014-07-31 00:35:19 +00:00
|
|
|
i.name = "foobar";
|
2015-04-27 21:57:04 +00:00
|
|
|
i.calendar_time = "Mon Aug 25 12:10:57 2014";
|
|
|
|
i.time = 1408993857;
|
|
|
|
i.identifier = "foobaz";
|
2014-07-31 00:35:19 +00:00
|
|
|
root.add_child("diffResults", dr.first);
|
|
|
|
root.put<std::string>("name", "foobar");
|
2014-11-04 02:08:13 +00:00
|
|
|
root.put<std::string>("hostIdentifier", "foobaz");
|
2014-08-21 21:35:51 +00:00
|
|
|
root.put<std::string>("calendarTime", "Mon Aug 25 12:10:57 2014");
|
|
|
|
root.put<int>("unixTime", 1408993857);
|
2014-07-31 00:35:19 +00:00
|
|
|
return std::make_pair(root, i);
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
std::pair<std::string, QueryLogItem> getSerializedQueryLogItemJSON() {
|
|
|
|
auto results = getSerializedQueryLogItem();
|
2014-07-31 00:35:19 +00:00
|
|
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
pt::write_json(ss, results.first, false);
|
|
|
|
|
|
|
|
return std::make_pair(ss.str(), results.second);
|
|
|
|
}
|
|
|
|
|
2014-09-05 14:54:41 +00:00
|
|
|
std::vector<SplitStringTestData> generateSplitStringTestData() {
|
|
|
|
SplitStringTestData s1;
|
|
|
|
s1.test_string = "a b\tc";
|
|
|
|
s1.test_vector = {"a", "b", "c"};
|
|
|
|
|
|
|
|
SplitStringTestData s2;
|
|
|
|
s2.test_string = " a b c";
|
|
|
|
s2.test_vector = {"a", "b", "c"};
|
|
|
|
|
|
|
|
SplitStringTestData s3;
|
|
|
|
s3.test_string = " a b c";
|
|
|
|
s3.test_vector = {"a", "b", "c"};
|
|
|
|
|
|
|
|
return {s1, s2, s3};
|
|
|
|
}
|
|
|
|
|
2014-08-17 08:44:22 +00:00
|
|
|
std::string getCACertificateContent() {
|
2014-12-17 05:35:26 +00:00
|
|
|
std::string content;
|
2016-09-20 21:18:58 +00:00
|
|
|
readFile(fs::path(kTestDataPath) / "test_cert.pem", content);
|
2014-08-17 08:44:22 +00:00
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2014-08-02 03:46:22 +00:00
|
|
|
std::string getEtcHostsContent() {
|
2014-12-17 05:35:26 +00:00
|
|
|
std::string content;
|
2016-09-20 21:18:58 +00:00
|
|
|
readFile(fs::path(kTestDataPath) / "test_hosts.txt", content);
|
2014-08-02 03:46:22 +00:00
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2015-04-23 00:49:27 +00:00
|
|
|
std::string getEtcProtocolsContent() {
|
|
|
|
std::string content;
|
2016-09-20 21:18:58 +00:00
|
|
|
readFile(fs::path(kTestDataPath) / "test_protocols.txt", content);
|
2015-04-23 00:49:27 +00:00
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2015-03-22 21:58:00 +00:00
|
|
|
QueryData getEtcHostsExpectedResults() {
|
2014-08-02 03:46:22 +00:00
|
|
|
Row row1;
|
|
|
|
Row row2;
|
|
|
|
Row row3;
|
|
|
|
Row row4;
|
2015-04-07 06:32:56 +00:00
|
|
|
Row row5;
|
|
|
|
Row row6;
|
2014-08-02 03:46:22 +00:00
|
|
|
|
2014-08-04 21:12:06 +00:00
|
|
|
row1["address"] = "127.0.0.1";
|
|
|
|
row1["hostnames"] = "localhost";
|
|
|
|
row2["address"] = "255.255.255.255";
|
|
|
|
row2["hostnames"] = "broadcasthost";
|
|
|
|
row3["address"] = "::1";
|
|
|
|
row3["hostnames"] = "localhost";
|
|
|
|
row4["address"] = "fe80::1%lo0";
|
|
|
|
row4["hostnames"] = "localhost";
|
2015-04-07 06:32:56 +00:00
|
|
|
row5["address"] = "127.0.0.1";
|
|
|
|
row5["hostnames"] = "example.com example";
|
|
|
|
row6["address"] = "127.0.0.1";
|
|
|
|
row6["hostnames"] = "example.net";
|
|
|
|
return {row1, row2, row3, row4, row5, row6};
|
2014-08-02 03:46:22 +00:00
|
|
|
}
|
2015-01-30 01:33:41 +00:00
|
|
|
|
|
|
|
::std::ostream& operator<<(::std::ostream& os, const Status& s) {
|
|
|
|
return os << "Status(" << s.getCode() << ", \"" << s.getMessage() << "\")";
|
|
|
|
}
|
2015-02-14 00:52:11 +00:00
|
|
|
|
2015-04-23 00:49:27 +00:00
|
|
|
QueryData getEtcProtocolsExpectedResults() {
|
|
|
|
Row row1;
|
|
|
|
Row row2;
|
|
|
|
Row row3;
|
|
|
|
|
|
|
|
row1["name"] = "ip";
|
|
|
|
row1["number"] = "0";
|
|
|
|
row1["alias"] = "IP";
|
|
|
|
row1["comment"] = "internet protocol, pseudo protocol number";
|
|
|
|
row2["name"] = "icmp";
|
|
|
|
row2["number"] = "1";
|
|
|
|
row2["alias"] = "ICMP";
|
|
|
|
row2["comment"] = "internet control message protocol";
|
|
|
|
row3["name"] = "tcp";
|
|
|
|
row3["number"] = "6";
|
|
|
|
row3["alias"] = "TCP";
|
|
|
|
row3["comment"] = "transmission control protocol";
|
|
|
|
|
|
|
|
return {row1, row2, row3};
|
|
|
|
}
|
|
|
|
|
2015-02-14 00:52:11 +00:00
|
|
|
void createMockFileStructure() {
|
2015-06-30 18:48:11 +00:00
|
|
|
fs::create_directories(kFakeDirectory + "/deep11/deep2/deep3/");
|
|
|
|
fs::create_directories(kFakeDirectory + "/deep1/deep2/");
|
2015-02-14 00:52:11 +00:00
|
|
|
writeTextFile(kFakeDirectory + "/root.txt", "root");
|
2015-03-02 02:26:37 +00:00
|
|
|
writeTextFile(kFakeDirectory + "/door.txt", "toor");
|
2015-02-14 00:52:11 +00:00
|
|
|
writeTextFile(kFakeDirectory + "/roto.txt", "roto");
|
|
|
|
writeTextFile(kFakeDirectory + "/deep1/level1.txt", "l1");
|
|
|
|
writeTextFile(kFakeDirectory + "/deep11/not_bash", "l1");
|
|
|
|
writeTextFile(kFakeDirectory + "/deep1/deep2/level2.txt", "l2");
|
|
|
|
|
|
|
|
writeTextFile(kFakeDirectory + "/deep11/level1.txt", "l1");
|
|
|
|
writeTextFile(kFakeDirectory + "/deep11/deep2/level2.txt", "l2");
|
|
|
|
writeTextFile(kFakeDirectory + "/deep11/deep2/deep3/level3.txt", "l3");
|
2015-06-30 18:48:11 +00:00
|
|
|
|
2016-09-12 16:46:52 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
writeTextFile(kFakeDirectory + "/root2.txt", "l1");
|
|
|
|
#else
|
2015-06-30 18:48:11 +00:00
|
|
|
boost::system::error_code ec;
|
|
|
|
fs::create_symlink(
|
|
|
|
kFakeDirectory + "/root.txt", kFakeDirectory + "/root2.txt", ec);
|
2016-09-12 16:46:52 +00:00
|
|
|
#endif
|
2015-02-14 00:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tearDownMockFileStructure() {
|
|
|
|
boost::filesystem::remove_all(kFakeDirectory);
|
|
|
|
}
|
2015-09-07 18:09:06 +00:00
|
|
|
|
|
|
|
void TLSServerRunner::start() {
|
|
|
|
auto& self = instance();
|
|
|
|
if (self.server_ != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-21 08:23:05 +00:00
|
|
|
// Pick a port in an ephemeral range at random.
|
|
|
|
self.port_ = std::to_string(rand() % 10000 + 20000);
|
|
|
|
|
2015-09-07 18:09:06 +00:00
|
|
|
// Fork then exec a shell.
|
2016-09-12 16:46:52 +00:00
|
|
|
auto python_server = (fs::path(kTestDataPath) / "test_http_server.py")
|
|
|
|
.make_preferred()
|
|
|
|
.string() +
|
|
|
|
" --tls " + self.port_;
|
|
|
|
self.server_ = PlatformProcess::launchPythonScript(python_server);
|
|
|
|
if (self.server_ == nullptr) {
|
|
|
|
return;
|
2015-09-07 18:09:06 +00:00
|
|
|
}
|
2016-03-07 09:48:09 +00:00
|
|
|
|
|
|
|
size_t delay = 0;
|
|
|
|
std::string query =
|
|
|
|
"select pid from listening_ports where port = '" + self.port_ + "'";
|
|
|
|
while (delay < 2 * 1000) {
|
|
|
|
auto results = SQL(query);
|
|
|
|
if (results.rows().size() > 0) {
|
2016-09-12 16:46:52 +00:00
|
|
|
self.server_.reset(
|
|
|
|
new PlatformProcess(std::atoi(results.rows()[0].at("pid").c_str())));
|
2016-03-07 09:48:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
delay += 100;
|
|
|
|
}
|
2015-09-07 18:09:06 +00:00
|
|
|
}
|
|
|
|
|
2016-01-21 08:23:05 +00:00
|
|
|
void TLSServerRunner::setClientConfig() {
|
|
|
|
auto& self = instance();
|
|
|
|
|
|
|
|
self.tls_hostname_ = Flag::getValue("tls_hostname");
|
|
|
|
Flag::updateValue("tls_hostname", "localhost:" + port());
|
|
|
|
|
|
|
|
self.enroll_tls_endpoint_ = Flag::getValue("enroll_tls_endpoint");
|
|
|
|
Flag::updateValue("enroll_tls_endpoint", "/enroll");
|
|
|
|
|
|
|
|
self.tls_server_certs_ = Flag::getValue("tls_server_certs");
|
2016-09-20 21:18:58 +00:00
|
|
|
Flag::updateValue("tls_server_certs",
|
|
|
|
(fs::path(kTestDataPath) / "test_server_ca.pem")
|
|
|
|
.make_preferred()
|
|
|
|
.string());
|
2016-01-21 08:23:05 +00:00
|
|
|
|
|
|
|
self.enroll_secret_path_ = Flag::getValue("enroll_secret_path");
|
|
|
|
Flag::updateValue("enroll_secret_path",
|
2016-09-20 21:18:58 +00:00
|
|
|
(fs::path(kTestDataPath) / "test_enroll_secret.txt")
|
|
|
|
.make_preferred()
|
|
|
|
.string());
|
2016-01-21 08:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TLSServerRunner::unsetClientConfig() {
|
|
|
|
auto& self = instance();
|
|
|
|
Flag::updateValue("tls_hostname", self.tls_hostname_);
|
|
|
|
Flag::updateValue("enroll_tls_endpoint", self.enroll_tls_endpoint_);
|
|
|
|
Flag::updateValue("tls_server_certs", self.tls_server_certs_);
|
|
|
|
Flag::updateValue("enroll_secret_path", self.enroll_secret_path_);
|
|
|
|
}
|
|
|
|
|
2015-09-07 18:09:06 +00:00
|
|
|
void TLSServerRunner::stop() {
|
|
|
|
auto& self = instance();
|
2016-09-12 16:46:52 +00:00
|
|
|
if (self.server_ != nullptr) {
|
|
|
|
self.server_->kill();
|
|
|
|
self.server_.reset();
|
|
|
|
}
|
2015-09-07 18:09:06 +00:00
|
|
|
}
|
2014-08-15 07:25:30 +00:00
|
|
|
}
|