forked from osquery/osquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_util.cpp
160 lines (127 loc) · 4.52 KB
/
test_util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Copyright (c) 2014-present, The osquery authors
*
* This source code is licensed as defined by the LICENSE file found in the
* root directory of this source tree.
*
* SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)
*/
#include <chrono>
#include <deque>
#include <random>
#include <sstream>
#include <thread>
#include <csignal>
#include <ctime>
#include <gtest/gtest.h>
#include <boost/filesystem/operations.hpp>
#include <osquery/core/system.h>
#include <osquery/logger/logger.h>
#include <osquery/registry/registry_factory.h>
#include <osquery/sql/sql.h>
#include <osquery/utils/conversions/tryto.h>
#include <osquery/utils/system/time.h>
#include <osquery/process/process.h>
#include <osquery/tests/test_util.h>
#include <osquery/utils/info/platform_type.h>
namespace fs = boost::filesystem;
namespace osquery {
/// Will be set with initTesting in test harness main.
std::string kFakeDirectory;
/// Will be set with initTesting in test harness main.
std::string kTestWorkingDirectory;
/// The relative path within the source repo to find test content.
std::string kTestDataPath{"../../../tools/tests/"};
DECLARE_string(database_path);
DECLARE_string(extensions_socket);
DECLARE_string(extensions_autoload);
DECLARE_string(enroll_tls_endpoint);
DECLARE_bool(disable_logging);
DECLARE_bool(disable_database);
using chrono_clock = std::chrono::high_resolution_clock;
void initTesting() {
setStartTime(getUnixTime());
setToolType(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.
setDatabaseAllowOpen();
initDatabasePlugin();
platformSetup();
}
void shutdownTesting() {
shutdownDatabase();
platformTeardown();
}
ScheduledQuery getOsqueryScheduledQuery() {
ScheduledQuery sq(
"path_pack",
"bin",
"SELECT filename FROM fs WHERE path = '/bin' ORDER BY filename");
sq.interval = 5;
return sq;
}
TableRows genRows(EventSubscriberPlugin* sub) {
auto vtc = std::make_shared<VirtualTableContent>();
QueryContext context(vtc);
RowGenerator::pull_type generator(std::bind(&EventSubscriberPlugin::genTable,
sub,
std::placeholders::_1,
std::move(context)));
TableRows results;
if (!generator) {
return results;
}
while (generator) {
results.push_back(generator.get());
generator();
}
return results;
}
} // namespace osquery