Skip to content

Commit ced148c

Browse files
committed
intercept: add unit test
1 parent cfec982 commit ced148c

File tree

2 files changed

+89
-6
lines changed

2 files changed

+89
-6
lines changed

source/intercept/CMakeLists.txt

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
include(GNUInstallDirs)
2+
3+
# RPC related types and stubs.
14
add_subdirectory(proto)
25

3-
include(GNUInstallDirs)
46

7+
# Domain types and methods for intercept artefacts.
58
add_library(domain_a OBJECT
69
source/Domain.cc
710
source/Convert.cc
@@ -11,7 +14,7 @@ target_include_directories(domain_a PUBLIC source)
1114
target_link_libraries(domain_a PUBLIC rpc_a)
1215

1316

14-
# Create a static library, which is used for unit tests and the final shared library.
17+
# Intercept orchestrator code.
1518
add_library(intercept_a OBJECT
1619
source/collect/Application.cc
1720
source/collect/EventsDatabase.cc
@@ -37,7 +40,8 @@ target_link_libraries(intercept_a PUBLIC result_a)
3740
target_link_libraries(intercept_a PUBLIC spdlog::spdlog)
3841
target_link_libraries(intercept_a PUBLIC PkgConfig::sqlite)
3942

40-
# Create an executable from the sub projects.
43+
44+
# Intercept orchestrator executable.
4145
add_executable(intercept
4246
source/collect/main.cc
4347
)
@@ -53,6 +57,7 @@ target_link_libraries(intercept exec_a)
5357
install(TARGETS intercept
5458
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
5559

60+
5661
# Markdown file is the source to the man file. Please modify that and generate
5762
# the man file from it with pandoc.
5863
#
@@ -65,22 +70,38 @@ install(TARGETS intercept
6570
install(FILES man/intercept.1
6671
DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
6772

68-
add_executable(wrapper
73+
74+
# Intercept report sender code.
75+
add_library(wrapper_a OBJECT
6976
source/report/wrapper/EventFactory.cc
7077
source/report/wrapper/EventReporter.cc
7178
source/report/wrapper/RpcClients.cc
7279
source/report/wrapper/Application.cc
80+
)
81+
82+
target_include_directories(wrapper_a PUBLIC source/)
83+
target_link_libraries(wrapper_a PUBLIC domain_a)
84+
target_link_libraries(wrapper_a PUBLIC flags_a)
85+
target_link_libraries(wrapper_a PUBLIC main_a)
86+
target_link_libraries(wrapper_a PUBLIC result_a)
87+
target_link_libraries(wrapper_a PUBLIC sys_a)
88+
target_link_libraries(wrapper_a PUBLIC rpc_a)
89+
target_link_libraries(wrapper_a PUBLIC spdlog::spdlog)
90+
91+
92+
# Intercept report sender executable.
93+
add_executable(wrapper
7394
source/report/wrapper/main.cc
7495
)
7596

7697
target_include_directories(wrapper PUBLIC source/)
98+
target_link_libraries(wrapper wrapper_a)
7799
target_link_libraries(wrapper domain_a)
78100
target_link_libraries(wrapper main_a)
79101
target_link_libraries(wrapper flags_a)
80102
target_link_libraries(wrapper result_a)
81103
target_link_libraries(wrapper sys_a)
82104
target_link_libraries(wrapper rpc_a)
83-
target_link_libraries(wrapper spdlog::spdlog)
84105

85106
install(TARGETS wrapper
86107
RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR})
@@ -123,6 +144,8 @@ install(CODE "
123144
)
124145
")
125146

147+
148+
# Execution interceptor code.
126149
add_library(exec_a OBJECT
127150
source/report/libexec/Buffer.cc
128151
source/report/libexec/Environment.cc
@@ -141,7 +164,8 @@ set_target_properties(exec_a PROPERTIES
141164
LINKER_LANGUAGE "C"
142165
POSITION_INDEPENDENT_CODE 1)
143166

144-
# Create a shared library from the sub projects.
167+
168+
# Execution interceptor shared library.
145169
add_library(exec SHARED
146170
source/report/libexec/lib.cc
147171
source/report/libexec/std.cc
@@ -165,6 +189,7 @@ endif ()
165189
install(TARGETS exec
166190
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBEXECDIR})
167191

192+
168193
# Create unit test.
169194
if (ENABLE_UNIT_TESTS)
170195
add_executable(exec_unit_test
@@ -182,6 +207,7 @@ if (ENABLE_UNIT_TESTS)
182207
add_test(NAME bear::exec_unit_test COMMAND $<TARGET_FILE:exec_unit_test>)
183208

184209
add_executable(intercept_unit_test
210+
test/EventFactoryTest.cc
185211
test/SessionTest.cc
186212
test/ThreadSafeQueueTest.cc
187213
)
@@ -190,6 +216,7 @@ if (ENABLE_UNIT_TESTS)
190216
target_link_libraries(intercept_unit_test main_a)
191217
target_link_libraries(intercept_unit_test exec_a)
192218
target_link_libraries(intercept_unit_test intercept_a)
219+
target_link_libraries(intercept_unit_test wrapper_a)
193220
target_link_libraries(intercept_unit_test rpc_a)
194221
target_link_libraries(intercept_unit_test sys_a)
195222
target_link_libraries(intercept_unit_test flags_a)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* Copyright (C) 2012-2021 by László Nagy
2+
This file is part of Bear.
3+
4+
Bear is a tool to generate compilation database for clang tooling.
5+
6+
Bear is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
Bear is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "gtest/gtest.h"
21+
22+
#include "report/wrapper/EventFactory.h"
23+
24+
namespace {
25+
26+
const wr::ProcessId TEST_PID = 11;
27+
const wr::ProcessId TEST_PPID = 10;
28+
const wr::Execution TEST_EXECUTION = wr::Execution {
29+
fs::path("/usr/bin/ls"),
30+
{"ls", "-la"},
31+
fs::path("/home/user"),
32+
{ {"PATH", "/usr/bin:/usr/sbin"} }
33+
};
34+
35+
TEST(event_factory, same_factory_creates_events_with_same_id)
36+
{
37+
wr::EventFactory sut;
38+
auto start = sut.start(TEST_PID, TEST_PPID, TEST_EXECUTION);
39+
auto signal = sut.signal(11);
40+
auto stop = sut.terminate(5);
41+
42+
EXPECT_EQ(start.rid(), signal.rid());
43+
EXPECT_EQ(start.rid(), stop.rid());
44+
}
45+
46+
TEST(event_factory, different_factory_creates_event_with_different_id)
47+
{
48+
wr::EventFactory sut1;
49+
auto start1 = sut1.start(TEST_PID, TEST_PPID, TEST_EXECUTION);
50+
51+
wr::EventFactory sut2;
52+
auto start2 = sut2.start(TEST_PID, TEST_PPID, TEST_EXECUTION);
53+
54+
EXPECT_NE(start1.rid(), start2.rid());
55+
}
56+
}

0 commit comments

Comments
 (0)