Skip to content

Commit

Permalink
add some unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
KayzzzZ committed Jan 22, 2025
1 parent 71ae1a4 commit 471a99f
Show file tree
Hide file tree
Showing 9 changed files with 809 additions and 413 deletions.
755 changes: 379 additions & 376 deletions core/common/ProcParser.cpp

Large diffs are not rendered by default.

23 changes: 1 addition & 22 deletions core/ebpf/plugin/network_observer/ConnTrackerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,28 +179,6 @@ void ConnTrackerManager::AcceptNetCtrlEvent(struct conn_ctrl_event_t* event) {
conn_tracker->RecordActive();
}

void ConnTrackerManager::Report() {
// int count = 0;
// while(flag_) {
// int n = 0;
// for (auto it = conn_trackers_.begin(); it != conn_trackers_.end(); ++it) {
// LOG(INFO) << "[Report] Key: " << it->first << " Value: " << it->second ;
// ConnId id = it->first;
// std::shared_ptr<ConnTracker> ct = it->second;

// aggregate
// TODO @qianlu.kk wait!!!!
// auto record = ct->GetConnStatsGauge();
// Aggregator::GetInstance().Aggregate(record);
// n++;
// }
// count++;
// LOG(INFO) << "[Report] " << count << " push " << n << " conn stats to aggregator ..." ;
// std::this_thread::sleep_for(std::chrono::seconds(report_interval_sec_));
// }
return;
}

void ConnTrackerManager::IterationsInternal(int count_) {
std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now();
auto now_ts = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
Expand Down Expand Up @@ -369,5 +347,6 @@ void ConnTrackerManager::Start() {
void ConnTrackerManager::Stop() {
flag_ = false;
}

} // namespace ebpf
} // namespace logtail
12 changes: 0 additions & 12 deletions core/ebpf/plugin/network_observer/NetworkObserverManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,6 @@ int NetworkObserverManager::Init(const std::variant<SecurityOptions*, ObserverNe
mEnableSpan = opt->mEnableSpan;
mEnableMetric = opt->mEnableMetric;

// TODO @qianlu.kk
if (StartAggregator()) {
LOG_ERROR(sLogger, ("failed to start aggregator", ""));
}

// diff opt
if (mPreviousOpt) {
CompareAndUpdate("mDisableConnStats",
Expand Down Expand Up @@ -647,13 +642,6 @@ int NetworkObserverManager::Init(const std::variant<SecurityOptions*, ObserverNe
return 0;
}

int NetworkObserverManager::StartAggregator() {
return 0;
}
int NetworkObserverManager::StopAggregator() {
return 0;
}

void NetworkObserverManager::RunInThread() {
// periodically poll perf buffer ...
LOG_INFO(sLogger, ("enter core thread ", ""));
Expand Down
3 changes: 0 additions & 3 deletions core/ebpf/plugin/network_observer/NetworkObserverManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ class NetworkObserverManager : public AbstractManager {

bool UpdateParsers(const std::vector<std::string>& protocols);

int StartAggregator();
int StopAggregator();

std::shared_ptr<ConnTrackerManager> mConnTrackerMgr;

// TODO @qianlu.kk modify T for abstract event
Expand Down
13 changes: 13 additions & 0 deletions core/unittest/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ target_link_libraries(timer_unittest ${UT_BASE_TARGET})
add_executable(curl_unittest http/CurlUnittest.cpp)
target_link_libraries(curl_unittest ${UT_BASE_TARGET})

add_executable(proc_parser_unittest ProcParserUnittest.cpp)
target_link_libraries(proc_parser_unittest ${UT_BASE_TARGET})

add_executable(capability_util_unittest CapabilityUtilUnittest.cpp)
target_link_libraries(capability_util_unittest ${UT_BASE_TARGET})

add_executable(network_util_unittest NetworkUtilUnittest.cpp)
target_link_libraries(network_util_unittest ${UT_BASE_TARGET})

include(GoogleTest)
gtest_discover_tests(common_simple_utils_unittest)
gtest_discover_tests(common_logfileoperator_unittest)
Expand All @@ -69,3 +78,7 @@ gtest_discover_tests(safe_queue_unittest)
gtest_discover_tests(http_request_timer_event_unittest)
gtest_discover_tests(timer_unittest)
gtest_discover_tests(curl_unittest)
gtest_discover_tests(proc_parser_unittest)
gtest_discover_tests(proc_parser_unittest)
gtest_discover_tests(capability_util_unittest)
gtest_discover_tests(network_util_unittest)
82 changes: 82 additions & 0 deletions core/unittest/common/CapabilityUtilUnittest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2023 iLogtail Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "common/CapabilityUtil.h"
#include "unittest/Unittest.h"

namespace logtail {

class CapabilityUtilUnittest : public ::testing::Test {
public:
void TestGetCapability();
void TestGetCapabilities();
void TestInvalidCapability();
};

void CapabilityUtilUnittest::TestGetCapability() {
// Test some common capabilities
APSARA_TEST_STREQ_DESC(GetCapability(0).c_str(), "CAP_CHOWN", "CAP_CHOWN should match");
APSARA_TEST_STREQ_DESC(GetCapability(1).c_str(), "DAC_OVERRIDE", "DAC_OVERRIDE should match");
APSARA_TEST_STREQ_DESC(GetCapability(2).c_str(), "CAP_DAC_READ_SEARCH", "CAP_DAC_READ_SEARCH should match");
APSARA_TEST_STREQ_DESC(GetCapability(7).c_str(), "CAP_SETUID", "CAP_SETUID should match");
APSARA_TEST_STREQ_DESC(GetCapability(21).c_str(), "CAP_SYS_ADMIN", "CAP_SYS_ADMIN should match");
}

void CapabilityUtilUnittest::TestGetCapabilities() {
// Test single capability
uint64_t singleCap = 1ULL << 0; // CAP_CHOWN
APSARA_TEST_STREQ_DESC(GetCapabilities(singleCap).c_str(), "CAP_CHOWN", "Single capability should match");

// Test multiple capabilities
uint64_t multipleCaps = (1ULL << 0) | (1ULL << 1) | (1ULL << 7); // CAP_CHOWN, DAC_OVERRIDE, CAP_SETUID
std::string result = GetCapabilities(multipleCaps);
APSARA_TEST_TRUE(result.find("CAP_CHOWN") != std::string::npos);
APSARA_TEST_TRUE(result.find("DAC_OVERRIDE") != std::string::npos);
APSARA_TEST_TRUE(result.find("CAP_SETUID") != std::string::npos);

// Test no capabilities
APSARA_TEST_STREQ_DESC(GetCapabilities(0).c_str(), "", "No capabilities should return empty string");

// Test all capabilities
uint64_t allCaps = ~0ULL;
result = GetCapabilities(allCaps);
APSARA_TEST_TRUE(result.find("CAP_CHOWN") != std::string::npos);
APSARA_TEST_TRUE(result.find("CAP_SYS_ADMIN") != std::string::npos);
APSARA_TEST_TRUE(result.find("CAP_CHECKPOINT_RESTORE") != std::string::npos);
}

void CapabilityUtilUnittest::TestInvalidCapability() {
// Test invalid capability value
try {
GetCapability(-1);
APSARA_TEST_TRUE(false); // Should not reach here
} catch (const std::invalid_argument& e) {
APSARA_TEST_TRUE(true);
}

try {
GetCapability(100);
APSARA_TEST_TRUE(false); // Should not reach here
} catch (const std::invalid_argument& e) {
APSARA_TEST_TRUE(true);
}
}

UNIT_TEST_CASE(CapabilityUtilUnittest, TestGetCapability);
UNIT_TEST_CASE(CapabilityUtilUnittest, TestGetCapabilities);
UNIT_TEST_CASE(CapabilityUtilUnittest, TestInvalidCapability);

} // namespace logtail

UNIT_TEST_MAIN
89 changes: 89 additions & 0 deletions core/unittest/common/NetworkUtilUnittest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2023 iLogtail Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "common/NetworkUtil.h"
#include "unittest/Unittest.h"

#if defined(__linux__)
#include <arpa/inet.h>
#endif

namespace logtail {

class NetworkUtilUnittest : public ::testing::Test {
public:
void TestGetAddrString();
void TestGetFamilyString();
void TestGetProtocolString();
void TestGetStateString();
};

void NetworkUtilUnittest::TestGetAddrString() {
#if defined(__linux__)
// Test IP address conversion
// 127.0.0.1 in network byte order
uint32_t localhost = 0x0100007F;
APSARA_TEST_STREQ_DESC(GetAddrString(localhost).c_str(), "127.0.0.1", "Localhost IP should match");

// 192.168.1.1 in network byte order
uint32_t localIP = 0x0101A8C0;
APSARA_TEST_STREQ_DESC(GetAddrString(localIP).c_str(), "192.168.1.1", "Local IP should match");

// 8.8.8.8 in network byte order
uint32_t googleDNS = 0x08080808;
APSARA_TEST_STREQ_DESC(GetAddrString(googleDNS).c_str(), "8.8.8.8", "Google DNS IP should match");
#endif
}

void NetworkUtilUnittest::TestGetFamilyString() {
#if defined(__linux__)
// Test common address families
APSARA_TEST_STREQ_DESC(GetFamilyString(AF_INET).c_str(), "AF_INET", "IPv4 family should match");
APSARA_TEST_STREQ_DESC(GetFamilyString(AF_INET6).c_str(), "AF_INET6", "IPv6 family should match");
APSARA_TEST_STREQ_DESC(GetFamilyString(AF_UNIX).c_str(), "AF_UNIX", "Unix domain socket family should match");

// Test unknown family
APSARA_TEST_TRUE(GetFamilyString(9999).find_first_not_of("0123456789") == std::string::npos);
#endif
}

void NetworkUtilUnittest::TestGetProtocolString() {
// Test common protocols
APSARA_TEST_STREQ_DESC(GetProtocolString(1).c_str(), "ICMP", "ICMP protocol should match");
APSARA_TEST_STREQ_DESC(GetProtocolString(6).c_str(), "TCP", "TCP protocol should match");
APSARA_TEST_STREQ_DESC(GetProtocolString(17).c_str(), "UDP", "UDP protocol should match");
APSARA_TEST_STREQ_DESC(GetProtocolString(89).c_str(), "OSPF", "OSPF protocol should match");

// Test unknown protocol
APSARA_TEST_STREQ_DESC(GetProtocolString(999).c_str(), "Unknown", "Unknown protocol should return 'Unknown'");
}

void NetworkUtilUnittest::TestGetStateString() {
// Test TCP states
APSARA_TEST_STREQ_DESC(GetStateString(1).c_str(), "TCP_ESTABLISHED", "TCP ESTABLISHED state should match");
APSARA_TEST_STREQ_DESC(GetStateString(2).c_str(), "TCP_SYN_SENT", "TCP SYN_SENT state should match");
APSARA_TEST_STREQ_DESC(GetStateString(10).c_str(), "TCP_LISTEN", "TCP LISTEN state should match");

// Test invalid state
APSARA_TEST_STREQ_DESC(GetStateString(999).c_str(), "INVALID_STATE", "Invalid state should return 'INVALID_STATE'");
}

UNIT_TEST_CASE(NetworkUtilUnittest, TestGetAddrString);
UNIT_TEST_CASE(NetworkUtilUnittest, TestGetFamilyString);
UNIT_TEST_CASE(NetworkUtilUnittest, TestGetProtocolString);
UNIT_TEST_CASE(NetworkUtilUnittest, TestGetStateString);

} // namespace logtail

UNIT_TEST_MAIN
Loading

0 comments on commit 471a99f

Please sign in to comment.