Skip to content

Commit efc64f1

Browse files
authored
Merge pull request #13 from maxDcb/codex/assess-test-coverage-for-beacon-and-listener
Introduce Catch2-based tests for beacon and listener
2 parents 985bc8d + faa0e52 commit efc64f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+397
-405
lines changed

.github/workflows/Tests_Linux.yml

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Module Tests (Linux)
1+
name: C2Core Tests (Linux)
22

33
on: workflow_dispatch
44

@@ -11,34 +11,13 @@ jobs:
1111
uses: actions/checkout@v3
1212

1313
- name: Install dependencies
14-
run: sudo apt-get update && sudo apt-get install -y cmake g++ curl make
15-
16-
- name: Fetch base64.cpp
17-
run: |
18-
mkdir -p thirdParty/base64
19-
curl -L https://raw.githubusercontent.com/ReneNyffenegger/cpp-base64/82147d6d89636217b870f54ec07ddd3e544d5f69/base64.cpp \
20-
-o thirdParty/base64/base64.cpp
21-
22-
- name: Fetch module dependencies
23-
run: |
24-
cd modules
25-
mkdir -p ModuleCmd/nlohmann
26-
curl -o ModuleCmd/nlohmann/json.hpp https://raw.githubusercontent.com/maxDcb/C2TeamServer/refs/heads/master/thirdParty/nlohmann/json.hpp
27-
curl -o ModuleCmd/base64.h https://raw.githubusercontent.com/ReneNyffenegger/cpp-base64/82147d6d89636217b870f54ec07ddd3e544d5f69/base64.h
14+
run: sudo apt-get update && sudo apt-get install -y cmake g++ curl make libssl-dev
2815

2916
- name: Configure with CMake
30-
run: |
31-
cd modules
32-
mkdir build
33-
cd build
34-
cmake -DWITH_TESTS=ON ..
17+
run: cmake -S . -B build -DC2CORE_BUILD_TESTS=ON
3518

3619
- name: Build
37-
run: |
38-
cd modules/build
39-
make -j$(nproc)
20+
run: cmake --build build -j$(nproc)
4021

4122
- name: Run tests
42-
run: |
43-
cd modules/build
44-
ctest --output-on-failure
23+
run: ctest --test-dir build --output-on-failure
Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Module Tests (Windows)
1+
name: C2Core Tests (Windows)
22

33
on: workflow_dispatch
44

@@ -15,23 +15,11 @@ jobs:
1515
with:
1616
msbuild-architecture: x64
1717

18-
- name: Fetch base64.cpp
19-
run: |
20-
mkdir thirdParty\base64
21-
curl -L https://raw.githubusercontent.com/ReneNyffenegger/cpp-base64/82147d6d89636217b870f54ec07ddd3e544d5f69/base64.cpp -o thirdParty\base64\base64.cpp
22-
23-
- name: Fetch module dependencies
24-
run: |
25-
cd modules
26-
mkdir ModuleCmd\nlohmann
27-
curl -o ModuleCmd\nlohmann\json.hpp https://raw.githubusercontent.com/maxDcb/C2TeamServer/refs/heads/master/thirdParty/nlohmann/json.hpp
28-
curl -o ModuleCmd\base64.h https://raw.githubusercontent.com/ReneNyffenegger/cpp-base64/82147d6d89636217b870f54ec07ddd3e544d5f69/base64.h
29-
3018
- name: Configure CMake for tests
31-
run: cmake -G "Visual Studio 17 2022" -S modules -B modules/build -DWITH_TESTS=ON
19+
run: cmake -G "Visual Studio 17 2022" -S . -B build -DC2CORE_BUILD_TESTS=ON
3220

33-
- name: Build modules with tests
34-
run: cmake --build modules/build --config Release --parallel
21+
- name: Build
22+
run: cmake --build build --config Release --parallel
3523

36-
- name: Run unit tests
37-
run: ctest --test-dir modules/build -C Release
24+
- name: Run tests
25+
run: ctest --test-dir build -C Release

AGENT.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ You are an expert C++/CMake contributor tasked with maintaining, and expending *
1111

1212
```bash
1313
mkdir -p build && cd build
14-
cmake -DWITH_TESTS=ON ..
14+
cmake -DC2CORE_BUILD_TESTS=ON ..
1515
cmake --build . -j
1616

1717
ctest --output-on-failure
18-
```
18+
```

CMakeLists.txt

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ FetchContent_Declare(
6767
SOURCE_DIR ${BASE64_SRC_DIR}
6868
)
6969
FetchContent_Populate(base64)
70+
include_directories(${BASE64_SRC_DIR})
7071

7172
set(DONUT_SRC_DIR ${CMAKE_SOURCE_DIR}/thirdParty/donut)
7273
FetchContent_Declare(
@@ -89,12 +90,31 @@ find_package(OpenSSL REQUIRED)
8990
add_library(openssl::openssl INTERFACE IMPORTED)
9091
target_link_libraries(openssl::openssl INTERFACE OpenSSL::SSL OpenSSL::Crypto)
9192

92-
option(BUILD_TESTING "Build unit tests" ON)
93-
if(BUILD_TESTING)
93+
# Build tests only when explicitly enabled. Default to ON when C2Core is the
94+
# top-level project and OFF when included from another build.
95+
option(C2CORE_BUILD_TESTS "Build C2Core unit tests" OFF)
96+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
97+
set(C2CORE_BUILD_TESTS ON CACHE BOOL "Build C2Core unit tests" FORCE)
98+
endif()
99+
100+
if(C2CORE_BUILD_TESTS)
94101
enable_testing()
95-
set(WITH_TESTS ON CACHE BOOL "" FORCE)
102+
103+
FetchContent_Declare(
104+
Catch2
105+
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
106+
GIT_TAG v3.4.0
107+
)
108+
FetchContent_Declare(
109+
nlohmann_json
110+
GIT_REPOSITORY https://github.com/nlohmann/json.git
111+
GIT_TAG v3.11.2
112+
)
113+
FetchContent_MakeAvailable(Catch2 nlohmann_json)
114+
include_directories(${nlohmann_json_SOURCE_DIR}/include)
115+
116+
add_subdirectory(beacon/tests)
117+
add_subdirectory(listener/tests)
96118
endif()
97119

98-
add_subdirectory(beacon/tests)
99-
add_subdirectory(listener/tests)
100120
add_subdirectory(modules)

beacon/tests/CMakeLists.txt

Lines changed: 112 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,115 @@
11
include_directories(..)
22
include_directories(../../modules/ModuleCmd)
33

4-
# Test testBeacon
5-
add_executable(testBeacon
6-
testBeacon.cpp
7-
../Beacon.cpp
8-
../../listener/Listener.cpp
9-
../../listener/ListenerTcp.cpp
10-
../../listener/ListenerSmb.cpp
11-
../../../thirdParty/base64/base64.cpp)
12-
IF (WIN32)
13-
set_property(TARGET testBeacon PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
14-
target_link_libraries(testBeacon SocketHandler PipeHandler MemoryModule SocksServer )
15-
ELSE()
16-
target_link_libraries(testBeacon SocketHandler PipeHandler MemoryModule SocksServer )
17-
ENDIF()
18-
add_custom_command(TARGET testBeacon POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
19-
$<TARGET_FILE:testBeacon> "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeacon>")
20-
add_test(NAME testBeacon COMMAND "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeacon>")
21-
22-
# Test testBeaconDns
23-
add_executable(testBeaconDns
24-
testBeaconDns.cpp
25-
../Beacon.cpp
26-
../../listener/Listener.cpp
27-
../../listener/ListenerTcp.cpp
28-
../../listener/ListenerSmb.cpp
29-
../../../thirdParty/base64/base64.cpp)
30-
IF (WIN32)
31-
set_property(TARGET testBeaconDns PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
32-
target_link_libraries(testBeaconDns SocketHandler Dnscommunication PipeHandler MemoryModule SocksServer )
33-
ELSE()
34-
target_link_libraries(testBeaconDns SocketHandler Dnscommunication PipeHandler MemoryModule SocksServer )
35-
ENDIF()
36-
add_custom_command(TARGET testBeaconDns POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
37-
$<TARGET_FILE:testBeaconDns> "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeaconDns>")
38-
add_test(NAME testBeaconDns COMMAND "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeaconDns>")
39-
40-
# Test testBeaconGithub
41-
add_executable(testBeaconGithub
42-
testBeaconGithub.cpp
43-
../Beacon.cpp
44-
../../listener/Listener.cpp
45-
../../listener/ListenerTcp.cpp
46-
../../listener/ListenerSmb.cpp
47-
../../../thirdParty/base64/base64.cpp)
48-
IF (WIN32)
49-
set_property(TARGET testBeaconGithub PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
50-
target_link_libraries(testBeaconGithub SocketHandler PipeHandler MemoryModule SocksServer )
51-
ELSE()
52-
target_link_libraries(testBeaconGithub SocketHandler PipeHandler MemoryModule SocksServer httplib::httplib)
53-
ENDIF()
54-
add_custom_command(TARGET testBeaconGithub POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
55-
$<TARGET_FILE:testBeaconGithub> "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeaconGithub>")
56-
add_test(NAME testBeaconGithub COMMAND "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeaconGithub>")
57-
58-
# Test testBeaconHttp
59-
add_executable(testBeaconHttp
60-
testBeaconHttp.cpp
61-
../Beacon.cpp
62-
../../listener/Listener.cpp
63-
../../listener/ListenerTcp.cpp
64-
../../listener/ListenerSmb.cpp
65-
../../../thirdParty/base64/base64.cpp)
66-
IF (WIN32)
67-
set_property(TARGET testBeaconHttp PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
68-
target_link_libraries(testBeaconHttp SocketHandler PipeHandler MemoryModule SocksServer )
69-
ELSE()
70-
target_link_libraries(testBeaconHttp SocketHandler PipeHandler MemoryModule SocksServer httplib::httplib)
71-
ENDIF()
72-
add_custom_command(TARGET testBeaconHttp POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
73-
$<TARGET_FILE:testBeaconHttp> "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeaconHttp>")
74-
add_test(NAME testBeaconHttp COMMAND "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeaconHttp>")
75-
76-
# Test testBeaconSmb
77-
add_executable(testBeaconSmb
78-
testBeaconSmb.cpp
79-
../Beacon.cpp
80-
../../listener/Listener.cpp
81-
../../listener/ListenerTcp.cpp
82-
../../listener/ListenerSmb.cpp
83-
../../../thirdParty/base64/base64.cpp)
84-
IF (WIN32)
85-
set_property(TARGET testBeaconSmb PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
86-
target_link_libraries(testBeaconSmb SocketHandler PipeHandler MemoryModule SocksServer )
87-
ELSE()
88-
target_link_libraries(testBeaconSmb SocketHandler PipeHandler MemoryModule SocksServer )
89-
ENDIF()
90-
add_custom_command(TARGET testBeaconSmb POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
91-
$<TARGET_FILE:testBeaconSmb> "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeaconSmb>")
92-
add_test(NAME testBeaconSmb COMMAND "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeaconSmb>")
93-
94-
# Test testBeaconTcp
95-
add_executable(testBeaconTcp
96-
testBeaconTcp.cpp
97-
../Beacon.cpp
98-
../../listener/Listener.cpp
99-
../../listener/ListenerTcp.cpp
100-
../../listener/ListenerSmb.cpp
101-
../../../thirdParty/base64/base64.cpp)
102-
IF (WIN32)
103-
set_property(TARGET testBeaconTcp PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
104-
target_link_libraries(testBeaconTcp SocketHandler PipeHandler MemoryModule SocksServer )
105-
ELSE()
106-
target_link_libraries(testBeaconTcp SocketHandler PipeHandler MemoryModule SocksServer )
107-
ENDIF()
108-
add_custom_command(TARGET testBeaconTcp POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
109-
$<TARGET_FILE:testBeaconTcp> "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeaconTcp>")
110-
add_test(NAME testBeaconTcp COMMAND "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testBeaconTcp>")
4+
# Unit tests using Catch2
5+
add_executable(beacon_unit_tests
6+
testBeacon.cpp
7+
../Beacon.cpp
8+
../../listener/Listener.cpp
9+
../../listener/ListenerTcp.cpp
10+
../../listener/ListenerSmb.cpp
11+
../../../thirdParty/base64/base64.cpp)
12+
13+
target_link_libraries(beacon_unit_tests
14+
Catch2::Catch2WithMain
15+
nlohmann_json::nlohmann_json
16+
SocketHandler
17+
PipeHandler
18+
MemoryModule
19+
SocksServer)
20+
21+
add_test(NAME beacon_unit_tests COMMAND beacon_unit_tests)
22+
23+
# Functional tests for transport variants (simple main)
24+
add_executable(testBeaconDns
25+
testBeaconDns.cpp
26+
../Beacon.cpp
27+
../BeaconDns.cpp
28+
../../listener/Listener.cpp
29+
../../listener/ListenerTcp.cpp
30+
../../listener/ListenerSmb.cpp
31+
../../../thirdParty/base64/base64.cpp)
32+
33+
target_link_libraries(testBeaconDns
34+
nlohmann_json::nlohmann_json
35+
SocketHandler
36+
Dnscommunication
37+
PipeHandler
38+
MemoryModule
39+
SocksServer)
40+
41+
add_test(NAME testBeaconDns COMMAND testBeaconDns)
42+
43+
add_executable(testBeaconGithub
44+
testBeaconGithub.cpp
45+
../Beacon.cpp
46+
../BeaconGithub.cpp
47+
../../listener/Listener.cpp
48+
../../listener/ListenerTcp.cpp
49+
../../listener/ListenerSmb.cpp
50+
../../../thirdParty/base64/base64.cpp)
51+
52+
target_link_libraries(testBeaconGithub
53+
nlohmann_json::nlohmann_json
54+
SocketHandler
55+
PipeHandler
56+
MemoryModule
57+
SocksServer
58+
httplib::httplib)
59+
60+
add_test(NAME testBeaconGithub COMMAND testBeaconGithub)
61+
62+
add_executable(testBeaconHttp
63+
testBeaconHttp.cpp
64+
../Beacon.cpp
65+
../BeaconHttp.cpp
66+
../../listener/Listener.cpp
67+
../../listener/ListenerTcp.cpp
68+
../../listener/ListenerSmb.cpp
69+
../../../thirdParty/base64/base64.cpp)
70+
71+
target_link_libraries(testBeaconHttp
72+
nlohmann_json::nlohmann_json
73+
SocketHandler
74+
PipeHandler
75+
MemoryModule
76+
SocksServer
77+
httplib::httplib)
78+
79+
add_test(NAME testBeaconHttp COMMAND testBeaconHttp)
80+
81+
add_executable(testBeaconSmb
82+
testBeaconSmb.cpp
83+
../Beacon.cpp
84+
../BeaconSmb.cpp
85+
../../listener/Listener.cpp
86+
../../listener/ListenerTcp.cpp
87+
../../listener/ListenerSmb.cpp
88+
../../../thirdParty/base64/base64.cpp)
89+
90+
target_link_libraries(testBeaconSmb
91+
nlohmann_json::nlohmann_json
92+
SocketHandler
93+
PipeHandler
94+
MemoryModule
95+
SocksServer)
96+
97+
add_test(NAME testBeaconSmb COMMAND testBeaconSmb)
98+
99+
add_executable(testBeaconTcp
100+
testBeaconTcp.cpp
101+
../Beacon.cpp
102+
../BeaconTcp.cpp
103+
../../listener/Listener.cpp
104+
../../listener/ListenerTcp.cpp
105+
../../listener/ListenerSmb.cpp
106+
../../../thirdParty/base64/base64.cpp)
107+
108+
target_link_libraries(testBeaconTcp
109+
nlohmann_json::nlohmann_json
110+
SocketHandler
111+
PipeHandler
112+
MemoryModule
113+
SocksServer)
114+
115+
add_test(NAME testBeaconTcp COMMAND testBeaconTcp)

0 commit comments

Comments
 (0)