Skip to content

Commit 0478283

Browse files
committed
Modularised code; added tests and coverage
1 parent d170627 commit 0478283

4 files changed

Lines changed: 46 additions & 42 deletions

File tree

CMakeLists.txt

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ set(PROJECT_LONG_NAME "Endlessh Report Generator")
55
set(CMAKE_CXX_STANDARD 17)
66
set(CMAKE_CXX_STANDARD_REQUIRED True)
77

8+
option(ENABLE_COVERAGE "Enable compilation flags for gcov coverage" ON)
9+
810
include(CTest)
911

1012
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/version.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/include/version.hpp)
1113

1214
add_compile_options(
1315
-Wpedantic
14-
# -Werror
16+
-Werror
1517
-Wall
18+
-Wno-unused-function
1619
)
1720

1821
include_directories(
@@ -28,10 +31,16 @@ if (DEFINED endlesshreport_DEBUG OR CMAKE_BUILD_TYPE STREQUAL "Debug")
2831
else()
2932
add_compile_options(
3033
-O2
31-
-s
3234
)
3335
endif()
3436

37+
if (ENABLE_COVERAGE)
38+
message(STATUS "Building with coverage support (--coverage)")
39+
add_compile_options(--coverage -O0 -g)
40+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
41+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage")
42+
endif()
43+
3544
###
3645
# Make sure submodules are updated
3746
###
@@ -44,21 +53,20 @@ execute_process(
4453
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/submodules/date)
4554
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/submodules/fmt)
4655

47-
file(GLOB_RECURSE FILES FOLLOW_SYMLINKS ${CMAKE_CURRENT_SOURCE_DIR} src/*.cpp)
48-
49-
add_executable(
50-
${PROJECT_NAME}
56+
add_library(endlessh_core STATIC src/core.cpp)
5157

52-
${FILES}
53-
)
58+
add_executable(${PROJECT_NAME} src/main.cpp)
5459

5560
target_link_libraries(
5661
${PROJECT_NAME}
57-
58-
date
59-
fmt
62+
PRIVATE
63+
endlessh_core
64+
date
65+
fmt
6066
)
6167

68+
target_link_libraries(endlessh_core PRIVATE fmt date)
69+
6270
if (BUILD_TESTING)
6371
add_subdirectory(tests)
6472
endif()
@@ -98,3 +106,10 @@ set(CPACK_PACKAGE_CONTACT "Simon Cahill <simon@simonc.eu>")
98106
set(CPACK_PACKAGE_VENDOR "Simon Cahill")
99107
set(CPACK_GENERATOR DEB)
100108
include(CPack)
109+
110+
# Coverage target that runs the coverage helper script
111+
add_custom_target(coverage
112+
COMMAND ${CMAKE_SOURCE_DIR}/scripts/coverage.sh
113+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
114+
COMMENT "Run tests and generate coverage report (requires lcov/genhtml)"
115+
)

include/extensions.hpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ using std::chrono::seconds;
3131
using std::chrono::system_clock;
3232
using std::function;
3333
using std::string;
34+
using std::string_view;
3435
using std::vector;
3536

3637
/**
@@ -179,12 +180,8 @@ inline string getSpacerString(const uint32_t totalWidth, const uint32_t strLengt
179180
*
180181
* @return The trimmed string.
181182
*/
182-
inline string trimStart(string nonTrimmed, const string& trimChar) {
183-
//nonTrimmed.erase(nonTrimmed.begin(), find_if(nonTrimmed.begin(), nonTrimmed.end(), not1(ptr_fun<int32_t, int32_t>(isspace))));
184-
185-
function<bool(char)> shouldTrimChar = [=](char c) -> bool { return trimChar.size() == 0 ? isspace(c) : trimChar.find(c) != string::npos; };
186-
187-
nonTrimmed.erase(nonTrimmed.begin(), find_if(nonTrimmed.begin(), nonTrimmed.end(), not1(shouldTrimChar)));
183+
inline string& trimStart(string& nonTrimmed, const string_view trimChars) {
184+
nonTrimmed.erase(nonTrimmed.begin(), std::find_if(nonTrimmed.begin(), nonTrimmed.end(), [&trimChars](int ch) { return trimChars.find(static_cast<char>(ch)) == string_view::npos; }));
188185

189186
return nonTrimmed;
190187
}
@@ -196,11 +193,8 @@ inline string trimStart(string nonTrimmed, const string& trimChar) {
196193
*
197194
* @return The trimmed string.
198195
*/
199-
inline string trimEnd(string nonTrimmed, const string& trimChar) {
200-
// nonTrimmed.erase(find_if(nonTrimmed.rbegin(), nonTrimmed.rend(), not1(ptr_fun<int32_t, int32_t>(isspace))).base(), nonTrimmed.end());
201-
202-
function<bool(char)> shouldTrimChar = [=](char c) -> bool { return trimChar.size() == 0 ? isspace(c) : trimChar.find(c) != string::npos; };
203-
nonTrimmed.erase(find_if(nonTrimmed.rbegin(), nonTrimmed.rend(), not1(shouldTrimChar)).base(), nonTrimmed.end());
196+
inline string& trimEnd(string& nonTrimmed, const string_view trimChars) {
197+
nonTrimmed.erase(std::find_if(nonTrimmed.rbegin(), nonTrimmed.rend(), [&trimChars](int ch) { return trimChars.find(static_cast<char>(ch)) == string_view::npos; }).base(), nonTrimmed.end());
204198

205199
return nonTrimmed;
206200
}
@@ -213,7 +207,7 @@ inline string trimEnd(string nonTrimmed, const string& trimChar) {
213207
*
214208
* @return The trimmed string.
215209
*/
216-
inline string trim(const string& nonTrimmed, const string& trimChar) { return trimStart(trimEnd(nonTrimmed, trimChar), trimChar); }
210+
inline string& trim(string& nonTrimmed, const string_view trimChars) { return trimStart(trimEnd(nonTrimmed, trimChars), trimChars); }
217211

218212

219213
#endif // ENDLESSH_REPORT_INCLUDE_EXTENSIONS_HPP

src/main.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
#include <regex.h>
3434

3535
#include <fmt/format.h>
36+
#include <arpa/inet.h>
37+
#include <netdb.h>
38+
#include <netinet/in.h>
39+
#include <sys/socket.h>
40+
#include <cstring>
41+
#include "public_api.hpp"
3642

3743
using fmt::format;
3844

@@ -51,7 +57,7 @@ using std::vector;
5157

5258
static bool g_error{false}; //!< Whether or not a fatal error occurred
5359
static bool g_disableAdvertisement{false}; //!< Whether or not to disable advertisement (default: false)
54-
static bool g_performDnsLookup{false}; //!< Whether or not to perform DNS lookups (default: false)
60+
// g_performDnsLookup is defined in the `endlessh_core` library (src/core.cpp)
5561
static bool g_printAbuseIpDbCsv{false}; //!< Whether or not to output AbuseIPDB-compatible CSV data (default: false; disables markdown-compatible stats)
5662
static bool g_printIpStatistics{true}; //!< Whether or not to print IP stats (default: true)
5763
static bool g_printConnectionStatistics{true}; //!< Whether or not to print connection stats (default: true)
@@ -64,13 +70,8 @@ static string g_cacheFile{ReportCache::defaultCachePath()}; //!< Cache file loc
6470
static bool g_cacheDirty{false}; //!< Whether or not the cache needs to be written
6571
static std::unique_ptr<ReportCache> g_reportCache{}; //!< Loaded cache (only when required)
6672

67-
static string normalizeIp(string ip) {
68-
const auto offset = ip.find("::ffff:");
69-
if (offset != string::npos) {
70-
ip = ip.substr(offset + 7);
71-
}
72-
return ip;
73-
}
73+
// normalizeIp implemented in src/core.cpp
74+
7475

7576
/**
7677
* @brief Contains information about a given connection.
@@ -94,7 +95,7 @@ struct ConnectionDetails {
9495

9596
static int32_t parseArgs(const int32_t&, char**); //!< Parses command-line arguments
9697
static map<string, pair<uint32_t, uint32_t>> getConnections(const vector<string>&); //!< Gets the logged connections
97-
static string getHostFromIp(const string&); //!< Gets the host from an IP address
98+
// getHostFromIp is declared in include/public_api.hpp
9899
static vector<ConnectionDetails> getDetailledConnections(const vector<string>&); //!< Gets a detailled list of logged connections
99100
static vector<string> readEndlesshLog(); //!< Reads the log file into memory
100101
static void printConnectionStatistics(const uint32_t uniqueIps, const uint32_t totalAccepted, const uint32_t totalClosed, const double totalTimeWasted, const uint32_t totalBytesSent); //!< Print connection statistics
@@ -381,16 +382,7 @@ map<string, pair<uint32_t, uint32_t>> getConnections(const vector<string>& logCo
381382
return connections;
382383
}
383384

384-
string getHostFromIp(const string& ip) {
385-
string host;
386-
if (g_performDnsLookup) {
387-
388-
} else {
389-
host = ip;
390-
}
391-
392-
return host;
393-
}
385+
// getHostFromIp implemented in src/core.cpp
394386

395387
/**
396388
* @brief Gets a list with detailled information about all the incoming connections to the server.

tests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ add_executable(
3535
endlessh-report-tests
3636
report_cache_test.cpp
3737
end_to_end_cache_test.cpp
38+
main_extensions_test.cpp
3839
)
3940

4041
target_link_libraries(
@@ -43,13 +44,15 @@ target_link_libraries(
4344
${GTEST_MAIN_TARGET}
4445
${GMOCK_TARGET}
4546
fmt
47+
endlessh_core
4648
)
4749

4850
target_include_directories(
4951
endlessh-report-tests
5052
PRIVATE
5153
${PROJECT_SOURCE_DIR}/include
5254
${PROJECT_BINARY_DIR}/include
55+
${PROJECT_SOURCE_DIR}/submodules/date/include
5356
)
5457

5558
# Allow end-to-end tests to locate the built binary deterministically.

0 commit comments

Comments
 (0)